@zenflux/commands 0.0.0-stage → 0.0.1
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/LICENSE +674 -0
- package/package.json +22 -3
- package/readme.md +40 -0
- package/src/api.ts +158 -0
- package/src/core/commands-internal.ts +11 -0
- package/src/core/commands.ts +674 -0
- package/src/core/components.ts +103 -0
- package/src/core/hooks/base.ts +444 -0
- package/src/core/hooks/data.ts +54 -0
- package/src/core/hooks/ui.ts +40 -0
- package/src/core/hooks.ts +257 -0
- package/src/core/routes.ts +243 -0
- package/src/core/shortcuts.ts +351 -0
- package/src/core/store.ts +155 -0
- package/src/env.d.ts +15 -0
- package/src/extras/dev-tools.ts +427 -0
- package/src/index.ts +83 -0
- package/src/instance.ts +30 -0
- package/src/modules/args-object.ts +113 -0
- package/src/modules/command-base.ts +51 -0
- package/src/modules/command-callback-base.ts +42 -0
- package/src/modules/command-infra.ts +230 -0
- package/src/modules/command-internal-base.ts +19 -0
- package/src/modules/component-base.ts +479 -0
- package/src/modules/hook-base.ts +149 -0
- package/src/modules/hook-break.ts +8 -0
- package/src/modules/hooks/data/after.ts +15 -0
- package/src/modules/hooks/data/base.ts +11 -0
- package/src/modules/hooks/data/catch.ts +12 -0
- package/src/modules/hooks/data/dependency.ts +12 -0
- package/src/modules/hooks/data/index.ts +6 -0
- package/src/modules/hooks/ui/after.ts +14 -0
- package/src/modules/hooks/ui/base.ts +11 -0
- package/src/modules/hooks/ui/before.ts +12 -0
- package/src/modules/hooks/ui/catch.ts +12 -0
- package/src/modules/hooks/ui/index.ts +6 -0
- package/src/modules/instance-type.ts +114 -0
- package/src/modules/module.ts +216 -0
- package/src/types.ts +194 -0
- package/src/utils/console.ts +19 -0
- package/src/utils/instances-slice.ts +123 -0
- package/src/utils/object.ts +162 -0
- package/src/utils/value-store.ts +145 -0
- package/README.md +0 -3
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import Console from "@zenflux/commands/src/utils/console";
|
|
2
|
+
import { $e } from "@zenflux/commands/src/instance";
|
|
3
|
+
|
|
4
|
+
const isMac = "undefined" !== typeof navigator && navigator.userAgent.includes( "Macintosh" );
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param element
|
|
8
|
+
*
|
|
9
|
+
* @return whether the element is a frame. By its tag: a frame's elements are of its own window's classes.
|
|
10
|
+
*/
|
|
11
|
+
const isFrame = ( element: Element ): element is HTMLIFrameElement => "IFRAME" === element.tagName;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param doc
|
|
15
|
+
*
|
|
16
|
+
* @return the frame that has the focus in the document (through Shadow DOMs), or null
|
|
17
|
+
*/
|
|
18
|
+
const focusedFrame = ( doc: Document | undefined ): HTMLIFrameElement | null => {
|
|
19
|
+
let active = doc?.activeElement;
|
|
20
|
+
|
|
21
|
+
while ( active?.shadowRoot?.activeElement ) {
|
|
22
|
+
active = active.shadowRoot.activeElement;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return active && isFrame( active ) ? active : null;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param target
|
|
30
|
+
*
|
|
31
|
+
* @return whether the target is an element: one of the page's, or of a frame's (of its own window's classes)
|
|
32
|
+
*/
|
|
33
|
+
const isElement = ( target: EventTarget ): target is Element => "matches" in target && "function" === typeof target.matches;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A handler of keys (`$e.shortcuts.register()`).
|
|
37
|
+
*/
|
|
38
|
+
export interface ShortcutHandler {
|
|
39
|
+
/**
|
|
40
|
+
* Required.
|
|
41
|
+
*/
|
|
42
|
+
callback: ( event: KeyboardEvent ) => void;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The command it runs (set by `$e.commands.register()`).
|
|
46
|
+
*/
|
|
47
|
+
command?: string;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Namespaces: runs only while the last active component is one of them.
|
|
51
|
+
*/
|
|
52
|
+
scopes?: string[];
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Runs only when it returns true for the event.
|
|
56
|
+
*/
|
|
57
|
+
dependency?: ( event: KeyboardEvent ) => boolean;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 'input': not while typing in a field.
|
|
61
|
+
*/
|
|
62
|
+
exclude?: string[];
|
|
63
|
+
|
|
64
|
+
allowAltKey?: boolean;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* What it does, in words, for a list of the shortcuts: read as the list shows, so it is in the language of the moment.
|
|
68
|
+
*/
|
|
69
|
+
title?: () => string;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The heading it is listed under, in words, read as the list shows; none: the list's first.
|
|
73
|
+
*/
|
|
74
|
+
group?: () => string;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* When its group's shortcuts work, in words, under the heading: read as the list shows. Each of the group says the same.
|
|
78
|
+
*/
|
|
79
|
+
groupNote?: () => string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* A component's shortcut of a command (`ComponentBase.defaultShortcuts()`): its keys, and the handler's options.
|
|
84
|
+
* Registering the command makes it the command's handler (it sets `command` and `callback`).
|
|
85
|
+
*/
|
|
86
|
+
export interface ShortcutConfig extends Omit<ShortcutHandler, "callback"> {
|
|
87
|
+
keys: string;
|
|
88
|
+
callback?: ShortcutHandler[ "callback" ];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* `$e.shortcuts`: keys ('ctrl+shift+z', 'esc', 'ctrl+comma', 'ctrl+s,ctrl+enter'; ctrl is cmd on a Mac) to handlers, the ones in the
|
|
93
|
+
* active component's scope first.
|
|
94
|
+
*/
|
|
95
|
+
export default class Shortcuts {
|
|
96
|
+
/**
|
|
97
|
+
* By `event.code`, the key's place on the keyboard.
|
|
98
|
+
*/
|
|
99
|
+
public specialKeys: { [ code: string ]: string };
|
|
100
|
+
|
|
101
|
+
public handlers: { [ shortcut: string ]: ShortcutHandler[] };
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @param target Window to listen to, and to the frames in it as they take the focus (their keys do not reach it).
|
|
105
|
+
*/
|
|
106
|
+
public constructor( target: Window ) {
|
|
107
|
+
this.specialKeys = {
|
|
108
|
+
Enter: "enter",
|
|
109
|
+
NumpadEnter: "enter",
|
|
110
|
+
Escape: "esc",
|
|
111
|
+
ArrowUp: "up",
|
|
112
|
+
ArrowDown: "down",
|
|
113
|
+
Delete: "del",
|
|
114
|
+
Slash: "?",
|
|
115
|
+
// By name, as a comma separates shortcuts: 'ctrl+comma'. The same key in any layout (in Hebrew it types ת).
|
|
116
|
+
Comma: "comma",
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
this.handlers = {};
|
|
120
|
+
|
|
121
|
+
this.bindListener( target );
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// The documents listened to, each once: a frame that loads another page has the same window, and a new document.
|
|
125
|
+
#targets = new WeakSet<Document>();
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Listen to another window too, such as a preview iframe's; and to the same-origin frames in it, each as it takes
|
|
129
|
+
* the focus (its window loses it then).
|
|
130
|
+
*
|
|
131
|
+
* @param target
|
|
132
|
+
*/
|
|
133
|
+
public bindListener( target: Window ): void {
|
|
134
|
+
let doc;
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
doc = target.document;
|
|
138
|
+
|
|
139
|
+
if ( this.#targets.has( doc ) ) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
target.addEventListener( "keydown", ( event ) => this.handle( event ) );
|
|
144
|
+
} catch {
|
|
145
|
+
// Another origin's frame.
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
this.#targets.add( doc );
|
|
150
|
+
|
|
151
|
+
target.addEventListener( "blur", () =>
|
|
152
|
+
// Once the focus is in the frame.
|
|
153
|
+
setTimeout( () => {
|
|
154
|
+
const frame = focusedFrame( target.document );
|
|
155
|
+
|
|
156
|
+
if ( frame?.contentWindow ) {
|
|
157
|
+
this.bindListener( frame.contentWindow );
|
|
158
|
+
}
|
|
159
|
+
} )
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @return the keys of each command with a shortcut: { command: keys }
|
|
165
|
+
*/
|
|
166
|
+
public getAll(): { [ command: string ]: string } {
|
|
167
|
+
const shortcuts: { [ command: string ]: string } = {};
|
|
168
|
+
|
|
169
|
+
Object.entries( this.handlers ).forEach( ( [ key, handlers ] ) => {
|
|
170
|
+
handlers.forEach( ( config ) => {
|
|
171
|
+
// A handler of no command is listed as 'undefined', as a property key makes it.
|
|
172
|
+
shortcuts[ String( config.command ) ] = key;
|
|
173
|
+
} );
|
|
174
|
+
} );
|
|
175
|
+
|
|
176
|
+
return shortcuts;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* @param shortcuts Keys, several separated by commas.
|
|
181
|
+
* @param args
|
|
182
|
+
*/
|
|
183
|
+
public register( shortcuts: string, args: ShortcutHandler ): void {
|
|
184
|
+
shortcuts.replace( /\s/g, "" ).split( "," ).forEach( ( shortcut ) => {
|
|
185
|
+
const handlers = this.handlers[ shortcut ] || [];
|
|
186
|
+
|
|
187
|
+
this.handlers[ shortcut ] = handlers;
|
|
188
|
+
|
|
189
|
+
handlers.push( args );
|
|
190
|
+
} );
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
public unregister( shortcuts: string, args: ShortcutHandler | ShortcutConfig ): void {
|
|
194
|
+
shortcuts.replace( /\s/g, "" ).split( "," ).forEach( ( shortcut ) => {
|
|
195
|
+
if ( this.handlers[ shortcut ] ) {
|
|
196
|
+
this.handlers[ shortcut ] = this.handlers[ shortcut ].filter( ( handler ) => args !== handler );
|
|
197
|
+
}
|
|
198
|
+
} );
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
public handle( event: KeyboardEvent ): void {
|
|
202
|
+
const handlers = this.getHandlersByPriority( event );
|
|
203
|
+
|
|
204
|
+
if ( ! handlers ) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Inside a Shadow DOM, `event.target` at the window is the shadow host: the element typed in is the path's first.
|
|
209
|
+
const target = event.composedPath?.()[ 0 ] || event.target;
|
|
210
|
+
|
|
211
|
+
const filteredHandlers = handlers.filter( ( handler ) => {
|
|
212
|
+
if ( handler.exclude && handler.exclude.includes( "input" ) && this.isInputTarget( target ) ) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if ( handler.dependency && ! handler.dependency( event ) ) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Fix for some keyboard sources that consider alt key as ctrl key
|
|
221
|
+
if ( ! handler.allowAltKey && event.altKey ) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return true;
|
|
226
|
+
} );
|
|
227
|
+
|
|
228
|
+
if ( ! filteredHandlers.length ) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if ( 1 < filteredHandlers.length && $e.config.isDebug ) {
|
|
233
|
+
Console.warn( "Multiple handlers for shortcut.", filteredHandlers, event );
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
event.preventDefault();
|
|
237
|
+
|
|
238
|
+
filteredHandlers[ 0 ].callback( event );
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* @param target
|
|
243
|
+
*
|
|
244
|
+
* @return whether the user types there: a field, or editable content. Not a button: Ctrl+Z goes on undoing after a
|
|
245
|
+
* click on an Undo button, which keeps the focus.
|
|
246
|
+
*/
|
|
247
|
+
public isInputTarget( target: EventTarget | null ): boolean {
|
|
248
|
+
return !! target && isElement( target ) && (
|
|
249
|
+
target.matches( "input, textarea, select" ) ||
|
|
250
|
+
( "isContentEditable" in target && true === target.isContentEditable )
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* @param event
|
|
256
|
+
*
|
|
257
|
+
* @return whether 'ctrl' is pressed: Cmd on a Mac, Ctrl elsewhere
|
|
258
|
+
*/
|
|
259
|
+
public isControlEvent( event: KeyboardEvent ): boolean {
|
|
260
|
+
return event[ isMac ? "metaKey" : "ctrlKey" ];
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
public getEventShortcut( event: KeyboardEvent ): string {
|
|
264
|
+
const shortcut = [];
|
|
265
|
+
|
|
266
|
+
if ( event.altKey ) {
|
|
267
|
+
shortcut.push( "alt" );
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if ( this.isControlEvent( event ) ) {
|
|
271
|
+
shortcut.push( "ctrl" );
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if ( event.shiftKey ) {
|
|
275
|
+
shortcut.push( "shift" );
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
shortcut.push( this.getEventKey( event ) );
|
|
279
|
+
|
|
280
|
+
return shortcut.join( "+" );
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* @param event
|
|
285
|
+
*
|
|
286
|
+
* @return the key's name: letters and digits by their place on the keyboard, so 'ctrl+z' is the same key in any
|
|
287
|
+
* layout (Hebrew too), as Elementor's `event.which` was
|
|
288
|
+
*/
|
|
289
|
+
public getEventKey( event: KeyboardEvent ): string {
|
|
290
|
+
if ( this.specialKeys[ event.code ] ) {
|
|
291
|
+
return this.specialKeys[ event.code ];
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const letterOrDigit = /^(?:Key|Digit)(.)$/.exec( event.code || "" );
|
|
295
|
+
|
|
296
|
+
if ( letterOrDigit ) {
|
|
297
|
+
return letterOrDigit[ 1 ].toLowerCase();
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return ( event.key || "" ).toLowerCase();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
public isActiveScope( scopes: string[] ): boolean {
|
|
304
|
+
const activeComponents = Object.keys( $e.components.activeComponents ),
|
|
305
|
+
activeComponent = activeComponents[ activeComponents.length - 1 ],
|
|
306
|
+
component = $e.components.getByNamespace( activeComponent );
|
|
307
|
+
|
|
308
|
+
if ( ! component ) {
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const namespace = component.getNamespace();
|
|
313
|
+
|
|
314
|
+
const filteredByNamespace = scopes.some( ( scope ) => namespace === scope );
|
|
315
|
+
|
|
316
|
+
if ( filteredByNamespace ) {
|
|
317
|
+
return true;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Else filter by namespaceRoot.
|
|
321
|
+
const namespaceRoot = component.getServiceName();
|
|
322
|
+
|
|
323
|
+
return scopes.some( ( scope ) => namespaceRoot === scope );
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
public getHandlersByPriority( event: KeyboardEvent ): ShortcutHandler[] | false {
|
|
327
|
+
const handlers = this.handlers[ this.getEventShortcut( event ) ];
|
|
328
|
+
|
|
329
|
+
if ( ! handlers ) {
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const inCurrentScope = handlers.filter( ( handler ) => {
|
|
334
|
+
return handler.scopes && this.isActiveScope( handler.scopes );
|
|
335
|
+
} );
|
|
336
|
+
|
|
337
|
+
if ( inCurrentScope.length ) {
|
|
338
|
+
return inCurrentScope;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const noScope = handlers.filter( ( handler ) => {
|
|
342
|
+
return ! handler.scopes;
|
|
343
|
+
} );
|
|
344
|
+
|
|
345
|
+
if ( noScope.length ) {
|
|
346
|
+
return noScope;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The components' stores: a component's state, made by its reducer from the actions dispatched to it, and nothing
|
|
3
|
+
* more (no registry, resolvers or middleware). A component's `defaultStore()` gives its config (`valueStore()`,
|
|
4
|
+
* `instancesStore()` or its own): the reducer, the action creators and the selectors. `select( store )` and
|
|
5
|
+
* `dispatch( store )` bind them to the store, `subscribe( listener, store )` hears its changes.
|
|
6
|
+
*/
|
|
7
|
+
import type { StoreAction } from "@zenflux/commands/src/utils/value-store";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A store's config: its reducer, the creators of the actions it takes, and the selectors of its state.
|
|
11
|
+
*/
|
|
12
|
+
export interface StoreConfig<TState extends object = object> {
|
|
13
|
+
// A method, so a config of any state is a StoreConfig (its reducer's state is the config's own).
|
|
14
|
+
reducer( state: TState | undefined, action: StoreAction ): TState;
|
|
15
|
+
actions: object;
|
|
16
|
+
selectors: object;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The state a config's reducer makes.
|
|
21
|
+
*/
|
|
22
|
+
export type StateOf<TConfig extends StoreConfig> = ReturnType<TConfig[ "reducer" ]>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A config's selectors, bound to its store: called without the state, which the store gives them.
|
|
26
|
+
*/
|
|
27
|
+
export type CurriedSelectors<TConfig extends StoreConfig> = {
|
|
28
|
+
[ TName in keyof TConfig[ "selectors" ] ]: TConfig[ "selectors" ][ TName ] extends ( state: never, ...args: infer TArgs ) => infer TResult
|
|
29
|
+
? ( ...args: TArgs ) => TResult
|
|
30
|
+
: never;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A config's action creators, bound to its store: each dispatches the action it creates.
|
|
35
|
+
*/
|
|
36
|
+
export type BoundActions<TConfig extends StoreConfig> = {
|
|
37
|
+
[ TName in keyof TConfig[ "actions" ] ]: TConfig[ "actions" ][ TName ] extends ( ...args: infer TArgs ) => StoreAction
|
|
38
|
+
? ( ...args: TArgs ) => void
|
|
39
|
+
: never;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A store: its state, what changes it, and who hears it change.
|
|
44
|
+
*/
|
|
45
|
+
export interface Store<TConfig extends StoreConfig = StoreConfig> {
|
|
46
|
+
readonly name: string;
|
|
47
|
+
readonly config: TConfig;
|
|
48
|
+
getState(): StateOf<TConfig>;
|
|
49
|
+
dispatch( action: StoreAction ): void;
|
|
50
|
+
/**
|
|
51
|
+
* @param listener Called after each change of the state: an action its reducer answered with another state.
|
|
52
|
+
*
|
|
53
|
+
* @return what stops it
|
|
54
|
+
*/
|
|
55
|
+
subscribe( listener: () => void ): () => void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const INIT: StoreAction = { type: "@@zenflux/commands/INIT" };
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The selectors and actions bound to each store, made once.
|
|
62
|
+
*/
|
|
63
|
+
const bound = new WeakMap<Store, { selectors: object; actions: object }>();
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param name The store's name (`<storePrefix>/<namespace>`).
|
|
67
|
+
* @param config
|
|
68
|
+
*
|
|
69
|
+
* @return a store of the config's reducer, at its initial state
|
|
70
|
+
*/
|
|
71
|
+
export function createStore<TConfig extends StoreConfig>( name: string, config: TConfig ): Store<TConfig> {
|
|
72
|
+
let state: StateOf<TConfig> = config.reducer( undefined, INIT ) as StateOf<TConfig>;
|
|
73
|
+
|
|
74
|
+
const listeners = new Set<() => void>();
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
name,
|
|
78
|
+
config,
|
|
79
|
+
getState: () => state,
|
|
80
|
+
dispatch( action: StoreAction ) {
|
|
81
|
+
const next = config.reducer( state, action ) as StateOf<TConfig>;
|
|
82
|
+
|
|
83
|
+
// A reducer answers an action that changes nothing with the same state: no one is told of it.
|
|
84
|
+
if ( next === state ) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
state = next;
|
|
89
|
+
|
|
90
|
+
// A copy: a listener may stop itself, or another, as it is called.
|
|
91
|
+
[ ...listeners ].forEach( ( listener ) => listener() );
|
|
92
|
+
},
|
|
93
|
+
subscribe( listener: () => void ) {
|
|
94
|
+
listeners.add( listener );
|
|
95
|
+
|
|
96
|
+
return () => {
|
|
97
|
+
listeners.delete( listener );
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function boundOf( store: Store ) {
|
|
104
|
+
let found = bound.get( store );
|
|
105
|
+
|
|
106
|
+
if ( ! found ) {
|
|
107
|
+
const { selectors, actions } = store.config;
|
|
108
|
+
|
|
109
|
+
found = {
|
|
110
|
+
selectors: Object.fromEntries( Object.entries( selectors ).map( ( [ name, selector ] ) => [
|
|
111
|
+
name,
|
|
112
|
+
( ...args: never[] ) => selector( store.getState(), ...args ),
|
|
113
|
+
] ) ),
|
|
114
|
+
actions: Object.fromEntries( Object.entries( actions ).map( ( [ name, creator ] ) => [
|
|
115
|
+
name,
|
|
116
|
+
( ...args: never[] ) => store.dispatch( creator( ...args ) ),
|
|
117
|
+
] ) ),
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
bound.set( store, found );
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return found;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @param store
|
|
128
|
+
*
|
|
129
|
+
* @return its selectors, each reading the store's state as it is called
|
|
130
|
+
*/
|
|
131
|
+
export function select<TConfig extends StoreConfig>( store: Store<TConfig> ): CurriedSelectors<TConfig> {
|
|
132
|
+
// Made from the config's selectors, each one's own state argument bound: the config's types, which TS can't
|
|
133
|
+
// follow through `Object.entries()`.
|
|
134
|
+
return boundOf( store ).selectors as CurriedSelectors<TConfig>;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @param store
|
|
139
|
+
*
|
|
140
|
+
* @return its action creators, each dispatching the action it creates to the store
|
|
141
|
+
*/
|
|
142
|
+
export function dispatch<TConfig extends StoreConfig>( store: Store<TConfig> ): BoundActions<TConfig> {
|
|
143
|
+
// As `select()`: the config's action creators, bound.
|
|
144
|
+
return boundOf( store ).actions as BoundActions<TConfig>;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param listener
|
|
149
|
+
* @param store
|
|
150
|
+
*
|
|
151
|
+
* @return what stops it
|
|
152
|
+
*/
|
|
153
|
+
export function subscribe( listener: () => void, store: Store ): () => void {
|
|
154
|
+
return store.subscribe( listener );
|
|
155
|
+
}
|
package/src/env.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `process.env.NODE_ENV`, which the app's build replaces ('production' in a production build): the dev tools are only
|
|
3
|
+
* in a development build (api.ts). Declared as @types/node declares it, so the two merge where an app has both.
|
|
4
|
+
*/
|
|
5
|
+
declare namespace NodeJS {
|
|
6
|
+
interface ProcessEnv {
|
|
7
|
+
NODE_ENV?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface Process {
|
|
11
|
+
env: ProcessEnv;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
declare var process: NodeJS.Process;
|