leeluolee/stateman

[Feature] Add EventEmitter `namespace` support

leeluolee opened this issue · 0 comments

In practice, I found that using stateman as a mediator for passing message among states is a good idea.
For example.

var stateman = stateman();

stateman.state('app.blog', {
    enter: function(){
        stateman.emit('enter-blog')
    }
})
stateman.state('app.user', {
    enter: function(){
        this.handle = function(){console.log('enter the app.blog')}
        stateman.on('enter-blog', this.handle)
    },
    leave: function(){
       stateman.off('enter-blog', this.handle)

    }
})

But , we should kept the reference (this.handle above) for listener, beacuse we will remove it when state is leaving.

So, if we introduced namespace, we can coding like:

stateman.state('app.user', {
    enter: function(){
        stateman.on('user:enter-blog', function(){console.log('app.blog')})
    },
    leave: function(){
       stateman.off('user:enter-blog')
    }
})

Now, we can remove enter-blog event with the namespace user directly without keeping the reference for handle. It won't affect other listener with the same eventType( enter-blog)