소스 검색

Merge pull request #29 from ryangjchandler/feature/off-listener

Feature/off listener
Ryan Chandler 5 년 전
부모
커밋
e3c22e443f
10개의 변경된 파일59개의 추가작업 그리고 1개의 파일을 삭제
  1. 16 0
      README.md
  2. 0 0
      dist/spruce.js
  3. 0 0
      dist/spruce.js.map
  4. 0 0
      dist/spruce.module.js
  5. 0 0
      dist/spruce.module.js.map
  6. 0 0
      dist/spruce.umd.js
  7. 0 0
      dist/spruce.umd.js.map
  8. 8 0
      src/bus.js
  9. 5 1
      src/index.js
  10. 30 0
      tests/bus.spec.js

+ 16 - 0
README.md

@@ -196,6 +196,22 @@ Spruce.on('init', ({ store }) => {
 })
 ```
 
+* `Spruce.off(eventName, callback)` - this can be used to unhook or de-register an event listener.
+
+```js
+var callback = () => {}
+
+Spruce.off('init', callback)
+```
+
+You can also unhook a listener using the function returned by `Spruce.on()`. This is especially useful for anonymous function callbacks.
+
+```js
+var off = Spruce.on('event', () => {})
+
+off()
+```
+
 * `Spruce.emit(eventName, data = {})` - this can be used to emit an event. The first argument should be the name of the event, the second should be an object containing data. This will be merged in with the core data, which consists of a `store` property. When emitting an event, a browser event will also be dispatched with a `spruce:` prefix.
 
 ```js

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/spruce.js


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/spruce.js.map


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/spruce.module.js


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/spruce.module.js.map


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/spruce.umd.js


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/spruce.umd.js.map


+ 8 - 0
src/bus.js

@@ -9,6 +9,14 @@ export default {
         }
 
         this.events[name].push(callback)
+
+        return () => this.off(name, callback)
+    },
+
+    off(name, callback) {
+        this.events[name] = this.events[name].filter(registerCallback => {
+            return registerCallback !== callback
+        })
     },
 
     emit(name, data = {}) {

+ 5 - 1
src/index.js

@@ -69,7 +69,11 @@ const Spruce = {
     },
 
     on(name, callback) {
-        this.events.on(name, callback)
+        return this.events.on(name, callback)
+    },
+
+    off(name, callback) {
+        this.events.off(name, callback)
     },
 
     emit(name, data = {}) {

+ 30 - 0
tests/bus.spec.js

@@ -87,4 +87,34 @@ test('.watch() > can listen for changes to property', async () => {
 
     expect(fixture).toEqual('amazing')
     expect(oldFixture).toEqual('stuff')
+})
+
+test('.off() > can unregister listener', () => {
+    let fixture = undefined;
+
+    const callback = () => {
+        fixture = 0
+    }
+
+    Spruce.on('fixture-event', callback)
+    
+    Spruce.off('fixture-event', callback)
+
+    Spruce.emit('fixture-event')
+
+    expect(fixture).toBeUndefined()
+})
+
+test('.off() > returned when calling .on()', () => {
+    let fixture = undefined;
+
+    let off = Spruce.on('fixture-event', () => {
+        fixture = 0
+    })
+
+    off()
+
+    Spruce.emit('fixture-event')
+
+    expect(fixture).toBeUndefined()
 })

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.