Skip to content

Commit

Permalink
Pass action details through the dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed May 1, 2015
1 parent 623dea3 commit 05398a6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/alt/AltAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import * as Sym from './symbols/symbols'
const { ACTION_HANDLER, ACTION_UID } = Sym

export default class AltAction {
constructor(alt, name, action, actions) {
constructor(alt, name, action, actions, actionDetails) {
this[ACTION_UID] = name
this[ACTION_HANDLER] = action.bind(this)
this.actionDetails = actionDetails
this.actions = actions
this.alt = alt
}

dispatch(data) {
this.alt.dispatch(this[ACTION_UID], data)
this.alt.dispatch(this[ACTION_UID], data, this.actionDetails)
}
}
4 changes: 2 additions & 2 deletions src/alt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class Alt {
this[LAST_SNAPSHOT] = {}
}

dispatch(action, data) {
this.dispatcher.dispatch({ action, data })
dispatch(action, data, details) {
this.dispatcher.dispatch({ action, data, details })
}

createUnsavedStore(StoreModel, ...args) {
Expand Down
10 changes: 9 additions & 1 deletion src/alt/utils/makeAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ export default function makeAction(alt, namespace, name, implementation, obj) {
alt[ACTIONS_REGISTRY][actionId] = 1
const actionSymbol = Symbol.for(`alt/${actionId}`)

const data = {
namespace,
name,
id: actionId,
symbol: actionSymbol
}

// Wrap the action so we can provide a dispatch method
const newAction = new AltAction(alt, actionSymbol, implementation, obj)
const newAction = new AltAction(alt, actionSymbol, implementation, obj, data)

// the action itself
const action = newAction[ACTION_HANDLER]
Expand All @@ -22,6 +29,7 @@ export default function makeAction(alt, namespace, name, implementation, obj) {
})
}
action[ACTION_KEY] = actionSymbol
action.data = data

// ensure each reference is unique in the namespace
const container = alt.actions[namespace]
Expand Down
2 changes: 1 addition & 1 deletion src/utils/ActionListeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ActionListeners.prototype.addActionListener = function (symAction, handler) {
const id = this.dispatcher.register((payload) => {
/* istanbul ignore else */
if (symAction === payload.action) {
handler(payload.data)
handler(payload.data, payload.details)
}
})
this[ALT_LISTENERS][id] = true
Expand Down
6 changes: 5 additions & 1 deletion test/listen-to-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import ActionListeners from '../utils/ActionListeners'
const alt = new Alt()

class MyActions {
static displayName = 'ActionListenerActions'

constructor() {
this.generateActions('updateName')
}
Expand All @@ -17,8 +19,10 @@ const listener = new ActionListeners(alt)

export default {
'listen to actions globally'() {
const id = listener.addActionListener(myActions.UPDATE_NAME, (name) => {
const id = listener.addActionListener(myActions.UPDATE_NAME, (name, details) => {
assert(name === 'yes', 'proper data was passed in')
assert(details.namespace === 'ActionListenerActions')
assert(details.name === 'updateName')
})

assert.isString(id, 'the dispatcher id is returned for the listener')
Expand Down

0 comments on commit 05398a6

Please sign in to comment.