-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
328 lines (301 loc) · 12.6 KB
/
index.ts
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* @Author: JB ([email protected])
* @Date: 2016-12-09 14:42:59
* @Last Modified by: JB ([email protected])
* @Last Modified time: 2017-01-18 12:10:02
*/
const IDGenCharacters = 'abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ';
const IDGenCharactersLength = IDGenCharacters.length;
export function generateID(length: number) {
let id = '';
let counter = length;
while (--counter >= 0) {
id += IDGenCharacters.charAt(Math.floor(Math.random() *IDGenCharactersLength));
}
return id;
}
// a global list of module names to ensure unique generated action type names across modules.
const globalModuleIDs: {[id: string]: string} = {};
export class Module<STATETYPE, ACTIONEXTRADATA> {
actionDefs: {[id: string]: (state: STATETYPE, action: any) => Partial<STATETYPE>} = {};
initialState: STATETYPE;
actionExtraData: () => ACTIONEXTRADATA;
postReducer: (state: STATETYPE) => STATETYPE;
reducerCreated: boolean = false;
moduleID: string;
constructor(options: {
moduleID?: string,
initialState: STATETYPE,
actionExtraData?: () => ACTIONEXTRADATA,
postReducer?: (state: STATETYPE) => STATETYPE,
}) {
this.initialState = options.initialState;
this.actionExtraData = options.actionExtraData || (() => { return {} as ACTIONEXTRADATA;});
this.postReducer = options.postReducer || null;
this.moduleID = options.moduleID || null;
if (this.moduleID && typeof globalModuleIDs[this.moduleID] !== 'undefined') throw new Error(`Module name "${this.moduleID}" is not unique.`);
// generate a unique id for this module.
if (!this.moduleID) {
this.moduleID = generateID(6);
let retryCount = 10;
while (typeof globalModuleIDs[this.moduleID] !== 'undefined' && --retryCount >= 0) {
this.moduleID = generateID(6);
}
// extremely unlikely to ever happen
if (typeof globalModuleIDs[this.moduleID] !== 'undefined') throw new Error('failed to generate a unique module id');
}
globalModuleIDs[this.moduleID] = this.moduleID;
}
// no action params
createAction<ACTIONTYPE>(options: {
type?: string,
action: () => ACTIONTYPE,
reducer?: (state: Readonly<STATETYPE>, action: Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>) => Partial<STATETYPE>,
}) : () => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>;
// action params
createAction<ACTIONTYPE, A>(options: {
type?: string,
action: (a: A) => ACTIONTYPE,
reducer?: (state: Readonly<STATETYPE>, action: Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>) => Partial<STATETYPE>,
}) : (a: A) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>;
createAction<ACTIONTYPE, A, B>(options: {
type?: string,
action: (a: A, b: B) => ACTIONTYPE,
reducer?: (state: Readonly<STATETYPE>, action: Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>) => Partial<STATETYPE>,
}) : (a: A, b: B) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>;
createAction<ACTIONTYPE, A, B, C>(options: {
type?: string,
action: (a: A, b: B, c: C) => ACTIONTYPE,
reducer?: (state: Readonly<STATETYPE>, action: Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>) => Partial<STATETYPE>,
}) : (a: A, b: B, c: C) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>;
createAction<ACTIONTYPE, A, B, C, D>(options: {
type?: string,
action: (a: A, b: B, c: C, d: D) => ACTIONTYPE,
reducer?: (state: Readonly<STATETYPE>, action: Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>) => Partial<STATETYPE>,
}) : (a: A, b: B, c: C, d: D) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>;
createAction<ACTIONTYPE, A, B, C, D, E>(options: {
type?: string,
action: (a: A, b: B, c: C, d: D, e: E) => ACTIONTYPE,
reducer?: (state: Readonly<STATETYPE>, action: Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>) => Partial<STATETYPE>,
}) : (a: A, b: B, c: C, d: D, e: E) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>;
createAction<ACTIONTYPE, A, B, C, D, E, F>(options: {
type?: string,
action: (a: A, b: B, c: C, d: D, e: E, f: F) => ACTIONTYPE,
reducer?: (state: Readonly<STATETYPE>, action: Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>) => Partial<STATETYPE>,
}) : (a: A, b: B, c: C, d: D, e: E, f: F) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>;
createAction<ACTIONTYPE, A, B, C, D, E, F, G>(options: {
type?: string,
action: (a: A, b: B, c: C, d: D, e: E, f: F, g: G) => ACTIONTYPE,
reducer?: (state: Readonly<STATETYPE>, action: Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>) => Partial<STATETYPE>,
}) : (a: A, b: B, c: C, d: D, e: E, f: F, g: G) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>;
// combined
createAction<ACTIONTYPE, A, B, C, D, E, F, G>(options: {
type?: string,
action: (() => ACTIONTYPE)
| ((a: A) => ACTIONTYPE)
| ((a: A, b: B, c: C) => ACTIONTYPE)
| ((a: A, b: B, c: C, d: D) => ACTIONTYPE)
| ((a: A, b: B, c: C, d: D, e: E) => ACTIONTYPE)
| ((a: A, b: B, c: C, d: D, e: E, f: F) => ACTIONTYPE)
| ((a: A, b: B, c: C, d: D, e: E, f: F, g: G) => ACTIONTYPE),
reducer?: (state: Readonly<STATETYPE>, action: Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>) => Partial<STATETYPE>,
}) : (() => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>)
| ((a: A) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>)
| ((a: A, b: B) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>)
| ((a: A, b: B, c: C) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>)
| ((a: A, b: B, c: C, d: D) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>)
| ((a: A, b: B, c: C, d: D, e: E) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>)
| ((a: A, b: B, c: C, d: D, e: E, f: F) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>)
| ((a: A, b: B, c: C, d: D, e: E, f: F, g: G) => Readonly<ACTIONTYPE & {type: string} & ACTIONEXTRADATA>)
{
if (this.reducerCreated) throw new Error("createAction may only be called before createReducer.");
const {action, reducer} = options;
let type = options.type;
if (typeof type === 'undefined') {
// generate a type
type = this.moduleID + '/' + generateID(6);
let retry = 10;
while (typeof this.actionDefs[type] !== 'undefined' && --retry >= 0) {
type = this.moduleID + '/' + generateID(6);
}
} else if (type.indexOf('/') === 0) {
// When the type name starts with a "/", we assume we want to prepend the module name.
type = this.moduleID + type;
}
if (typeof this.actionDefs[type] !== 'undefined') throw new Error('duplicate type name provided to createAction or type name generation failed to generate a unique type name');
this.actionDefs[type] = reducer || function(s, a) { return <Partial<STATETYPE>>{}; };
const actionExtraData = this.actionExtraData;
if (action.length == 0) {
return () => {
const actionResult = (<() => ACTIONTYPE>action)() as any;
if (typeof actionResult === 'function') {
return (dispatch: (action: any) => any, state: STATETYPE) => {
return {
...(actionResult(dispatch, state) as any),
type: type,
...(actionExtraData() as any)
}
}
}
return {
...actionResult,
type: type,
...(actionExtraData() as any)
};
};
} else if (action.length === 1) {
return (a: A) => {
const actionResult = (<(a: A) => ACTIONTYPE>action)(a) as any;
if (typeof actionResult === 'function') {
return (dispatch: (action: any) => any, state: STATETYPE) => {
return {
...(actionResult(dispatch, state) as any),
type: type,
...(actionExtraData() as any)
}
}
}
return {
...actionResult,
type: type,
...(actionExtraData() as any)
};
};
} else if (action.length === 2) {
return (a: A, b: B) => {
const actionResult = (<(a: A, b: B) => ACTIONTYPE>action)(a, b) as any;
if (typeof actionResult === 'function') {
return (dispatch: (action: any) => any, state: STATETYPE) => {
return {
...(actionResult(dispatch, state) as any),
type: type,
...(actionExtraData() as any)
}
}
}
return {
...actionResult,
type: type,
...(actionExtraData() as any)
};
};
} else if (action.length === 3) {
return (a: A, b: B, c: C) => {
const actionResult = (<(a: A, b: B, c: C) => ACTIONTYPE>action)(a, b, c) as any;
if (typeof actionResult === 'function') {
return (dispatch: (action: any) => any, state: STATETYPE) => {
return {
...(actionResult(dispatch, state) as any),
type: type,
...(actionExtraData() as any)
}
}
}
return {
...actionResult,
type: type,
...(actionExtraData() as any)
};
};
} else if (action.length === 4) {
return (a: A, b: B, c: C, d: D) => {
const actionResult = (<(a: A, b: B, c: C, d: D) => ACTIONTYPE>action)(a, b, c, d) as any;
if (typeof actionResult === 'function') {
return (dispatch: (action: any) => any, state: STATETYPE) => {
return {
...(actionResult(dispatch, state) as any),
type: type,
...(actionExtraData() as any)
}
}
}
return {
...actionResult,
type: type,
...(actionExtraData() as any)
};
};
} else if (action.length === 5) {
return (a: A, b: B, c: C, d: D, e: E) => {
const actionResult = (<(a: A, b: B, c: C, d: D, e: E) => ACTIONTYPE>action)(a, b, c, d, e) as any;
if (typeof actionResult === 'function') {
return (dispatch: (action: any) => any, state: STATETYPE) => {
return {
...(actionResult(dispatch, state) as any),
type: type,
...(actionExtraData() as any)
}
}
}
return {
...actionResult,
type: type,
...(actionExtraData() as any)
};
};
} else if (action.length === 6) {
return (a: A, b: B, c: C, d: D, e: E, f: F) => {
const actionResult = (<(a: A, b: B, c: C, d: D, e: E, f: F) => ACTIONTYPE>action)(a, b, c, d, e, f) as any;
if (typeof actionResult === 'function') {
return (dispatch: (action: any) => any, state: STATETYPE) => {
return {
...(actionResult(dispatch, state) as any),
type: type,
...(actionExtraData() as any)
}
}
}
return {
...actionResult,
type: type,
...(actionExtraData() as any)
};
};
} else if (action.length === 7) {
return (a: A, b: B, c: C, d: D, e: E, f: F, g: G) => {
const actionResult = (<(a: A, b: B, c: C, d: D, e: E, f: F, g: G) => ACTIONTYPE>action)(a, b, c, d, e, f, g) as any;
if (typeof actionResult === 'function') {
return (dispatch: (action: any) => any, state: STATETYPE) => {
return {
...(actionResult(dispatch, state) as any),
type: type,
...(actionExtraData() as any)
}
}
}
return {
...actionResult,
type: type,
...(actionExtraData() as any)
};
};
}
}
createReducer() {
this.reducerCreated = true;
const postReducer = this.postReducer;
if (postReducer === null) {
return (state: STATETYPE = this.initialState, action: {type: string}) => {
let def = this.actionDefs[action.type];
if (typeof def === 'undefined' || def === null) return state;
return {
...(state as any),
...(def(state, action) as any)
};
};
} else {
return (state: STATETYPE = this.initialState, action: {type: string}) => {
let def = this.actionDefs[action.type];
if (typeof def === 'undefined' || def === null) return state;
return postReducer({
...(state as any),
...(def(state, action) as any)
});
};
}
}
}