-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ActionListener util for global listening
- Loading branch information
1 parent
351aadb
commit ce5ddca
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import assert from 'assert' | ||
import Alt from '../dist/alt-with-runtime' | ||
|
||
import ActionListeners from '../utils/ActionListeners' | ||
|
||
let alt = new Alt() | ||
|
||
class MyActions { | ||
constructor() { | ||
this.generateActions('updateName') | ||
} | ||
} | ||
|
||
let myActions = alt.createActions(MyActions) | ||
|
||
let listener = new ActionListeners(alt) | ||
|
||
export default { | ||
'listen to actions globally'() { | ||
let id = listener.addActionListener(myActions.UPDATE_NAME, (name) => { | ||
assert.equal(name, 'yes', 'proper data was passed in') | ||
}) | ||
|
||
assert.equal(typeof id, 'string', 'the dispatcher id is returned for the listener') | ||
|
||
myActions.updateName('yes') | ||
|
||
listener.removeActionListener(id) | ||
|
||
myActions.updateName('no') | ||
assert.equal(true, true, 'no error was thrown by above action since listener was removed') | ||
|
||
listener.addActionListener(myActions.UPDATE_NAME, (name) => { | ||
assert.equal(name, 'mud', 'proper data was passed in again') | ||
}) | ||
|
||
myActions.updateName('mud') | ||
|
||
listener.removeAllActionListeners() | ||
|
||
myActions.updateName('bill') | ||
|
||
assert.equal(true, true, 'all listeners were removed') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* ActionListeners(alt: AltInstance): ActionListenersInstance | ||
* | ||
* > Globally listen to individual actions | ||
* | ||
* If you need to listen to an action but don't want the weight of a store | ||
* then this util is what you can use. | ||
* | ||
* Usage: | ||
* | ||
* ```js | ||
* var actionListener = new ActionListeners(alt); | ||
* | ||
* actionListener.addActionListener(Action.ACTION_NAME, function (data) { | ||
* // do something with data | ||
* }) | ||
* ``` | ||
*/ | ||
module.exports = ActionListeners | ||
|
||
var Symbol = require('es-symbol') | ||
var ALT_LISTENERS = Symbol('global dispatcher listeners') | ||
|
||
function ActionListeners(alt) { | ||
this.dispatcher = alt.dispatcher | ||
this[ALT_LISTENERS] = {} | ||
} | ||
|
||
/* | ||
* addActionListener(symAction: symbol, handler: function): number | ||
* Adds a listener to a specified action and returns the dispatch token. | ||
*/ | ||
ActionListeners.prototype.addActionListener = function (symAction, handler) { | ||
var id = this.dispatcher.register(function (payload) { | ||
symAction === payload.action && handler(payload.data) | ||
}) | ||
this[ALT_LISTENERS][id] = true | ||
return id | ||
} | ||
|
||
/* | ||
* removeActionListener(id: number): undefined | ||
* Removes the specified dispatch registration. | ||
*/ | ||
ActionListeners.prototype.removeActionListener = function (id) { | ||
delete this[ALT_LISTENERS][id] | ||
this.dispatcher.unregister(id) | ||
} | ||
|
||
/** | ||
* Remove all listeners. | ||
*/ | ||
ActionListeners.prototype.removeAllActionListeners = function () { | ||
Object.keys(this[ALT_LISTENERS]).forEach( | ||
this.removeActionListener.bind(this) | ||
) | ||
this[ALT_LISTENERS] = {} | ||
} |