소스 검색

feature(new Spruce.off method)

Ryan Chandler 5 년 전
부모
커밋
88303e5253
9개의 변경된 파일26개의 추가작업 그리고 0개의 파일을 삭제
  1. 0 0
      dist/spruce.js
  2. 0 0
      dist/spruce.js.map
  3. 0 0
      dist/spruce.module.js
  4. 0 0
      dist/spruce.module.js.map
  5. 0 0
      dist/spruce.umd.js
  6. 0 0
      dist/spruce.umd.js.map
  7. 6 0
      src/bus.js
  8. 4 0
      src/index.js
  9. 16 0
      tests/bus.spec.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


+ 6 - 0
src/bus.js

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

+ 4 - 0
src/index.js

@@ -72,6 +72,10 @@ const Spruce = {
         this.events.on(name, callback)
     },
 
+    off(name, callback) {
+        this.events.off(name, callback)
+    },
+
     emit(name, data = {}) {
         this.events.emit(name, { ...data, store: this.stores })
     },

+ 16 - 0
tests/bus.spec.js

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

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