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