-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
256 lines (235 loc) · 7.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var typeIndex = 0;
function createRandomType() {
typeIndex++;
return 'RAND_' + typeIndex + '_' + Math.random();
}
function makeActionCreator(type) {
if (type === void 0) {
type = createRandomType();
}
var actionCreator = function(payload) {
return {
type: type,
payload: payload
};
};
actionCreator.type = type;
return actionCreator;
}
var reset = makeActionCreator('cooldux-RESET');
function resetReducer(initialState, reducer) {
return function(state, action) {
return reset.type === action.type ? initialState : reducer(state, action);
};
}
function promiseHandler(type, options) {
if (options === void 0) {
options = {};
}
if (typeof options === 'string') {
options = {
namespace: options
};
}
var name = (options.namespace ? options.namespace + '-' : '') + type;
var initialState = {};
initialState[type] = null;
initialState[type + 'Pending'] = false;
initialState[type + 'Error'] = null;
var creators = {};
creators[type + 'InitialState'] = initialState;
creators[type + 'Start'] = makeActionCreator(name + '_Start');
creators[type + 'End'] = makeActionCreator(name + '_End');
creators[type + 'Error'] = makeActionCreator(name + '_Error');
creators[type + 'Handler'] = function(promise, dispatch) {
if (!promise || !promise.then) {
dispatch(creators[type + 'End'](promise));
return Promise.resolve(promise);
}
dispatch(creators[type + 'Start']());
return Promise.resolve(promise).then(function(result) {
dispatch(creators[type + 'End'](result));
return result;
}).catch(function(error) {
dispatch(creators[type + 'Error'](error));
if (options.throwErrors) {
throw error;
}
return null;
});
};
creators[type + 'Action'] = function(promise) {
if (!promise || !promise.then) {
promise = Promise.resolve(promise);
promise._cooldux = {
name: name,
options: options,
sync: true
};
return promise;
}
promise._cooldux = {
name: name,
options: options
};
return promise;
};
creators[type + 'Reducer'] = function(state, action) {
var obj, obj$1, obj$2, obj$3, obj$4, obj$5;
state = state || initialState;
switch (action.type) {
case creators[type + 'Start'].type:
return Object.assign({}, state, (obj = {}, obj[type + 'Pending'] = true, obj), (obj$1 = {},
obj$1[type + 'Error'] = null, obj$1));
case creators[type + 'End'].type:
return Object.assign({}, state, (obj$2 = {}, obj$2[type + 'Pending'] = false, obj$2), (obj$3 = {},
obj$3[type] = action.payload, obj$3));
case creators[type + 'Error'].type:
return Object.assign({}, state, (obj$4 = {}, obj$4[type + 'Pending'] = false, obj$4), (obj$5 = {},
obj$5[type + 'Error'] = action.payload, obj$5));
default:
return state;
}
};
if (options.cache) {
if (!options.namespace) {
throw new Error('Must specify a namespace option when using cached actions');
}
creators[type + 'ActionCached'] = function(actionFunc, actionArgs) {
var promise = Promise.resolve(type + 'ActionCached');
promise._cooldux = {
namespace: options.namespace,
name: name,
options: options,
cache: true,
actionFunc: actionFunc,
actionArgs: actionArgs,
type: type
};
return promise;
};
}
return creators;
}
function combinedHandler(types, options) {
var handlers = {};
var initialState = types.reduce(function(state, type) {
var handler = promiseHandler(type, options);
Object.assign(handlers, handler);
Object.assign(state, handler[type + 'InitialState']);
return state;
}, {});
Object.assign(handlers, {
initialStateCombined: Object.assign({}, initialState),
reducerCombined: function(state, action) {
return types.reduce(function(state, type) {
return handlers[type + 'Reducer'](state, action);
}, Object.assign({}, state || initialState));
}
});
return handlers;
}
function actionResolver(promise, _cooldux, dispatch) {
return promise.then(function(payload) {
dispatch({
type: _cooldux.name + '_End',
payload: payload
});
return payload;
}).catch(function(err) {
dispatch({
type: _cooldux.name + '_Error',
payload: err
});
if (_cooldux.options.throwErrors) {
throw err;
}
return null;
});
}
var promiseMiddleware = function(ref) {
var dispatch = ref.dispatch;
var getState = ref.getState;
return function(next) {
return function(action) {
if (action.then && action._cooldux) {
var _cooldux = action._cooldux;
if (_cooldux.sync) {
return action.then(function(payload) {
dispatch({
type: _cooldux.name + '_End',
payload: payload
});
return payload;
});
} else if (_cooldux.cache) {
var state = getState();
if (state[_cooldux.namespace]) {
var cachedValue = state[_cooldux.namespace][_cooldux.type];
if (cachedValue) {
dispatch({
type: _cooldux.name + '_End',
payload: cachedValue
});
return Promise.resolve(cachedValue);
}
dispatch({
type: _cooldux.name + '_Start'
});
var promise = Promise.resolve(_cooldux.actionFunc.apply(null, _cooldux.actionArgs));
return actionResolver(promise, _cooldux, dispatch);
}
return actionResolver(Promise.reject(_cooldux.namespace + ' namespace not in state'), _cooldux, dispatch);
}
dispatch({
type: _cooldux.name + '_Start'
});
return actionResolver(action, _cooldux, dispatch);
}
next(action);
return action;
};
};
};
function makeDuck(actions, options) {
if (options === void 0) {
options = {};
}
var actionProps = Object.keys(actions).filter(function(key) {
return typeof actions[key] !== 'object';
});
var duck = combinedHandler(actionProps, options);
actionProps.forEach(function(key) {
if (typeof actions[key] === 'function') {
duck[key] = function() {
return duck[key + 'Action'](actions[key].apply(null, arguments));
};
if (options.cache) {
duck[key + 'Cached'] = function() {
return duck[key + 'ActionCached'](actions[key], arguments);
};
}
return;
}
if (typeof actions[key] === 'undefined') {
duck[key] = function() {
return duck[key + 'Action'](arguments[0]);
};
return;
}
throw new Error('actions must be functions or undefined');
});
duck.reducer = duck.reducerCombined;
return duck;
}
exports.makeActionCreator = makeActionCreator;
exports.reset = reset;
exports.resetReducer = resetReducer;
exports.promiseHandler = promiseHandler;
exports.combinedHandler = combinedHandler;
exports.promiseMiddleware = promiseMiddleware;
exports.makeDuck = makeDuck;