Skip to content

Instantly share code, notes, and snippets.

@fgarcia
Last active October 7, 2017 11:50
Show Gist options
  • Select an option

  • Save fgarcia/043c1afac5a5b6ef6edd4aed739a8cb0 to your computer and use it in GitHub Desktop.

Select an option

Save fgarcia/043c1afac5a5b6ef6edd4aed739a8cb0 to your computer and use it in GitHub Desktop.

Revisions

  1. Francisco Garcia revised this gist Oct 7, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion test.js
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ function test() {
    //
    // You might be tempted to just test your code with 'once'. This however has two problems:
    // 1. You will not notice unexpected extra calls
    // 2. The code subscribing is out of your test, and it always must listen
    // 2. The code subscribing is out of your test, and it must always listen
    bus.on('my-signal', () => {
    console.log(' Catch!')
    })
  2. Francisco Garcia revised this gist Oct 7, 2017. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -14,8 +14,12 @@ function reset_2() {
    }

    function reset_3() {
    // solves test problem,
    // but breaks code that made a copy of the previous 'bus'
    // WARNING

    // this solves the test problem, but also breaks code which makes a copy of your bus.
    //
    // If you pass around instances of your bus, it is very likely you will break something
    // if you just replace it with a new one
    bus = new EventEmitter2()
    }

  3. Francisco Garcia revised this gist Oct 7, 2017. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,11 @@ function test() {
    console.log('Start test')
    // reset_1()

    // WARNING
    //
    // You might be tempted to just test your code with 'once'. This however has two problems:
    // 1. You will not notice unexpected extra calls
    // 2. The code subscribing is out of your test, and it always must listen
    bus.on('my-signal', () => {
    console.log(' Catch!')
    })
  4. Francisco Garcia created this gist Oct 7, 2017.
    20 changes: 20 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    > node test.js

    Start test
    Catch!
    end


    Start test
    Catch!
    Catch!
    end


    Start test
    Catch!
    Catch!
    Catch!
    end


    40 changes: 40 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    const {EventEmitter2} = require('eventemitter2')

    let bus
    bus = new EventEmitter2()

    function reset_1() {
    // fail !
    bus.offAny()
    }

    function reset_2() {
    bus._all = []
    bus._events = {}
    }

    function reset_3() {
    // solves test problem,
    // but breaks code that made a copy of the previous 'bus'
    bus = new EventEmitter2()
    }


    function test() {
    console.log('Start test')
    // reset_1()

    bus.on('my-signal', () => {
    console.log(' Catch!')
    })

    bus.emit('my-signal', 'hello')

    console.log('end')
    console.log()
    console.log()
    }

    test()
    test()
    test()