Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80508 views
1
'use strict';
2
3
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj['default'] : obj; };
4
5
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } };
6
7
/**
8
* Actions
9
*
10
* Instances of the Actions class represent a set of actions. (In Flux parlance,
11
* these might be more accurately denoted as Action Creators, while Action
12
* refers to the payload sent to the dispatcher, but this is... confusing. We
13
* will use Action to mean the function you call to trigger a dispatch.)
14
*
15
* Create actions by extending from the base Actions class and adding methods.
16
* All methods on the prototype (except the constructor) will be
17
* converted into actions. The return value of an action is used as the body
18
* of the payload sent to the dispatcher.
19
*/
20
21
var _uniqueId = require('uniqueid');
22
23
var uniqueId = _interopRequire(_uniqueId);
24
25
var Actions = (function () {
26
function Actions() {
27
_classCallCheck(this, Actions);
28
29
this._baseId = uniqueId();
30
31
var methodNames = this._getActionMethodNames();
32
for (var i = 0; i < methodNames.length; i++) {
33
var methodName = methodNames[i];
34
this._wrapAction(methodName);
35
}
36
37
this.getConstants = this.getActionIds;
38
}
39
40
Actions.prototype.getActionIds = function getActionIds() {
41
var _this = this;
42
43
return this._getActionMethodNames().reduce(function (result, actionName) {
44
result[actionName] = _this[actionName]._id;
45
return result;
46
}, {});
47
};
48
49
Actions.prototype._getActionMethodNames = function _getActionMethodNames(instance) {
50
var _this2 = this;
51
52
return Object.getOwnPropertyNames(this.constructor.prototype).filter(function (name) {
53
return name !== 'constructor' && typeof _this2[name] === 'function';
54
});
55
};
56
57
Actions.prototype._wrapAction = function _wrapAction(methodName) {
58
var _this3 = this;
59
60
var originalMethod = this[methodName];
61
var actionId = this._createActionId(methodName);
62
63
var action = function action() {
64
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
65
args[_key] = arguments[_key];
66
}
67
68
var body = originalMethod.apply(_this3, args);
69
70
if (isPromise(body)) {
71
var promise = body;
72
_this3._dispatchAsync(actionId, promise, args, methodName);
73
} else {
74
_this3._dispatch(actionId, body, args, methodName);
75
}
76
77
// Return original method's return value to caller
78
return body;
79
};
80
81
action._id = actionId;
82
83
this[methodName] = action;
84
};
85
86
/**
87
* Create unique string constant for an action method, using
88
* @param {string} methodName - Name of the action method
89
*/
90
91
Actions.prototype._createActionId = function _createActionId(methodName) {
92
return '' + this._baseId + '-' + methodName;
93
};
94
95
Actions.prototype._dispatch = function _dispatch(actionId, body, args, methodName) {
96
if (typeof this.dispatch === 'function') {
97
if (typeof body !== 'undefined') {
98
this.dispatch(actionId, body, args);
99
}
100
} else {
101
if (process.env.NODE_ENV !== 'production') {
102
console.warn('You\'ve attempted to perform the action ' + ('' + this.constructor.name + '#' + methodName + ', but it hasn\'t been added ') + 'to a Flux instance.');
103
}
104
}
105
106
return body;
107
};
108
109
Actions.prototype._dispatchAsync = function _dispatchAsync(actionId, promise, args, methodName) {
110
if (typeof this.dispatchAsync === 'function') {
111
this.dispatchAsync(actionId, promise, args);
112
} else {
113
if (process.env.NODE_ENV !== 'production') {
114
console.warn('You\'ve attempted to perform the asynchronous action ' + ('' + this.constructor.name + '#' + methodName + ', but it hasn\'t been added ') + 'to a Flux instance.');
115
}
116
}
117
};
118
119
return Actions;
120
})();
121
122
module.exports = Actions;
123
124
function isPromise(value) {
125
return value && typeof value.then === 'function';
126
}
127