Skip to content

Commit 63d3a72

Browse files
committed
Creating single actions
1 parent 1bd3112 commit 63d3a72

File tree

6 files changed

+140
-106
lines changed

6 files changed

+140
-106
lines changed

dist/alt-browser-with-addons.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,29 @@ var Alt = (function () {
14651465
});
14661466
}
14671467
},
1468+
createAction: {
1469+
value: function createAction(name, implementation, obj) {
1470+
var actionId = uid(GlobalActionsNameRegistry, "#" + name);
1471+
GlobalActionsNameRegistry[actionId] = 1;
1472+
var actionName = Symbol["for"](actionId);
1473+
1474+
// Wrap the action so we can provide a dispatch method
1475+
var newAction = new ActionCreator(this, actionName, implementation, obj);
1476+
1477+
var action = newAction[ACTION_HANDLER];
1478+
action.defer = function () {
1479+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
1480+
args[_key] = arguments[_key];
1481+
}
1482+
1483+
setTimeout(function () {
1484+
newAction[ACTION_HANDLER].apply(null, args);
1485+
});
1486+
};
1487+
action[ACTION_KEY] = actionName;
1488+
return action;
1489+
}
1490+
},
14681491
createActions: {
14691492
value: function createActions(ActionsClass) {
14701493
var _this8 = this;
@@ -1518,28 +1541,9 @@ var Alt = (function () {
15181541
}
15191542

15201543
return Object.keys(actions).reduce(function (obj, action) {
1544+
obj[action] = _this8.createAction(action, actions[action], obj);
15211545
var constant = formatAsConstant(action);
1522-
var actionId = uid(GlobalActionsNameRegistry, "" + key + "#" + action);
1523-
GlobalActionsNameRegistry[actionId] = 1;
1524-
var actionName = Symbol["for"](actionId);
1525-
1526-
// Wrap the action so we can provide a dispatch method
1527-
var newAction = new ActionCreator(_this8, actionName, actions[action], obj);
1528-
1529-
// Set all the properties on action
1530-
obj[action] = newAction[ACTION_HANDLER];
1531-
obj[action].defer = function () {
1532-
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
1533-
args[_key] = arguments[_key];
1534-
}
1535-
1536-
setTimeout(function () {
1537-
newAction[ACTION_HANDLER].apply(null, args);
1538-
});
1539-
};
1540-
obj[action][ACTION_KEY] = actionName;
1541-
obj[constant] = actionName;
1542-
1546+
obj[constant] = obj[action][ACTION_KEY];
15431547
return obj;
15441548
}, exportObj);
15451549
}

dist/alt-browser.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,29 @@ var Alt = (function () {
12091209
});
12101210
}
12111211
},
1212+
createAction: {
1213+
value: function createAction(name, implementation, obj) {
1214+
var actionId = uid(GlobalActionsNameRegistry, "#" + name);
1215+
GlobalActionsNameRegistry[actionId] = 1;
1216+
var actionName = Symbol["for"](actionId);
1217+
1218+
// Wrap the action so we can provide a dispatch method
1219+
var newAction = new ActionCreator(this, actionName, implementation, obj);
1220+
1221+
var action = newAction[ACTION_HANDLER];
1222+
action.defer = function () {
1223+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
1224+
args[_key] = arguments[_key];
1225+
}
1226+
1227+
setTimeout(function () {
1228+
newAction[ACTION_HANDLER].apply(null, args);
1229+
});
1230+
};
1231+
action[ACTION_KEY] = actionName;
1232+
return action;
1233+
}
1234+
},
12121235
createActions: {
12131236
value: function createActions(ActionsClass) {
12141237
var _this8 = this;
@@ -1262,28 +1285,9 @@ var Alt = (function () {
12621285
}
12631286

12641287
return Object.keys(actions).reduce(function (obj, action) {
1288+
obj[action] = _this8.createAction(action, actions[action], obj);
12651289
var constant = formatAsConstant(action);
1266-
var actionId = uid(GlobalActionsNameRegistry, "" + key + "#" + action);
1267-
GlobalActionsNameRegistry[actionId] = 1;
1268-
var actionName = Symbol["for"](actionId);
1269-
1270-
// Wrap the action so we can provide a dispatch method
1271-
var newAction = new ActionCreator(_this8, actionName, actions[action], obj);
1272-
1273-
// Set all the properties on action
1274-
obj[action] = newAction[ACTION_HANDLER];
1275-
obj[action].defer = function () {
1276-
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
1277-
args[_key] = arguments[_key];
1278-
}
1279-
1280-
setTimeout(function () {
1281-
newAction[ACTION_HANDLER].apply(null, args);
1282-
});
1283-
};
1284-
obj[action][ACTION_KEY] = actionName;
1285-
obj[constant] = actionName;
1286-
1290+
obj[constant] = obj[action][ACTION_KEY];
12871291
return obj;
12881292
}, exportObj);
12891293
}

dist/alt-with-runtime.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,29 @@ var Alt = (function () {
460460
});
461461
}
462462
},
463+
createAction: {
464+
value: function createAction(name, implementation, obj) {
465+
var actionId = uid(GlobalActionsNameRegistry, "#" + name);
466+
GlobalActionsNameRegistry[actionId] = 1;
467+
var actionName = Symbol["for"](actionId);
468+
469+
// Wrap the action so we can provide a dispatch method
470+
var newAction = new ActionCreator(this, actionName, implementation, obj);
471+
472+
var action = newAction[ACTION_HANDLER];
473+
action.defer = function () {
474+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
475+
args[_key] = arguments[_key];
476+
}
477+
478+
setTimeout(function () {
479+
newAction[ACTION_HANDLER].apply(null, args);
480+
});
481+
};
482+
action[ACTION_KEY] = actionName;
483+
return action;
484+
}
485+
},
463486
createActions: {
464487
value: function createActions(ActionsClass) {
465488
var _this8 = this;
@@ -511,28 +534,9 @@ var Alt = (function () {
511534
}
512535

513536
return Object.keys(actions).reduce(function (obj, action) {
537+
obj[action] = _this8.createAction(action, actions[action], obj);
514538
var constant = formatAsConstant(action);
515-
var actionId = uid(GlobalActionsNameRegistry, "" + key + "#" + action);
516-
GlobalActionsNameRegistry[actionId] = 1;
517-
var actionName = Symbol["for"](actionId);
518-
519-
// Wrap the action so we can provide a dispatch method
520-
var newAction = new ActionCreator(_this8, actionName, actions[action], obj);
521-
522-
// Set all the properties on action
523-
obj[action] = newAction[ACTION_HANDLER];
524-
obj[action].defer = function () {
525-
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
526-
args[_key] = arguments[_key];
527-
}
528-
529-
setTimeout(function () {
530-
newAction[ACTION_HANDLER].apply(null, args);
531-
});
532-
};
533-
obj[action][ACTION_KEY] = actionName;
534-
obj[constant] = actionName;
535-
539+
obj[constant] = obj[action][ACTION_KEY];
536540
return obj;
537541
}, exportObj);
538542
}

dist/alt.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,29 @@ var Alt = (function () {
476476
});
477477
}
478478
},
479+
createAction: {
480+
value: function createAction(name, implementation, obj) {
481+
var actionId = uid(GlobalActionsNameRegistry, "#" + name);
482+
GlobalActionsNameRegistry[actionId] = 1;
483+
var actionName = Symbol["for"](actionId);
484+
485+
// Wrap the action so we can provide a dispatch method
486+
var newAction = new ActionCreator(this, actionName, implementation, obj);
487+
488+
var action = newAction[ACTION_HANDLER];
489+
action.defer = function () {
490+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
491+
args[_key] = arguments[_key];
492+
}
493+
494+
setTimeout(function () {
495+
newAction[ACTION_HANDLER].apply(null, args);
496+
});
497+
};
498+
action[ACTION_KEY] = actionName;
499+
return action;
500+
}
501+
},
479502
createActions: {
480503
value: function createActions(ActionsClass) {
481504
var _this8 = this;
@@ -529,28 +552,9 @@ var Alt = (function () {
529552
}
530553

531554
return Object.keys(actions).reduce(function (obj, action) {
555+
obj[action] = _this8.createAction(action, actions[action], obj);
532556
var constant = formatAsConstant(action);
533-
var actionId = uid(GlobalActionsNameRegistry, "" + key + "#" + action);
534-
GlobalActionsNameRegistry[actionId] = 1;
535-
var actionName = Symbol["for"](actionId);
536-
537-
// Wrap the action so we can provide a dispatch method
538-
var newAction = new ActionCreator(_this8, actionName, actions[action], obj);
539-
540-
// Set all the properties on action
541-
obj[action] = newAction[ACTION_HANDLER];
542-
obj[action].defer = function () {
543-
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
544-
args[_key] = arguments[_key];
545-
}
546-
547-
setTimeout(function () {
548-
newAction[ACTION_HANDLER].apply(null, args);
549-
});
550-
};
551-
obj[action][ACTION_KEY] = actionName;
552-
obj[constant] = actionName;
553-
557+
obj[constant] = obj[action][ACTION_KEY];
554558
return obj;
555559
}, exportObj);
556560
}

src/alt.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,24 @@ class Alt {
433433
})
434434
}
435435

436+
createAction(name, implementation, obj) {
437+
const actionId = uid(GlobalActionsNameRegistry, `#${name}`)
438+
GlobalActionsNameRegistry[actionId] = 1
439+
const actionName = Symbol.for(actionId)
440+
441+
// Wrap the action so we can provide a dispatch method
442+
const newAction = new ActionCreator(this, actionName, implementation, obj)
443+
444+
const action = newAction[ACTION_HANDLER]
445+
action.defer = (...args) => {
446+
setTimeout(() => {
447+
newAction[ACTION_HANDLER].apply(null, args)
448+
})
449+
}
450+
action[ACTION_KEY] = actionName
451+
return action
452+
}
453+
436454
createActions(ActionsClass, exportObj = {}) {
437455
const actions = {}
438456
const key = ActionsClass.name || ActionsClass.displayName || ''
@@ -460,29 +478,9 @@ class Alt {
460478
}
461479

462480
return Object.keys(actions).reduce((obj, action) => {
481+
obj[action] = this.createAction(action, actions[action], obj)
463482
const constant = formatAsConstant(action)
464-
const actionId = uid(GlobalActionsNameRegistry, `${key}#${action}`)
465-
GlobalActionsNameRegistry[actionId] = 1
466-
const actionName = Symbol.for(actionId)
467-
468-
// Wrap the action so we can provide a dispatch method
469-
const newAction = new ActionCreator(
470-
this,
471-
actionName,
472-
actions[action],
473-
obj
474-
)
475-
476-
// Set all the properties on action
477-
obj[action] = newAction[ACTION_HANDLER]
478-
obj[action].defer = (...args) => {
479-
setTimeout(() => {
480-
newAction[ACTION_HANDLER].apply(null, args)
481-
})
482-
}
483-
obj[action][ACTION_KEY] = actionName
484-
obj[constant] = actionName
485-
483+
obj[constant] = obj[action][ACTION_KEY]
486484
return obj
487485
}, exportObj)
488486
}

test/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,26 @@ const tests = {
12371237
alt.bootstrap('{"AStoreThatIJustMadeUpButDoesNotReallyExist": {}}')
12381238
})
12391239
},
1240+
1241+
'creating one off actions'() {
1242+
const action = alt.createAction('hello', function (x) {
1243+
this.dispatch(x)
1244+
})
1245+
1246+
const store = alt.createStore({
1247+
state: { x: 0 },
1248+
bindListeners: {
1249+
hello: action
1250+
},
1251+
hello(x) { this.state.x = x }
1252+
})
1253+
1254+
assert.isFunction(action, 'action was created')
1255+
1256+
action(1)
1257+
1258+
assert(store.getState().x === 1, 'action fires correctly')
1259+
},
12401260
}
12411261

12421262
export default tests

0 commit comments

Comments
 (0)