@makoojs/core 0.5.0 → 0.6.0
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/README.CN.md +32 -11
- package/README.md +32 -11
- package/dist/adapter/types.d.ts +35 -7
- package/dist/error/MakooError.d.ts +17 -10
- package/dist/error/coreErrors.d.ts +6793 -0
- package/dist/error/defineMakooErrors.d.ts +55 -0
- package/dist/index.cjs +3 -4
- package/dist/index.d.ts +16 -10
- package/dist/index.js +920 -818
- package/dist/injections/inject/declaration.d.ts +32 -0
- package/dist/injections/inject/inject.d.ts +38 -0
- package/dist/injections/injectEach/declaration.d.ts +25 -0
- package/dist/injections/injectEach/injectEach.d.ts +43 -0
- package/dist/injections/kinds.d.ts +12 -0
- package/dist/injections/listen/declaration.d.ts +34 -0
- package/dist/injections/listen/listen.d.ts +36 -0
- package/dist/injections/mount.d.ts +17 -0
- package/dist/{core → instance}/createMakoo.d.ts +1 -0
- package/dist/instance/residue.d.ts +7 -0
- package/dist/instance/types.d.ts +81 -0
- package/dist/runtime/injection.d.ts +94 -0
- package/dist/runtime/match.d.ts +50 -0
- package/dist/runtime/observer.d.ts +9 -0
- package/dist/runtime/scope.d.ts +28 -0
- package/dist/runtime/selector.d.ts +1 -0
- package/dist/runtime/stateNode.d.ts +51 -0
- package/package.json +1 -4
- package/dist/adapter/registry.d.ts +0 -5
- package/dist/component/component.d.ts +0 -5
- package/dist/component/declaration.d.ts +0 -3
- package/dist/component/execution.d.ts +0 -24
- package/dist/component/types.d.ts +0 -37
- package/dist/core/types.d.ts +0 -27
- package/dist/dom/observer.d.ts +0 -4
- package/dist/dom/waitForElement.d.ts +0 -3
- package/dist/dom/watchElement.d.ts +0 -2
- package/dist/error/ErrorCode.d.ts +0 -27
- package/dist/listener/attach.d.ts +0 -12
- package/dist/listener/declaration.d.ts +0 -3
- package/dist/listener/execution.d.ts +0 -19
- package/dist/listener/listener.d.ts +0 -42
- package/dist/listener/types.d.ts +0 -31
- package/dist/state/createState.d.ts +0 -5
- package/dist/state/types.d.ts +0 -4
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { MountAdapter } from '../../adapter/types';
|
|
2
|
+
import { ListenerDeclaration } from '../listen/declaration';
|
|
3
|
+
/** A component declaration created by `inject()`; it does nothing until applied to a Core Instance. */
|
|
4
|
+
export type ComponentDeclaration<TComponent = unknown, TProps = unknown> = Readonly<ComponentInput<TComponent, TProps> & {
|
|
5
|
+
kind: 'component';
|
|
6
|
+
}>;
|
|
7
|
+
/** Options for `inject()`: a framework component to mount into a host page element. */
|
|
8
|
+
export interface ComponentInput<TComponent = unknown, TProps = unknown> {
|
|
9
|
+
/** Injection name, unique among the top-level injections of a Core Instance. */
|
|
10
|
+
name: string;
|
|
11
|
+
/** CSS selector of the mount target; the first match receives the component container. */
|
|
12
|
+
injectAt: string;
|
|
13
|
+
/** The component adapter that mounts `component`; `component` and `props` are checked against it. */
|
|
14
|
+
adapter: MountAdapter<TComponent, TProps>;
|
|
15
|
+
component: NoInfer<TComponent>;
|
|
16
|
+
props?: NoInfer<TProps>;
|
|
17
|
+
/** DOM search budget in milliseconds. Defaults to 15000. */
|
|
18
|
+
timeout?: number;
|
|
19
|
+
/** Recover when the mount target or component container becomes invalid. */
|
|
20
|
+
reinject?: boolean;
|
|
21
|
+
/** Named host-page listeners owned by this component. */
|
|
22
|
+
listeners?: readonly Omit<ListenerDeclaration, 'reinject'>[];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Declares a component injection. Validation happens when the declaration is applied.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* makoo.apply([inject({ name: 'panel', injectAt: '#sidebar', adapter: vueAdapter, component: Panel })]);
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare function inject<TComponent = unknown, TProps = unknown>(input: ComponentInput<TComponent, TProps>): ComponentDeclaration<TComponent, TProps>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { MakooError } from '../../error/MakooError';
|
|
2
|
+
import { InjectionKind } from '../../runtime/injection';
|
|
3
|
+
import { ListenerStatus } from '../listen/listen';
|
|
4
|
+
import { ComponentDeclaration } from './declaration';
|
|
5
|
+
/**
|
|
6
|
+
* The run phase of a component.
|
|
7
|
+
*
|
|
8
|
+
* - `idle`: not started yet, or stopped.
|
|
9
|
+
* - `waiting`: looking for the mount target, or mounting into it.
|
|
10
|
+
* - `mounted`: mounted, with every attached listener started.
|
|
11
|
+
* - `failed`: the last run ended in an execution failure or a cleanup failure; see
|
|
12
|
+
* `lastError`.
|
|
13
|
+
*/
|
|
14
|
+
export type ComponentStatus = 'idle' | 'waiting' | 'mounted' | 'failed';
|
|
15
|
+
/**
|
|
16
|
+
* A top-level component injection: flattened control, status, subscription, and read-only
|
|
17
|
+
* views of its attached listeners. `start`, `stop` and `remove` return `void`; outcomes live
|
|
18
|
+
* on `status` / `lastError`.
|
|
19
|
+
*/
|
|
20
|
+
export type ComponentInjection = {
|
|
21
|
+
readonly kind: 'component';
|
|
22
|
+
readonly name: string;
|
|
23
|
+
readonly status: ComponentStatus;
|
|
24
|
+
readonly lastError: MakooError | undefined;
|
|
25
|
+
readonly attachedListeners: Readonly<Record<string, ListenerView | undefined>>;
|
|
26
|
+
start(): void;
|
|
27
|
+
stop(): void;
|
|
28
|
+
remove(): void;
|
|
29
|
+
subscribe(fn: () => void): () => void;
|
|
30
|
+
};
|
|
31
|
+
/** Read-only view of one attached listener owned by a component. */
|
|
32
|
+
export type ListenerView = {
|
|
33
|
+
readonly status: ListenerStatus;
|
|
34
|
+
readonly lastError: MakooError | undefined;
|
|
35
|
+
subscribe(fn: () => void): () => void;
|
|
36
|
+
};
|
|
37
|
+
/** The `inject` injection kind: `first`-mode match + shared `mount` + attached listeners. */
|
|
38
|
+
export declare const componentKind: InjectionKind<ComponentDeclaration, ComponentInjection>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { MountAdapter } from '../../adapter/types';
|
|
2
|
+
/** An Each Injection declaration created by `injectEach()`; inert until applied. */
|
|
3
|
+
export type ComponentEachDeclaration<TComponent = unknown, TProps = unknown> = Readonly<ComponentEachInput<TComponent, TProps> & {
|
|
4
|
+
kind: 'component-each';
|
|
5
|
+
}>;
|
|
6
|
+
/** Options for `injectEach()`: mount the same component on every matching host element. */
|
|
7
|
+
export interface ComponentEachInput<TComponent = unknown, TProps = unknown> {
|
|
8
|
+
/** Injection name, unique among the top-level injections of a Core Instance. */
|
|
9
|
+
name: string;
|
|
10
|
+
/** CSS selector; every current match receives a Child Injection (document order). */
|
|
11
|
+
injectAt: string;
|
|
12
|
+
/** The component adapter that mounts `component`; `component` and `props` are checked against it. */
|
|
13
|
+
adapter: MountAdapter<TComponent, TProps>;
|
|
14
|
+
component: NoInfer<TComponent>;
|
|
15
|
+
props?: NoInfer<TProps>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Declares an Each Injection: one Child per matching mount target. Validation happens on apply.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* makoo.apply([injectEach({ name: 'badges', injectAt: '.badge', adapter: vueAdapter, component: Badge })]);
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function injectEach<TComponent = unknown, TProps = unknown>(input: ComponentEachInput<TComponent, TProps>): ComponentEachDeclaration<TComponent, TProps>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { MakooError } from '../../error/MakooError';
|
|
2
|
+
import { InjectionKind } from '../../runtime/injection';
|
|
3
|
+
import { ComponentEachDeclaration } from './declaration';
|
|
4
|
+
/**
|
|
5
|
+
* The run phase of an Each Injection.
|
|
6
|
+
*
|
|
7
|
+
* - `idle`: not started yet, or stopped.
|
|
8
|
+
* - `collecting`: started; mounting current matches and waiting for later ones. Zero matches
|
|
9
|
+
* stay here without a target-wait timeout.
|
|
10
|
+
* - `failed`: a group-level orchestration or cleanup failure; see `lastError`.
|
|
11
|
+
*/
|
|
12
|
+
export type ComponentEachStatus = 'idle' | 'collecting' | 'failed';
|
|
13
|
+
/** The run phase of one Child Injection under an Each Injection. */
|
|
14
|
+
export type ChildStatus = 'idle' | 'mounted' | 'failed';
|
|
15
|
+
/**
|
|
16
|
+
* One Child Injection under an Each Injection: flattened control, status, and subscription.
|
|
17
|
+
* Not registered in the top-level name map, so there is no `start` or `remove`.
|
|
18
|
+
*/
|
|
19
|
+
export type ChildInjection = {
|
|
20
|
+
readonly kind: 'component-child';
|
|
21
|
+
readonly name: string;
|
|
22
|
+
readonly status: ChildStatus;
|
|
23
|
+
readonly lastError: MakooError | undefined;
|
|
24
|
+
stop(): void;
|
|
25
|
+
subscribe(fn: () => void): () => void;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* A top-level Each Injection: flattened group control, status, subscription, and a live
|
|
29
|
+
* document-order snapshot of currently mounted Children.
|
|
30
|
+
*/
|
|
31
|
+
export type ComponentEachInjection = {
|
|
32
|
+
readonly kind: 'component-each';
|
|
33
|
+
readonly name: string;
|
|
34
|
+
readonly status: ComponentEachStatus;
|
|
35
|
+
readonly lastError: MakooError | undefined;
|
|
36
|
+
readonly children: readonly ChildInjection[];
|
|
37
|
+
start(): void;
|
|
38
|
+
stop(): void;
|
|
39
|
+
remove(): void;
|
|
40
|
+
subscribe(fn: () => void): () => void;
|
|
41
|
+
};
|
|
42
|
+
/** The `injectEach` injection kind: `all`-mode match + one bound Child unit per element. */
|
|
43
|
+
export declare const componentEachKind: InjectionKind<ComponentEachDeclaration, ComponentEachInjection>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BaseInjection, InjectionKind } from '../runtime/injection';
|
|
2
|
+
import { ComponentDeclaration } from './inject/declaration';
|
|
3
|
+
import { ComponentInjection } from './inject/inject';
|
|
4
|
+
import { ComponentEachDeclaration } from './injectEach/declaration';
|
|
5
|
+
import { ComponentEachInjection } from './injectEach/injectEach';
|
|
6
|
+
import { ListenerDeclaration } from './listen/declaration';
|
|
7
|
+
import { ListenerInjection } from './listen/listen';
|
|
8
|
+
/** A declaration accepted by {@link MakooRuntime.apply}, created with `listen()`, `inject()`, or `injectEach()`. */
|
|
9
|
+
export type InjectionDeclaration = ListenerDeclaration | ComponentDeclaration | ComponentEachDeclaration;
|
|
10
|
+
export type Injection = ComponentInjection | ComponentEachInjection | ListenerInjection;
|
|
11
|
+
/** Declaration `kind` → its injection kind. */
|
|
12
|
+
export declare const kinds: Record<string, InjectionKind<unknown, BaseInjection>>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** Options for `listen()`: an event listener on a host page element. */
|
|
2
|
+
export interface ListenerInput {
|
|
3
|
+
/** Listener name, unique within its owning Core Instance or component. */
|
|
4
|
+
name: string;
|
|
5
|
+
/** CSS selector of the listener target; the first match is bound. */
|
|
6
|
+
listenAt: string;
|
|
7
|
+
/** DOM event type, such as `click`. */
|
|
8
|
+
type: string;
|
|
9
|
+
/**
|
|
10
|
+
* Event handler, called with the listener target as `this`. A throw or rejected promise is
|
|
11
|
+
* reported as a `MAKOO_LISTENER_CALLBACK_FAILED` diagnostic notice and leaves the listener bound.
|
|
12
|
+
*/
|
|
13
|
+
callback: (this: Element, event: Event) => unknown;
|
|
14
|
+
/** Passed to `addEventListener` as the `capture` option. */
|
|
15
|
+
capture?: boolean;
|
|
16
|
+
/** DOM search budget in milliseconds. Defaults to 15000. */
|
|
17
|
+
timeout?: number;
|
|
18
|
+
/** Recover after the bound host disconnects. Defaults to false. */
|
|
19
|
+
reinject?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** A listener declaration created by `listen()`; it does nothing until applied. */
|
|
22
|
+
export type ListenerDeclaration = Readonly<ListenerInput & {
|
|
23
|
+
kind: 'listener';
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* Declares a host listener. Apply it directly for a standalone listener, or put it in a
|
|
27
|
+
* component's `listeners` for an attached listener. Validation happens when it is applied.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* makoo.apply([listen({ name: 'play', listenAt: 'video', type: 'play', callback: onPlay })]);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function listen(input: ListenerInput): ListenerDeclaration;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { MakooError } from '../../error/MakooError';
|
|
2
|
+
import { InjectionKind, SetupContext } from '../../runtime/injection';
|
|
3
|
+
import { ListenerDeclaration } from './declaration';
|
|
4
|
+
/**
|
|
5
|
+
* The availability of a host listener.
|
|
6
|
+
*
|
|
7
|
+
* - `idle`: not started yet, or stopped.
|
|
8
|
+
* - `waiting`: looking for the listener target.
|
|
9
|
+
* - `bound`: the event listener is attached to the target.
|
|
10
|
+
* - `failed`: the last run ended in an execution failure or a cleanup failure; see
|
|
11
|
+
* `lastError`. A standalone listener may start again after a cleanup failure; the
|
|
12
|
+
* affected element stays residue-locked.
|
|
13
|
+
*/
|
|
14
|
+
export type ListenerStatus = 'idle' | 'waiting' | 'bound' | 'failed';
|
|
15
|
+
/**
|
|
16
|
+
* A standalone host listener: the flattened control, status and subscription of one
|
|
17
|
+
* registration. `start`, `stop` and `remove` return `void`; outcomes live on `status` /
|
|
18
|
+
* `lastError`.
|
|
19
|
+
*/
|
|
20
|
+
export type ListenerInjection = {
|
|
21
|
+
readonly kind: 'listener';
|
|
22
|
+
readonly name: string;
|
|
23
|
+
readonly status: ListenerStatus;
|
|
24
|
+
readonly lastError: MakooError | undefined;
|
|
25
|
+
start(): void;
|
|
26
|
+
stop(): void;
|
|
27
|
+
remove(): void;
|
|
28
|
+
subscribe(fn: () => void): () => void;
|
|
29
|
+
};
|
|
30
|
+
/** The `listen` injection kind: `first`-mode match + an event binding per appearance. */
|
|
31
|
+
export declare const listenerKind: InjectionKind<ListenerDeclaration, ListenerInjection>;
|
|
32
|
+
/**
|
|
33
|
+
* Seeks the first listener target and binds the event handler; also used by `inject`
|
|
34
|
+
* for attached listeners.
|
|
35
|
+
*/
|
|
36
|
+
export declare function setupListener(setupContext: SetupContext, declaration: ListenerDeclaration): void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { MountAdapter } from '../adapter/types';
|
|
2
|
+
import { MountTarget } from '../runtime/injection';
|
|
3
|
+
import { DOMObserver } from '../runtime/observer';
|
|
4
|
+
import { ListenerInjection } from './listen/listen';
|
|
5
|
+
export type MountOptions = {
|
|
6
|
+
readonly adapter: MountAdapter;
|
|
7
|
+
readonly component: unknown;
|
|
8
|
+
readonly listener: (name: string) => ListenerInjection;
|
|
9
|
+
readonly observer: DOMObserver;
|
|
10
|
+
readonly props: unknown;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Shared component mount setup for `inject` and `injectEach`: creates the exclusive
|
|
14
|
+
* component container, excludes it from self-matching, watches for host-side container
|
|
15
|
+
* removal, calls `adapter.mount`, and registers unmount before container removal.
|
|
16
|
+
*/
|
|
17
|
+
export declare function mount(target: MountTarget, options: MountOptions): void;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { LockView } from '../runtime/injection';
|
|
2
|
+
/**
|
|
3
|
+
* Residue locks of one Core Instance: elements whose cleanup left residue, grouped by lock
|
|
4
|
+
* key — a top-level injection name, or a nested group such as component name + attached
|
|
5
|
+
* listener name. Locks outlive their injection, so a same-named re-application inherits them.
|
|
6
|
+
*/
|
|
7
|
+
export declare function createResidueLocks(): (key: string | readonly string[]) => LockView;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { MakooError } from '../error/MakooError';
|
|
2
|
+
import { ComponentInjection } from '../injections/inject/inject';
|
|
3
|
+
import { ComponentEachInjection } from '../injections/injectEach/injectEach';
|
|
4
|
+
import { InjectionDeclaration } from '../injections/kinds';
|
|
5
|
+
import { ListenerInjection } from '../injections/listen/listen';
|
|
6
|
+
/**
|
|
7
|
+
* An instance-level notice, delivered after the fact it reports has been committed.
|
|
8
|
+
*
|
|
9
|
+
* - `injection-removed`: a top-level injection finished cleanup and left the instance;
|
|
10
|
+
* `injection` is the injection object that left.
|
|
11
|
+
* - `instance-disposed`: {@link MakooRuntime.dispose} finished tearing down every injection;
|
|
12
|
+
* `error` is the aggregate cleanup failure when one or more removals failed.
|
|
13
|
+
* - `diagnostic`: a failure reported outside the caller's control flow: a throwing status
|
|
14
|
+
* subscriber, event handler or observer handler, or a Child Injection that failed on its own
|
|
15
|
+
* (its status becomes `failed`); `name` is the owning injection (omitted when there is none),
|
|
16
|
+
* `child` names the Child Injection the failure belongs to.
|
|
17
|
+
*/
|
|
18
|
+
export type ObservationNotice = {
|
|
19
|
+
type: 'injection-removed';
|
|
20
|
+
name: string;
|
|
21
|
+
injection: ComponentInjection | ComponentEachInjection | ListenerInjection;
|
|
22
|
+
} | {
|
|
23
|
+
type: 'instance-disposed';
|
|
24
|
+
error?: MakooError;
|
|
25
|
+
} | {
|
|
26
|
+
type: 'diagnostic';
|
|
27
|
+
name?: string;
|
|
28
|
+
child?: string;
|
|
29
|
+
error: MakooError;
|
|
30
|
+
};
|
|
31
|
+
/** Receives instance notices; a throwing handler is reported and does not stop delivery. */
|
|
32
|
+
export type ObservationHandler = (notice: ObservationNotice) => void;
|
|
33
|
+
/** A Core Instance: owns a set of injections. */
|
|
34
|
+
export interface MakooRuntime {
|
|
35
|
+
/**
|
|
36
|
+
* Registers a batch of injections and starts each of them.
|
|
37
|
+
*
|
|
38
|
+
* The batch is checked as a whole first; if any declaration is rejected, nothing in the
|
|
39
|
+
* batch is registered. Rejected: an empty batch, an unknown `kind`, an invalid `injectAt` or
|
|
40
|
+
* `listenAt` selector, an attached listener that sets `reinject`, duplicate attached
|
|
41
|
+
* listener names, or a name already taken in the instance or the batch.
|
|
42
|
+
*
|
|
43
|
+
* @throws {@link MakooError} `MAKOO_DECLARATION_INVALID`, `MAKOO_INJECTION_NAME_CONFLICT`,
|
|
44
|
+
* or `MAKOO_INSTANCE_DISPOSED`.
|
|
45
|
+
*/
|
|
46
|
+
apply(declarations: readonly InjectionDeclaration[]): void;
|
|
47
|
+
/**
|
|
48
|
+
* Returns the component or Each Injection currently registered under `name`.
|
|
49
|
+
*
|
|
50
|
+
* Repeated calls for the same registration return the same object. After that injection is
|
|
51
|
+
* removed and a new one is applied under the same name, a later call returns a new object.
|
|
52
|
+
* Narrow with `kind` (`component` vs `component-each`) for `children`.
|
|
53
|
+
*
|
|
54
|
+
* @throws {@link MakooError} `MAKOO_INJECTION_NOT_FOUND` when no component or Each Injection
|
|
55
|
+
* has that name, or `MAKOO_INSTANCE_DISPOSED` after {@link MakooRuntime.dispose}.
|
|
56
|
+
*/
|
|
57
|
+
injection(name: string): ComponentInjection | ComponentEachInjection;
|
|
58
|
+
/**
|
|
59
|
+
* Returns the standalone listener currently registered under `name`.
|
|
60
|
+
*
|
|
61
|
+
* Repeated calls for the same registration return the same object. After that listener is
|
|
62
|
+
* removed and a new one is applied under the same name, a later call returns a new object.
|
|
63
|
+
*
|
|
64
|
+
* @throws {@link MakooError} `MAKOO_INJECTION_NOT_FOUND` when no standalone listener has
|
|
65
|
+
* that name, or `MAKOO_INSTANCE_DISPOSED` after {@link MakooRuntime.dispose}.
|
|
66
|
+
*/
|
|
67
|
+
listener(name: string): ListenerInjection;
|
|
68
|
+
/**
|
|
69
|
+
* Subscribes to instance notices.
|
|
70
|
+
*
|
|
71
|
+
* @returns A function that ends the subscription.
|
|
72
|
+
* @throws {@link MakooError} `MAKOO_INSTANCE_DISPOSED` after {@link MakooRuntime.dispose}.
|
|
73
|
+
*/
|
|
74
|
+
observe(handler: ObservationHandler): () => void;
|
|
75
|
+
/**
|
|
76
|
+
* Closes the instance and removes every injection in registration order in one outermost
|
|
77
|
+
* operation. Cleanup failures are reported via `console.error` and the `instance-disposed`
|
|
78
|
+
* notice's `error` (`MAKOO_INSTANCE_CLEANUP_FAILED`). Idempotent once disposed.
|
|
79
|
+
*/
|
|
80
|
+
dispose(): void;
|
|
81
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { MakooError } from '../error/MakooError';
|
|
2
|
+
import { Appearance } from './match';
|
|
3
|
+
import { DOMObserver } from './observer';
|
|
4
|
+
import { StateTree } from './stateNode';
|
|
5
|
+
/** Fields every injection object shares; a kind's `expose` adds the rest. */
|
|
6
|
+
export type BaseInjection = {
|
|
7
|
+
readonly kind: string;
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly status: InjectionStatus;
|
|
10
|
+
readonly lastError: MakooError | undefined;
|
|
11
|
+
start(): void;
|
|
12
|
+
stop(): void;
|
|
13
|
+
remove(): void;
|
|
14
|
+
subscribe(fn: () => void): () => void;
|
|
15
|
+
};
|
|
16
|
+
/** Injection-lifetime capabilities handed to `create`: valid from registration until `remove`. */
|
|
17
|
+
export type InjectionContext = {
|
|
18
|
+
stateNode: StateTree['stateNode'];
|
|
19
|
+
};
|
|
20
|
+
/** The instance-level machinery the injection delegates to (built per Core Instance). */
|
|
21
|
+
export type InjectionHost = {
|
|
22
|
+
/** Queues a function for flush time — it runs inside its own operation (deferred starts). */
|
|
23
|
+
defer(fn: () => void): void;
|
|
24
|
+
/** Marks an injection as possibly changed; recomputed once at flush via its registered delivery function. */
|
|
25
|
+
dirty(injection: BaseInjection): void;
|
|
26
|
+
operation<T>(run: () => T): T;
|
|
27
|
+
/** Queues a removed injection for registry removal at flush (`injection-removed`). */
|
|
28
|
+
removed(injection: BaseInjection): void;
|
|
29
|
+
/** Registers an injection and its deliver function; flush notifies in registration order. */
|
|
30
|
+
track(injection: BaseInjection, deliver: () => void): void;
|
|
31
|
+
/** Throws `MAKOO_INSTANCE_DISPOSED` when the instance is closed; guards every launch (start and deferred start). */
|
|
32
|
+
assertOpen(): void;
|
|
33
|
+
diagnostic(error: MakooError, name?: string, child?: string): void;
|
|
34
|
+
getLocks(key: string | readonly string[]): LockView;
|
|
35
|
+
/** Mounts a component into the target Appearance with the adapter from its declaration. */
|
|
36
|
+
mount(target: MountTarget, declaration: MountDeclaration): void;
|
|
37
|
+
readonly observer: DOMObserver;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* What a new injection type provides: its name, the active word used while running, a
|
|
41
|
+
* batch-time `resolve`, and a `create` that returns the kind's fields and per-round setup.
|
|
42
|
+
* The framework owns commands, status derivation, notifications, locks and scopes.
|
|
43
|
+
*/
|
|
44
|
+
export type InjectionKind<Declaration, Injection extends BaseInjection, Resolved = Declaration> = {
|
|
45
|
+
readonly active: 'mounted' | 'bound' | 'collecting';
|
|
46
|
+
readonly kind: string;
|
|
47
|
+
resolve(declaration: Declaration): Resolved;
|
|
48
|
+
create(resolved: Resolved, injectionContext: InjectionContext): KindContribution<Injection>;
|
|
49
|
+
};
|
|
50
|
+
export type InjectionStatus = 'bound' | 'collecting' | 'failed' | 'idle' | 'mounted' | 'waiting';
|
|
51
|
+
/** What a kind hands the framework for one injection: its public fields and its per-round setup. */
|
|
52
|
+
export type KindContribution<Injection extends BaseInjection> = {
|
|
53
|
+
expose: Omit<Injection, keyof BaseInjection>;
|
|
54
|
+
setup(setupContext: SetupContext): void;
|
|
55
|
+
};
|
|
56
|
+
export type LockView = {
|
|
57
|
+
isLocked(element: Element): boolean;
|
|
58
|
+
lock(element: Element): void;
|
|
59
|
+
};
|
|
60
|
+
export type MountDeclaration = {
|
|
61
|
+
readonly adapter: unknown;
|
|
62
|
+
readonly component: unknown;
|
|
63
|
+
readonly props?: unknown;
|
|
64
|
+
};
|
|
65
|
+
export type MountTarget = {
|
|
66
|
+
readonly appearance: Appearance;
|
|
67
|
+
/**
|
|
68
|
+
* The public object of the injection or Child Injection this mount belongs to, handed through
|
|
69
|
+
* to the adapter unread.
|
|
70
|
+
*/
|
|
71
|
+
readonly injection: object;
|
|
72
|
+
readonly name: string;
|
|
73
|
+
};
|
|
74
|
+
export type OperationContext = Pick<InjectionHost, 'defer' | 'dirty' | 'operation' | 'removed' | 'track'> & {
|
|
75
|
+
isRemoved(injection: BaseInjection): boolean;
|
|
76
|
+
};
|
|
77
|
+
/** One setup round's capabilities; `match`, `mount` and `onCleanup` act on the current scope. */
|
|
78
|
+
export type SetupContext = {
|
|
79
|
+
diagnostic(error: MakooError): void;
|
|
80
|
+
match(mode: 'first', selector: string, declaration: {
|
|
81
|
+
readonly reinject?: boolean;
|
|
82
|
+
readonly timeout?: number;
|
|
83
|
+
}, appearanceSetup: (element: Element) => void): void;
|
|
84
|
+
match(mode: 'all', selector: string, appearanceSetup: (element: Element) => void): void;
|
|
85
|
+
mount(declaration: MountDeclaration): void;
|
|
86
|
+
onCleanup(fn: () => void): void;
|
|
87
|
+
};
|
|
88
|
+
export declare function createInjection<Injection extends BaseInjection, Resolved>(resolved: Resolved, injectionkind: InjectionKind<unknown, Injection, Resolved>, name: string, host: InjectionHost): Injection;
|
|
89
|
+
/**
|
|
90
|
+
* The instance operation machinery: every command/mutation/timer enters through
|
|
91
|
+
* `operation`, and only the outermost one flushes — deferred starts first, then
|
|
92
|
+
* removals, then merged notifications in registration order.
|
|
93
|
+
*/
|
|
94
|
+
export declare function createOperationContext(onRemoved: (injection: BaseInjection) => void, onFlushed: () => void): OperationContext;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { DOMObserver } from './observer';
|
|
2
|
+
import { Scope, ScopeOutcome } from './scope';
|
|
3
|
+
export type AllMatchOptions = {
|
|
4
|
+
mode: 'all';
|
|
5
|
+
};
|
|
6
|
+
export type FirstMatchOptions = {
|
|
7
|
+
mode: 'first';
|
|
8
|
+
reinject?: boolean;
|
|
9
|
+
timeout?: number;
|
|
10
|
+
};
|
|
11
|
+
export type MatchOptions = AllMatchOptions | FirstMatchOptions;
|
|
12
|
+
export declare const DEFAULT_MATCH_TIMEOUT = 15000;
|
|
13
|
+
/** Framework-side wiring of one `match` call: reporting, locks and instance machinery. */
|
|
14
|
+
export type MatchContext = {
|
|
15
|
+
changed(): void;
|
|
16
|
+
/** Called once per appearance with its outcome, before `changed`. */
|
|
17
|
+
ended(element: Element, outcome: AppearanceOutcome): void;
|
|
18
|
+
/** Owner name used in wait-timeout and detached errors. */
|
|
19
|
+
name: string;
|
|
20
|
+
isLocked(element: Element): boolean;
|
|
21
|
+
lock(element: Element): void;
|
|
22
|
+
observer: DOMObserver;
|
|
23
|
+
operation<T>(run: () => T): T;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* One element's matched span: the element, the child scope match spawned for it, and the
|
|
27
|
+
* DOM-only verbs.
|
|
28
|
+
*/
|
|
29
|
+
export type Appearance = {
|
|
30
|
+
readonly element: Element;
|
|
31
|
+
readonly scope: Scope;
|
|
32
|
+
/** Excludes a container element (e.g. the component's own root) from future matching. */
|
|
33
|
+
exclude(container: Element): void;
|
|
34
|
+
/** Ends this appearance with reason 'left' (e.g. its container was removed by the host). No-op once ended. */
|
|
35
|
+
leave(): void;
|
|
36
|
+
};
|
|
37
|
+
export type AppearanceOutcome = Omit<ScopeOutcome, 'reason'> & {
|
|
38
|
+
readonly reason: ScopeOutcome['reason'] | 'left';
|
|
39
|
+
};
|
|
40
|
+
export type MatchHandle = {
|
|
41
|
+
/** Live appearances (setup completed, scope still alive) in document order. */
|
|
42
|
+
appearances(): readonly Element[];
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* The only DOM lifecycle primitive. `match` keeps appearances alive while their element
|
|
46
|
+
* stays connected and still matches: `first` keeps at most one (with wait timeout,
|
|
47
|
+
* optional reinject after leave, escalate supervision); `all` collects every match
|
|
48
|
+
* (isolate supervision). Cleanup failures always residue-lock the element.
|
|
49
|
+
*/
|
|
50
|
+
export declare function match(scope: Scope, selector: string, options: MatchOptions, context: MatchContext, appearanceSetup: (appearance: Appearance) => void): MatchHandle;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type DOMObserver = {
|
|
2
|
+
subscribe(subscriber: () => void, signal: AbortSignal): () => void;
|
|
3
|
+
};
|
|
4
|
+
/**
|
|
5
|
+
* The shared MutationObserver of a Core Instance: one observer for the whole document,
|
|
6
|
+
* one delivery wraps all subscribed checks in a single outermost operation.
|
|
7
|
+
* Watches childList and attributes only — text changes never reach a selector match.
|
|
8
|
+
*/
|
|
9
|
+
export declare function createDomObserver(operation: (run: () => void) => void): DOMObserver;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export type Scope = {
|
|
2
|
+
fail(error: unknown): void;
|
|
3
|
+
stop(): void;
|
|
4
|
+
/** Merges cleanup errors collected elsewhere into this scope's outcome (used by `mount` for adapter-reported errors). */
|
|
5
|
+
addCleanupErrors(errors: readonly unknown[]): void;
|
|
6
|
+
onCleanup(fn: () => void): void;
|
|
7
|
+
spawn(options: ScopeOptions): Scope;
|
|
8
|
+
readonly isEnded: boolean;
|
|
9
|
+
/** Whether any scope in this tree is running setup or teardown right now. */
|
|
10
|
+
readonly isTransitioning: boolean;
|
|
11
|
+
readonly outcome: ScopeOutcome | undefined;
|
|
12
|
+
};
|
|
13
|
+
export type ScopeErrorHandles = {
|
|
14
|
+
/** Maps this scope's error when it escalates to the parent; the parent ends with the mapped error. Not called for cleanup-only escalation or under `isolate`. This scope's own outcome keeps the original error. */
|
|
15
|
+
readonly escalateAs?: (error: unknown) => unknown;
|
|
16
|
+
};
|
|
17
|
+
export type ScopeOptions = {
|
|
18
|
+
readonly setup: (scope: Scope) => void;
|
|
19
|
+
readonly supervision?: 'escalate' | 'isolate';
|
|
20
|
+
readonly ended?: (outcome: ScopeOutcome) => void;
|
|
21
|
+
readonly errorHandles?: ScopeErrorHandles;
|
|
22
|
+
};
|
|
23
|
+
export type ScopeOutcome = {
|
|
24
|
+
readonly error?: unknown;
|
|
25
|
+
readonly reason: 'stopped' | 'failed';
|
|
26
|
+
readonly cleanupErrors: readonly unknown[];
|
|
27
|
+
};
|
|
28
|
+
export declare function createScope(options: ScopeOptions): Scope;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function checkSelector(field: string, selector: string): void;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { MakooError } from '../error/MakooError';
|
|
2
|
+
import { InjectionHost, InjectionStatus, SetupContext } from './injection';
|
|
3
|
+
import { Scope } from './scope';
|
|
4
|
+
/** The top-level injection's unit: it owns the root scope that `start` and `stop` drive. */
|
|
5
|
+
export type RootStateNode = {
|
|
6
|
+
readonly hasErrors: boolean;
|
|
7
|
+
readonly isRunning: boolean;
|
|
8
|
+
readonly isTransitioning: boolean;
|
|
9
|
+
readonly scope: Scope | undefined;
|
|
10
|
+
/** Drops a settled root scope: status becomes idle, lastError is kept. */
|
|
11
|
+
consume(): void;
|
|
12
|
+
flatten(...sources: object[]): object;
|
|
13
|
+
run(roundSetup: (setupContext: SetupContext) => void, injection: object): void;
|
|
14
|
+
};
|
|
15
|
+
/** A state unit: one status / lastError / subscribe triple, derived from the scope it is bound to. */
|
|
16
|
+
export type StateNode<Status extends InjectionStatus = InjectionStatus> = {
|
|
17
|
+
readonly name: string;
|
|
18
|
+
readonly view: StateView<Status>;
|
|
19
|
+
/** Binds this unit to the current Appearance and registers `injection` as its public object. */
|
|
20
|
+
bind(injection: object): void;
|
|
21
|
+
/** Copies `fields` by property descriptor, adds this unit's state, and freezes the result. */
|
|
22
|
+
flatten<Fields extends object>(fields: Fields & {
|
|
23
|
+
status?: never;
|
|
24
|
+
lastError?: never;
|
|
25
|
+
subscribe?: never;
|
|
26
|
+
}): Readonly<Fields> & StateView<Status>;
|
|
27
|
+
/** Runs one round of `roundSetup` in a child scope of the current scope (escalate supervision). */
|
|
28
|
+
launch(roundSetup: (setupContext: SetupContext) => void, escalateAs: (cause: unknown) => MakooError): void;
|
|
29
|
+
stop(): void;
|
|
30
|
+
};
|
|
31
|
+
/** The state units of one top-level injection: a root unit plus nested units notified after it. */
|
|
32
|
+
export type StateTree = {
|
|
33
|
+
readonly root: RootStateNode;
|
|
34
|
+
deliver(): void;
|
|
35
|
+
stateNode<Active extends 'mounted' | 'bound', Waiting extends 'idle' | 'waiting' = 'waiting'>(options: {
|
|
36
|
+
name: string;
|
|
37
|
+
active: Active;
|
|
38
|
+
waiting?: Waiting;
|
|
39
|
+
}): StateNode<Active | Waiting | 'idle' | 'failed'>;
|
|
40
|
+
};
|
|
41
|
+
export type StateView<Status> = {
|
|
42
|
+
readonly status: Status;
|
|
43
|
+
readonly lastError: MakooError | undefined;
|
|
44
|
+
subscribe(fn: () => void): () => void;
|
|
45
|
+
};
|
|
46
|
+
export declare function createStateTree(options: {
|
|
47
|
+
readonly active: 'mounted' | 'bound' | 'collecting';
|
|
48
|
+
readonly changed: () => void;
|
|
49
|
+
readonly host: InjectionHost;
|
|
50
|
+
readonly name: string;
|
|
51
|
+
}): StateTree;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@makoojs/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Framework-agnostic runtime core for Makoo userscript applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -29,8 +29,5 @@
|
|
|
29
29
|
"import": "./dist/index.js",
|
|
30
30
|
"require": "./dist/index.cjs"
|
|
31
31
|
}
|
|
32
|
-
},
|
|
33
|
-
"dependencies": {
|
|
34
|
-
"zod": "^4.4.3"
|
|
35
32
|
}
|
|
36
33
|
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { AdapterMountParams, MountAdapter } from '../adapter/types';
|
|
2
|
-
import { DOMObserver } from '../dom/observer';
|
|
3
|
-
import { MakooError } from '../error/MakooError';
|
|
4
|
-
import { ComponentInjection, MakooComponentDeclaration } from './types';
|
|
5
|
-
export declare function createComponent(config: MakooComponentDeclaration, adapter: MountAdapter, dom: DOMObserver, onRemoved: () => void, isDisposed: () => boolean, globalListener: AdapterMountParams['globalListener'], onDiagnostic: (name: string, error: MakooError) => void): ComponentInjection;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { MakooComponentDeclaration, MakooComponentInput } from './types';
|
|
2
|
-
export declare function inject<TComponent = unknown, TProps = unknown>(input: MakooComponentInput<TComponent, TProps>): MakooComponentDeclaration<TComponent, TProps>;
|
|
3
|
-
export declare function validateComponent(input: unknown): MakooComponentDeclaration;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { DOMObserver } from '../dom/observer';
|
|
2
|
-
import { MakooError } from '../error/MakooError';
|
|
3
|
-
import { AttachListener } from '../listener/attach';
|
|
4
|
-
export type ComponentMounter = {
|
|
5
|
-
mount(container: HTMLElement): unknown;
|
|
6
|
-
unmount(mountHandle: unknown): void;
|
|
7
|
-
};
|
|
8
|
-
type ComponentExecutionConfig = {
|
|
9
|
-
readonly name: string;
|
|
10
|
-
readonly injectAt: string;
|
|
11
|
-
readonly timeout?: number;
|
|
12
|
-
};
|
|
13
|
-
export type ComponentExecutionControl = {
|
|
14
|
-
readonly phase: 'pending' | 'mounting' | 'mounted' | 'ended';
|
|
15
|
-
start(): void;
|
|
16
|
-
cancel(): void;
|
|
17
|
-
finish(): Promise<unknown[]>;
|
|
18
|
-
};
|
|
19
|
-
type ExecutionCallbacks = {
|
|
20
|
-
setStatus(nextStatus: 'waiting' | 'mounted'): void;
|
|
21
|
-
ended(reason: 'stopped' | 'detached' | 'failed', error?: MakooError): void;
|
|
22
|
-
};
|
|
23
|
-
export declare function createComponentExecution(config: ComponentExecutionConfig, dom: DOMObserver, mounter: ComponentMounter, attachListeners: ReadonlyMap<string, AttachListener>, callbacks: ExecutionCallbacks): ComponentExecutionControl;
|
|
24
|
-
export {};
|