@zenflux/commands 0.0.0-stage → 0.0.2

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.
Files changed (43) hide show
  1. package/LICENSE +674 -0
  2. package/package.json +25 -3
  3. package/readme.md +42 -0
  4. package/src/api.ts +166 -0
  5. package/src/core/commands-internal.ts +15 -0
  6. package/src/core/commands.ts +684 -0
  7. package/src/core/components.ts +107 -0
  8. package/src/core/hooks/base.ts +451 -0
  9. package/src/core/hooks/data.ts +58 -0
  10. package/src/core/hooks/ui.ts +44 -0
  11. package/src/core/hooks.ts +267 -0
  12. package/src/core/routes.ts +247 -0
  13. package/src/core/shortcuts.ts +358 -0
  14. package/src/core/store.ts +155 -0
  15. package/src/env.d.ts +15 -0
  16. package/src/extras/dev-tools.ts +443 -0
  17. package/src/index.ts +83 -0
  18. package/src/instance.ts +30 -0
  19. package/src/modules/args-object.ts +113 -0
  20. package/src/modules/command-base.ts +51 -0
  21. package/src/modules/command-callback-base.ts +42 -0
  22. package/src/modules/command-infra.ts +230 -0
  23. package/src/modules/command-internal-base.ts +19 -0
  24. package/src/modules/component-base.ts +483 -0
  25. package/src/modules/hook-base.ts +158 -0
  26. package/src/modules/hook-break.ts +8 -0
  27. package/src/modules/hooks/data/after.ts +19 -0
  28. package/src/modules/hooks/data/base.ts +15 -0
  29. package/src/modules/hooks/data/catch.ts +16 -0
  30. package/src/modules/hooks/data/dependency.ts +16 -0
  31. package/src/modules/hooks/data/index.ts +6 -0
  32. package/src/modules/hooks/ui/after.ts +18 -0
  33. package/src/modules/hooks/ui/base.ts +15 -0
  34. package/src/modules/hooks/ui/before.ts +16 -0
  35. package/src/modules/hooks/ui/catch.ts +16 -0
  36. package/src/modules/hooks/ui/index.ts +6 -0
  37. package/src/modules/instance-type.ts +119 -0
  38. package/src/modules/module.ts +224 -0
  39. package/src/types.ts +194 -0
  40. package/src/utils/instances-slice.ts +123 -0
  41. package/src/utils/object.ts +162 -0
  42. package/src/utils/value-store.ts +145 -0
  43. package/README.md +0 -3
@@ -0,0 +1,267 @@
1
+ import { InitializeBase } from "@zenflux/core/src/bases/initialize-base";
2
+
3
+ import HooksData from "./hooks/data";
4
+ import HooksUI from "./hooks/ui";
5
+
6
+ import type HooksBase from "./hooks/base";
7
+ import type { HookCallback, HookCallbacksByEvent, HookCallbacksById } from "./hooks/base";
8
+ import type { HookType, RunnableHook } from "@zenflux/commands/src/modules/hook-base";
9
+ import type { CommandArgs, HookReturn } from "@zenflux/commands/src/types";
10
+
11
+ export default class Hooks extends InitializeBase {
12
+ public static getName(): string {
13
+ return "ZenFlux/Commands/Core/Hooks";
14
+ }
15
+
16
+ public data = new HooksData();
17
+ public ui = new HooksUI();
18
+
19
+ public constructor() {
20
+ super();
21
+ }
22
+
23
+ /**
24
+ * Activate all hooks.
25
+ */
26
+ public activate(): void {
27
+ this.getTypes().forEach( ( hooksType ) => {
28
+ hooksType.activate();
29
+ } );
30
+ }
31
+
32
+ /**
33
+ * Deactivate all hooks.
34
+ */
35
+ public deactivate(): void {
36
+ this.getTypes().forEach( ( hooksType ) => {
37
+ hooksType.deactivate();
38
+ } );
39
+ }
40
+
41
+ /**
42
+ * @param flat Each type's by id.
43
+ *
44
+ * @return every type's callbacks
45
+ */
46
+ public getAll( flat: true ): { [ type: string ]: HookCallbacksById };
47
+ public getAll( flat?: false ): { [ type: string ]: HookCallbacksByEvent };
48
+ public getAll( flat = false ): { [ type: string ]: HookCallbacksById | HookCallbacksByEvent } {
49
+ const result: { [ type: string ]: HookCallbacksById | HookCallbacksByEvent } = {};
50
+
51
+ this.getTypes().forEach( ( hooksType ) => {
52
+ result[ hooksType.getType() ] = hooksType.getAll( flat );
53
+ } );
54
+
55
+ return result;
56
+ }
57
+
58
+ public getTypes(): HooksBase[] {
59
+ return [
60
+ this.data,
61
+ this.ui,
62
+ ];
63
+ }
64
+
65
+ public getType( type: HookType ): HooksBase | undefined {
66
+ return this.getTypes().find(
67
+ ( hooks ) => type === hooks.getType(),
68
+ );
69
+ }
70
+
71
+ /**
72
+ * Register hook.
73
+ *
74
+ * @param type
75
+ * @param event
76
+ * @param instance
77
+ *
78
+ * @return Created callback
79
+ */
80
+ public register( type: HookType, event: string, instance: RunnableHook ): HookCallback {
81
+ return this.getHooks( type ).register( event, instance );
82
+ }
83
+
84
+ /**
85
+ * Take a hook back, by its type and id (`HookBase.getId()`).
86
+ *
87
+ * @param type
88
+ * @param id
89
+ */
90
+ public unregister( type: HookType, id: string ): void {
91
+ this.getHooks( type ).unregister( id );
92
+ }
93
+
94
+ /**
95
+ * Run's a hook.
96
+ *
97
+ * @param type
98
+ * @param event
99
+ * @param command
100
+ * @param args
101
+ * @param result
102
+ *
103
+ * @return result
104
+ */
105
+ public run<TValue>( type: HookType, event: string, command: string, args: CommandArgs, result?: TValue ): HookReturn[] | false {
106
+ return this.getHooks( type ).run( event, command, args, result );
107
+ }
108
+
109
+ /**
110
+ * Register data hook that's run after the command.
111
+ *
112
+ * @param instance
113
+ *
114
+ * @return callback
115
+ */
116
+ public registerDataAfter( instance: RunnableHook ): HookCallback {
117
+ return this.register( "data", "after", instance );
118
+ }
119
+
120
+ /**
121
+ * Register data hook that's run when the command fails.
122
+ *
123
+ * @param instance
124
+ *
125
+ * @return callback
126
+ */
127
+ public registerDataCatch( instance: RunnableHook ): HookCallback {
128
+ return this.register( "data", "catch", instance );
129
+ }
130
+
131
+ /**
132
+ * Register data hook that's run before the command as dependency.
133
+ *
134
+ * @param instance
135
+ *
136
+ * @return callback
137
+ */
138
+ public registerDataDependency( instance: RunnableHook ): HookCallback {
139
+ return this.register( "data", "dependency", instance );
140
+ }
141
+
142
+ /**
143
+ * Register UI hook that's run after the commands run.
144
+ *
145
+ * @param instance
146
+ *
147
+ * @return callback
148
+ */
149
+ public registerUIAfter( instance: RunnableHook ): HookCallback {
150
+ return this.register( "ui", "after", instance );
151
+ }
152
+
153
+ /**
154
+ * Register UI hook that's run when the command fails.
155
+ *
156
+ * @param instance
157
+ *
158
+ * @return callback
159
+ */
160
+ public registerUICatch( instance: RunnableHook ): HookCallback {
161
+ return this.register( "ui", "catch", instance );
162
+ }
163
+
164
+ /**
165
+ * Register UI hook that's run before the command.
166
+ *
167
+ * @param instance
168
+ *
169
+ * @return callback
170
+ */
171
+ public registerUIBefore( instance: RunnableHook ): HookCallback {
172
+ return this.register( "ui", "before", instance );
173
+ }
174
+
175
+ /**
176
+ * Run data hook that's run after the command.
177
+ *
178
+ * @param command
179
+ * @param args
180
+ * @param result
181
+ *
182
+ * @return result
183
+ */
184
+ public runDataAfter<TValue>( command: string, args: CommandArgs, result: TValue ): HookReturn[] | false {
185
+ return this.run( "data", "after", command, args, result );
186
+ }
187
+
188
+ /**
189
+ * Run data hook that's run when the command fails.
190
+ *
191
+ * @param command
192
+ * @param args
193
+ * @param error
194
+ *
195
+ * @return result
196
+ */
197
+ public runDataCatch<TError>( command: string, args: CommandArgs, error: TError ): HookReturn[] | false {
198
+ return this.run( "data", "catch", command, args, error );
199
+ }
200
+
201
+ /**
202
+ * Run data hook that's run before the command as dependency.
203
+ *
204
+ * @param command
205
+ * @param args
206
+ *
207
+ * @return result
208
+ */
209
+ public runDataDependency( command: string, args: CommandArgs ): HookReturn[] | false {
210
+ return this.run( "data", "dependency", command, args );
211
+ }
212
+
213
+ /**
214
+ * Run UI hook that's run after the commands run.
215
+ *
216
+ * @param command
217
+ * @param args
218
+ * @param result
219
+ *
220
+ * @return result
221
+ */
222
+ public runUIAfter<TValue>( command: string, args: CommandArgs, result: TValue ): HookReturn[] | false {
223
+ return this.run( "ui", "after", command, args, result );
224
+ }
225
+
226
+ /**
227
+ * Run UI hook that's run when the command fails.
228
+ *
229
+ * @param command
230
+ * @param args
231
+ * @param e
232
+ *
233
+ * @return result
234
+ */
235
+ public runUICatch<TError>( command: string, args: CommandArgs, e: TError ): HookReturn[] | false {
236
+ return this.run( "ui", "catch", command, args, e );
237
+ }
238
+
239
+ /**
240
+ * Run UI hook that's run before the command.
241
+ *
242
+ * @param command
243
+ * @param args
244
+ *
245
+ * @return result
246
+ */
247
+ public runUIBefore( command: string, args: CommandArgs ): HookReturn[] | false {
248
+ return this.run( "ui", "before", command, args );
249
+ }
250
+
251
+ /**
252
+ * @param type
253
+ *
254
+ * @throws {Error} for a type that has no hooks
255
+ *
256
+ * @return the hooks of the type
257
+ */
258
+ private getHooks( type: HookType ): HooksBase {
259
+ const hooks = this.getType( type );
260
+
261
+ if ( ! hooks ) {
262
+ throw Error( `No hooks of type '${ type }'.` );
263
+ }
264
+
265
+ return hooks;
266
+ }
267
+ }
@@ -0,0 +1,247 @@
1
+ import Commands, { CommandsBase } from "./commands";
2
+
3
+ import { isEqual } from "@zenflux/commands/src/utils/object";
4
+
5
+ import type { RegisteredCallback } from "@zenflux/commands/src/modules/command-infra";
6
+ import type { CommandArgs } from "@zenflux/commands/src/types";
7
+
8
+ export interface RouteOptions {
9
+ /**
10
+ * Keep it in its container's history (`back()`).
11
+ */
12
+ history?: boolean;
13
+ }
14
+
15
+ /**
16
+ * A route that ran in a container, with its args.
17
+ */
18
+ export interface RouteState {
19
+ route: string;
20
+ args?: CommandArgs;
21
+ }
22
+
23
+ /**
24
+ * `$e.routes`: what each container shows ('panel/pages', 'panel/styles'), one route per container, with history.
25
+ * A route opens its component first (`component.open()`).
26
+ */
27
+ export default class Routes extends Commands<RegisteredCallback> {
28
+ public static getName(): string {
29
+ return "ZenFlux/Commands/Core/Routes";
30
+ }
31
+
32
+ public savedStates: { [ container: string ]: Partial<RouteState> };
33
+ public historyPerComponent: { [ namespaceRoot: string ]: RouteState[] };
34
+
35
+ public constructor( args?: object ) {
36
+ super( args );
37
+
38
+ this.savedStates = {};
39
+ this.historyPerComponent = {};
40
+ }
41
+
42
+ public refreshContainer( container: string ): void {
43
+ const currentRoute = this.getCurrent( container ),
44
+ currentArgs = this.getCurrentArgs( container );
45
+
46
+ if ( ! currentRoute ) {
47
+ this.error( `\`${ container }\` has no current route.` );
48
+ }
49
+
50
+ this.clearCurrent( container );
51
+
52
+ this.to( currentRoute, currentArgs || undefined );
53
+ }
54
+
55
+ public getHistory(): { [ namespaceRoot: string ]: RouteState[] };
56
+ public getHistory( namespaceRoot: string ): RouteState[];
57
+ public getHistory( namespaceRoot = "" ): { [ namespaceRoot: string ]: RouteState[] } | RouteState[] {
58
+ if ( namespaceRoot ) {
59
+ return this.historyPerComponent[ namespaceRoot ] || [];
60
+ }
61
+
62
+ return this.historyPerComponent;
63
+ }
64
+
65
+ public clearHistory( namespaceRoot: string ): void {
66
+ delete this.historyPerComponent[ namespaceRoot ];
67
+ }
68
+
69
+ public clearCurrent( container: string ): void {
70
+ const route = this.current[ container ];
71
+
72
+ if ( ! route ) {
73
+ return;
74
+ }
75
+
76
+ this.detachCurrent( container );
77
+
78
+ this.requireComponent( route ).onCloseRoute( route );
79
+ }
80
+
81
+ public clear(): void {
82
+ Object.keys( this.current ).forEach( ( container ) => this.clearCurrent( container ) );
83
+ }
84
+
85
+ public saveState( container: string ): this {
86
+ this.savedStates[ container ] = {
87
+ route: this.current[ container ],
88
+ args: this.currentArgs[ container ],
89
+ };
90
+
91
+ return this;
92
+ }
93
+
94
+ public restoreState( container: string ): boolean {
95
+ const state = this.savedStates[ container ];
96
+
97
+ if ( ! state ) {
98
+ return false;
99
+ }
100
+
101
+ if ( ! state.route ) {
102
+ this.error( `\`${ container }\` had no route to restore.` );
103
+ }
104
+
105
+ this.to( state.route, state.args );
106
+
107
+ return true;
108
+ }
109
+
110
+ public validateRun( route: string, args: CommandArgs = {} ): boolean {
111
+ if ( ! super.validateRun( route, args ) ) {
112
+ return false;
113
+ }
114
+
115
+ if ( this.is( route, args ) && ! args.refresh ) {
116
+ return false;
117
+ }
118
+
119
+ const component = this.requireComponent( route );
120
+
121
+ if ( ! component.isOpen || args.reOpen ) {
122
+ component.isOpen = component.open( args );
123
+ }
124
+
125
+ return !! component.isOpen;
126
+ }
127
+
128
+ /**
129
+ * @override
130
+ */
131
+ public beforeRun( route: string, args: CommandArgs ): void {
132
+ const component = this.requireComponent( route ),
133
+ container = component.getServiceName(),
134
+ oldRoute = this.current[ container ];
135
+
136
+ if ( oldRoute ) {
137
+ this.requireComponent( oldRoute ).onCloseRoute( oldRoute );
138
+ }
139
+
140
+ CommandsBase.trace.push( route );
141
+
142
+ super.beforeRun( route, args, false );
143
+
144
+ this.attachCurrent( container, route, args );
145
+ }
146
+
147
+ /**
148
+ * Go to a route, by a name known at runtime (`$e.route()` takes the app's names, typed).
149
+ *
150
+ * @param route
151
+ * @param args
152
+ * @param options
153
+ */
154
+ public to( route: string, args?: CommandArgs, options: RouteOptions = { history: true } ): void {
155
+ this.run( route, args );
156
+
157
+ const namespaceRoot = this.requireComponent( route ).getServiceName();
158
+
159
+ if ( options.history ) {
160
+ const history = this.historyPerComponent[ namespaceRoot ] || [];
161
+
162
+ this.historyPerComponent[ namespaceRoot ] = history;
163
+
164
+ history.push( {
165
+ route,
166
+ args,
167
+ } );
168
+ }
169
+ }
170
+
171
+ public back( namespaceRoot: string ): void {
172
+ const history = this.getHistory( namespaceRoot );
173
+
174
+ // Remove current.
175
+ history.pop();
176
+
177
+ const last = history.pop();
178
+
179
+ if ( ! last ) {
180
+ return;
181
+ }
182
+
183
+ this.to( last.route, last.args );
184
+ }
185
+
186
+ // Don't use the event object.
187
+ public runShortcut( command: string ): void {
188
+ this.to( command );
189
+ }
190
+
191
+ // Don't clear current route.
192
+ public afterRun<TResult>( route: string, args: CommandArgs, results?: TResult ): void {
193
+ const component = this.requireComponent( route );
194
+
195
+ component.onRoute( route, args );
196
+
197
+ super.afterRun( route, args, results, false );
198
+
199
+ CommandsBase.trace.pop();
200
+ }
201
+
202
+ public is( route: string, args: CommandArgs = {} ): boolean {
203
+ if ( ! super.is( route ) ) {
204
+ return false;
205
+ }
206
+
207
+ const container = this.requireComponent( route ).getServiceName();
208
+
209
+ return isEqual( args, this.currentArgs[ container ] );
210
+ }
211
+
212
+ /**
213
+ * Check against current command hierarchically.
214
+ * For example `isPartOf( 'panel' )` will be true for `panel/elements`,
215
+ * `isPartOf( 'panel/editor' )` will be true for `panel/editor/style`.
216
+ *
217
+ * @param route
218
+ *
219
+ * @return whether the container's current route is the route or under it
220
+ */
221
+ public isPartOf( route: string ): boolean {
222
+ const parts = route.split( "/" ),
223
+ container = parts[ 0 ],
224
+ toCheck: string[] = [],
225
+ currentParts = this.current[ container ] ? this.current[ container ].split( "/" ) : [];
226
+
227
+ let match = false;
228
+
229
+ currentParts.forEach( ( part ) => {
230
+ toCheck.push( part );
231
+
232
+ if ( toCheck.join( "/" ) === route ) {
233
+ match = true;
234
+ }
235
+ } );
236
+
237
+ return match;
238
+ }
239
+
240
+ public getCall(): string {
241
+ return "$e.route";
242
+ }
243
+
244
+ public error( message: string ): never {
245
+ throw Error( "Routes: " + message );
246
+ }
247
+ }