@bitakit/core 0.2.6 → 0.2.7
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/AGENTS.md +1 -1
- package/dist/interceptors/define.d.ts +13 -4
- package/dist/interceptors/service-observers.d.ts +3 -2
- package/dist/interceptors/service-observers.js +10 -3
- package/dist/runtime.js +1 -1
- package/dist/settings/define.d.ts +18 -3
- package/dist/settings/define.js +9 -2
- package/package.json +1 -1
package/AGENTS.md
CHANGED
|
@@ -17,7 +17,7 @@ rarely call the runtime directly: files in `src/plugins/<id>/` are discovered, a
|
|
|
17
17
|
| `events.ts` | `defineEvents({ changed: event<T>() })` | `<plugin>.changed` |
|
|
18
18
|
| `settings.ts` | `defineSettings({ name: setting({ default }) }, page)` | `<plugin>.<name>` |
|
|
19
19
|
| `slots/<name>.ts` | `defineSlot({ accepts, policy?, payload? })` | `<plugin>.<name>` |
|
|
20
|
-
| `interceptors/<name>.ts` | `defineInterceptor({ action, before, after })`
|
|
20
|
+
| `interceptors/<name>.ts` | `defineInterceptor({ action or service, before, after })` | — |
|
|
21
21
|
| `configs/<name>.ts` | `defineConfig({ defaults, schema? })` | `<plugin>.<name>` |
|
|
22
22
|
| `setup.ts` | `Setup` function (`scope.slots.attach`, cleanup) | — |
|
|
23
23
|
|
|
@@ -35,10 +35,19 @@ type Methods<S> = {
|
|
|
35
35
|
type ServiceOf<Name extends string> = Name extends keyof ServiceMap ? NonNullable<ServiceMap[Name]> : Record<string, (...args: any[]) => unknown>;
|
|
36
36
|
type MethodOf<Name extends string, Key extends string> = Key extends keyof ServiceOf<Name> ? Extract<ServiceOf<Name>[Key], (...args: any[]) => any> : (...args: any[]) => unknown;
|
|
37
37
|
export type ServiceCall<Name extends string = string, Key extends string = string> = {
|
|
38
|
-
|
|
38
|
+
/** The called service's ID; a glob or alias target still reports the actual ID. */
|
|
39
|
+
readonly service: string;
|
|
39
40
|
readonly method: Key;
|
|
40
41
|
readonly args: Readonly<Parameters<MethodOf<Name, Key>>>;
|
|
41
42
|
};
|
|
43
|
+
/**
|
|
44
|
+
* What a service hook receives besides the call: the services, as Action interceptors get them,
|
|
45
|
+
* so an observer can record into its own service. Calls to the observer's own service are
|
|
46
|
+
* observed too; skip them to avoid recursion.
|
|
47
|
+
*/
|
|
48
|
+
export type ServiceHookContext = {
|
|
49
|
+
readonly services: Services;
|
|
50
|
+
};
|
|
42
51
|
/** Observes one method or every public method of a service. Hooks never change the outcome. */
|
|
43
52
|
export type ServiceInterceptorSpec<Name extends string = string, Key extends string = string> = {
|
|
44
53
|
readonly kind?: 'interceptor';
|
|
@@ -48,11 +57,11 @@ export type ServiceInterceptorSpec<Name extends string = string, Key extends str
|
|
|
48
57
|
scope?: never;
|
|
49
58
|
requires?: never;
|
|
50
59
|
order?: number;
|
|
51
|
-
before?(call: ServiceCall<Name, Key>): void;
|
|
52
|
-
after?(call: ServiceCall<Name, Key> & {
|
|
60
|
+
before?(call: ServiceCall<Name, Key> & ServiceHookContext): void;
|
|
61
|
+
after?(call: ServiceCall<Name, Key> & ServiceHookContext & {
|
|
53
62
|
readonly result: Awaited<ReturnType<MethodOf<Name, Key>>>;
|
|
54
63
|
}): void;
|
|
55
|
-
onError?(call: ServiceCall<Name, Key> & {
|
|
64
|
+
onError?(call: ServiceCall<Name, Key> & ServiceHookContext & {
|
|
56
65
|
readonly error: unknown;
|
|
57
66
|
}): void;
|
|
58
67
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type InterceptorSpec } from './define.js';
|
|
2
2
|
/**
|
|
3
3
|
* Wraps service instances whose ID or alias matches an interceptor's `service` target. All
|
|
4
|
-
* container consumers receive the same wrapper; frozen instances are supported.
|
|
4
|
+
* container consumers receive the same wrapper; frozen instances are supported. `services`
|
|
5
|
+
* is read when a call happens, because the service tree exists only after the container does.
|
|
5
6
|
*/
|
|
6
|
-
export declare function serviceObserver(interceptors: readonly InterceptorSpec[], namesOf: (id: string) => readonly string[]): (id: string, instance: any, active: () => boolean) => any;
|
|
7
|
+
export declare function serviceObserver(interceptors: readonly InterceptorSpec[], namesOf: (id: string) => readonly string[], services: () => unknown): (id: string, instance: any, active: () => boolean) => any;
|
|
@@ -12,9 +12,10 @@ function notify(callback, event) {
|
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Wraps service instances whose ID or alias matches an interceptor's `service` target. All
|
|
15
|
-
* container consumers receive the same wrapper; frozen instances are supported.
|
|
15
|
+
* container consumers receive the same wrapper; frozen instances are supported. `services`
|
|
16
|
+
* is read when a call happens, because the service tree exists only after the container does.
|
|
16
17
|
*/
|
|
17
|
-
export function serviceObserver(interceptors, namesOf) {
|
|
18
|
+
export function serviceObserver(interceptors, namesOf, services) {
|
|
18
19
|
const entries = interceptors
|
|
19
20
|
.filter(isServiceInterceptor)
|
|
20
21
|
.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
@@ -47,7 +48,9 @@ export function serviceObserver(interceptors, namesOf) {
|
|
|
47
48
|
const observers = typeof key === 'string'
|
|
48
49
|
? selected.filter((entry) => entry.method === undefined || entry.method === key)
|
|
49
50
|
: [];
|
|
50
|
-
|
|
51
|
+
// A function Proxy keeps the method's own properties (for example `translate.has`);
|
|
52
|
+
// only the call itself is observed.
|
|
53
|
+
const call = (...args) => {
|
|
51
54
|
const emit = (phase, extra = {}) => {
|
|
52
55
|
if (!active())
|
|
53
56
|
return;
|
|
@@ -56,6 +59,7 @@ export function serviceObserver(interceptors, namesOf) {
|
|
|
56
59
|
service: id,
|
|
57
60
|
method: key,
|
|
58
61
|
args: Object.freeze([...args]),
|
|
62
|
+
services: services(),
|
|
59
63
|
...extra,
|
|
60
64
|
});
|
|
61
65
|
};
|
|
@@ -75,6 +79,9 @@ export function serviceObserver(interceptors, namesOf) {
|
|
|
75
79
|
emit('after', { result });
|
|
76
80
|
return result;
|
|
77
81
|
};
|
|
82
|
+
const wrapped = new Proxy(value, {
|
|
83
|
+
apply: (_method, _this, args) => call(...args),
|
|
84
|
+
});
|
|
78
85
|
cache.set(key, { original: value, wrapped });
|
|
79
86
|
return wrapped;
|
|
80
87
|
},
|
package/dist/runtime.js
CHANGED
|
@@ -686,7 +686,7 @@ export class BitaKitRuntime {
|
|
|
686
686
|
const interceptors = plugins.flatMap((plugin) => plugin.interceptors ?? []);
|
|
687
687
|
this.container = createServices(plugins.flatMap((plugin) => plugin.services), this.providedServices, (definition) => this.configuration.context(owners.get(definition) ??
|
|
688
688
|
this.modules.find((module) => module.services.includes(definition))?.id ??
|
|
689
|
-
this.appId), serviceObserver(interceptors, (id) => this.aliasesOf(id)), this.events, {
|
|
689
|
+
this.appId), serviceObserver(interceptors, (id) => this.aliasesOf(id), () => this.view), this.events, {
|
|
690
690
|
names: this.names,
|
|
691
691
|
appId: this.appId,
|
|
692
692
|
moduleOf: (definition) => owners.get(definition) ??
|
|
@@ -3,13 +3,19 @@ import type { PresentationType } from '../presentation.js';
|
|
|
3
3
|
import type { GeneratedName } from '../names.js';
|
|
4
4
|
/** Who a value belongs to: the signed-in user, the active organization, or this browser. */
|
|
5
5
|
export type SettingScope = 'user' | 'organization' | 'device';
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* How a generated settings page edits a value. `list` edits a list of short texts (add and
|
|
8
|
+
* remove); `checkboxes` chooses several of the options (a list default).
|
|
9
|
+
*/
|
|
10
|
+
export type SettingControl = 'switch' | 'select' | 'radio' | 'cards' | 'text' | 'textarea' | 'number' | 'list' | 'checkboxes';
|
|
7
11
|
export type SettingOption<T = unknown> = {
|
|
8
12
|
readonly value: T;
|
|
9
13
|
readonly label?: string;
|
|
10
14
|
readonly description?: string;
|
|
11
15
|
readonly icon?: PresentationType<'icon'>;
|
|
12
16
|
};
|
|
17
|
+
/** The value of one option: the setting's value, or one item of a list setting. */
|
|
18
|
+
type OptionValue<T> = T extends readonly (infer Item)[] ? Item : T;
|
|
13
19
|
declare const settingType: unique symbol;
|
|
14
20
|
/** One declared setting. Values are read and written through `settings.<plugin>.<name>`. */
|
|
15
21
|
export type SettingSpec<T = unknown> = {
|
|
@@ -22,7 +28,7 @@ export type SettingSpec<T = unknown> = {
|
|
|
22
28
|
readonly description?: string;
|
|
23
29
|
/** Presentation hint for generated settings pages; inferred from the value when omitted. */
|
|
24
30
|
readonly control?: SettingControl;
|
|
25
|
-
readonly options?: readonly SettingOption<T
|
|
31
|
+
readonly options?: readonly SettingOption<OptionValue<T>>[];
|
|
26
32
|
readonly order?: number;
|
|
27
33
|
readonly group?: string;
|
|
28
34
|
/** Readable and writable, but left out of generated settings pages. */
|
|
@@ -36,7 +42,7 @@ type SettingInput<T> = {
|
|
|
36
42
|
label?: string;
|
|
37
43
|
description?: string;
|
|
38
44
|
control?: SettingControl;
|
|
39
|
-
options?: readonly SettingOption<T
|
|
45
|
+
options?: readonly SettingOption<OptionValue<T>>[];
|
|
40
46
|
order?: number;
|
|
41
47
|
group?: string;
|
|
42
48
|
hidden?: boolean;
|
|
@@ -45,6 +51,15 @@ type SettingInput<T> = {
|
|
|
45
51
|
export declare function setting<S extends InputSchema<any>>(spec: Omit<SettingInput<SchemaOutput<S>>, 'schema'> & {
|
|
46
52
|
schema: S;
|
|
47
53
|
}): SettingSpec<SchemaOutput<S>>;
|
|
54
|
+
/**
|
|
55
|
+
* `setting({ default: ['email'], options: [{ value: 'email' }, { value: 'sms' }] })`: several of
|
|
56
|
+
* the options, a list of the option union (shown as checkboxes).
|
|
57
|
+
*/
|
|
58
|
+
export declare function setting<const O extends readonly SettingOption<any>[]>(spec: Omit<SettingInput<O[number]['value'][]>, 'schema' | 'options' | 'default'> & {
|
|
59
|
+
schema?: undefined;
|
|
60
|
+
default: readonly O[number]['value'][];
|
|
61
|
+
options: O;
|
|
62
|
+
}): SettingSpec<O[number]['value'][]>;
|
|
48
63
|
/** `setting({ default: 'system', options: [{ value: 'light' }, ...] })`: the type is the option union. */
|
|
49
64
|
export declare function setting<const O extends readonly SettingOption<any>[]>(spec: Omit<SettingInput<O[number]['value']>, 'schema' | 'options'> & {
|
|
50
65
|
schema?: undefined;
|
package/dist/settings/define.js
CHANGED
|
@@ -8,6 +8,8 @@ const controls = [
|
|
|
8
8
|
'text',
|
|
9
9
|
'textarea',
|
|
10
10
|
'number',
|
|
11
|
+
'list',
|
|
12
|
+
'checkboxes',
|
|
11
13
|
];
|
|
12
14
|
export function setting(spec) {
|
|
13
15
|
if (!spec || typeof spec !== 'object' || !('default' in spec))
|
|
@@ -70,10 +72,15 @@ export function parseSetting(spec, value, key = 'setting') {
|
|
|
70
72
|
return checked.value;
|
|
71
73
|
}
|
|
72
74
|
if (spec.options) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
+
const accepts = (item) => spec.options.some((option) => Object.is(option.value, item));
|
|
76
|
+
// A list default chooses several of the options.
|
|
77
|
+
const several = Array.isArray(spec.default);
|
|
78
|
+
if (several ? !Array.isArray(value) || !value.every(accepts) : !accepts(value))
|
|
79
|
+
throw new Error(`Setting "${key}" accepts ${several ? 'a list of ' : ''}${spec.options.map((option) => show(option.value)).join(', ')}; got ${show(value)}.`);
|
|
75
80
|
return value;
|
|
76
81
|
}
|
|
82
|
+
if (Array.isArray(spec.default) && !Array.isArray(value))
|
|
83
|
+
throw new Error(`Setting "${key}" expects a list; got ${show(value)}.`);
|
|
77
84
|
const expected = typeof spec.default;
|
|
78
85
|
if (spec.default !== null && spec.default !== undefined && typeof value !== expected)
|
|
79
86
|
throw new Error(`Setting "${key}" expects a ${expected}; got ${value === null ? 'null' : typeof value}.`);
|