1 | 'use strict';
|
2 |
|
3 | function checkIsEmpty(arg) {
|
4 | return arg == null;
|
5 | }
|
6 | function throwIsEmpty(argPosition) {
|
7 | throw new Error("Argument " + argPosition + " is empty.");
|
8 | }
|
9 | function checkValidActionCreator(arg) {
|
10 | return typeof arg === 'function' && 'getType' in arg;
|
11 | }
|
12 | function checkInvalidActionCreator(arg) {
|
13 | return !checkValidActionCreator(arg);
|
14 | }
|
15 | function throwInvalidActionCreator(argPosition) {
|
16 | throw new Error("Argument " + argPosition + " is invalid, it should be an action-creator instance from \"typesafe-actions\"");
|
17 | }
|
18 | function checkInvalidActionCreatorInArray(arg, idx) {
|
19 | if (arg == null) {
|
20 | throw new Error("Argument contains array with empty element at index " + idx);
|
21 | } else if (arg.getType == null) {
|
22 | throw new Error("Argument contains array with invalid element at index " + idx + ", it should be an action-creator instance from \"typesafe-actions\"");
|
23 | }
|
24 | }
|
25 | function checkValidActionType(arg) {
|
26 | return typeof arg === 'string' || typeof arg === 'symbol';
|
27 | }
|
28 | function checkInvalidActionType(arg) {
|
29 | return !checkValidActionType(arg);
|
30 | }
|
31 | function throwInvalidActionType(argPosition) {
|
32 | throw new Error("Argument " + argPosition + " is invalid, it should be an action type of type: string | symbol");
|
33 | }
|
34 | function checkInvalidActionTypeInArray(arg, idx) {
|
35 | if (arg == null) {
|
36 | throw new Error("Argument contains array with empty element at index " + idx);
|
37 | } else if (typeof arg !== 'string' && typeof arg !== 'symbol') {
|
38 | throw new Error("Argument contains array with invalid element at index " + idx + ", it should be of type: string | symbol");
|
39 | }
|
40 | }
|
41 | function throwInvalidActionTypeOrActionCreator(argPosition) {
|
42 | throw new Error("Argument " + argPosition + " is invalid, it should be an action-creator instance from \"typesafe-actions\" or action type of type: string | symbol");
|
43 | }
|
44 |
|
45 | function action(type, payload, meta, error) {
|
46 | if (checkIsEmpty(type)) {
|
47 | throwIsEmpty(1);
|
48 | }
|
49 |
|
50 | if (checkInvalidActionType(type)) {
|
51 | throwInvalidActionCreator(1);
|
52 | }
|
53 |
|
54 | return {
|
55 | type: type,
|
56 | payload: payload,
|
57 | meta: meta,
|
58 | error: error
|
59 | };
|
60 | }
|
61 |
|
62 | function createCustomAction(type, createHandler) {
|
63 | if (checkIsEmpty(type)) {
|
64 | throwIsEmpty(1);
|
65 | }
|
66 |
|
67 | if (checkInvalidActionType(type)) {
|
68 | throwInvalidActionType(1);
|
69 | }
|
70 |
|
71 | var actionCreator = function actionCreator() {
|
72 | var customProps = createHandler != null ? createHandler.apply(void 0, arguments) : undefined;
|
73 | return Object.assign({
|
74 | type: type
|
75 | }, customProps);
|
76 | };
|
77 |
|
78 | var typeMeta = {
|
79 | getType: function getType() {
|
80 | return type;
|
81 | },
|
82 | toString: function toString() {
|
83 | return type;
|
84 | }
|
85 | };
|
86 | return Object.assign(actionCreator, typeMeta);
|
87 | }
|
88 |
|
89 | function createAction(type, payloadCreator, metaCreator) {
|
90 | if (checkIsEmpty(type)) {
|
91 | throwIsEmpty(1);
|
92 | }
|
93 |
|
94 | if (checkInvalidActionType(type)) {
|
95 | throwInvalidActionType(1);
|
96 | }
|
97 |
|
98 | return function () {
|
99 | return createCustomAction(type, function () {
|
100 | var payload = arguments.length <= 0 ? undefined : arguments[0];
|
101 | var meta = arguments.length <= 1 ? undefined : arguments[1];
|
102 |
|
103 | if (payloadCreator != null || metaCreator != null) {
|
104 | payload = payloadCreator != null ? payloadCreator.apply(void 0, arguments) : undefined;
|
105 | meta = metaCreator != null ? metaCreator.apply(void 0, arguments) : undefined;
|
106 | }
|
107 |
|
108 | return Object.assign({}, payload !== undefined && {
|
109 | payload: payload
|
110 | }, {}, meta !== undefined && {
|
111 | meta: meta
|
112 | });
|
113 | });
|
114 | };
|
115 | }
|
116 |
|
117 | function throwInvalidAsyncActionArgument(argPosition) {
|
118 | throw new Error("Argument " + argPosition + " is invalid, it should be an action type of \"string | symbol\" or a tuple of \"[string | symbol, Function, Function?]\"");
|
119 | }
|
120 | function createAsyncAction(requestArg, successArg, failureArg, cancelArg) {
|
121 | var constructor = function constructor() {
|
122 | var results = [requestArg, successArg, failureArg, cancelArg].map(function (arg, index) {
|
123 | if (Array.isArray(arg)) {
|
124 | return createAction(arg[0], arg[1], arg[2])();
|
125 | } else if (typeof arg === 'string' || typeof arg === 'symbol') {
|
126 | return createAction(arg)();
|
127 | } else if (index < 3) {
|
128 | throwInvalidAsyncActionArgument(index);
|
129 | }
|
130 | });
|
131 | var request = results[0],
|
132 | success = results[1],
|
133 | failure = results[2],
|
134 | cancel = results[3];
|
135 | return {
|
136 | request: request,
|
137 | success: success,
|
138 | failure: failure,
|
139 | cancel: cancel
|
140 | };
|
141 | };
|
142 |
|
143 | return constructor;
|
144 | }
|
145 |
|
146 | function getType(actionCreator) {
|
147 | if (checkIsEmpty(actionCreator)) {
|
148 | throwIsEmpty(1);
|
149 | }
|
150 |
|
151 | if (checkInvalidActionCreator(actionCreator)) {
|
152 | throwInvalidActionCreator(1);
|
153 | }
|
154 |
|
155 | return actionCreator.getType();
|
156 | }
|
157 |
|
158 | function createReducer(initialState, initialHandlers) {
|
159 | if (initialHandlers === void 0) {
|
160 | initialHandlers = {};
|
161 | }
|
162 |
|
163 | var handlers = Object.assign({}, initialHandlers);
|
164 |
|
165 | var rootReducer = function rootReducer(state, action) {
|
166 | if (state === void 0) {
|
167 | state = initialState;
|
168 | }
|
169 |
|
170 | if (handlers.hasOwnProperty(action.type)) {
|
171 | var reducer = handlers[action.type];
|
172 |
|
173 | if (typeof reducer !== 'function') {
|
174 | throw Error("Reducer under \"" + action.type + "\" key is not a valid reducer");
|
175 | }
|
176 |
|
177 | return reducer(state, action);
|
178 | } else {
|
179 | return state;
|
180 | }
|
181 | };
|
182 |
|
183 | var reducerHandler = function reducerHandler(singleOrMultipleCreatorsAndTypes, reducer) {
|
184 | var creatorsAndTypes = Array.isArray(singleOrMultipleCreatorsAndTypes) ? singleOrMultipleCreatorsAndTypes : [singleOrMultipleCreatorsAndTypes];
|
185 | var newHandlers = {};
|
186 | creatorsAndTypes.map(function (acOrType, index) {
|
187 | return checkValidActionCreator(acOrType) ? getType(acOrType) : checkValidActionType(acOrType) ? acOrType : throwInvalidActionTypeOrActionCreator(index + 1);
|
188 | }).forEach(function (type) {
|
189 | return newHandlers[type] = reducer;
|
190 | });
|
191 | return createReducer(initialState, Object.assign({}, handlers, {}, newHandlers));
|
192 | };
|
193 |
|
194 | var chainApi = Object.assign(rootReducer, {
|
195 | handlers: Object.assign({}, handlers),
|
196 | handleAction: reducerHandler,
|
197 | handleType: reducerHandler
|
198 | });
|
199 | return chainApi;
|
200 | }
|
201 |
|
202 | function isOfType(actionTypeOrTypes, action) {
|
203 | if (checkIsEmpty(actionTypeOrTypes)) {
|
204 | throwIsEmpty(1);
|
205 | }
|
206 |
|
207 | var actionTypes = Array.isArray(actionTypeOrTypes) ? actionTypeOrTypes : [actionTypeOrTypes];
|
208 | actionTypes.forEach(checkInvalidActionTypeInArray);
|
209 |
|
210 | var assertFn = function assertFn(_action) {
|
211 | return actionTypes.includes(_action.type);
|
212 | };
|
213 |
|
214 | if (action === undefined) {
|
215 | return assertFn;
|
216 | }
|
217 |
|
218 | return assertFn(action);
|
219 | }
|
220 |
|
221 | function isActionOf(actionCreatorOrCreators, action) {
|
222 | if (checkIsEmpty(actionCreatorOrCreators)) {
|
223 | throwIsEmpty(1);
|
224 | }
|
225 |
|
226 | var actionCreators = Array.isArray(actionCreatorOrCreators) ? actionCreatorOrCreators : [actionCreatorOrCreators];
|
227 | actionCreators.forEach(checkInvalidActionCreatorInArray);
|
228 |
|
229 | var assertFn = function assertFn(_action) {
|
230 | return actionCreators.some(function (actionCreator) {
|
231 | return _action.type === actionCreator.getType();
|
232 | });
|
233 | };
|
234 |
|
235 | if (action === undefined) {
|
236 | return assertFn;
|
237 | }
|
238 |
|
239 | return assertFn(action);
|
240 | }
|
241 |
|
242 | function createAction$1(type, createHandler) {
|
243 | var actionCreator = createHandler == null ? function () {
|
244 | return action(type);
|
245 | } : createHandler(action.bind(null, type));
|
246 | return Object.assign(actionCreator, {
|
247 | getType: function getType() {
|
248 | return type;
|
249 | },
|
250 | toString: function toString() {
|
251 | return type;
|
252 | }
|
253 | });
|
254 | }
|
255 |
|
256 | function createCustomAction$1(type, createHandler) {
|
257 | if (checkIsEmpty(type)) {
|
258 | throwIsEmpty(1);
|
259 | }
|
260 |
|
261 | if (checkInvalidActionType(type)) {
|
262 | throwInvalidActionType(1);
|
263 | }
|
264 |
|
265 | var actionCreator = createHandler != null ? createHandler(type) : function () {
|
266 | return {
|
267 | type: type
|
268 | };
|
269 | };
|
270 | return Object.assign(actionCreator, {
|
271 | getType: function getType() {
|
272 | return type;
|
273 | },
|
274 | toString: function toString() {
|
275 | return type;
|
276 | }
|
277 | });
|
278 | }
|
279 |
|
280 | function createStandardAction(type) {
|
281 | if (checkIsEmpty(type)) {
|
282 | throwIsEmpty(1);
|
283 | }
|
284 |
|
285 | if (checkInvalidActionType(type)) {
|
286 | throwInvalidActionType(1);
|
287 | }
|
288 |
|
289 | function constructor() {
|
290 | return createCustomAction$1(type, function (_type) {
|
291 | return function (payload, meta) {
|
292 | return {
|
293 | type: _type,
|
294 | payload: payload,
|
295 | meta: meta
|
296 | };
|
297 | };
|
298 | });
|
299 | }
|
300 |
|
301 | function map(fn) {
|
302 | return createCustomAction$1(type, function (_type) {
|
303 | return function (payload, meta) {
|
304 | return Object.assign(fn(payload, meta), {
|
305 | type: _type
|
306 | });
|
307 | };
|
308 | });
|
309 | }
|
310 |
|
311 | return Object.assign(constructor, {
|
312 | map: map
|
313 | });
|
314 | }
|
315 |
|
316 | var index = {
|
317 | createAction: createAction$1,
|
318 | createCustomAction: createCustomAction$1,
|
319 | createStandardAction: createStandardAction
|
320 | };
|
321 |
|
322 | exports.action = action;
|
323 | exports.createAction = createAction;
|
324 | exports.createAsyncAction = createAsyncAction;
|
325 | exports.createCustomAction = createCustomAction;
|
326 | exports.createReducer = createReducer;
|
327 | exports.deprecated = index;
|
328 | exports.getType = getType;
|
329 | exports.isActionOf = isActionOf;
|
330 | exports.isOfType = isOfType;
|
331 |
|