@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,443 @@
1
+ import { InitializeBase } from "@zenflux/core/src/bases/initialize-base";
2
+
3
+ import { CommandsBase } from "@zenflux/commands/src/core/commands";
4
+ import { $e } from "@zenflux/commands/src/instance";
5
+ import { isCommandClass } from "@zenflux/commands/src/modules/command-infra";
6
+ import { ownKeys } from "@zenflux/commands/src/utils/object";
7
+
8
+ import type { CommandArgs, CommandInfo } from "@zenflux/commands/src/types";
9
+
10
+ /**
11
+ * @param system
12
+ * @param command
13
+ *
14
+ * @return the command's info (CommandInfra.getInfo()); none for a callback
15
+ */
16
+ const infoOf = ( system: CommandsBase, command: string ): CommandInfo => {
17
+ const entry = system.commands[ command ];
18
+
19
+ return ( entry && isCommandClass( entry ) && entry.getInfo() ) || {};
20
+ };
21
+
22
+ const STYLE = {
23
+ call: "color: #a21caf; font-weight: 600",
24
+ done: "color: #16a34a",
25
+ failed: "color: #dc2626; font-weight: 600",
26
+ request: "color: #0891b2",
27
+ hooks: "color: #2563eb",
28
+ callback: "color: #64748b",
29
+ time: "color: #94a3b8",
30
+ };
31
+
32
+ const indent = ( depth: number ): string => " ".repeat( Math.max( 0, depth ) );
33
+
34
+ // What the console shows instead of a secret.
35
+ const HIDDEN = "••••";
36
+
37
+ /**
38
+ * What logs a REST request's end (`Log.request()`): its answer, or the error it failed with.
39
+ */
40
+ export type RequestEnd = <TAnswer>( answer: TAnswer, failed: boolean ) => void;
41
+
42
+ /**
43
+ * What the hooks report (core/hooks/base.ts, modules/hook-base.ts).
44
+ */
45
+ export interface HookLog {
46
+ /**
47
+ * A command's hooks of a type and event, about to run.
48
+ */
49
+ run( type: string, command: string, args: CommandArgs, event: string ): void;
50
+
51
+ /**
52
+ * One of them, about to check its conditions.
53
+ */
54
+ callback( type: string, command: string, args: CommandArgs, event: string, id: string ): void;
55
+
56
+ /**
57
+ * One whose conditions held: it applies.
58
+ */
59
+ active( type: string, command: string, id: string ): void;
60
+ }
61
+
62
+ /**
63
+ * The console's log of what runs, as it runs (`$e.devTools.log`). Each command as it starts, with the call of the
64
+ * system that ran it ($e.run, $e.internal, $e.route) and its args; as it ends, with its time and
65
+ * result (or its error); the hooks it runs, and each callback that applies. Lines are indented as deep as they run
66
+ * within other commands (CommandsBase.trace). A command's secrets are hidden: the args its `getInfo().secretArgs`
67
+ * names, and its result with `getInfo().secretResult`. The app's REST requests too, under the command that made them
68
+ * (`request()`).
69
+ */
70
+ class Log extends InitializeBase {
71
+ public static getName(): string {
72
+ return "ZenFlux/Commands/Extras/Log";
73
+ }
74
+
75
+ /**
76
+ * Off: nothing is logged (`$e.devTools.log.enabled = false`).
77
+ */
78
+ public enabled = true;
79
+
80
+ /**
81
+ * Off: no REST request is logged, while commands still are (`$e.devTools.log.requests = false`).
82
+ */
83
+ public requests = true;
84
+
85
+ // When each command running now started: 'call command' => times.
86
+ #started = new Map<string, number[]>();
87
+
88
+ // The systems it logs, whose commands' secrets hide the requests they make.
89
+ #systems: CommandsBase[] = [];
90
+
91
+ /**
92
+ * Logs the commands of these systems as they run (their run:before and run:after events).
93
+ *
94
+ * @param systems
95
+ */
96
+ public listen( systems: CommandsBase[] ): void {
97
+ this.#systems.push( ...systems );
98
+
99
+ systems.forEach( ( system ) => {
100
+ system.on( "run:before", ( component, command, args ) =>
101
+ this.run( system.getCall(), command, this.withoutSecrets( system, command, args ) )
102
+ );
103
+ system.on( "run:after", ( component, command, args, result ) =>
104
+ this.done(
105
+ system.getCall(),
106
+ command,
107
+ infoOf( system, command ).secretResult && ! ( result instanceof Error ) ? HIDDEN : result
108
+ )
109
+ );
110
+ } );
111
+ }
112
+
113
+ /**
114
+ * @param system
115
+ * @param command
116
+ * @param args
117
+ *
118
+ * @return the args, as the console may show them
119
+ */
120
+ public withoutSecrets<TArgs extends object>( system: CommandsBase, command: string, args: TArgs ): TArgs | { [ name: string ]: TArgs[ keyof TArgs & string ] | string } {
121
+ const secret = infoOf( system, command ).secretArgs || [];
122
+
123
+ if ( ! secret.length || ! args || "object" !== typeof args ) {
124
+ return args;
125
+ }
126
+
127
+ return Object.fromEntries(
128
+ ownKeys( args ).map( ( name ) => [ name, secret.includes( name ) ? HIDDEN : args[ name ] ] )
129
+ );
130
+ }
131
+
132
+ /**
133
+ * @param call The system's call: '$e.run'...
134
+ * @param command
135
+ * @param args
136
+ */
137
+ public run<TArgs>( call: string, command: string, args: TArgs ): void {
138
+ const key = `${ call } ${ command }`;
139
+
140
+ this.#started.set( key, [ ...( this.#started.get( key ) || [] ), performance.now() ] );
141
+
142
+ if ( ! this.enabled ) {
143
+ return;
144
+ }
145
+
146
+ const shown = args && "object" === typeof args && ! Object.keys( args ).length ? [] : [ args ];
147
+
148
+ console.log(
149
+ `%c${ indent( CommandsBase.trace.length - 1 ) }${ call }%c( '${ command }' )`,
150
+ STYLE.call,
151
+ "",
152
+ ...shown
153
+ );
154
+ }
155
+
156
+ /**
157
+ * @param call
158
+ * @param command
159
+ * @param result Its result, or the error it failed with.
160
+ */
161
+ public done<TResult>( call: string, command: string, result: TResult ): void {
162
+ const key = `${ call } ${ command }`,
163
+ times = this.#started.get( key ) || [],
164
+ started = times.pop();
165
+
166
+ if ( ! times.length ) {
167
+ this.#started.delete( key );
168
+ }
169
+
170
+ if ( ! this.enabled ) {
171
+ return;
172
+ }
173
+
174
+ const time = undefined === started ? "" : ` ${ Math.round( performance.now() - started ) }ms`,
175
+ failed = result instanceof Error,
176
+ shown = undefined === result ? [] : [ result ];
177
+
178
+ console.log(
179
+ `%c${ indent( CommandsBase.trace.length - 1 ) }${ failed ? "✗" : "✓" } ${ command }%c${ time }`,
180
+ failed ? STYLE.failed : STYLE.done,
181
+ STYLE.time,
182
+ ...shown
183
+ );
184
+ }
185
+
186
+ /**
187
+ * Logs a REST request as it starts, one level under the command running now (CommandsBase.trace): the one that
188
+ * made it, unless another one started since and still runs. Its body and its answer are hidden while a command in
189
+ * the trace has secrets.
190
+ *
191
+ * @param method
192
+ * @param path
193
+ * @param body What it sends, if anything.
194
+ *
195
+ * @return what logs its end, with its time
196
+ */
197
+ public request<TBody>( method: string, path: string, body?: TBody ): RequestEnd {
198
+ if ( ! this.enabled || ! this.requests ) {
199
+ return () => {};
200
+ }
201
+
202
+ const started = performance.now(),
203
+ depth = CommandsBase.trace.length,
204
+ secret = this.#hasSecrets(),
205
+ shown = <TValue>( value: TValue ): Array<TValue | string> => {
206
+ if ( undefined === value ) {
207
+ return [];
208
+ }
209
+
210
+ return [ secret ? HIDDEN : value ];
211
+ };
212
+
213
+ console.log(
214
+ `%c${ indent( depth ) }⇢ ${ method } ${ path }%c${ depth ? "" : " (outside a command)" }`,
215
+ STYLE.request,
216
+ STYLE.time,
217
+ ...shown( body )
218
+ );
219
+
220
+ return ( answer, failed ) => {
221
+ console.log(
222
+ `%c${ indent( depth ) }${ failed ? "✗" : "⇠" } ${ method } ${ path.split( "?" )[ 0 ] }%c ${ Math.round(
223
+ performance.now() - started
224
+ ) }ms`,
225
+ failed ? STYLE.failed : STYLE.request,
226
+ STYLE.time,
227
+ ...shown( answer )
228
+ );
229
+ };
230
+ }
231
+
232
+ /**
233
+ * @return whether a command running now has secrets: its args, or its result
234
+ */
235
+ #hasSecrets(): boolean {
236
+ return CommandsBase.trace.some( ( command ) =>
237
+ this.#systems.some( ( system ) => {
238
+ const info = infoOf( system, command );
239
+
240
+ return !! info.secretResult || !! info.secretArgs?.length;
241
+ } )
242
+ );
243
+ }
244
+
245
+ /**
246
+ * What the hooks report (core/hooks/base.ts, modules/hook-base.ts).
247
+ *
248
+ * @return the hooks' reports: run(), callback(), active()
249
+ */
250
+ public callbacks(): HookLog {
251
+ return {
252
+ // A command's hooks of a type and event, about to run.
253
+ run: ( type, command, args, event ) => {
254
+ if ( this.enabled ) {
255
+ console.log(
256
+ `%c${ indent( CommandsBase.trace.length ) }hooks ${ type }:${ event }%c ${ command }`,
257
+ STYLE.hooks,
258
+ ""
259
+ );
260
+ }
261
+ },
262
+ // One of them, about to check its conditions.
263
+ callback: () => {},
264
+ // One whose conditions held: it applies.
265
+ active: ( type, command, id ) => {
266
+ if ( this.enabled ) {
267
+ console.log(
268
+ `%c${ indent( CommandsBase.trace.length + 1 ) }↳ ${ id }`,
269
+ STYLE.callback
270
+ );
271
+ }
272
+ },
273
+ };
274
+ }
275
+
276
+ public constructor() {
277
+ super();
278
+ }
279
+
280
+ public warn<TParams extends unknown[]>( message: string, ...params: TParams ): void {
281
+ this.logger.warn( this.warn, message, ...params );
282
+ }
283
+
284
+ public error<TError>( error: TError ): void {
285
+ this.logger.error( this.error, "failed:", error );
286
+ }
287
+ }
288
+
289
+ /**
290
+ * A command, as `$e.devTools.commands()` lists it.
291
+ */
292
+ export interface CommandRow {
293
+ command: string;
294
+ call: string;
295
+ component: string;
296
+ }
297
+
298
+ /**
299
+ * A component, as `$e.devTools.components()` lists it.
300
+ */
301
+ export interface ComponentRow {
302
+ namespace: string;
303
+ commands: number;
304
+ internal: number;
305
+ routes: number;
306
+ hooks: number;
307
+ store: string;
308
+ active: boolean;
309
+ }
310
+
311
+ /**
312
+ * A hook, as `$e.devTools.hooks()` lists it.
313
+ */
314
+ export interface HookRow {
315
+ id: string;
316
+ type: string;
317
+ event: string;
318
+ command: string;
319
+ container: string;
320
+ active: boolean;
321
+ }
322
+
323
+ /**
324
+ * `$e.devTools`, with `isDebug`, as Elementor's dev tools: the log of what runs (`log`), and what is registered, as
325
+ * console tables that the functions also return.
326
+ */
327
+ export default class DevTools extends InitializeBase {
328
+ public static getName(): string {
329
+ return "ZenFlux/Commands/Extras/DevTools";
330
+ }
331
+
332
+ public log: Log;
333
+
334
+ public constructor() {
335
+ super();
336
+
337
+ this.log = new Log();
338
+
339
+ this.log.listen( this.getSystems() );
340
+ }
341
+
342
+ /**
343
+ * @return the API's systems of commands
344
+ */
345
+ public getSystems(): CommandsBase[] {
346
+ return [ $e.commands, $e.commandsInternal, $e.routes ];
347
+ }
348
+
349
+ /**
350
+ * @return every command: { command, call (the system that runs it), component }
351
+ */
352
+ public commands(): CommandRow[] {
353
+ return this.table(
354
+ this.getSystems().flatMap( ( system ) =>
355
+ system.getAll().map( ( command ) => ( {
356
+ command,
357
+ call: system.getCall(),
358
+ component: system.components[ command ],
359
+ } ) )
360
+ )
361
+ );
362
+ }
363
+
364
+ /**
365
+ * @return every component: what it registers, its store, whether it is active
366
+ */
367
+ public components(): ComponentRow[] {
368
+ return this.table(
369
+ $e.components.getAll().flatMap( ( namespace ) => {
370
+ const component = $e.components.getByNamespace( namespace );
371
+
372
+ if ( ! component ) {
373
+ return [];
374
+ }
375
+
376
+ return [ {
377
+ namespace,
378
+ commands: Object.keys( component.getCommands() ).length,
379
+ internal: Object.keys( component.getCommandsInternal() ).length,
380
+ routes:
381
+ Object.keys( component.getRoutes() ).length +
382
+ Object.keys( component.getTabs() ).length,
383
+ hooks: Object.keys( component.getHooks() ).length,
384
+ store: component.hasStore() ? component.getStoreName() : "",
385
+ active: $e.components.isActive( namespace ),
386
+ } ];
387
+ } )
388
+ );
389
+ }
390
+
391
+ /**
392
+ * @return every hook: { id, type, event, command, container, active }
393
+ */
394
+ public hooks(): HookRow[] {
395
+ const rows: HookRow[] = [];
396
+
397
+ Object.entries( $e.hooks.getAll() ).forEach( ( [ type, events ] ) =>
398
+ Object.entries( events ).forEach( ( [ event, list ] ) =>
399
+ list.forEach( ( { command, callbacks } ) =>
400
+ Object.entries( callbacks ).forEach( ( [ container, bucket ] ) =>
401
+ bucket.forEach( ( callback ) =>
402
+ rows.push( {
403
+ id: callback.id,
404
+ type,
405
+ event,
406
+ command,
407
+ container,
408
+ active: callback.isActive,
409
+ } )
410
+ )
411
+ )
412
+ )
413
+ )
414
+ );
415
+
416
+ return this.table( rows );
417
+ }
418
+
419
+ /**
420
+ * @return the keys of each command with a shortcut: { command: keys }
421
+ */
422
+ public shortcuts(): { [ command: string ]: string } {
423
+ return this.table( $e.shortcuts.getAll() );
424
+ }
425
+
426
+ /**
427
+ * @return the commands running now, first to last
428
+ */
429
+ public trace(): string[] {
430
+ return [ ...CommandsBase.trace ];
431
+ }
432
+
433
+ /**
434
+ * @param rows
435
+ *
436
+ * @return the rows, after the console shows them as a table
437
+ */
438
+ public table<TRows>( rows: TRows ): TRows {
439
+ console.table( rows );
440
+
441
+ return rows;
442
+ }
443
+ }
package/src/index.ts ADDED
@@ -0,0 +1,83 @@
1
+ /**
2
+ * The commands system: Elementor's web-cli (modules/web-cli/assets/js), the `$e` API. `new API( config )` creates it;
3
+ * the classes here are what components, commands and hooks extend. An app types its own names by declaration merging
4
+ * of the registries (types.ts).
5
+ */
6
+ export { default as API } from "./api";
7
+ export type { APIConfig, RouteParams } from "./api";
8
+ export { $e } from "./instance";
9
+
10
+ // The components' stores: a reducer's state, and the calls that read, change and hear it.
11
+ export { createStore, dispatch, select, subscribe } from "./core/store";
12
+ export type { BoundActions, CurriedSelectors, StateOf, Store } from "./core/store";
13
+
14
+ export { default as CommandInfra } from "./modules/command-infra";
15
+ export type { CommandCallback, CommandClass, RegisterConfig, RunnableCommand } from "./modules/command-infra";
16
+ export { default as CommandBase } from "./modules/command-base";
17
+ export { default as CommandInternalBase } from "./modules/command-internal-base";
18
+ export { default as CommandCallbackBase } from "./modules/command-callback-base";
19
+
20
+ export { default as ComponentBase } from "./modules/component-base";
21
+ export type {
22
+ CommandClasses,
23
+ ComponentArgs,
24
+ HookClass,
25
+ HookInstances,
26
+ RouteCallbacks,
27
+ ShortcutConfigs,
28
+ StoreConfig,
29
+ TabConfig,
30
+ TabConfigs,
31
+ } from "./modules/component-base";
32
+
33
+ export { default as HookBase } from "./modules/hook-base";
34
+ export type { HookType, RunnableHook } from "./modules/hook-base";
35
+ export { default as HookBreak } from "./modules/hook-break";
36
+ export * as hookData from "./modules/hooks/data";
37
+ export * as hookUI from "./modules/hooks/ui";
38
+
39
+ export type { RouteOptions } from "./core/routes";
40
+ export type { ShortcutConfig, ShortcutHandler } from "./core/shortcuts";
41
+ export type { CommandsRunResult } from "./core/commands";
42
+
43
+ export { default as Module } from "./modules/module";
44
+ export type { ModuleEvents } from "./modules/module";
45
+ export { default as ArgsObject } from "./modules/args-object";
46
+ export { default as InstanceType } from "./modules/instance-type";
47
+ export { setter, setters, getters, valuesReducer, valueStore } from "./utils/value-store";
48
+ export type { Getters, SetAction, Setters, StoreAction, Update, ValueStoreConfig } from "./utils/value-store";
49
+ export { instancesStore, withInstances } from "./utils/instances-slice";
50
+ export type { InstancesState, InstancesStoreConfig } from "./utils/instances-slice";
51
+ export type { JsonObject, JsonValue } from "./utils/object";
52
+ export { default as DevTools } from "./extras/dev-tools";
53
+
54
+ export type {
55
+ ArgsOf,
56
+ ArgsParam,
57
+ CommandArgs,
58
+ CommandInfo,
59
+ CommandInternalName,
60
+ CommandInternalRegistry,
61
+ CommandName,
62
+ CommandRegistry,
63
+ CommandsInternalOf,
64
+ CommandsOf,
65
+ ComponentName,
66
+ ComponentRegistry,
67
+ EmptyArgs,
68
+ HookArgs,
69
+ HookContainer,
70
+ HookResult,
71
+ HookReturn,
72
+ HookableName,
73
+ OwnNames,
74
+ ResultOf,
75
+ RouteArgs,
76
+ RouteName,
77
+ RouteRegistry,
78
+ RoutesOf,
79
+ RunOptions,
80
+ RunResult,
81
+ ShortcutsOf,
82
+ SystemArgs,
83
+ } from "./types";
@@ -0,0 +1,30 @@
1
+ import type API from "./api";
2
+
3
+ declare global {
4
+ interface Window {
5
+ /**
6
+ * The API, on the page as Elementor's was: set by `new API()`, unless another `$e` is there already
7
+ * (Elementor's own, in its editor and wp-admin), which it leaves alone.
8
+ */
9
+ $e?: API;
10
+ }
11
+ }
12
+
13
+ /**
14
+ * The API that the package's classes run through: `new API()` sets it (undefined until then). The classes read this
15
+ * binding, not `window.$e`, so on a page where Elementor has its own `window.$e` both work.
16
+ */
17
+
18
+ export let $e: API;
19
+
20
+ /**
21
+ * @param api
22
+ */
23
+ export const setAPI = ( api: API ): void => {
24
+ // On the page: in place of an API of ours made before it, never in place of another `$e` (Elementor's).
25
+ if ( typeof window !== "undefined" && ( ! window.$e || window.$e === $e ) ) {
26
+ window.$e = api;
27
+ }
28
+
29
+ $e = api;
30
+ };
@@ -0,0 +1,113 @@
1
+ import InstanceType from "./instance-type";
2
+
3
+ import { ownKeys } from "@zenflux/commands/src/utils/object";
4
+
5
+ /**
6
+ * What `typeof` says.
7
+ */
8
+ export type ArgumentType = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
9
+
10
+ /**
11
+ * A class whose instances an argument may have to be (`requireArgumentInstance()`).
12
+ */
13
+ export type ArgumentClass = abstract new ( ...args: never[] ) => object;
14
+
15
+ /**
16
+ * @param args
17
+ * @param property
18
+ *
19
+ * @throws {Error} when the args do not have the property of their own
20
+ */
21
+ const requireOwn = ( args: object, property: string ): void => {
22
+ if ( ! Object.prototype.hasOwnProperty.call( args, property ) ) {
23
+ throw Error( `${ property } is required.` );
24
+ }
25
+ };
26
+
27
+ /**
28
+ * @param args
29
+ * @param property A name known at runtime: one of the args' own (`requireOwn()`).
30
+ *
31
+ * @return the args' own property by that name
32
+ */
33
+ const ownArgument = <TObject extends object>( args: TObject, property: string ): TObject[ keyof TObject & string ] | undefined => {
34
+ const key = ownKeys( args ).find( ( own ) => own === property );
35
+
36
+ return undefined === key ? undefined : args[ key ];
37
+ };
38
+
39
+ export default class ArgsObject<TArgs extends object = object> extends InstanceType {
40
+ public static getName(): string {
41
+ return "ZenFlux/Commands/Modules/ArgsObject";
42
+ }
43
+
44
+ public args: TArgs;
45
+
46
+ public constructor( args: TArgs ) {
47
+ super();
48
+
49
+ this.args = args;
50
+ }
51
+
52
+ /**
53
+ * Validate property in args (the object's own args, or the ones given).
54
+ *
55
+ * @throws {Error}
56
+ */
57
+ public requireArgument( property: keyof TArgs & string ): void;
58
+ public requireArgument<TObject extends object>( property: keyof TObject & string, args: TObject ): void;
59
+ public requireArgument<TObject extends object>( property: string, args: TObject | TArgs = this.args ): void {
60
+ requireOwn( args, property );
61
+ }
62
+
63
+ /**
64
+ * Validate property in args using `type === typeof(args.whatever)`.
65
+ *
66
+ * @throws {Error}
67
+ */
68
+ public requireArgumentType( property: keyof TArgs & string, type: ArgumentType ): void;
69
+ public requireArgumentType<TObject extends object>( property: keyof TObject & string, type: ArgumentType, args: TObject ): void;
70
+ public requireArgumentType<TObject extends object>( property: string, type: ArgumentType, args: TObject | TArgs = this.args ): void {
71
+ requireOwn( args, property );
72
+
73
+ if ( typeof ownArgument( args, property ) !== type ) {
74
+ throw Error( `${ property } invalid type: ${ type }.` );
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Validate property in args using `args.whatever instanceof instance`.
80
+ *
81
+ * @throws {Error}
82
+ */
83
+ public requireArgumentInstance( property: keyof TArgs & string, instance: ArgumentClass ): void;
84
+ public requireArgumentInstance<TObject extends object>( property: keyof TObject & string, instance: ArgumentClass, args: TObject ): void;
85
+ public requireArgumentInstance<TObject extends object>( property: string, instance: ArgumentClass, args: TObject | TArgs = this.args ): void {
86
+ requireOwn( args, property );
87
+
88
+ if ( ! ( ownArgument( args, property ) instanceof instance ) ) {
89
+ throw Error( `${ property } invalid instance.` );
90
+ }
91
+ }
92
+
93
+ /**
94
+ * Validate property in args using `type === args.whatever.constructor`.
95
+ *
96
+ * @throws {Error}
97
+ */
98
+ public requireArgumentConstructor( property: keyof TArgs & string, type: Function ): void;
99
+ public requireArgumentConstructor<TObject extends object>( property: keyof TObject & string, type: Function, args: TObject ): void;
100
+ public requireArgumentConstructor<TObject extends object>( property: string, type: Function, args: TObject | TArgs = this.args ): void {
101
+ requireOwn( args, property );
102
+
103
+ const value = ownArgument( args, property ),
104
+ // Typed as `any` by TS: a class's prototype has the class as its constructor.
105
+ expected: Function = type.prototype.constructor;
106
+
107
+ // Compared as strings: the constructors of an iframe's objects are not the same objects
108
+ // (window.Object !== window.top.Object).
109
+ if ( null === value || undefined === value || value.constructor.toString() !== expected.toString() ) {
110
+ throw Error( `${ property } invalid constructor type.` );
111
+ }
112
+ }
113
+ }