-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
137 lines (127 loc) · 3.56 KB
/
middleware.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
/**
* @module reducer/middleware
* Description: Default reducer store functionality. Handles all REDUCER_ACTIONS.
*/
import {
noop,
} from '../combinators';
import {
createLogger,
type Logger,
} from '../logger';
import type {
ArrowFunction,
Callable,
Json,
State,
} from "../types";
import type {
HistoryController,
} from "./history";
/**
* Name: DefaultEnhancerParams
* Description: DefaultEnhancerParams object
* @private
* @typedef {Object}
*/
type DefaultEnhancerParams<
T extends Json = Json,
S extends State<T> = State<T>,
> = {
historyCtl: HistoryController<T, S>;
}
/**
* Name: defaultEnhancerMiddleware
* Description: Default store "enhancer" middleware. Handles logging, dispatch and subscribing.
* @public
* @template T, S
* @param params DefaultEnhancerParams
* @returns enhancer middleware
*/
export const defaultEnhancerMiddleware = <
T extends Json = Json,
S extends State<T> = State<T>,
>(
params: DefaultEnhancerParams<T, S>
) => {
const {
historyCtl,
} = params;
return (api: any) => {
const {
data,
type,
} = api.action;
const {
subscribers,
} = data;
let logger: Logger = {
log: noop,
updateOptions: noop,
};
if (data?.options?.logger) {
const prefix = ['Reducer Store Dispatch', data.options.logger.prefix].join(' - ');
logger = createLogger({
prefix,
});
}
return (middleware: Callable<any[], any>) => {
return (params: any) => {
const {
action,
callback,
reducerAction,
state,
} = params;
if (type === '@DISPATCH_ACTION') {
switch(action.type) {
case '@CLEAR': {
logger.log('Action: [@CLEAR]');
return historyCtl.clear();
}
case '@JUMP_TO_FUTURE_REVISION': {
logger.log('Action: [@JUMP_TO_FUTURE_REVISION]', { action, state });
const { revisionId } = action.data;
return historyCtl.jumpToFutureRevision(revisionId);
}
case '@JUMP_TO_PAST_REVISION': {
logger.log('Action: [@JUMP_TO_PAST_REVISION]', { action, state });
const { revisionId } = action.data;
return historyCtl.jumpToPastRevision(revisionId);
}
case '@JUMP_TO_REVISION': {
logger.log('Action: [@JUMP_TO_REVISION]', { action, state });
const { revisionId } = action.data;
return historyCtl.jumpToRevision(revisionId);
}
case '@REDO': {
logger.log('Action: [@REDO]', { action });
const { steps } = action.data;
return historyCtl.redo(steps);
}
case '@UNDO': {
logger.log('Action: [@UNDO]: ', { action });
const { steps } = action.data;
return historyCtl.undo(steps);
}
default: {
logger.log({ action, state });
const result = reducerAction(state, action);
logger.log({ result });
const newState = historyCtl.setState(result.value);
for(const [_, cb] of <Map<ArrowFunction, ArrowFunction>>subscribers) {
cb({action, state, newState});
}
return;
}
}
} else if (type === '@SUBSCRIBE') {
subscribers.set(callback, callback);
} else if (type === '@UNSUBSCRIBE') {
subscribers.delete(callback);
}
return middleware(params);
};
};
};
};