-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #309 from goatslacker/0.17.0-rebased
Working 0.17.0 release
- Loading branch information
Showing
29 changed files
with
257 additions
and
326 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
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
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
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
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
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 |
---|---|---|
@@ -1,52 +1,57 @@ | ||
import Symbol from 'es-symbol' | ||
|
||
import * as Sym from '../symbols/symbols' | ||
import * as fn from '../../utils/functions' | ||
import * as utils from '../utils/AltUtils' | ||
|
||
class AltAction { | ||
constructor(alt, name, action, actions, actionDetails) { | ||
this[Sym.ACTION_UID] = name | ||
this[Sym.ACTION_HANDLER] = action.bind(this) | ||
constructor(alt, id, action, actions, actionDetails) { | ||
this.id = id | ||
this._dispatch = action.bind(this) | ||
this.actions = actions | ||
this.actionDetails = actionDetails | ||
this.alt = alt | ||
} | ||
|
||
dispatch(data) { | ||
this.alt.dispatch(this[Sym.ACTION_UID], data, this.actionDetails) | ||
this.dispatched = true | ||
this.alt.dispatch(this.id, data, this.actionDetails) | ||
} | ||
} | ||
|
||
export default function makeAction(alt, namespace, name, implementation, obj) { | ||
// make sure each Symbol is unique | ||
const actionId = utils.uid(alt[Sym.ACTIONS_REGISTRY], `${namespace}.${name}`) | ||
alt[Sym.ACTIONS_REGISTRY][actionId] = 1 | ||
const actionSymbol = Symbol.for(`alt/${actionId}`) | ||
|
||
const data = { | ||
namespace, | ||
name, | ||
id: actionId, | ||
symbol: actionSymbol | ||
} | ||
const id = utils.uid(alt._actionsRegistry, `${namespace}.${name}`) | ||
alt._actionsRegistry[id] = 1 | ||
|
||
const data = { id, namespace, name } | ||
|
||
// Wrap the action so we can provide a dispatch method | ||
const newAction = new AltAction(alt, actionSymbol, implementation, obj, data) | ||
const newAction = new AltAction(alt, id, implementation, obj, data) | ||
|
||
const dispatch = (payload) => alt.dispatch(id, payload, data) | ||
|
||
// the action itself | ||
const action = newAction[Sym.ACTION_HANDLER] | ||
const action = (...args) => { | ||
newAction.dispatched = false | ||
const result = newAction._dispatch(...args) | ||
if (!newAction.dispatched && result !== undefined) { | ||
if (fn.isFunction(result)) { | ||
result(dispatch) | ||
} else { | ||
dispatch(result) | ||
} | ||
} | ||
return result | ||
} | ||
action.defer = (...args) => { | ||
setTimeout(() => { | ||
newAction[Sym.ACTION_HANDLER].apply(null, args) | ||
newAction._dispatch.apply(null, args) | ||
}) | ||
} | ||
action[Sym.ACTION_KEY] = actionSymbol | ||
action.id = id | ||
action.data = data | ||
|
||
// ensure each reference is unique in the namespace | ||
const container = alt.actions[namespace] | ||
const id = utils.uid(container, name) | ||
container[id] = action | ||
const namespaceId = utils.uid(container, name) | ||
container[namespaceId] = action | ||
|
||
return action | ||
} |
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
Oops, something went wrong.