Skip to content

Commit

Permalink
Define actions as plain JS objects
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 7, 2015
1 parent 06d07e1 commit d99a872
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 126 deletions.
62 changes: 35 additions & 27 deletions dist/alt-browser-with-addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -1471,43 +1471,51 @@ var Alt = (function () {

var exportObj = arguments[1] === undefined ? {} : arguments[1];

var actions = assign({}, getInternalMethods(ActionsClass.prototype, builtInProto));
var actions = {};
var key = ActionsClass.name || ActionsClass.displayName || "";

var ActionsGenerator = (function (_ActionsClass) {
function ActionsGenerator(alt) {
_classCallCheck(this, ActionsGenerator);
if (typeof ActionsClass === "function") {
(function () {
assign(actions, getInternalMethods(ActionsClass.prototype, builtInProto));

_get(Object.getPrototypeOf(ActionsGenerator.prototype), "constructor", this).call(this, alt);
}
var ActionsGenerator = (function (_ActionsClass) {
function ActionsGenerator(alt) {
_classCallCheck(this, ActionsGenerator);

_inherits(ActionsGenerator, _ActionsClass);
_get(Object.getPrototypeOf(ActionsGenerator.prototype), "constructor", this).call(this, alt);
}

_createClass(ActionsGenerator, {
generateActions: {
value: function generateActions() {
for (var _len = arguments.length, actionNames = Array(_len), _key = 0; _key < _len; _key++) {
actionNames[_key] = arguments[_key];
}
_inherits(ActionsGenerator, _ActionsClass);

actionNames.forEach(function (actionName) {
// This is a function so we can later bind this to ActionCreator
actions[actionName] = function (x) {
for (var _len2 = arguments.length, a = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
a[_key2 - 1] = arguments[_key2];
_createClass(ActionsGenerator, {
generateActions: {
value: function generateActions() {
for (var _len = arguments.length, actionNames = Array(_len), _key = 0; _key < _len; _key++) {
actionNames[_key] = arguments[_key];
}

this.dispatch(a.length ? [x].concat(a) : x);
};
});
}
}
});
actionNames.forEach(function (actionName) {
// This is a function so we can later bind this to ActionCreator
actions[actionName] = function (x) {
for (var _len2 = arguments.length, a = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
a[_key2 - 1] = arguments[_key2];
}

this.dispatch(a.length ? [x].concat(a) : x);
};
});
}
}
});

return ActionsGenerator;
})(ActionsClass);
return ActionsGenerator;
})(ActionsClass);

new ActionsGenerator(this);
new ActionsGenerator(_this8);
})();
} else {
assign(actions, ActionsClass);
}

return Object.keys(actions).reduce(function (obj, action) {
var constant = formatAsConstant(action);
Expand Down
62 changes: 35 additions & 27 deletions dist/alt-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1215,43 +1215,51 @@ var Alt = (function () {

var exportObj = arguments[1] === undefined ? {} : arguments[1];

var actions = assign({}, getInternalMethods(ActionsClass.prototype, builtInProto));
var actions = {};
var key = ActionsClass.name || ActionsClass.displayName || "";

var ActionsGenerator = (function (_ActionsClass) {
function ActionsGenerator(alt) {
_classCallCheck(this, ActionsGenerator);
if (typeof ActionsClass === "function") {
(function () {
assign(actions, getInternalMethods(ActionsClass.prototype, builtInProto));

_get(Object.getPrototypeOf(ActionsGenerator.prototype), "constructor", this).call(this, alt);
}
var ActionsGenerator = (function (_ActionsClass) {
function ActionsGenerator(alt) {
_classCallCheck(this, ActionsGenerator);

_inherits(ActionsGenerator, _ActionsClass);
_get(Object.getPrototypeOf(ActionsGenerator.prototype), "constructor", this).call(this, alt);
}

_createClass(ActionsGenerator, {
generateActions: {
value: function generateActions() {
for (var _len = arguments.length, actionNames = Array(_len), _key = 0; _key < _len; _key++) {
actionNames[_key] = arguments[_key];
}
_inherits(ActionsGenerator, _ActionsClass);

actionNames.forEach(function (actionName) {
// This is a function so we can later bind this to ActionCreator
actions[actionName] = function (x) {
for (var _len2 = arguments.length, a = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
a[_key2 - 1] = arguments[_key2];
_createClass(ActionsGenerator, {
generateActions: {
value: function generateActions() {
for (var _len = arguments.length, actionNames = Array(_len), _key = 0; _key < _len; _key++) {
actionNames[_key] = arguments[_key];
}

this.dispatch(a.length ? [x].concat(a) : x);
};
});
}
}
});
actionNames.forEach(function (actionName) {
// This is a function so we can later bind this to ActionCreator
actions[actionName] = function (x) {
for (var _len2 = arguments.length, a = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
a[_key2 - 1] = arguments[_key2];
}

this.dispatch(a.length ? [x].concat(a) : x);
};
});
}
}
});

return ActionsGenerator;
})(ActionsClass);
return ActionsGenerator;
})(ActionsClass);

new ActionsGenerator(this);
new ActionsGenerator(_this8);
})();
} else {
assign(actions, ActionsClass);
}

return Object.keys(actions).reduce(function (obj, action) {
var constant = formatAsConstant(action);
Expand Down
64 changes: 36 additions & 28 deletions dist/alt-with-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,41 +466,49 @@ var Alt = (function () {

var exportObj = arguments[1] === undefined ? {} : arguments[1];

var actions = assign({}, getInternalMethods(ActionsClass.prototype, builtInProto));
var actions = {};
var key = ActionsClass.name || ActionsClass.displayName || "";

var ActionsGenerator = (function (_ActionsClass) {
function ActionsGenerator(alt) {
babelHelpers.classCallCheck(this, ActionsGenerator);
if (typeof ActionsClass === "function") {
(function () {
assign(actions, getInternalMethods(ActionsClass.prototype, builtInProto));

babelHelpers.get(Object.getPrototypeOf(ActionsGenerator.prototype), "constructor", this).call(this, alt);
}
var ActionsGenerator = (function (_ActionsClass) {
function ActionsGenerator(alt) {
babelHelpers.classCallCheck(this, ActionsGenerator);

babelHelpers.inherits(ActionsGenerator, _ActionsClass);
babelHelpers.createClass(ActionsGenerator, {
generateActions: {
value: function generateActions() {
for (var _len = arguments.length, actionNames = Array(_len), _key = 0; _key < _len; _key++) {
actionNames[_key] = arguments[_key];
}
babelHelpers.get(Object.getPrototypeOf(ActionsGenerator.prototype), "constructor", this).call(this, alt);
}

actionNames.forEach(function (actionName) {
// This is a function so we can later bind this to ActionCreator
actions[actionName] = function (x) {
for (var _len2 = arguments.length, a = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
a[_key2 - 1] = arguments[_key2];
babelHelpers.inherits(ActionsGenerator, _ActionsClass);
babelHelpers.createClass(ActionsGenerator, {
generateActions: {
value: function generateActions() {
for (var _len = arguments.length, actionNames = Array(_len), _key = 0; _key < _len; _key++) {
actionNames[_key] = arguments[_key];
}

this.dispatch(a.length ? [x].concat(a) : x);
};
});
}
}
});
return ActionsGenerator;
})(ActionsClass);

new ActionsGenerator(this);
actionNames.forEach(function (actionName) {
// This is a function so we can later bind this to ActionCreator
actions[actionName] = function (x) {
for (var _len2 = arguments.length, a = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
a[_key2 - 1] = arguments[_key2];
}

this.dispatch(a.length ? [x].concat(a) : x);
};
});
}
}
});
return ActionsGenerator;
})(ActionsClass);

new ActionsGenerator(_this8);
})();
} else {
assign(actions, ActionsClass);
}

return Object.keys(actions).reduce(function (obj, action) {
var constant = formatAsConstant(action);
Expand Down
62 changes: 35 additions & 27 deletions dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,43 +482,51 @@ var Alt = (function () {

var exportObj = arguments[1] === undefined ? {} : arguments[1];

var actions = assign({}, getInternalMethods(ActionsClass.prototype, builtInProto));
var actions = {};
var key = ActionsClass.name || ActionsClass.displayName || "";

var ActionsGenerator = (function (_ActionsClass) {
function ActionsGenerator(alt) {
_classCallCheck(this, ActionsGenerator);
if (typeof ActionsClass === "function") {
(function () {
assign(actions, getInternalMethods(ActionsClass.prototype, builtInProto));

_get(Object.getPrototypeOf(ActionsGenerator.prototype), "constructor", this).call(this, alt);
}
var ActionsGenerator = (function (_ActionsClass) {
function ActionsGenerator(alt) {
_classCallCheck(this, ActionsGenerator);

_inherits(ActionsGenerator, _ActionsClass);
_get(Object.getPrototypeOf(ActionsGenerator.prototype), "constructor", this).call(this, alt);
}

_createClass(ActionsGenerator, {
generateActions: {
value: function generateActions() {
for (var _len = arguments.length, actionNames = Array(_len), _key = 0; _key < _len; _key++) {
actionNames[_key] = arguments[_key];
}
_inherits(ActionsGenerator, _ActionsClass);

actionNames.forEach(function (actionName) {
// This is a function so we can later bind this to ActionCreator
actions[actionName] = function (x) {
for (var _len2 = arguments.length, a = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
a[_key2 - 1] = arguments[_key2];
_createClass(ActionsGenerator, {
generateActions: {
value: function generateActions() {
for (var _len = arguments.length, actionNames = Array(_len), _key = 0; _key < _len; _key++) {
actionNames[_key] = arguments[_key];
}

this.dispatch(a.length ? [x].concat(a) : x);
};
});
}
}
});
actionNames.forEach(function (actionName) {
// This is a function so we can later bind this to ActionCreator
actions[actionName] = function (x) {
for (var _len2 = arguments.length, a = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
a[_key2 - 1] = arguments[_key2];
}

this.dispatch(a.length ? [x].concat(a) : x);
};
});
}
}
});

return ActionsGenerator;
})(ActionsClass);
return ActionsGenerator;
})(ActionsClass);

new ActionsGenerator(this);
new ActionsGenerator(_this8);
})();
} else {
assign(actions, ActionsClass);
}

return Object.keys(actions).reduce(function (obj, action) {
var constant = formatAsConstant(action);
Expand Down
36 changes: 19 additions & 17 deletions src/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,28 +434,30 @@ class Alt {
}

createActions(ActionsClass, exportObj = {}) {
const actions = assign(
{},
getInternalMethods(ActionsClass.prototype, builtInProto)
)
const actions = {}
const key = ActionsClass.name || ActionsClass.displayName || ''

class ActionsGenerator extends ActionsClass {
constructor(alt) {
super(alt)
}
if (typeof ActionsClass === 'function') {
assign(actions, getInternalMethods(ActionsClass.prototype, builtInProto))
class ActionsGenerator extends ActionsClass {
constructor(alt) {
super(alt)
}

generateActions(...actionNames) {
actionNames.forEach((actionName) => {
// This is a function so we can later bind this to ActionCreator
actions[actionName] = function (x, ...a) {
this.dispatch(a.length ? [x].concat(a) : x)
}
})
generateActions(...actionNames) {
actionNames.forEach((actionName) => {
// This is a function so we can later bind this to ActionCreator
actions[actionName] = function (x, ...a) {
this.dispatch(a.length ? [x].concat(a) : x)
}
})
}
}
}

new ActionsGenerator(this)
new ActionsGenerator(this)
} else {
assign(actions, ActionsClass)
}

return Object.keys(actions).reduce((obj, action) => {
const constant = formatAsConstant(action)
Expand Down
7 changes: 7 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class MyActions {
const myActions = {}
alt.createActions(MyActions, myActions)

const objActions = alt.createActions({
hello() { },
world() { }
})

const myShorthandActions = alt.generateActions("actionOne", "actionTwo")

class MyStore {
Expand Down Expand Up @@ -427,6 +432,8 @@ const tests = {
assert(myActions.updateTwo.length === 2, 'actions can have > 1 arity')
assert.isFunction(myShorthandActions.actionOne, 'action created with shorthand createActions exists')
assert.isFunction(myShorthandActions.actionTwo, 'other action created with shorthand createActions exists')
assert.isFunction(objActions.hello, 'actions created by obj are functions')
assert.isFunction(objActions.world, 'actions created by obj are functions')
},

'existence of constants'() {
Expand Down

0 comments on commit d99a872

Please sign in to comment.