@pihanga2/core 0.3.2 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/card.d.ts +4 -2
- package/dist/card.d.ts.map +1 -1
- package/dist/card.js +16 -10
- package/dist/card.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/reducer.d.ts.map +1 -1
- package/dist/reducer.js +65 -18
- package/dist/reducer.js.map +1 -1
- package/dist/redux.d.ts.map +1 -1
- package/dist/redux.js.map +1 -1
- package/dist/register_cards.d.ts +2 -2
- package/dist/register_cards.d.ts.map +1 -1
- package/dist/register_cards.js +1 -1
- package/dist/register_cards.js.map +1 -1
- package/dist/router.d.ts +11 -5
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +33 -28
- package/dist/router.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +5 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +2 -2
- package/src/card.tsx +33 -21
- package/src/index.ts +94 -76
- package/src/reducer.ts +133 -76
- package/src/redux.ts +26 -25
- package/src/register_cards.ts +28 -25
- package/src/router.ts +129 -100
- package/src/types.ts +14 -10
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import ReactDOM from "react-dom/client"
|
|
2
|
-
import {
|
|
1
|
+
import ReactDOM from "react-dom/client";
|
|
2
|
+
import {Dispatch} from "react";
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
5
|
PiCardDef,
|
|
@@ -14,15 +14,21 @@ import {
|
|
|
14
14
|
PiMapProps,
|
|
15
15
|
WindowProps,
|
|
16
16
|
GenericCardParameterT,
|
|
17
|
-
} from "./types"
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
} from "./types";
|
|
18
|
+
import {
|
|
19
|
+
createCardDeclaration,
|
|
20
|
+
registerCard,
|
|
21
|
+
registerCardComponent,
|
|
22
|
+
registerMetacard,
|
|
23
|
+
updateOrRegisterCard,
|
|
24
|
+
} from "./register_cards";
|
|
25
|
+
import {createReducer} from "./reducer";
|
|
26
|
+
import {ON_INIT_ACTION, currentRoute, init as routerInit} from "./router";
|
|
21
27
|
|
|
22
|
-
import {
|
|
28
|
+
import {configureStore} from "@reduxjs/toolkit";
|
|
23
29
|
|
|
24
30
|
//import monitorReducerEnhancer from "./monitor_enhancer"
|
|
25
|
-
import {
|
|
31
|
+
import {getLogger} from "./logger";
|
|
26
32
|
import {
|
|
27
33
|
PiRegisterDeleteProps,
|
|
28
34
|
PiRegisterGetProps,
|
|
@@ -32,9 +38,9 @@ import {
|
|
|
32
38
|
registerPATCH,
|
|
33
39
|
registerPOST,
|
|
34
40
|
registerPUT,
|
|
35
|
-
} from "./rest"
|
|
36
|
-
import {
|
|
37
|
-
const logger = getLogger("root")
|
|
41
|
+
} from "./rest";
|
|
42
|
+
import {RootComponent} from "./root";
|
|
43
|
+
const logger = getLogger("root");
|
|
38
44
|
|
|
39
45
|
export type {
|
|
40
46
|
PiMapProps,
|
|
@@ -50,27 +56,38 @@ export type {
|
|
|
50
56
|
PiReducer,
|
|
51
57
|
PiRegisterMetaCard,
|
|
52
58
|
WindowProps,
|
|
53
|
-
} from "./types"
|
|
54
|
-
export {
|
|
55
|
-
export {
|
|
56
|
-
export {
|
|
57
|
-
export {
|
|
58
|
-
export type {
|
|
59
|
-
export type {
|
|
60
|
-
export {
|
|
61
|
-
|
|
62
|
-
export {
|
|
63
|
-
|
|
59
|
+
} from "./types";
|
|
60
|
+
export {registerActions, actionTypesToEvents, createOnAction} from "./redux";
|
|
61
|
+
export {Card, usePiReducer, cls_f} from "./card";
|
|
62
|
+
export {memo, createCardDeclaration, isCardRef} from "./register_cards";
|
|
63
|
+
export {getLogger} from "./logger";
|
|
64
|
+
export type {PiCardProps, PiCardRef} from "./types";
|
|
65
|
+
export type {ErrorAction as RestErrorAction} from "./rest";
|
|
66
|
+
export {RestContentType} from "./rest";
|
|
67
|
+
|
|
68
|
+
export {
|
|
69
|
+
showPage,
|
|
70
|
+
onInit,
|
|
71
|
+
onShowPage,
|
|
72
|
+
createShowPageAction,
|
|
73
|
+
onNavigateToPage,
|
|
74
|
+
} from "./router";
|
|
75
|
+
export type {ShowPageEvent, NavigateToPageEvent} from "./router";
|
|
64
76
|
|
|
65
77
|
export interface PiRegister {
|
|
66
78
|
//window(parameters: PiCardDef): PiCardRef
|
|
67
79
|
|
|
68
|
-
window<S extends ReduxState>(
|
|
80
|
+
window<S extends ReduxState>(
|
|
81
|
+
parameters: PiMapProps<WindowProps, S, {}>
|
|
82
|
+
): PiCardRef;
|
|
69
83
|
|
|
70
|
-
card(name: string, parameters: PiCardDef): PiCardRef
|
|
71
|
-
updateCard(
|
|
84
|
+
card(name: string, parameters: PiCardDef): PiCardRef;
|
|
85
|
+
updateCard(
|
|
86
|
+
name: string,
|
|
87
|
+
parameters: {[key: string]: GenericCardParameterT}
|
|
88
|
+
): PiCardRef;
|
|
72
89
|
|
|
73
|
-
cardComponent(declaration: PiRegisterComponent): void
|
|
90
|
+
cardComponent(declaration: PiRegisterComponent): void;
|
|
74
91
|
|
|
75
92
|
/**
|
|
76
93
|
* Register a meta card which expands a single card definition of type `name`
|
|
@@ -82,41 +99,41 @@ export interface PiRegister {
|
|
|
82
99
|
* @param {string} type
|
|
83
100
|
* @param {function} mapper
|
|
84
101
|
*/
|
|
85
|
-
metaCard<C>(declaration: PiRegisterMetaCard): void
|
|
102
|
+
metaCard<C>(declaration: PiRegisterMetaCard): void;
|
|
86
103
|
|
|
87
104
|
GET<S extends ReduxState, A extends ReduxAction, R, C = any>(
|
|
88
|
-
props: PiRegisterGetProps<S, A, R, C
|
|
89
|
-
): void
|
|
105
|
+
props: PiRegisterGetProps<S, A, R, C>
|
|
106
|
+
): void;
|
|
90
107
|
PUT<S extends ReduxState, A extends ReduxAction, R, C = any>(
|
|
91
|
-
props: PiRegisterPoPuPaProps<S, A, R, C
|
|
92
|
-
): void
|
|
108
|
+
props: PiRegisterPoPuPaProps<S, A, R, C>
|
|
109
|
+
): void;
|
|
93
110
|
POST<S extends ReduxState, A extends ReduxAction, R, C = any>(
|
|
94
|
-
props: PiRegisterPoPuPaProps<S, A, R, C
|
|
95
|
-
): void
|
|
111
|
+
props: PiRegisterPoPuPaProps<S, A, R, C>
|
|
112
|
+
): void;
|
|
96
113
|
PATCH<S extends ReduxState, A extends ReduxAction, R, C = any>(
|
|
97
|
-
props: PiRegisterPoPuPaProps<S, A, R, C
|
|
98
|
-
): void
|
|
114
|
+
props: PiRegisterPoPuPaProps<S, A, R, C>
|
|
115
|
+
): void;
|
|
99
116
|
DELETE<S extends ReduxState, A extends ReduxAction, R, C = any>(
|
|
100
|
-
props: PiRegisterDeleteProps<S, A, R, C
|
|
101
|
-
): void
|
|
117
|
+
props: PiRegisterDeleteProps<S, A, R, C>
|
|
118
|
+
): void;
|
|
102
119
|
//registerPeriodicGET<S extends ReduxState, A extends ReduxAction, R>(props: PiRegisterPeridicGetProps<S, A, R>): void;
|
|
103
120
|
|
|
104
|
-
reducer: PiReducer
|
|
121
|
+
reducer: PiReducer;
|
|
105
122
|
}
|
|
106
123
|
|
|
107
124
|
export const DEFAULT_REDUX_STATE = {
|
|
108
|
-
route: {
|
|
125
|
+
route: {path: [], query: {}, url: "", fromBrowser: false},
|
|
109
126
|
pihanga: {},
|
|
110
|
-
}
|
|
127
|
+
};
|
|
111
128
|
|
|
112
129
|
export type StartProps = {
|
|
113
130
|
// redux settins
|
|
114
|
-
disableSerializableStateCheck?: boolean
|
|
115
|
-
disableSerializableActionCheck?: boolean
|
|
116
|
-
ignoredActions?: string[]
|
|
117
|
-
ignoredActionPaths?: string[]
|
|
118
|
-
ignoredStatePaths?: string[]
|
|
119
|
-
}
|
|
131
|
+
disableSerializableStateCheck?: boolean;
|
|
132
|
+
disableSerializableActionCheck?: boolean;
|
|
133
|
+
ignoredActions?: string[];
|
|
134
|
+
ignoredActionPaths?: string[];
|
|
135
|
+
ignoredStatePaths?: string[];
|
|
136
|
+
};
|
|
120
137
|
|
|
121
138
|
export function start<S extends Partial<ReduxState>>(
|
|
122
139
|
initialState: S,
|
|
@@ -126,20 +143,20 @@ export function start<S extends Partial<ReduxState>>(
|
|
|
126
143
|
const state = {
|
|
127
144
|
...DEFAULT_REDUX_STATE,
|
|
128
145
|
...initialState,
|
|
129
|
-
...{
|
|
130
|
-
}
|
|
131
|
-
let dispatchF: Dispatch<any> | null = null
|
|
146
|
+
...{route: currentRoute()}, // override route with current one
|
|
147
|
+
};
|
|
148
|
+
let dispatchF: Dispatch<any> | null = null;
|
|
132
149
|
const dispatcherW: Dispatch<any> = (a: any): void => {
|
|
133
150
|
if (dispatchF) {
|
|
134
|
-
dispatchF(a)
|
|
151
|
+
dispatchF(a);
|
|
135
152
|
} else {
|
|
136
|
-
logger.error("dispatch function is not properly set")
|
|
153
|
+
logger.error("dispatch function is not properly set");
|
|
137
154
|
}
|
|
138
|
-
}
|
|
139
|
-
const [reducer, piReducer] = createReducer(state, dispatcherW)
|
|
140
|
-
const route = routerInit(piReducer)
|
|
155
|
+
};
|
|
156
|
+
const [reducer, piReducer] = createReducer(state, dispatcherW);
|
|
157
|
+
const route = routerInit(piReducer);
|
|
141
158
|
|
|
142
|
-
const ignoredActions = ([] as string[]).concat(props.ignoredActions || [])
|
|
159
|
+
const ignoredActions = ([] as string[]).concat(props.ignoredActions || []);
|
|
143
160
|
const ignoredActionPaths = [
|
|
144
161
|
"apiURL", // from IVCAP
|
|
145
162
|
"mapper",
|
|
@@ -148,8 +165,8 @@ export function start<S extends Partial<ReduxState>>(
|
|
|
148
165
|
"headers",
|
|
149
166
|
"cause",
|
|
150
167
|
"data",
|
|
151
|
-
].concat(props.ignoredActionPaths || [])
|
|
152
|
-
const ignoredPaths = ["cause.content"].concat(props.ignoredStatePaths || [])
|
|
168
|
+
].concat(props.ignoredActionPaths || []);
|
|
169
|
+
const ignoredPaths = ["cause.content"].concat(props.ignoredStatePaths || []);
|
|
153
170
|
|
|
154
171
|
const store = configureStore({
|
|
155
172
|
reducer,
|
|
@@ -163,21 +180,22 @@ export function start<S extends Partial<ReduxState>>(
|
|
|
163
180
|
ignoredPaths,
|
|
164
181
|
ignoreState: props.disableSerializableStateCheck,
|
|
165
182
|
ignoreAction: props.disableSerializableActionCheck,
|
|
166
|
-
}
|
|
167
|
-
})
|
|
168
|
-
})
|
|
183
|
+
},
|
|
184
|
+
}),
|
|
185
|
+
});
|
|
169
186
|
// make pihanga's reducer interface available to cards
|
|
170
|
-
const anyStore: any = store
|
|
171
|
-
anyStore.piReducer = piReducer
|
|
172
|
-
|
|
173
|
-
dispatchF = store.dispatch
|
|
187
|
+
const anyStore: any = store;
|
|
188
|
+
anyStore.piReducer = piReducer;
|
|
174
189
|
|
|
175
|
-
|
|
176
|
-
const updateCard = updateOrRegisterCard(piReducer.register, dispatchF)
|
|
177
|
-
const window = <S extends ReduxState>(p: PiMapProps<WindowProps, S, {}>): PiCardRef => {
|
|
178
|
-
return card("_window", { cardType: "framework", ...p })
|
|
179
|
-
}
|
|
190
|
+
dispatchF = store.dispatch;
|
|
180
191
|
|
|
192
|
+
const card = registerCard(piReducer.register, dispatchF);
|
|
193
|
+
const updateCard = updateOrRegisterCard(piReducer.register, dispatchF);
|
|
194
|
+
const window = <S extends ReduxState>(
|
|
195
|
+
p: PiMapProps<WindowProps, S, {}>
|
|
196
|
+
): PiCardRef => {
|
|
197
|
+
return card("_window", {cardType: "framework", ...p});
|
|
198
|
+
};
|
|
181
199
|
|
|
182
200
|
const register: PiRegister = {
|
|
183
201
|
window,
|
|
@@ -191,14 +209,14 @@ export function start<S extends Partial<ReduxState>>(
|
|
|
191
209
|
POST: registerPOST(piReducer),
|
|
192
210
|
PATCH: registerPATCH(piReducer),
|
|
193
211
|
DELETE: registerDELETE(piReducer),
|
|
194
|
-
}
|
|
195
|
-
inits.forEach((f) => f(register))
|
|
212
|
+
};
|
|
213
|
+
inits.forEach((f) => f(register));
|
|
196
214
|
|
|
197
|
-
piReducer.dispatch({
|
|
215
|
+
piReducer.dispatch({type: ON_INIT_ACTION});
|
|
198
216
|
|
|
199
|
-
const rootComp = RootComponent(store)
|
|
200
|
-
const root = ReactDOM.createRoot(document.getElementById("root")!)
|
|
201
|
-
root.render(rootComp)
|
|
217
|
+
const rootComp = RootComponent(store);
|
|
218
|
+
const root = ReactDOM.createRoot(document.getElementById("root")!);
|
|
219
|
+
root.render(rootComp);
|
|
202
220
|
|
|
203
|
-
return register
|
|
221
|
+
return register;
|
|
204
222
|
}
|
package/src/reducer.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {Action, Reducer} from "@reduxjs/toolkit";
|
|
2
2
|
import {
|
|
3
3
|
DispatchF,
|
|
4
4
|
PiReducer,
|
|
@@ -9,110 +9,137 @@ import {
|
|
|
9
9
|
ReduceOnceF,
|
|
10
10
|
ReduxAction,
|
|
11
11
|
ReduxState,
|
|
12
|
-
} from "./types"
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import StackTrace from "stacktrace-js"
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
12
|
+
} from "./types";
|
|
13
|
+
import {produce} from "immer";
|
|
14
|
+
import {RegisterCardState, UPDATE_STATE_ACTION} from "./card";
|
|
15
|
+
import StackTrace from "stacktrace-js";
|
|
16
|
+
import {getLogger} from "./logger";
|
|
17
|
+
import {Dispatch} from "react";
|
|
18
|
+
import {currentRoute} from "./router";
|
|
19
19
|
|
|
20
|
-
const logger = getLogger("reducer")
|
|
20
|
+
const logger = getLogger("reducer");
|
|
21
21
|
|
|
22
22
|
type ReducerDef<S extends ReduxState, A extends ReduxAction> = {
|
|
23
|
-
|
|
24
|
-
mapperOnce?: ReduceOnceF<S, A
|
|
25
|
-
priority?: number
|
|
26
|
-
key?: string
|
|
27
|
-
definedIn?: StackTrace.StackFrame
|
|
28
|
-
|
|
23
|
+
mapperMulti?: ReduceF<S, A>;
|
|
24
|
+
mapperOnce?: ReduceOnceF<S, A>;
|
|
25
|
+
priority?: number;
|
|
26
|
+
key?: string;
|
|
27
|
+
definedIn?: StackTrace.StackFrame;
|
|
28
|
+
targetMapper?: ReduceF<S, A>;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type Source = {
|
|
32
|
+
file?: string;
|
|
33
|
+
line?: number;
|
|
34
|
+
column?: number;
|
|
35
|
+
functionName?: string;
|
|
36
|
+
};
|
|
29
37
|
|
|
30
38
|
export function createReducer(
|
|
31
39
|
initialState: ReduxState,
|
|
32
|
-
dispatcher: Dispatch<any
|
|
40
|
+
dispatcher: Dispatch<any>
|
|
33
41
|
): [Reducer<ReduxState, Action>, PiReducer] {
|
|
34
|
-
const mappings: {
|
|
35
|
-
mappings[UPDATE_STATE_ACTION] = [
|
|
42
|
+
const mappings: {[k: string]: ReducerDef<ReduxState, Action>[]} = {};
|
|
43
|
+
mappings[UPDATE_STATE_ACTION] = [
|
|
44
|
+
{
|
|
45
|
+
mapperMulti: RegisterCardState.reducer,
|
|
46
|
+
key: "@builtin:card:UPDATE_STATE_ACTION",
|
|
47
|
+
},
|
|
48
|
+
];
|
|
36
49
|
|
|
37
50
|
const delayedDispatcher = (a: any): void => {
|
|
38
|
-
setTimeout(() => dispatcher(a), 0)
|
|
39
|
-
}
|
|
51
|
+
setTimeout(() => dispatcher(a), 0);
|
|
52
|
+
};
|
|
40
53
|
const reducer = (
|
|
41
54
|
state: ReduxState | undefined,
|
|
42
|
-
action: Action
|
|
55
|
+
action: Action
|
|
43
56
|
): ReduxState => {
|
|
44
|
-
const s = state || initialState
|
|
45
|
-
const ra = mappings[action.type]
|
|
46
|
-
const rany = mappings["*"]
|
|
57
|
+
const s = state || initialState;
|
|
58
|
+
const ra = mappings[action.type];
|
|
59
|
+
const rany = mappings["*"];
|
|
47
60
|
if ((!ra || ra.length === 0) && (!rany || rany.length === 0)) {
|
|
48
|
-
return s
|
|
61
|
+
return s;
|
|
49
62
|
}
|
|
50
63
|
|
|
51
64
|
const nextState = produce<ReduxState, ReduxState>(s, (draft) => {
|
|
65
|
+
if (!draft.pihanga) {
|
|
66
|
+
draft.pihanga = {};
|
|
67
|
+
}
|
|
68
|
+
draft.pihanga.reducers = [];
|
|
52
69
|
if (ra) {
|
|
53
|
-
const rout = _reduce(ra, draft, action, delayedDispatcher)
|
|
54
|
-
mappings[action.type] = rout
|
|
70
|
+
const rout = _reduce(ra, draft, action, delayedDispatcher);
|
|
71
|
+
mappings[action.type] = rout;
|
|
55
72
|
}
|
|
56
73
|
if (rany) {
|
|
57
|
-
const rout2 = _reduce(rany, draft, action, delayedDispatcher)
|
|
58
|
-
mappings["*"] = rout2
|
|
74
|
+
const rout2 = _reduce(rany, draft, action, delayedDispatcher);
|
|
75
|
+
mappings["*"] = rout2;
|
|
59
76
|
}
|
|
60
|
-
return
|
|
61
|
-
})
|
|
62
|
-
return nextState
|
|
63
|
-
}
|
|
77
|
+
return;
|
|
78
|
+
});
|
|
79
|
+
return nextState;
|
|
80
|
+
};
|
|
64
81
|
|
|
65
82
|
const registerReducer: PiRegisterReducerF = <
|
|
66
83
|
S extends ReduxState,
|
|
67
|
-
A extends ReduxAction
|
|
84
|
+
A extends ReduxAction
|
|
68
85
|
>(
|
|
69
86
|
eventType: string,
|
|
70
87
|
mapper: ReduceF<S, A>,
|
|
71
88
|
priority: number = 0,
|
|
72
|
-
key
|
|
89
|
+
key?: string,
|
|
90
|
+
targetMapper?: ReduceF<S, A>
|
|
73
91
|
): PiReducerCancelF => {
|
|
74
|
-
return addReducer(eventType, {
|
|
75
|
-
|
|
92
|
+
return addReducer(eventType, {
|
|
93
|
+
mapperMulti: mapper,
|
|
94
|
+
priority,
|
|
95
|
+
key,
|
|
96
|
+
targetMapper,
|
|
97
|
+
});
|
|
98
|
+
};
|
|
76
99
|
|
|
77
100
|
const registerOneShot: PiRegisterOneShotReducerF = <
|
|
78
101
|
S extends ReduxState,
|
|
79
|
-
A extends ReduxAction
|
|
102
|
+
A extends ReduxAction
|
|
80
103
|
>(
|
|
81
104
|
eventType: string,
|
|
82
105
|
mapper: (state: S, action: A, dispatch: DispatchF) => boolean,
|
|
83
106
|
priority: number = 0,
|
|
84
107
|
key: string | undefined = undefined
|
|
85
108
|
): PiReducerCancelF => {
|
|
86
|
-
return addReducer(eventType, {
|
|
87
|
-
}
|
|
109
|
+
return addReducer(eventType, {mapperOnce: mapper, priority, key});
|
|
110
|
+
};
|
|
88
111
|
|
|
89
|
-
const nonCancelF = () => {}
|
|
112
|
+
const nonCancelF = () => {};
|
|
90
113
|
|
|
91
114
|
function addReducer<S extends ReduxState, A extends ReduxAction>(
|
|
92
115
|
eventType: string,
|
|
93
|
-
reducerDef: ReducerDef<S, A
|
|
116
|
+
reducerDef: ReducerDef<S, A>
|
|
94
117
|
): PiReducerCancelF {
|
|
95
|
-
let m = mappings[eventType] || []
|
|
96
|
-
const key = reducerDef.key
|
|
97
|
-
m = removeReducer(key, m)
|
|
98
|
-
m.push(reducerDef as any as ReducerDef<ReduxState, Action<any>>) // keep typing happy
|
|
99
|
-
m.sort((a, b) => (b.priority || 0) - (a.priority || 0))
|
|
100
|
-
mappings[eventType] = m
|
|
101
|
-
|
|
102
|
-
const callback = (frames: StackTrace.StackFrame[]) => {
|
|
103
|
-
// const stack = frames.map((sf) => `${sf.fileName}:${sf.lineNumber}`)
|
|
104
|
-
// console.log(stack)
|
|
105
|
-
reducerDef.definedIn = frames[2]
|
|
106
|
-
}
|
|
118
|
+
let m = mappings[eventType] || [];
|
|
119
|
+
const key = reducerDef.key;
|
|
120
|
+
m = removeReducer(key, m);
|
|
121
|
+
m.push(reducerDef as any as ReducerDef<ReduxState, Action<any>>); // keep typing happy
|
|
122
|
+
m.sort((a, b) => (b.priority || 0) - (a.priority || 0));
|
|
123
|
+
mappings[eventType] = m;
|
|
107
124
|
|
|
108
|
-
|
|
109
|
-
|
|
125
|
+
if (!reducerDef.key) {
|
|
126
|
+
const frames = StackTrace.getSync();
|
|
127
|
+
const sf = _get_source_frame(frames);
|
|
128
|
+
if (sf) {
|
|
129
|
+
// reducerDef.definedIn = sf;
|
|
130
|
+
reducerDef.key = sf.toString();
|
|
131
|
+
} else {
|
|
132
|
+
reducerDef.definedIn = sf;
|
|
133
|
+
console.log(">> cannot find source frame", eventType, frames);
|
|
134
|
+
}
|
|
110
135
|
}
|
|
111
|
-
|
|
112
|
-
return key
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
136
|
+
|
|
137
|
+
return key
|
|
138
|
+
? () => {
|
|
139
|
+
let m = mappings[eventType] || [];
|
|
140
|
+
mappings[eventType] = removeReducer(key, m);
|
|
141
|
+
}
|
|
142
|
+
: nonCancelF;
|
|
116
143
|
}
|
|
117
144
|
|
|
118
145
|
const piReducer: PiReducer = {
|
|
@@ -120,9 +147,9 @@ export function createReducer(
|
|
|
120
147
|
registerOneShot,
|
|
121
148
|
dispatch: dispatcher,
|
|
122
149
|
dispatchFromReducer: delayedDispatcher,
|
|
123
|
-
}
|
|
150
|
+
};
|
|
124
151
|
|
|
125
|
-
return [reducer, piReducer]
|
|
152
|
+
return [reducer, piReducer];
|
|
126
153
|
}
|
|
127
154
|
|
|
128
155
|
function removeReducer(
|
|
@@ -130,9 +157,9 @@ function removeReducer(
|
|
|
130
157
|
m: ReducerDef<ReduxState, Action>[]
|
|
131
158
|
) {
|
|
132
159
|
if (key) {
|
|
133
|
-
return m.filter((r) => r.key !== key)
|
|
160
|
+
return m.filter((r) => r.key !== key);
|
|
134
161
|
} else {
|
|
135
|
-
return m
|
|
162
|
+
return m;
|
|
136
163
|
}
|
|
137
164
|
}
|
|
138
165
|
|
|
@@ -140,23 +167,53 @@ function _reduce(
|
|
|
140
167
|
ra: ReducerDef<ReduxState, Action>[],
|
|
141
168
|
draft: ReduxState,
|
|
142
169
|
action: Action,
|
|
143
|
-
delayedDispatcher: (a: any) => void
|
|
170
|
+
delayedDispatcher: (a: any) => void
|
|
144
171
|
): ReducerDef<ReduxState, Action<any>>[] {
|
|
145
|
-
const rout: ReducerDef<ReduxState, Action<any>>[] = []
|
|
172
|
+
const rout: ReducerDef<ReduxState, Action<any>>[] = [];
|
|
146
173
|
ra.forEach((m) => {
|
|
147
174
|
try {
|
|
148
|
-
if (m.
|
|
149
|
-
|
|
150
|
-
|
|
175
|
+
// if (m.definedIn || m.key) {
|
|
176
|
+
// console.log(">>> executing reducer", action.type, m.key, m.definedIn);
|
|
177
|
+
// }
|
|
178
|
+
if (m.mapperMulti) {
|
|
179
|
+
draft.pihanga?.reducers?.push(m.definedIn || m.key || "unknown");
|
|
180
|
+
m.mapperMulti(draft, action, delayedDispatcher);
|
|
181
|
+
rout.push(m);
|
|
151
182
|
} else if (m.mapperOnce) {
|
|
152
|
-
|
|
183
|
+
draft.pihanga?.reducers?.push(m.definedIn || m.key || "unknown");
|
|
184
|
+
const flag = m.mapperOnce(draft, action, delayedDispatcher);
|
|
153
185
|
if (!flag) {
|
|
154
|
-
rout.push(m)
|
|
186
|
+
rout.push(m);
|
|
155
187
|
}
|
|
156
188
|
}
|
|
157
189
|
} catch (err: any) {
|
|
158
|
-
logger.error(err.message, m.definedIn)
|
|
190
|
+
logger.error(err.message, m.definedIn);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
return rout;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function _get_source_frame(
|
|
197
|
+
frames: StackTrace.StackFrame[]
|
|
198
|
+
): StackTrace.StackFrame | undefined {
|
|
199
|
+
// Heuristic: frame 0 = Error, 1 = getCallerSiteInBrowser, 2 = your function, 3 = its caller
|
|
200
|
+
for (let i = 3; i < frames.length; i++) {
|
|
201
|
+
const f = frames[i];
|
|
202
|
+
const fn = f.fileName;
|
|
203
|
+
if (_is_src_file(fn)) {
|
|
204
|
+
return f;
|
|
159
205
|
}
|
|
160
|
-
}
|
|
161
|
-
return
|
|
162
|
-
}
|
|
206
|
+
}
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function _is_src_file(url: string | undefined): boolean {
|
|
211
|
+
if (!url) return false;
|
|
212
|
+
const m = url.match(/^(?:[a-z][a-z0-9+.-]*:)?\/\/[^\/]+\/([^\/?#]+)/);
|
|
213
|
+
if (m) {
|
|
214
|
+
const p1 = m[1];
|
|
215
|
+
const flag = !(p1.startsWith("@") || p1 === "node_modules");
|
|
216
|
+
return flag;
|
|
217
|
+
}
|
|
218
|
+
return true;
|
|
219
|
+
}
|
package/src/redux.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import {PiRegister} from ".";
|
|
2
|
+
import {pihanga as logger} from "./logger";
|
|
3
|
+
import {CardAction, ReduceF, ReduxState} from "./types";
|
|
4
4
|
|
|
5
|
-
const ns2Actions: {
|
|
5
|
+
const ns2Actions: {[k: string]: boolean} = {};
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Register a set of actions for a particular namespace.
|
|
@@ -18,32 +18,32 @@ const ns2Actions: { [k: string]: boolean } = {}
|
|
|
18
18
|
*/
|
|
19
19
|
export function registerActions<T extends string>(
|
|
20
20
|
namespace: string,
|
|
21
|
-
actions: readonly T[]
|
|
22
|
-
): {
|
|
21
|
+
actions: readonly T[]
|
|
22
|
+
): {[S in Uppercase<T>]: string} {
|
|
23
23
|
if (ns2Actions[namespace]) {
|
|
24
|
-
logger.warn(`Overwriting action namespace "${namespace}"`)
|
|
24
|
+
logger.warn(`Overwriting action namespace "${namespace}"`);
|
|
25
25
|
}
|
|
26
|
-
const ah: any = {}
|
|
26
|
+
const ah: any = {};
|
|
27
27
|
actions.forEach((a) => {
|
|
28
|
-
ah[a.toUpperCase()] = `${namespace}/${a}
|
|
29
|
-
})
|
|
30
|
-
logger.info(`Register action ns "${namespace}"`)
|
|
31
|
-
ns2Actions[namespace] = true
|
|
32
|
-
return ah as {
|
|
28
|
+
ah[a.toUpperCase()] = `${namespace}/${a}`;
|
|
29
|
+
});
|
|
30
|
+
logger.info(`Register action ns "${namespace}"`);
|
|
31
|
+
ns2Actions[namespace] = true;
|
|
32
|
+
return ah as {[S in Uppercase<T>]: string};
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
export function actionTypesToEvents(actionTypes: {
|
|
36
|
-
[k: string]: string
|
|
35
|
+
export function actionTypesToEvents(actionTypes: {[k: string]: string}): {
|
|
36
|
+
[k: string]: string;
|
|
37
37
|
} {
|
|
38
38
|
return Object.entries(actionTypes).reduce((p, el) => {
|
|
39
|
-
const [k, v] = el
|
|
39
|
+
const [k, v] = el;
|
|
40
40
|
const n = k
|
|
41
41
|
.split("_")
|
|
42
42
|
.map((s) => s.charAt(0).toUpperCase() + s.slice(1).toLowerCase())
|
|
43
|
-
.join("")
|
|
44
|
-
p[`on${n}`] = v
|
|
45
|
-
return p
|
|
46
|
-
}, {} as {
|
|
43
|
+
.join("");
|
|
44
|
+
p[`on${n}`] = v;
|
|
45
|
+
return p;
|
|
46
|
+
}, {} as {[k: string]: string});
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
@@ -53,12 +53,13 @@ export function actionTypesToEvents(actionTypes: { [k: string]: string }): {
|
|
|
53
53
|
* @param {string} actionType
|
|
54
54
|
* @returns a function to register a reducer for 'actionType'
|
|
55
55
|
*/
|
|
56
|
-
export function createOnAction<E>(
|
|
56
|
+
export function createOnAction<E>(
|
|
57
|
+
actionType: string
|
|
58
|
+
): <S extends ReduxState>(
|
|
57
59
|
register: PiRegister,
|
|
58
|
-
|
|
59
|
-
f: ReduceF<S, CardAction & E>,
|
|
60
|
+
f: ReduceF<S, CardAction & E>
|
|
60
61
|
) => void {
|
|
61
62
|
return (register, f) => {
|
|
62
|
-
register.reducer.register(actionType, f)
|
|
63
|
-
}
|
|
63
|
+
register.reducer.register(actionType, f);
|
|
64
|
+
};
|
|
64
65
|
}
|