Skip to content

Instantly share code, notes, and snippets.

@mbajur
Last active June 28, 2017 18:06
Show Gist options
  • Save mbajur/1eff0c08c60c0fefde1a5fb98b8f1633 to your computer and use it in GitHub Desktop.
Save mbajur/1eff0c08c60c0fefde1a5fb98b8f1633 to your computer and use it in GitHub Desktop.
alt.js - listening for events outside of the store
/**
* 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
* })
* ```
*/
// import { isFunction } from './functions';
export const isFunction = x => typeof x === 'function'
function ActionListeners(alt) {
this.dispatcher = alt.dispatcher
this.listeners = {}
}
/*
* addActionListener(symAction: symbol, handler: function): number
* Adds a listener to a specified action and returns the dispatch token.
*/
ActionListeners.prototype.addActionListener = function addActionListener(symAction, handler) {
if (!isFunction(handler)) {
throw new Error('addActionListener() expects a function as the second argument')
}
const id = this.dispatcher.register((payload) => {
/* istanbul ignore else */
if (symAction === payload.action) {
handler(payload.data, payload.details)
}
})
this.listeners[id] = true
return id
}
/*
* removeActionListener(id: number): undefined
* Removes the specified dispatch registration.
*/
ActionListeners.prototype.removeActionListener = function removeActionListener(id) {
delete this.listeners[id]
this.dispatcher.unregister(id)
}
/**
* Remove all listeners.
*/
ActionListeners.prototype.removeAllActionListeners = function removeAllActionListeners() {
Object.keys(this.listeners).forEach(
this.removeActionListener.bind(this)
)
this.listeners = {}
}
// export default ActionListeners
window.ActionListeners = ActionListeners
alt = new Alt()
actionListener = new ActionListeners(alt)
ready: {
actionListener.addActionListener(PanelItemFormAsideActions.UPDATE_ITEM_SUCCESS_RESULT, (data) => {
$.notify({message: 'Danie zostało zaktualizowane pomyślnie!'})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment