Browse Source

feature(return off function when calling Spruce.on)

Ryan Chandler 5 years ago
parent
commit
80506959d9
9 changed files with 18 additions and 2 deletions
  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. 2 0
      src/bus.js
  8. 1 1
      src/index.js
  9. 15 1
      tests/bus.spec.js

File diff suppressed because it is too large
+ 0 - 0
dist/spruce.js


File diff suppressed because it is too large
+ 0 - 0
dist/spruce.js.map


File diff suppressed because it is too large
+ 0 - 0
dist/spruce.module.js


File diff suppressed because it is too large
+ 0 - 0
dist/spruce.module.js.map


File diff suppressed because it is too large
+ 0 - 0
dist/spruce.umd.js


File diff suppressed because it is too large
+ 0 - 0
dist/spruce.umd.js.map


+ 2 - 0
src/bus.js

@@ -9,6 +9,8 @@ export default {
         }
 
         this.events[name].push(callback)
+
+        return () => this.off(name, callback)
     },
 
     off(name, callback) {

+ 1 - 1
src/index.js

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

+ 15 - 1
tests/bus.spec.js

@@ -89,7 +89,7 @@ test('.watch() > can listen for changes to property', async () => {
     expect(oldFixture).toEqual('stuff')
 })
 
-test('.off() > can unregister listener', async () => {
+test('.off() > can unregister listener', () => {
     let fixture = undefined;
 
     const callback = () => {
@@ -102,5 +102,19 @@ test('.off() > can unregister listener', async () => {
 
     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()
 })

Some files were not shown because too many files changed in this diff