@bitakit/core 0.1.1 → 0.2.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.md +47 -161
- package/assets/bitakit-logo-dark.svg +11 -0
- package/assets/bitakit-logo.svg +11 -0
- package/discovery/index.d.ts +1 -41
- package/discovery/index.mjs +1 -4
- package/dist/actions/action-list.d.ts +4 -0
- package/dist/actions/action-list.js +58 -16
- package/dist/actions/action.d.ts +2 -0
- package/dist/actions/action.js +4 -0
- package/dist/actions/brand.d.ts +21 -0
- package/dist/actions/brand.js +36 -0
- package/dist/actions/core.d.ts +4 -3
- package/dist/actions/core.js +4 -2
- package/dist/actions/execution.d.ts +2 -2
- package/dist/actions/execution.js +61 -9
- package/dist/actions/feedback.d.ts +2 -0
- package/dist/actions/feedback.js +8 -0
- package/dist/actions/groups.js +5 -1
- package/dist/actions/instance-bindings.js +3 -2
- package/dist/actions/interceptors.d.ts +9 -3
- package/dist/actions/interceptors.js +31 -3
- package/dist/actions/navigation.d.ts +2 -1
- package/dist/actions/navigation.js +10 -5
- package/dist/actions/reference.js +10 -5
- package/dist/actions/types.d.ts +9 -1
- package/dist/app/access.d.ts +4 -4
- package/dist/app/access.js +33 -71
- package/dist/app/action-access.d.ts +2 -2
- package/dist/app/action-access.js +6 -4
- package/dist/app/service-error.d.ts +2 -2
- package/dist/app/service-error.js +10 -6
- package/dist/app/types.d.ts +76 -33
- package/dist/config-service.d.ts +30 -6
- package/dist/config-service.js +25 -9
- package/dist/configuration.d.ts +6 -3
- package/dist/configuration.js +8 -6
- package/dist/contracts.d.ts +4 -2
- package/dist/contracts.js +3 -3
- package/dist/contribution-origin.d.ts +4 -1
- package/dist/contribution-origin.js +14 -1
- package/dist/contributions/action.d.ts +26 -0
- package/dist/contributions/action.js +171 -0
- package/dist/contributions/host.d.ts +14 -11
- package/dist/contributions/index.d.ts +1 -26
- package/dist/contributions/index.js +1 -18
- package/dist/conventions.d.ts +8 -4
- package/dist/conventions.js +12 -31
- package/dist/core.d.ts +41 -18
- package/dist/core.js +25 -13
- package/dist/definitions.d.ts +272 -306
- package/dist/definitions.js +28 -56
- package/dist/destinations.d.ts +25 -0
- package/dist/destinations.js +21 -0
- package/dist/discovered.d.ts +52 -21
- package/dist/discovered.js +89 -35
- package/dist/events/define.d.ts +43 -0
- package/dist/events/define.js +17 -0
- package/dist/events/service.d.ts +35 -8
- package/dist/events/service.js +184 -42
- package/dist/events/types.d.ts +29 -9
- package/dist/inspection/inventory.d.ts +86 -0
- package/dist/inspection/inventory.js +132 -0
- package/dist/interceptors/define.d.ts +76 -0
- package/dist/interceptors/define.js +23 -0
- package/dist/interceptors/service-observers.d.ts +6 -3
- package/dist/interceptors/service-observers.js +13 -10
- package/dist/lifecycle/cleanup.d.ts +5 -0
- package/dist/lifecycle/cleanup.js +41 -0
- package/dist/lifecycle/controller.d.ts +82 -0
- package/dist/lifecycle/controller.js +276 -0
- package/dist/lifecycle/reconcile.d.ts +19 -0
- package/dist/lifecycle/reconcile.js +68 -0
- package/dist/lifecycle/state.d.ts +53 -0
- package/dist/lifecycle/state.js +86 -0
- package/dist/names.d.ts +10 -0
- package/dist/names.js +1 -0
- package/dist/pages.d.ts +39 -6
- package/dist/pages.js +93 -11
- package/dist/presentation.d.ts +9 -0
- package/dist/runtime.d.ts +166 -28
- package/dist/runtime.js +1485 -248
- package/dist/schema.d.ts +13 -0
- package/dist/schema.js +38 -0
- package/dist/scope.d.ts +59 -4
- package/dist/scope.js +133 -11
- package/dist/service-key.d.ts +33 -14
- package/dist/service-key.js +21 -5
- package/dist/service-names.d.ts +39 -0
- package/dist/service-names.js +175 -0
- package/dist/services.d.ts +24 -5
- package/dist/services.js +230 -37
- package/dist/settings/access.d.ts +19 -0
- package/dist/settings/access.js +78 -0
- package/dist/settings/define.d.ts +97 -0
- package/dist/settings/define.js +83 -0
- package/dist/settings/storage.d.ts +45 -0
- package/dist/settings/storage.js +102 -0
- package/dist/settings/store.d.ts +74 -0
- package/dist/settings/store.js +345 -0
- package/dist/slots/action-placement.d.ts +47 -0
- package/dist/slots/action-placement.js +1 -0
- package/dist/slots/define.d.ts +52 -0
- package/dist/slots/define.js +12 -0
- package/dist/slots/normalize.d.ts +7 -0
- package/dist/slots/normalize.js +36 -0
- package/dist/slots/runtime.d.ts +43 -0
- package/dist/slots/runtime.js +348 -0
- package/dist/slots/types.d.ts +113 -0
- package/dist/slots/types.js +1 -0
- package/dist/startup-error.d.ts +21 -0
- package/dist/startup-error.js +29 -0
- package/dist/store.d.ts +24 -0
- package/dist/store.js +40 -0
- package/dist/translation.d.ts +9 -4
- package/dist/translation.js +2 -3
- package/examples/todos/actions/create.mjs +1 -3
- package/examples/todos/actions/delete.mjs +3 -10
- package/examples/todos/actions/list.mjs +1 -3
- package/examples/todos/events.mjs +5 -0
- package/examples/todos/plugin.mjs +4 -2
- package/examples/todos/services/tasks.mjs +14 -20
- package/examples/todos.mjs +8 -7
- package/package.json +37 -9
- package/testing/index.d.ts +59 -0
- package/testing/index.mjs +150 -0
- package/discovery/entries.mjs +0 -124
- package/discovery/generate.mjs +0 -321
- package/discovery/host.mjs +0 -12
- package/discovery/modules.mjs +0 -13
- package/discovery/packages.mjs +0 -57
- package/discovery/watch.mjs +0 -23
- package/dist/app/service-aliases.d.ts +0 -4
- package/dist/app/service-aliases.js +0 -14
- package/dist/contributions/command.d.ts +0 -8
- package/dist/contributions/command.js +0 -59
- package/dist/contributions/dialog.d.ts +0 -8
- package/dist/contributions/dialog.js +0 -18
- package/dist/contributions/form.d.ts +0 -10
- package/dist/contributions/form.js +0 -67
- package/dist/contributions/native.d.ts +0 -11
- package/dist/contributions/native.js +0 -87
- package/dist/contributions/shared.d.ts +0 -19
- package/dist/contributions/shared.js +0 -48
- package/dist/contributions/stateful-action.d.ts +0 -29
- package/dist/contributions/stateful-action.js +0 -33
- package/dist/events/action-setup.d.ts +0 -19
- package/dist/events/action-setup.js +0 -24
- package/dist/interceptors/service.d.ts +0 -37
- package/dist/interceptors/service.js +0 -7
- package/dist/replacements.d.ts +0 -22
- package/dist/replacements.js +0 -54
- package/dist/validation.d.ts +0 -2
- package/dist/validation.js +0 -16
- package/docs/auth.md +0 -106
- package/docs/events.md +0 -157
- package/docs/file-contributions.md +0 -174
- package/docs/reference.md +0 -404
- package/docs/todo-tutorial.md +0 -88
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ActionList } from '../actions/core.js';
|
|
2
|
+
import type { PluginScope } from '../scope.js';
|
|
3
|
+
import { BitaKitStartupError } from '../startup-error.js';
|
|
4
|
+
import { type LifecycleState } from '../lifecycle/state.js';
|
|
5
|
+
import type { OwnedSlotDefinition } from './define.js';
|
|
6
|
+
import type { ScopedSlots, SlotReader, SlotSnapshot } from './types.js';
|
|
7
|
+
export declare class SlotError extends BitaKitStartupError {
|
|
8
|
+
readonly code: 'unknown-slot' | 'unavailable-slot' | 'invalid-contribution' | 'conflict' | 'detached';
|
|
9
|
+
readonly slot: string;
|
|
10
|
+
constructor(code: 'unknown-slot' | 'unavailable-slot' | 'invalid-contribution' | 'conflict' | 'detached', slot: string, plugin: string, message: string, source?: string, app?: boolean);
|
|
11
|
+
}
|
|
12
|
+
/** One attachment owner for the runtime. Plugin readiness is read from the existing lifecycle. */
|
|
13
|
+
export declare class SlotRuntime {
|
|
14
|
+
private lifecycle;
|
|
15
|
+
private actions;
|
|
16
|
+
private publish;
|
|
17
|
+
private report;
|
|
18
|
+
private appId;
|
|
19
|
+
private entries;
|
|
20
|
+
private attachments;
|
|
21
|
+
private serial;
|
|
22
|
+
private flushing;
|
|
23
|
+
private dirty;
|
|
24
|
+
private actionRelease?;
|
|
25
|
+
readonly reader: SlotReader;
|
|
26
|
+
constructor(definitions: readonly OwnedSlotDefinition[], lifecycle: LifecycleState, actions: ActionList, publish: (snapshot: SlotSnapshot) => void, report: (error: unknown) => void, appId?: string);
|
|
27
|
+
/** `source` names the contributing file when an unknown target comes from an attachment. */
|
|
28
|
+
private entry;
|
|
29
|
+
/** Active contributors become dependents when their target declaration is removed. */
|
|
30
|
+
contributorsTo(owners: ReadonlySet<string>): readonly string[];
|
|
31
|
+
/** Add/remove owned declarations only after the affected scopes have stopped. */
|
|
32
|
+
replace(definitions: readonly OwnedSlotDefinition[]): void;
|
|
33
|
+
private listen;
|
|
34
|
+
bind(scope: PluginScope, plugin: string): ScopedSlots;
|
|
35
|
+
private attach;
|
|
36
|
+
private phase;
|
|
37
|
+
private refresh;
|
|
38
|
+
}
|
|
39
|
+
declare module '../events/types.js' {
|
|
40
|
+
interface EventMap {
|
|
41
|
+
'runtime.slot': SlotSnapshot;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import { BitaKitStartupError } from '../startup-error.js';
|
|
2
|
+
import { createStore } from '../store.js';
|
|
3
|
+
import { validate } from '../schema.js';
|
|
4
|
+
import { contributionSource } from '../contribution-origin.js';
|
|
5
|
+
import { notifyLifecycleObserver, } from '../lifecycle/state.js';
|
|
6
|
+
export class SlotError extends BitaKitStartupError {
|
|
7
|
+
code;
|
|
8
|
+
slot;
|
|
9
|
+
constructor(code, slot, plugin, message, source, app = false) {
|
|
10
|
+
super(`Slot "${slot}": ${message}`, plugin, { source, app });
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.slot = slot;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const empty = Object.freeze([]);
|
|
16
|
+
/** One attachment owner for the runtime. Plugin readiness is read from the existing lifecycle. */
|
|
17
|
+
export class SlotRuntime {
|
|
18
|
+
lifecycle;
|
|
19
|
+
actions;
|
|
20
|
+
publish;
|
|
21
|
+
report;
|
|
22
|
+
appId;
|
|
23
|
+
entries = new Map();
|
|
24
|
+
attachments = new Set();
|
|
25
|
+
serial = 0;
|
|
26
|
+
flushing = false;
|
|
27
|
+
dirty = false;
|
|
28
|
+
actionRelease;
|
|
29
|
+
reader;
|
|
30
|
+
constructor(definitions, lifecycle, actions, publish, report, appId = 'app') {
|
|
31
|
+
this.lifecycle = lifecycle;
|
|
32
|
+
this.actions = actions;
|
|
33
|
+
this.publish = publish;
|
|
34
|
+
this.report = report;
|
|
35
|
+
this.appId = appId;
|
|
36
|
+
for (const definition of definitions)
|
|
37
|
+
this.entries.set(definition.id, {
|
|
38
|
+
definition,
|
|
39
|
+
store: createStore(Object.freeze({
|
|
40
|
+
id: definition.id,
|
|
41
|
+
plugin: definition.plugin,
|
|
42
|
+
generation: 0,
|
|
43
|
+
phase: 'idle',
|
|
44
|
+
items: empty,
|
|
45
|
+
})),
|
|
46
|
+
});
|
|
47
|
+
this.reader = Object.freeze({
|
|
48
|
+
has: (name) => this.entries.has(name),
|
|
49
|
+
get: (name) => this.entry(name).store.get(),
|
|
50
|
+
subscribe: (name, listener) => this.listen(this.entry(name).store, listener),
|
|
51
|
+
watch: (name, listener) => this.listen(this.entry(name).store, listener, true),
|
|
52
|
+
});
|
|
53
|
+
lifecycle.subscribe(() => this.refresh());
|
|
54
|
+
}
|
|
55
|
+
/** `source` names the contributing file when an unknown target comes from an attachment. */
|
|
56
|
+
entry(name, plugin = this.appId, source) {
|
|
57
|
+
const entry = this.entries.get(name);
|
|
58
|
+
if (!entry)
|
|
59
|
+
throw new SlotError('unknown-slot', name, plugin, 'no owning definition exists. Install and enable the plugin that declares it (in tests, pass that plugin to mountPlugin).', source, plugin === this.appId);
|
|
60
|
+
return entry;
|
|
61
|
+
}
|
|
62
|
+
/** Active contributors become dependents when their target declaration is removed. */
|
|
63
|
+
contributorsTo(owners) {
|
|
64
|
+
return [
|
|
65
|
+
...new Set([...this.attachments]
|
|
66
|
+
.filter((attachment) => owners.has(this.entry(attachment.slot).definition.plugin))
|
|
67
|
+
.map((attachment) => attachment.plugin)),
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
/** Add/remove owned declarations only after the affected scopes have stopped. */
|
|
71
|
+
replace(definitions) {
|
|
72
|
+
const names = new Set(definitions.map((definition) => definition.id));
|
|
73
|
+
for (const name of this.entries.keys())
|
|
74
|
+
if (!names.has(name)) {
|
|
75
|
+
for (const attachment of [...this.attachments])
|
|
76
|
+
if (attachment.slot === name)
|
|
77
|
+
attachment.dispose();
|
|
78
|
+
this.entries.delete(name);
|
|
79
|
+
}
|
|
80
|
+
for (const definition of definitions) {
|
|
81
|
+
const current = this.entries.get(definition.id);
|
|
82
|
+
if (current)
|
|
83
|
+
current.definition = definition;
|
|
84
|
+
else
|
|
85
|
+
this.entries.set(definition.id, {
|
|
86
|
+
definition,
|
|
87
|
+
store: createStore(Object.freeze({
|
|
88
|
+
id: definition.id,
|
|
89
|
+
plugin: definition.plugin,
|
|
90
|
+
generation: 0,
|
|
91
|
+
phase: 'idle',
|
|
92
|
+
items: empty,
|
|
93
|
+
})),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
listen(store, listener, immediate = false, owned) {
|
|
98
|
+
const call = (value) => notifyLifecycleObserver(() => listener(value), this.report);
|
|
99
|
+
const unsubscribe = store.subscribe(call);
|
|
100
|
+
const off = () => {
|
|
101
|
+
unsubscribe();
|
|
102
|
+
owned?.delete(off);
|
|
103
|
+
};
|
|
104
|
+
owned?.add(off);
|
|
105
|
+
if (immediate)
|
|
106
|
+
call(store.get());
|
|
107
|
+
return off;
|
|
108
|
+
}
|
|
109
|
+
bind(scope, plugin) {
|
|
110
|
+
const observe = (name, listener, immediate) => {
|
|
111
|
+
const entry = this.entry(name, plugin);
|
|
112
|
+
let off = () => { };
|
|
113
|
+
const release = scope.onClose(() => off());
|
|
114
|
+
off = this.listen(entry.store, listener);
|
|
115
|
+
const dispose = () => {
|
|
116
|
+
off();
|
|
117
|
+
release();
|
|
118
|
+
};
|
|
119
|
+
if (immediate)
|
|
120
|
+
notifyLifecycleObserver(() => listener(entry.store.get()), this.report);
|
|
121
|
+
return dispose;
|
|
122
|
+
};
|
|
123
|
+
return Object.freeze({
|
|
124
|
+
...this.reader,
|
|
125
|
+
subscribe: (name, listener) => observe(name, listener, false),
|
|
126
|
+
watch: (name, listener) => observe(name, listener, true),
|
|
127
|
+
attach: (contribution, options) => {
|
|
128
|
+
const slot = contribution?.slot;
|
|
129
|
+
if (typeof slot !== 'string')
|
|
130
|
+
throw new TypeError('slots.attach expects one contribution with its target: attach({ slot: "name", component | data | actionId }).');
|
|
131
|
+
// The same object is attached, so its recorded source still names the contributing file.
|
|
132
|
+
return this.attach(scope, plugin, slot, contribution, options?.availability ?? 'require');
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
attach(scope, plugin, name, contribution, availability) {
|
|
137
|
+
const { definition } = this.entry(name, plugin, contributionSource(contribution));
|
|
138
|
+
const source = contributionSource(contribution) ?? contributionSource(definition);
|
|
139
|
+
const error = (code, message) => new SlotError(code, name, plugin, message, source, plugin === this.appId);
|
|
140
|
+
if (availability !== 'require' && availability !== 'wait')
|
|
141
|
+
throw error('invalid-contribution', 'availability must be require or wait.');
|
|
142
|
+
if (availability === 'require' &&
|
|
143
|
+
this.phase(definition.plugin, this.lifecycle.get()) !== 'ready')
|
|
144
|
+
throw error('unavailable-slot', `owner "${definition.plugin}" is not ready; use availability: 'wait' to attach later.`);
|
|
145
|
+
const kind = definition.accepts === 'actions' ? 'actionId' : definition.accepts;
|
|
146
|
+
if (!Object.hasOwn(contribution, kind) ||
|
|
147
|
+
['component', 'data', 'actionId'].some((key) => key !== kind && Object.hasOwn(contribution, key)))
|
|
148
|
+
throw error('invalid-contribution', `the contract accepts ${definition.accepts}.`);
|
|
149
|
+
if (kind === 'actionId' &&
|
|
150
|
+
(!this.actions.get(contribution.actionId) ||
|
|
151
|
+
this.actions.get(contribution.actionId)?.internal))
|
|
152
|
+
throw error('invalid-contribution', 'actionId must reference an existing public Action.');
|
|
153
|
+
if (definition.policy === 'keyed' &&
|
|
154
|
+
(typeof contribution.key !== 'string' || !contribution.key))
|
|
155
|
+
throw error('invalid-contribution', 'a keyed slot requires a non-empty key.');
|
|
156
|
+
const id = `${plugin}:${contribution.id ?? ++this.serial}`;
|
|
157
|
+
for (const current of this.attachments)
|
|
158
|
+
if (current.slot === name &&
|
|
159
|
+
(current.id === id ||
|
|
160
|
+
definition.policy === 'single' ||
|
|
161
|
+
(definition.policy === 'keyed' && current.key === contribution.key)))
|
|
162
|
+
throw error('conflict', `contribution "${id}" conflicts with "${current.id}"; remove it explicitly first.`);
|
|
163
|
+
let resolve, reject;
|
|
164
|
+
const ready = new Promise((done, fail) => {
|
|
165
|
+
resolve = done;
|
|
166
|
+
reject = fail;
|
|
167
|
+
});
|
|
168
|
+
void ready.catch(() => { });
|
|
169
|
+
const record = {
|
|
170
|
+
slot: name,
|
|
171
|
+
plugin,
|
|
172
|
+
id,
|
|
173
|
+
key: contribution.key,
|
|
174
|
+
order: contribution.order ?? 0,
|
|
175
|
+
content: {
|
|
176
|
+
...(kind === 'actionId'
|
|
177
|
+
? { actionId: contribution.actionId, input: contribution.input }
|
|
178
|
+
: kind === 'component'
|
|
179
|
+
? { component: contribution.component }
|
|
180
|
+
: { data: contribution.data }),
|
|
181
|
+
when: contribution.when,
|
|
182
|
+
},
|
|
183
|
+
valid: kind !== 'data',
|
|
184
|
+
listeners: new Set(),
|
|
185
|
+
resolve,
|
|
186
|
+
reject,
|
|
187
|
+
store: createStore(Object.freeze({
|
|
188
|
+
id,
|
|
189
|
+
slot: name,
|
|
190
|
+
plugin,
|
|
191
|
+
generation: this.lifecycle.get().generation,
|
|
192
|
+
phase: 'pending',
|
|
193
|
+
})),
|
|
194
|
+
dispose: () => { },
|
|
195
|
+
};
|
|
196
|
+
// Register cleanup before exposing any event: a callback may stop its own scope.
|
|
197
|
+
const release = scope.onClose(() => record.dispose());
|
|
198
|
+
record.dispose = () => {
|
|
199
|
+
if (!this.attachments.delete(record))
|
|
200
|
+
return;
|
|
201
|
+
release();
|
|
202
|
+
record.store.set(Object.freeze({ ...record.store.get(), phase: 'detached', reason: undefined }));
|
|
203
|
+
for (const off of record.listeners)
|
|
204
|
+
off();
|
|
205
|
+
reject(error('detached', 'contributor detached before attachment completed.'));
|
|
206
|
+
this.refresh();
|
|
207
|
+
};
|
|
208
|
+
this.attachments.add(record);
|
|
209
|
+
if (!this.actionRelease)
|
|
210
|
+
this.actionRelease = this.actions.subscribe(() => this.refresh());
|
|
211
|
+
this.refresh();
|
|
212
|
+
if (kind === 'data') {
|
|
213
|
+
void validate(definition.schema, contribution.data).then((data) => {
|
|
214
|
+
if (!this.attachments.has(record))
|
|
215
|
+
return;
|
|
216
|
+
record.content = { data };
|
|
217
|
+
record.valid = true;
|
|
218
|
+
this.refresh();
|
|
219
|
+
}, (cause) => {
|
|
220
|
+
if (!this.attachments.delete(record))
|
|
221
|
+
return;
|
|
222
|
+
release();
|
|
223
|
+
const failure = error('invalid-contribution', cause instanceof Error ? cause.message : String(cause));
|
|
224
|
+
record.store.set(Object.freeze({
|
|
225
|
+
...record.store.get(),
|
|
226
|
+
phase: 'failed',
|
|
227
|
+
reason: undefined,
|
|
228
|
+
failure: Object.freeze({ message: failure.message, plugin, source }),
|
|
229
|
+
}));
|
|
230
|
+
for (const off of record.listeners)
|
|
231
|
+
off();
|
|
232
|
+
reject(failure);
|
|
233
|
+
this.refresh();
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
return Object.freeze({
|
|
237
|
+
get: record.store.get,
|
|
238
|
+
subscribe: (listener) => this.attachments.has(record)
|
|
239
|
+
? this.listen(record.store, listener, false, record.listeners)
|
|
240
|
+
: () => { },
|
|
241
|
+
watch: (listener) => {
|
|
242
|
+
if (this.attachments.has(record))
|
|
243
|
+
return this.listen(record.store, listener, true, record.listeners);
|
|
244
|
+
notifyLifecycleObserver(() => listener(record.store.get()), this.report);
|
|
245
|
+
return () => { };
|
|
246
|
+
},
|
|
247
|
+
ready,
|
|
248
|
+
dispose: record.dispose,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
phase(plugin, state) {
|
|
252
|
+
const owner = state.plugins.find((entry) => entry.id === plugin);
|
|
253
|
+
if (!owner)
|
|
254
|
+
return 'blocked';
|
|
255
|
+
if (state.cleanup === 'pending')
|
|
256
|
+
return 'stopping';
|
|
257
|
+
return owner.phase;
|
|
258
|
+
}
|
|
259
|
+
refresh() {
|
|
260
|
+
this.dirty = true;
|
|
261
|
+
if (this.flushing)
|
|
262
|
+
return;
|
|
263
|
+
this.flushing = true;
|
|
264
|
+
try {
|
|
265
|
+
while (this.dirty) {
|
|
266
|
+
this.dirty = false;
|
|
267
|
+
const state = this.lifecycle.get();
|
|
268
|
+
for (const record of [...this.attachments]) {
|
|
269
|
+
if (!this.attachments.has(record))
|
|
270
|
+
continue;
|
|
271
|
+
const owner = this.entries.get(record.slot).definition.plugin;
|
|
272
|
+
const reason = !record.valid
|
|
273
|
+
? 'validating'
|
|
274
|
+
: this.phase(owner, state) !== 'ready'
|
|
275
|
+
? 'owner-unavailable'
|
|
276
|
+
: this.phase(record.plugin, state) !== 'ready'
|
|
277
|
+
? 'contributor-unavailable'
|
|
278
|
+
: 'actionId' in record.content &&
|
|
279
|
+
(!this.actions.get(record.content.actionId) ||
|
|
280
|
+
this.actions.get(record.content.actionId)?.internal)
|
|
281
|
+
? 'action-unavailable'
|
|
282
|
+
: undefined;
|
|
283
|
+
const phase = reason ? 'pending' : 'attached';
|
|
284
|
+
const previous = record.store.get();
|
|
285
|
+
if (previous.phase !== phase ||
|
|
286
|
+
previous.reason !== reason ||
|
|
287
|
+
previous.generation !== state.generation) {
|
|
288
|
+
record.item =
|
|
289
|
+
phase === 'attached'
|
|
290
|
+
? Object.freeze({
|
|
291
|
+
...record.content,
|
|
292
|
+
id: record.id,
|
|
293
|
+
plugin: record.plugin,
|
|
294
|
+
key: record.key,
|
|
295
|
+
order: record.order,
|
|
296
|
+
generation: state.generation,
|
|
297
|
+
})
|
|
298
|
+
: undefined;
|
|
299
|
+
record.store.set(Object.freeze({ ...previous, phase, reason, generation: state.generation }));
|
|
300
|
+
}
|
|
301
|
+
// An observer may change readiness or close a scope. Do not resolve or advance
|
|
302
|
+
// another attachment using the lifecycle snapshot captured before that callback.
|
|
303
|
+
if (this.dirty)
|
|
304
|
+
break;
|
|
305
|
+
if (phase === 'attached' && this.attachments.has(record))
|
|
306
|
+
record.resolve();
|
|
307
|
+
}
|
|
308
|
+
// Use the current lifecycle after reentrant observers; the next pass completes updates.
|
|
309
|
+
if (this.dirty)
|
|
310
|
+
continue;
|
|
311
|
+
for (const [name, entry] of this.entries) {
|
|
312
|
+
const phase = this.phase(entry.definition.plugin, state);
|
|
313
|
+
const failure = state.plugins.find((plugin) => plugin.id === entry.definition.plugin)?.failure;
|
|
314
|
+
const items = [...this.attachments]
|
|
315
|
+
.filter((record) => record.slot === name && record.item)
|
|
316
|
+
.map((record) => record.item)
|
|
317
|
+
.sort((a, b) => a.order - b.order);
|
|
318
|
+
const previous = entry.store.get();
|
|
319
|
+
if (previous.phase === phase &&
|
|
320
|
+
previous.generation === state.generation &&
|
|
321
|
+
previous.failure === failure &&
|
|
322
|
+
previous.items.length === items.length &&
|
|
323
|
+
previous.items.every((item, index) => item === items[index]))
|
|
324
|
+
continue;
|
|
325
|
+
const next = Object.freeze({
|
|
326
|
+
...previous,
|
|
327
|
+
phase,
|
|
328
|
+
failure,
|
|
329
|
+
generation: state.generation,
|
|
330
|
+
items: items.length ? Object.freeze(items) : empty,
|
|
331
|
+
});
|
|
332
|
+
entry.store.set(next);
|
|
333
|
+
if (!this.dirty && entry.store.get() === next)
|
|
334
|
+
notifyLifecycleObserver(() => this.publish(next), this.report);
|
|
335
|
+
if (this.dirty)
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
finally {
|
|
341
|
+
this.flushing = false;
|
|
342
|
+
}
|
|
343
|
+
if (!this.attachments.size) {
|
|
344
|
+
this.actionRelease?.();
|
|
345
|
+
this.actionRelease = undefined;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { PresentationType, SlotMap, SlotName } from '../presentation.js';
|
|
2
|
+
import type { LifecycleFailure, LifecyclePhase } from '../lifecycle/state.js';
|
|
3
|
+
import type { SlotPayload, SlotData } from './define.js';
|
|
4
|
+
import type { ActionId } from '../actions/core.js';
|
|
5
|
+
export type SlotPredicate<Name extends string = string> = (context: {
|
|
6
|
+
payload: Name extends keyof SlotMap ? SlotPayload<Name> : any;
|
|
7
|
+
}) => boolean;
|
|
8
|
+
export type SlotAvailability = 'require' | 'wait';
|
|
9
|
+
export type SlotContent = {
|
|
10
|
+
component: PresentationType<'component'>;
|
|
11
|
+
data?: never;
|
|
12
|
+
actionId?: never;
|
|
13
|
+
input?: never;
|
|
14
|
+
} | {
|
|
15
|
+
data: unknown;
|
|
16
|
+
component?: never;
|
|
17
|
+
actionId?: never;
|
|
18
|
+
input?: never;
|
|
19
|
+
} | {
|
|
20
|
+
actionId: ActionId;
|
|
21
|
+
input?: (context: {
|
|
22
|
+
payload: any;
|
|
23
|
+
}) => unknown;
|
|
24
|
+
component?: never;
|
|
25
|
+
data?: never;
|
|
26
|
+
};
|
|
27
|
+
type ContentFor<Name extends string> = Name extends keyof SlotMap ? SlotMap[Name] extends {
|
|
28
|
+
accepts: 'data';
|
|
29
|
+
} ? {
|
|
30
|
+
data: SlotData<Name>;
|
|
31
|
+
component?: never;
|
|
32
|
+
actionId?: never;
|
|
33
|
+
input?: never;
|
|
34
|
+
} : SlotMap[Name] extends {
|
|
35
|
+
accepts: 'actions';
|
|
36
|
+
} ? {
|
|
37
|
+
actionId: ActionId;
|
|
38
|
+
input?: (context: {
|
|
39
|
+
payload: import('./define.js').SlotPayload<Name>;
|
|
40
|
+
}) => unknown;
|
|
41
|
+
component?: never;
|
|
42
|
+
data?: never;
|
|
43
|
+
} : SlotMap[Name] extends {
|
|
44
|
+
accepts: 'component';
|
|
45
|
+
} ? {
|
|
46
|
+
component: PresentationType<'component'>;
|
|
47
|
+
data?: never;
|
|
48
|
+
actionId?: never;
|
|
49
|
+
input?: never;
|
|
50
|
+
} : SlotContent : SlotContent;
|
|
51
|
+
/** IDs are local to the contributor. Keys select an occupant in a keyed slot. */
|
|
52
|
+
export type SlotContribution<Name extends string = string> = ContentFor<Name> & {
|
|
53
|
+
id?: string;
|
|
54
|
+
order?: number;
|
|
55
|
+
/** Pure per-outlet presentation filter; never changes shared attachment state. */
|
|
56
|
+
when?: SlotPredicate<Name>;
|
|
57
|
+
} & (Name extends keyof SlotMap ? SlotMap[Name] extends {
|
|
58
|
+
policy: 'keyed';
|
|
59
|
+
} ? {
|
|
60
|
+
key: string;
|
|
61
|
+
} : {
|
|
62
|
+
key?: string;
|
|
63
|
+
} : {
|
|
64
|
+
key?: string;
|
|
65
|
+
});
|
|
66
|
+
export type SlotItem = Readonly<SlotContent & {
|
|
67
|
+
when?: SlotPredicate;
|
|
68
|
+
id: string;
|
|
69
|
+
plugin: string;
|
|
70
|
+
key?: string;
|
|
71
|
+
order: number;
|
|
72
|
+
generation: number;
|
|
73
|
+
}>;
|
|
74
|
+
export type SlotSnapshot = Readonly<{
|
|
75
|
+
id: string;
|
|
76
|
+
plugin: string;
|
|
77
|
+
generation: number;
|
|
78
|
+
phase: LifecyclePhase;
|
|
79
|
+
failure?: LifecycleFailure;
|
|
80
|
+
items: readonly SlotItem[];
|
|
81
|
+
}>;
|
|
82
|
+
export type SlotAttachmentSnapshot = Readonly<{
|
|
83
|
+
id: string;
|
|
84
|
+
slot: string;
|
|
85
|
+
plugin: string;
|
|
86
|
+
generation: number;
|
|
87
|
+
phase: 'pending' | 'attached' | 'detached' | 'failed';
|
|
88
|
+
reason?: 'owner-unavailable' | 'contributor-unavailable' | 'action-unavailable' | 'validating';
|
|
89
|
+
failure?: LifecycleFailure;
|
|
90
|
+
}>;
|
|
91
|
+
export type SlotAttachment = Readonly<{
|
|
92
|
+
get(): SlotAttachmentSnapshot;
|
|
93
|
+
subscribe(listener: (state: SlotAttachmentSnapshot) => void): () => void;
|
|
94
|
+
watch(listener: (state: SlotAttachmentSnapshot) => void): () => void;
|
|
95
|
+
/** First successful attachment only; later transitions are observed through watch. */
|
|
96
|
+
ready: Promise<void>;
|
|
97
|
+
dispose(): void;
|
|
98
|
+
}>;
|
|
99
|
+
export type SlotReader = Readonly<{
|
|
100
|
+
has(name: string): boolean;
|
|
101
|
+
get(name: SlotName): SlotSnapshot;
|
|
102
|
+
subscribe(name: SlotName, listener: (state: SlotSnapshot) => void): () => void;
|
|
103
|
+
watch(name: SlotName, listener: (state: SlotSnapshot) => void): () => void;
|
|
104
|
+
}>;
|
|
105
|
+
export type ScopedSlots = SlotReader & {
|
|
106
|
+
/** `scope.slots.attach({ slot: 'shell.toolbar', actionId: 'notes.open' })`; the slot is part of the contribution. */
|
|
107
|
+
attach<const Name extends SlotName>(contribution: {
|
|
108
|
+
slot: Name;
|
|
109
|
+
} & SlotContribution<Name>, options?: {
|
|
110
|
+
availability?: SlotAvailability;
|
|
111
|
+
}): SlotAttachment;
|
|
112
|
+
};
|
|
113
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A failure while starting the runtime, attributed to the module that caused it. UI integrations
|
|
3
|
+
* use the attribution to keep the rest of the application running during development.
|
|
4
|
+
*/
|
|
5
|
+
export declare class BitaKitStartupError extends Error {
|
|
6
|
+
/** Module ID of the failing plugin or the application module. */
|
|
7
|
+
readonly plugin: string;
|
|
8
|
+
/** True when the application module itself failed; there is nothing to isolate. */
|
|
9
|
+
readonly app: boolean;
|
|
10
|
+
/** Project-relative source file of the failing contribution, when discovery recorded it. */
|
|
11
|
+
readonly source?: string;
|
|
12
|
+
constructor(message: string, plugin: string, options?: {
|
|
13
|
+
cause?: unknown;
|
|
14
|
+
source?: string;
|
|
15
|
+
app?: boolean;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/** Recognizes startup errors by shape, so errors from another package copy also qualify. */
|
|
19
|
+
export declare function isBitaKitStartupError(error: unknown): error is BitaKitStartupError;
|
|
20
|
+
/** Human-readable owner of a module ID. */
|
|
21
|
+
export declare function moduleLabel(module: string, appId: string): string;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A failure while starting the runtime, attributed to the module that caused it. UI integrations
|
|
3
|
+
* use the attribution to keep the rest of the application running during development.
|
|
4
|
+
*/
|
|
5
|
+
export class BitaKitStartupError extends Error {
|
|
6
|
+
/** Module ID of the failing plugin or the application module. */
|
|
7
|
+
plugin;
|
|
8
|
+
/** True when the application module itself failed; there is nothing to isolate. */
|
|
9
|
+
app;
|
|
10
|
+
/** Project-relative source file of the failing contribution, when discovery recorded it. */
|
|
11
|
+
source;
|
|
12
|
+
constructor(message, plugin, options = {}) {
|
|
13
|
+
super(message, { cause: options.cause });
|
|
14
|
+
this.name = 'BitaKitStartupError';
|
|
15
|
+
this.plugin = plugin;
|
|
16
|
+
this.app = options.app ?? false;
|
|
17
|
+
this.source = options.source;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Recognizes startup errors by shape, so errors from another package copy also qualify. */
|
|
21
|
+
export function isBitaKitStartupError(error) {
|
|
22
|
+
return (error instanceof Error &&
|
|
23
|
+
error.name === 'BitaKitStartupError' &&
|
|
24
|
+
typeof error.plugin === 'string');
|
|
25
|
+
}
|
|
26
|
+
/** Human-readable owner of a module ID. */
|
|
27
|
+
export function moduleLabel(module, appId) {
|
|
28
|
+
return module === appId ? 'the app' : `plugin "${module}"`;
|
|
29
|
+
}
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Observable state owned by a service. `get` reads, `set` replaces (a value or an updater),
|
|
3
|
+
* `subscribe` notifies after every change. Actions re-evaluate their presentation and
|
|
4
|
+
* `useServiceState` re-renders when a store changes; services never notify by hand.
|
|
5
|
+
*/
|
|
6
|
+
export type Store<T> = {
|
|
7
|
+
get(): T;
|
|
8
|
+
set(update: T | ((current: T) => T)): void;
|
|
9
|
+
subscribe(listener: (state: T, previous: T) => void): () => void;
|
|
10
|
+
/**
|
|
11
|
+
* Apply a change now, run the commit, and roll it back if the commit fails. The error is
|
|
12
|
+
* rethrown so the calling action reports it. `reconcile` merges the committed value in.
|
|
13
|
+
*/
|
|
14
|
+
optimistic<R>(apply: (current: T) => T, commit: () => Promise<R>, options?: {
|
|
15
|
+
reconcile?: (current: T, result: R) => T;
|
|
16
|
+
revert?: (current: T) => T;
|
|
17
|
+
}): Promise<R>;
|
|
18
|
+
};
|
|
19
|
+
export declare function createStore<T>(initial: T): Store<T>;
|
|
20
|
+
export declare function isStore(value: unknown): value is Store<unknown>;
|
|
21
|
+
/** Anything a service exposes with `subscribe(listener)` is observed by the runtime. */
|
|
22
|
+
export declare function isObservable(value: unknown): value is {
|
|
23
|
+
subscribe(listener: () => void): () => void;
|
|
24
|
+
};
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createStore as createVanillaStore } from 'zustand/vanilla';
|
|
2
|
+
const marker = Symbol.for('bitakit.core.store');
|
|
3
|
+
export function createStore(initial) {
|
|
4
|
+
const inner = createVanillaStore(() => initial);
|
|
5
|
+
const store = {
|
|
6
|
+
get: inner.getState,
|
|
7
|
+
set: (update) => inner.setState(typeof update === 'function' ? update : () => update, true),
|
|
8
|
+
subscribe: inner.subscribe,
|
|
9
|
+
async optimistic(apply, commit, options = {}) {
|
|
10
|
+
const previous = inner.getState();
|
|
11
|
+
inner.setState(apply, true);
|
|
12
|
+
const applied = inner.getState();
|
|
13
|
+
try {
|
|
14
|
+
const result = await commit();
|
|
15
|
+
if (options.reconcile)
|
|
16
|
+
inner.setState((current) => options.reconcile(current, result), true);
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
// Revert only this change when nothing else changed meanwhile; a custom revert otherwise.
|
|
21
|
+
if (options.revert)
|
|
22
|
+
inner.setState(options.revert, true);
|
|
23
|
+
else if (inner.getState() === applied)
|
|
24
|
+
inner.setState(() => previous, true);
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(store, marker, { value: true });
|
|
30
|
+
return store;
|
|
31
|
+
}
|
|
32
|
+
export function isStore(value) {
|
|
33
|
+
return !!value && typeof value === 'object' && value[marker] === true;
|
|
34
|
+
}
|
|
35
|
+
/** Anything a service exposes with `subscribe(listener)` is observed by the runtime. */
|
|
36
|
+
export function isObservable(value) {
|
|
37
|
+
return (!!value &&
|
|
38
|
+
(typeof value === 'object' || typeof value === 'function') &&
|
|
39
|
+
typeof value.subscribe === 'function');
|
|
40
|
+
}
|
package/dist/translation.d.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Translator } from './definitions.js';
|
|
2
2
|
import { type TranslationService } from './contracts.js';
|
|
3
3
|
/** Shared key-or-literal resolution for automatic and imperatively registered Actions. */
|
|
4
4
|
export declare function resolveText(translate: Translator, value: string | undefined, fallback?: string, values?: Parameters<Translator>[1]): string;
|
|
5
5
|
/** Adapter primitive. No locale store: the current translator delegates to its provider. */
|
|
6
6
|
export declare function createTranslationPlugin(initial: Translator, catalog?: TranslationService['getCatalog']): {
|
|
7
7
|
readonly id: "translation";
|
|
8
|
-
readonly services:
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
readonly services: {
|
|
9
|
+
readonly i18n: import("./definitions.js").ServiceOptions & {
|
|
10
|
+
readonly kind: "service";
|
|
11
|
+
create(context: import("./definitions.js").ServiceContext): TranslationService;
|
|
12
|
+
} & {
|
|
13
|
+
readonly provides: import("./service-key.js").ServiceKey<TranslationService, "i18n">;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
11
16
|
readonly translation: TranslationService;
|
|
12
17
|
readonly update: (next: Translator, nextCatalog?: ((namespace?: string) => import("./contracts.js").TranslationCatalog | undefined) | undefined) => void;
|
|
13
18
|
};
|
package/dist/translation.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { definePlugin } from './definitions.js';
|
|
1
|
+
import { defineService, definePlugin } from './definitions.js';
|
|
3
2
|
import { translationService } from './contracts.js';
|
|
4
3
|
/** Shared key-or-literal resolution for automatic and imperatively registered Actions. */
|
|
5
4
|
export function resolveText(translate, value, fallback = value ?? '', values) {
|
|
@@ -32,7 +31,7 @@ export function createTranslationPlugin(initial, catalog) {
|
|
|
32
31
|
};
|
|
33
32
|
return definePlugin({
|
|
34
33
|
id: 'translation',
|
|
35
|
-
services:
|
|
34
|
+
services: { i18n: defineService(() => service, { provides: translationService }) },
|
|
36
35
|
translation: service,
|
|
37
36
|
update(next, nextCatalog = currentCatalog) {
|
|
38
37
|
current = next;
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { defineAction } from '@bitakit/core';
|
|
2
2
|
|
|
3
3
|
export default defineAction({
|
|
4
|
-
|
|
5
|
-
requires: ['todos'],
|
|
6
|
-
execute: ({ services, input }) => services.todos.create(input),
|
|
4
|
+
execute: ({ services, input }) => services.todos.tasks.create(input),
|
|
7
5
|
});
|