@histoire/plugin-kit 0.0.0-stage → 1.0.0-beta.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.
- package/LICENSE +21 -0
- package/README.md +182 -2
- package/dist/builtins.d.ts +13 -0
- package/dist/builtins.d.ts.map +1 -0
- package/dist/builtins.js +7 -0
- package/dist/define.d.ts +19 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +43 -0
- package/dist/errors.d.ts +11 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +14 -0
- package/dist/frame.d.ts +6 -0
- package/dist/frame.d.ts.map +1 -0
- package/dist/frame.js +3 -0
- package/dist/host/actions.d.ts +37 -0
- package/dist/host/actions.d.ts.map +1 -0
- package/dist/host/actions.js +47 -0
- package/dist/host/create-host.d.ts +9 -0
- package/dist/host/create-host.d.ts.map +1 -0
- package/dist/host/create-host.js +194 -0
- package/dist/host/diagnostics.d.ts +14 -0
- package/dist/host/diagnostics.d.ts.map +1 -0
- package/dist/host/diagnostics.js +26 -0
- package/dist/host/frames.d.ts +43 -0
- package/dist/host/frames.d.ts.map +1 -0
- package/dist/host/frames.js +63 -0
- package/dist/host/generation.d.ts +24 -0
- package/dist/host/generation.d.ts.map +1 -0
- package/dist/host/generation.js +38 -0
- package/dist/host/install.d.ts +43 -0
- package/dist/host/install.d.ts.map +1 -0
- package/dist/host/install.js +168 -0
- package/dist/host/manifest.d.ts +23 -0
- package/dist/host/manifest.d.ts.map +1 -0
- package/dist/host/manifest.js +37 -0
- package/dist/host/mount.d.ts +15 -0
- package/dist/host/mount.d.ts.map +1 -0
- package/dist/host/mount.js +57 -0
- package/dist/host/ordering.d.ts +20 -0
- package/dist/host/ordering.d.ts.map +1 -0
- package/dist/host/ordering.js +12 -0
- package/dist/host/plugin-api.d.ts +33 -0
- package/dist/host/plugin-api.d.ts.map +1 -0
- package/dist/host/plugin-api.js +113 -0
- package/dist/host/preferences.d.ts +57 -0
- package/dist/host/preferences.d.ts.map +1 -0
- package/dist/host/preferences.js +137 -0
- package/dist/host/registry.d.ts +43 -0
- package/dist/host/registry.d.ts.map +1 -0
- package/dist/host/registry.js +123 -0
- package/dist/host/resources.d.ts +20 -0
- package/dist/host/resources.d.ts.map +1 -0
- package/dist/host/resources.js +45 -0
- package/dist/ids.d.ts +32 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +51 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/react/index.d.ts +19 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +25 -0
- package/dist/render/keys.d.ts +7 -0
- package/dist/render/keys.d.ts.map +1 -0
- package/dist/render/keys.js +15 -0
- package/dist/render/mount-manager.d.ts +36 -0
- package/dist/render/mount-manager.d.ts.map +1 -0
- package/dist/render/mount-manager.js +128 -0
- package/dist/render/view-binding.d.ts +19 -0
- package/dist/render/view-binding.d.ts.map +1 -0
- package/dist/render/view-binding.js +7 -0
- package/dist/render/workbench-components.d.ts +39 -0
- package/dist/render/workbench-components.d.ts.map +1 -0
- package/dist/render/workbench-components.js +76 -0
- package/dist/session/context.d.ts +66 -0
- package/dist/session/context.d.ts.map +1 -0
- package/dist/session/context.js +173 -0
- package/dist/session/frames.d.ts +46 -0
- package/dist/session/frames.d.ts.map +1 -0
- package/dist/session/frames.js +68 -0
- package/dist/shortcuts.d.ts +94 -0
- package/dist/shortcuts.d.ts.map +1 -0
- package/dist/shortcuts.js +157 -0
- package/dist/styles.d.ts +3 -0
- package/dist/styles.d.ts.map +1 -0
- package/dist/styles.js +25 -0
- package/dist/svelte4/index.d.ts +27 -0
- package/dist/svelte4/index.d.ts.map +1 -0
- package/dist/svelte4/index.js +16 -0
- package/dist/svelte5/index.d.ts +21 -0
- package/dist/svelte5/index.d.ts.map +1 -0
- package/dist/svelte5/index.js +40 -0
- package/dist/types/actions.d.ts +12 -0
- package/dist/types/actions.d.ts.map +1 -0
- package/dist/types/actions.js +1 -0
- package/dist/types/base.d.ts +12 -0
- package/dist/types/base.d.ts.map +1 -0
- package/dist/types/base.js +1 -0
- package/dist/types/canvas.d.ts +150 -0
- package/dist/types/canvas.d.ts.map +1 -0
- package/dist/types/canvas.js +1 -0
- package/dist/types/channel.d.ts +28 -0
- package/dist/types/channel.d.ts.map +1 -0
- package/dist/types/channel.js +1 -0
- package/dist/types/commands.d.ts +35 -0
- package/dist/types/commands.d.ts.map +1 -0
- package/dist/types/commands.js +1 -0
- package/dist/types/common.d.ts +7 -0
- package/dist/types/common.d.ts.map +1 -0
- package/dist/types/common.js +1 -0
- package/dist/types/context.d.ts +72 -0
- package/dist/types/context.d.ts.map +1 -0
- package/dist/types/context.js +1 -0
- package/dist/types/contribution.d.ts +56 -0
- package/dist/types/contribution.d.ts.map +1 -0
- package/dist/types/contribution.js +1 -0
- package/dist/types/frame.d.ts +41 -0
- package/dist/types/frame.d.ts.map +1 -0
- package/dist/types/frame.js +1 -0
- package/dist/types/host.d.ts +123 -0
- package/dist/types/host.d.ts.map +1 -0
- package/dist/types/host.js +1 -0
- package/dist/types/index.d.ts +19 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/inspector.d.ts +38 -0
- package/dist/types/inspector.d.ts.map +1 -0
- package/dist/types/inspector.js +1 -0
- package/dist/types/menus.d.ts +71 -0
- package/dist/types/menus.d.ts.map +1 -0
- package/dist/types/menus.js +1 -0
- package/dist/types/plugin.d.ts +133 -0
- package/dist/types/plugin.d.ts.map +1 -0
- package/dist/types/plugin.js +1 -0
- package/dist/types/preferences.d.ts +31 -0
- package/dist/types/preferences.d.ts.map +1 -0
- package/dist/types/preferences.js +1 -0
- package/dist/types/services.d.ts +175 -0
- package/dist/types/services.d.ts.map +1 -0
- package/dist/types/services.js +1 -0
- package/dist/types/shell.d.ts +47 -0
- package/dist/types/shell.d.ts.map +1 -0
- package/dist/types/shell.js +1 -0
- package/dist/types/toolbar.d.ts +59 -0
- package/dist/types/toolbar.d.ts.map +1 -0
- package/dist/types/toolbar.js +1 -0
- package/dist/types/view.d.ts +82 -0
- package/dist/types/view.d.ts.map +1 -0
- package/dist/types/view.js +1 -0
- package/dist/vue/components.d.ts +122 -0
- package/dist/vue/components.d.ts.map +1 -0
- package/dist/vue/components.js +112 -0
- package/dist/vue/index.d.ts +21 -0
- package/dist/vue/index.d.ts.map +1 -0
- package/dist/vue/index.js +47 -0
- package/dist/vue/mount.d.ts +9 -0
- package/dist/vue/mount.d.ts.map +1 -0
- package/dist/vue/mount.js +32 -0
- package/dist/vue/panel-action.d.ts +60 -0
- package/dist/vue/panel-action.d.ts.map +1 -0
- package/dist/vue/panel-action.js +28 -0
- package/package.json +92 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Guillaume Chau
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,183 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @histoire/plugin-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Contracts and helpers for Histoire workbench plugins (`defineWorkbenchPlugin`) and frame plugins (`defineFramePlugin`), plus the extension host that embedding hosts use to run workbench plugins themselves.
|
|
4
|
+
|
|
5
|
+
This document covers **hosts**: apps that embed a Histoire book through `@histoire/sdk` and want plugin UI. Plugin authoring is documented separately.
|
|
6
|
+
|
|
7
|
+
## Who evaluates what
|
|
8
|
+
|
|
9
|
+
- The standalone workbench and the book's own source-origin explorer (`surface: 'explorer'`) load the book's plugin manifest, like any page the book serves.
|
|
10
|
+
- A remote host (your app on another origin) **never** evaluates modules provided by the book. The book descriptor lists frame plugins as data only (`session.plugins.frames()`). To get workbench UI, the host imports the plugin's `client` module itself and passes it to its own extension host.
|
|
11
|
+
- Frame plugins always run inside the book's story documents. A host-imported workbench plugin talks to them through the SDK session (`session.plugins`), across origins.
|
|
12
|
+
|
|
13
|
+
## Creating a host
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { createExtensionHost, createSessionContext, createSessionFrameService } from '@histoire/plugin-kit'
|
|
17
|
+
import { createHistoireSession } from '@histoire/sdk'
|
|
18
|
+
import acme from 'acme-histoire-plugin/client'
|
|
19
|
+
|
|
20
|
+
const session = createHistoireSession({ url: 'https://book.example.com/' })
|
|
21
|
+
await session.connect()
|
|
22
|
+
|
|
23
|
+
// Root context from public session methods; `frames` adds the `frames` capability.
|
|
24
|
+
const root = createSessionContext({
|
|
25
|
+
session,
|
|
26
|
+
frames: session.plugins ? createSessionFrameService(session.plugins) : undefined,
|
|
27
|
+
// mode: 'remote' by default, so `devOnly` contributions stay hidden.
|
|
28
|
+
// navigation: defaults to story selection only; other destinations are ignored.
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
const host = createExtensionHost({
|
|
32
|
+
plugins: [acme, { definition: other, options: { theme: 'compact' } }],
|
|
33
|
+
services: {
|
|
34
|
+
context: root.context,
|
|
35
|
+
// Optional. Without storage, plugin preferences live in memory only.
|
|
36
|
+
storage: window.localStorage,
|
|
37
|
+
storageNamespace: 'my-app',
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
await host.ready // every setup settled (failures become diagnostics, never throws)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The host belongs to you: dispose it (and close the root context) when your page or view goes away.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
await host.dispose()
|
|
47
|
+
root.close()
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Two hosts on one page are fully isolated: registries, preferences overlays, diagnostics and frame channels are per host.
|
|
51
|
+
|
|
52
|
+
## Reading contributions
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
// Committed contributions of one kind, registry order (built-ins keep theirs, plugins by
|
|
56
|
+
// `order`, then config order, then registration order).
|
|
57
|
+
host.contributions('inspector.tab')
|
|
58
|
+
|
|
59
|
+
// Same list filtered for a surface context: `available` predicates and `devOnly`.
|
|
60
|
+
host.available('toolbar.item', root.context)
|
|
61
|
+
|
|
62
|
+
// Re-read after registry commits, root context changes and new diagnostics.
|
|
63
|
+
const stop = host.subscribe(() => render())
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Contexts by surface:
|
|
67
|
+
|
|
68
|
+
| Kinds | Context |
|
|
69
|
+
| --- | --- |
|
|
70
|
+
| `toolbar.item`, `command`, `shortcut`, `shell.pane`, `settings.section`, `home.section` | root context (`root.context`) |
|
|
71
|
+
| `inspector.tab`, `inspector.section`, `frame-menu.item`, `canvas.frame-overlay` | frame-scoped: root fields plus `frame: { key, target }` and `runtime: { getSnapshot, subscribe }` |
|
|
72
|
+
| `canvas.*` | canvas-scoped (`canvas` service); not available outside the standalone canvas |
|
|
73
|
+
|
|
74
|
+
For native hosts, use the target key (`getHistoireTargetKey(target)` from `@histoire/protocol`) as `frame.key`; that is what `createSessionFrameService` uses, so `api.frames.open(context.frame)` reaches the frame your session mounted.
|
|
75
|
+
|
|
76
|
+
## Mounting views
|
|
77
|
+
|
|
78
|
+
Every view-bearing contribution has either a Vue `component` (rendered by a Vue app, see below) or a framework-neutral `mount`. Hosts mount the latter through the host, which owns lifecycle, update ordering and late disposal:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
const [tab] = host.available('inspector.tab', frameContext)
|
|
82
|
+
const view = host.mount(tab.id, container, frameContext, {
|
|
83
|
+
overlayRoot, // optional: popover root inside your theme scope
|
|
84
|
+
onError: (error, phase) => showErrorFor(tab, error),
|
|
85
|
+
renderState, // optional: (element, state | null) => void, your styled empty/loading/error states
|
|
86
|
+
})
|
|
87
|
+
await view.ready
|
|
88
|
+
view.update(nextFrameContext) // pushes a snapshot, never remounts
|
|
89
|
+
await view.dispose() // idempotent; also happens when the plugin or host retires
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
React host:
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
function PluginTab({ host, id, context }) {
|
|
96
|
+
const container = useRef(null)
|
|
97
|
+
const view = useRef()
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
view.current = host.mount(id, container.current, context)
|
|
100
|
+
return () => void view.current.dispose()
|
|
101
|
+
}, [host, id])
|
|
102
|
+
useEffect(() => view.current?.update(context), [context])
|
|
103
|
+
return <div ref={container} />
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Plain DOM host:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
for (const tab of host.available('inspector.tab', frameContext)) {
|
|
111
|
+
const container = panel.appendChild(document.createElement('section'))
|
|
112
|
+
host.mount(tab.id, container, frameContext)
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
A Vue `component` view mounted through `host.mount` rejects `ready`: render it with Vue (`HistoireExtensionOutlet`), or have the plugin ship a `vueView(Component)` from `@histoire/plugin-kit/vue/mount`, which creates an isolated app.
|
|
117
|
+
|
|
118
|
+
## Running actions
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
// Captures `context`; aborting `signal` (selection change) discards the result.
|
|
122
|
+
await host.run('acme:refresh', root.context, { signal: root.selectionSignal() })
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
`run` rejects with the action error (also recorded as a diagnostic) and resolves without a result when the plugin, the host or the signal retired first. Frame-menu children are addressed as `${itemId}:${childId}`.
|
|
126
|
+
|
|
127
|
+
## Shortcuts
|
|
128
|
+
|
|
129
|
+
`shortcut` contributions name a qualified command, keys (`mod` is command or control) and a scope. Hosts without a shortcut registry bind them through `createPluginShortcutBindings`, so registration rules and precedence match the workbench:
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import { createPluginShortcutBindings, isEditableTarget } from '@histoire/plugin-kit'
|
|
133
|
+
|
|
134
|
+
// Scopes this host routes keys to. Each committed binding is checked once: a scope the host
|
|
135
|
+
// lacks, an overlap with a built-in or with an earlier binding of the same scope rejects it with
|
|
136
|
+
// one `report` diagnostic (its command stays runnable).
|
|
137
|
+
const bindings = createPluginShortcutBindings(host, ['global'])
|
|
138
|
+
const available = new Set<string>()
|
|
139
|
+
|
|
140
|
+
element.addEventListener('keydown', (event) => {
|
|
141
|
+
if (event.defaultPrevented || isEditableTarget(event.target)) return
|
|
142
|
+
available.clear()
|
|
143
|
+
for (const shortcut of host.available('shortcut', root.context)) available.add(shortcut.id)
|
|
144
|
+
// Innermost focus scope first, e.g. ['frame', 'canvas', 'global'] inside a frame.
|
|
145
|
+
const shortcut = bindings.match(event, ['global'], candidate => available.has(candidate.id))
|
|
146
|
+
if (!shortcut) return
|
|
147
|
+
event.preventDefault()
|
|
148
|
+
void host.run(shortcut.command, root.context)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
// On teardown: bindings.dispose() (the host itself stays caller-owned).
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
`findPluginShortcutConflict(shortcut, { bound, scopes })` exposes the same check for hosts that keep their own registry.
|
|
155
|
+
|
|
156
|
+
## Diagnostics
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
host.diagnostics() // [{ pluginId, contributionId?, phase, error }]
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Failures are attributed and never retried: a failing setup, view, predicate or action affects only its own plugin or view.
|
|
163
|
+
|
|
164
|
+
## Vue hosts (`@histoire/vue`)
|
|
165
|
+
|
|
166
|
+
```vue
|
|
167
|
+
<HistoireProvider :session="session" :extensions="host">
|
|
168
|
+
<HistoireExplorer />
|
|
169
|
+
</HistoireProvider>
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
- `extensions` is caller-owned: the provider never disposes it and keeps it when `session` is replaced (scoped views of the old session unmount; the host stays).
|
|
173
|
+
- Native parts render plugin contributions after their built-ins: `ExplorerPanels` (inspector tabs and sections, same built-in tab descriptors as the standalone inspector) and `HistoireToolbar` (toolbar items after the built-ins of their `group`: `viewport`, `background`, `ui`; other groups and `plugins` trail after the built-in items).
|
|
174
|
+
- Plugin shortcuts are bound on the owning provider through `createPluginShortcutBindings` in the `global` (provider root), `canvas` (explorer canvas) and `frame` (its single-variant frame) scopes, innermost first. `comment` bindings have no native surface and are rejected with a diagnostic.
|
|
175
|
+
- Pending and failed plugin views, and the `@histoire/plugin-kit/vue` state and pane components, render native workbench state components, unless an enclosing app provides its own under `WORKBENCH_COMPONENTS_KEY`.
|
|
176
|
+
- Caller slots win: replacing `HistoireExplorer`'s `panels` or `toolbar` slot replaces the plugin contributions of that surface too.
|
|
177
|
+
- Nested providers without `extensions` reuse the nearest host for their surfaces; only the provider receiving the prop binds keys.
|
|
178
|
+
- `HistoireExtensionOutlet` places one kind anywhere inside a provider: `<HistoireExtensionOutlet kind="inspector.section" :filter="s => s.tab === 'footer'" />`. Inspector kinds default to the selected variant's context, others to the provider's root context.
|
|
179
|
+
- Plugin navigation inside a provider opens inspector tabs in its own explorer panels and selects stories through the session. Panes, settings sections and canvas views do not exist in native hosts: those requests are ignored with a warning, and contributions of these kinds stay registered but unrendered. Nothing touches the host page URL, title, theme or storage.
|
|
180
|
+
|
|
181
|
+
## Unsupported surfaces
|
|
182
|
+
|
|
183
|
+
A host renders only the surfaces it has. Contributions for others (rail panes, settings, home, canvas overlays/tools/views, menus) remain registered and visible through `host.contributions()`, are never mounted, and their capabilities (`canvas`, `devEvents`, `frames` without a session plugins API) are absent from `context.capabilities`. Accessing an absent service never throws.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { BuiltinInspectorTab } from './types/inspector.js';
|
|
2
|
+
/** Built-in inspector tab descriptor shared by the standalone inspector and native explorer panels. */
|
|
3
|
+
export interface BuiltinInspectorTabDescriptor {
|
|
4
|
+
/** Qualified id sections and navigation use. */
|
|
5
|
+
readonly id: BuiltinInspectorTab;
|
|
6
|
+
/** Legacy standalone `?tab=` value; Props is the empty value. */
|
|
7
|
+
readonly value: '' | 'docs' | 'events' | 'tests';
|
|
8
|
+
/** Visible label. */
|
|
9
|
+
readonly label: string;
|
|
10
|
+
}
|
|
11
|
+
/** Built-in inspector tabs in display order; plugin tabs follow them in every host. */
|
|
12
|
+
export declare const BUILTIN_INSPECTOR_TABS: readonly BuiltinInspectorTabDescriptor[];
|
|
13
|
+
//# sourceMappingURL=builtins.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builtins.d.ts","sourceRoot":"","sources":["../src/builtins.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAA;AAE/D,uGAAuG;AACvG,MAAM,WAAW,6BAA6B;IAC5C,gDAAgD;IAChD,QAAQ,CAAC,EAAE,EAAE,mBAAmB,CAAA;IAChC,iEAAiE;IACjE,QAAQ,CAAC,KAAK,EAAE,EAAE,GAAG,MAAM,GAAG,QAAQ,GAAG,OAAO,CAAA;IAChD,qBAAqB;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CACvB;AAED,uFAAuF;AACvF,eAAO,MAAM,sBAAsB,EAAE,SAAS,6BAA6B,EAK/B,CAAA"}
|
package/dist/builtins.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Built-in inspector tabs in display order; plugin tabs follow them in every host. */
|
|
2
|
+
export const BUILTIN_INSPECTOR_TABS = Object.freeze([
|
|
3
|
+
{ id: 'builtin:props', value: '', label: 'Props' },
|
|
4
|
+
{ id: 'builtin:docs', value: 'docs', label: 'Docs' },
|
|
5
|
+
{ id: 'builtin:events', value: 'events', label: 'Events' },
|
|
6
|
+
{ id: 'builtin:tests', value: 'tests', label: 'Tests' },
|
|
7
|
+
]);
|
package/dist/define.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { FramePluginDefinition } from './types/frame.js';
|
|
2
|
+
import type { WorkbenchPluginDefinition } from './types/plugin.js';
|
|
3
|
+
/** The only extension contract version. */
|
|
4
|
+
export declare const PLUGIN_API_VERSION = 1;
|
|
5
|
+
/**
|
|
6
|
+
* Declares the default export of a plugin `client` entry.
|
|
7
|
+
* Returns the definition unchanged after validating `id`, `apiVersion` and `setup`.
|
|
8
|
+
* @param definition Plugin identity and setup hook.
|
|
9
|
+
* @throws {PluginDefinitionError} When the definition is invalid.
|
|
10
|
+
*/
|
|
11
|
+
export declare function defineWorkbenchPlugin(definition: WorkbenchPluginDefinition): WorkbenchPluginDefinition;
|
|
12
|
+
/**
|
|
13
|
+
* Declares the default export of a plugin `frame` entry.
|
|
14
|
+
* Returns the definition unchanged after validating `id`, `apiVersion` and `attach`.
|
|
15
|
+
* @param definition Plugin identity and attach hook.
|
|
16
|
+
* @throws {PluginDefinitionError} When the definition is invalid.
|
|
17
|
+
*/
|
|
18
|
+
export declare function defineFramePlugin(definition: FramePluginDefinition): FramePluginDefinition;
|
|
19
|
+
//# sourceMappingURL=define.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define.d.ts","sourceRoot":"","sources":["../src/define.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAA;AAC7D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAA;AAIlE,2CAA2C;AAC3C,eAAO,MAAM,kBAAkB,IAAI,CAAA;AAsBnC;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,yBAAyB,GAAG,yBAAyB,CAGtG;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,qBAAqB,GAAG,qBAAqB,CAG1F"}
|
package/dist/define.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { PluginDefinitionError } from './errors.js';
|
|
2
|
+
import { assertPluginId } from './ids.js';
|
|
3
|
+
/** The only extension contract version. */
|
|
4
|
+
export const PLUGIN_API_VERSION = 1;
|
|
5
|
+
/**
|
|
6
|
+
* Validates fields shared by workbench and frame definitions plus the entry hook.
|
|
7
|
+
* Errors carry the plugin id once it is known to be valid.
|
|
8
|
+
* @param definition Untrusted definition value.
|
|
9
|
+
* @param hook Name of the required entry function (`setup` or `attach`).
|
|
10
|
+
*/
|
|
11
|
+
function validateDefinition(definition, hook) {
|
|
12
|
+
if (typeof definition !== 'object' || definition === null) {
|
|
13
|
+
throw new PluginDefinitionError(`plugin definition must be an object, received ${definition === null ? 'null' : typeof definition}`);
|
|
14
|
+
}
|
|
15
|
+
const { id, apiVersion } = definition;
|
|
16
|
+
assertPluginId(id);
|
|
17
|
+
if (apiVersion !== PLUGIN_API_VERSION) {
|
|
18
|
+
throw new PluginDefinitionError(`unsupported apiVersion ${JSON.stringify(apiVersion)}; expected ${PLUGIN_API_VERSION}`, id);
|
|
19
|
+
}
|
|
20
|
+
if (typeof definition[hook] !== 'function') {
|
|
21
|
+
throw new PluginDefinitionError(`missing \`${hook}\` function`, id);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Declares the default export of a plugin `client` entry.
|
|
26
|
+
* Returns the definition unchanged after validating `id`, `apiVersion` and `setup`.
|
|
27
|
+
* @param definition Plugin identity and setup hook.
|
|
28
|
+
* @throws {PluginDefinitionError} When the definition is invalid.
|
|
29
|
+
*/
|
|
30
|
+
export function defineWorkbenchPlugin(definition) {
|
|
31
|
+
validateDefinition(definition, 'setup');
|
|
32
|
+
return definition;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Declares the default export of a plugin `frame` entry.
|
|
36
|
+
* Returns the definition unchanged after validating `id`, `apiVersion` and `attach`.
|
|
37
|
+
* @param definition Plugin identity and attach hook.
|
|
38
|
+
* @throws {PluginDefinitionError} When the definition is invalid.
|
|
39
|
+
*/
|
|
40
|
+
export function defineFramePlugin(definition) {
|
|
41
|
+
validateDefinition(definition, 'attach');
|
|
42
|
+
return definition;
|
|
43
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Invalid plugin definition or id, attributed to the plugin when its id is known. */
|
|
2
|
+
export declare class PluginDefinitionError extends Error {
|
|
3
|
+
/** Offending plugin id, when it was readable. */
|
|
4
|
+
readonly pluginId: string | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* @param message Failure description without attribution prefix.
|
|
7
|
+
* @param pluginId Plugin the failure belongs to, if known.
|
|
8
|
+
*/
|
|
9
|
+
constructor(message: string, pluginId?: string);
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,sFAAsF;AACtF,qBAAa,qBAAsB,SAAQ,KAAK;IAC9C,iDAAiD;IACjD,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAA;IAErC;;;OAGG;IACH,YAAY,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAI7C;CACF"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Invalid plugin definition or id, attributed to the plugin when its id is known. */
|
|
2
|
+
export class PluginDefinitionError extends Error {
|
|
3
|
+
/** Offending plugin id, when it was readable. */
|
|
4
|
+
pluginId;
|
|
5
|
+
/**
|
|
6
|
+
* @param message Failure description without attribution prefix.
|
|
7
|
+
* @param pluginId Plugin the failure belongs to, if known.
|
|
8
|
+
*/
|
|
9
|
+
constructor(message, pluginId) {
|
|
10
|
+
super(pluginId === undefined ? `[histoire plugin] ${message}` : `[histoire plugin "${pluginId}"] ${message}`);
|
|
11
|
+
this.name = 'PluginDefinitionError';
|
|
12
|
+
this.pluginId = pluginId;
|
|
13
|
+
}
|
|
14
|
+
}
|
package/dist/frame.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { defineFramePlugin, PLUGIN_API_VERSION } from './define.js';
|
|
2
|
+
export { PluginDefinitionError } from './errors.js';
|
|
3
|
+
export type { PluginChannel, PluginChannelMessage } from './types/channel.js';
|
|
4
|
+
export type { Awaitable, Disposer } from './types/common.js';
|
|
5
|
+
export type { FramePluginContext, FramePluginDefinition, ReadonlyFrameState } from './types/frame.js';
|
|
6
|
+
//# sourceMappingURL=frame.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frame.d.ts","sourceRoot":"","sources":["../src/frame.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AACnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AACnD,YAAY,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AAC7E,YAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC5D,YAAY,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA"}
|
package/dist/frame.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ActionRun } from '../types/actions.js';
|
|
2
|
+
import type { Registry, RegistryEntry } from './registry.js';
|
|
3
|
+
/** Action resolved from a runnable id, with the registry entry owning it. */
|
|
4
|
+
export interface RunnableAction {
|
|
5
|
+
/** Owning registry entry (scope and attribution). */
|
|
6
|
+
entry: RegistryEntry;
|
|
7
|
+
/** Plugin action. */
|
|
8
|
+
run: ActionRun<unknown>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Resolves a runnable id: a contribution with a `run`, or a frame-menu child addressed as
|
|
12
|
+
* `${itemId}:${childId}` (for example `acme:export:json`). Local ids never contain `:`, so a
|
|
13
|
+
* child id can never collide with a contribution id. Children share their item's owner scope.
|
|
14
|
+
* @param registry Host registry.
|
|
15
|
+
* @param id Qualified contribution id or frame-menu child id.
|
|
16
|
+
*/
|
|
17
|
+
export declare function resolveRunnable(registry: Registry, id: string): RunnableAction | undefined;
|
|
18
|
+
/** Inputs of one action invocation. */
|
|
19
|
+
export interface RunActionOptions<C> {
|
|
20
|
+
/** Plugin action. */
|
|
21
|
+
run: ActionRun<C>;
|
|
22
|
+
/** Target scope captured when the invocation started. */
|
|
23
|
+
context: C;
|
|
24
|
+
/** Scopes whose retirement aborts the action (owner, host, caller scope). */
|
|
25
|
+
signals: ReadonlyArray<AbortSignal | undefined>;
|
|
26
|
+
/** Receives the action error when it settled while still current. */
|
|
27
|
+
onError: (error: unknown) => void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Framework-neutral action runner core. The action runs under a fresh scope linked to every
|
|
31
|
+
* given signal; the promise doubles as pending state for the invoking surface.
|
|
32
|
+
* Settlement after any linked scope retired is discarded: no error, no diagnostic.
|
|
33
|
+
* The action scope retires once the action settles, releasing parent listeners.
|
|
34
|
+
* @param options Action, captured context, linked signals and error sink.
|
|
35
|
+
*/
|
|
36
|
+
export declare function runAction<C>(options: RunActionOptions<C>): Promise<void>;
|
|
37
|
+
//# sourceMappingURL=actions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../../src/host/actions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAEpD,OAAO,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAG5D,6EAA6E;AAC7E,MAAM,WAAW,cAAc;IAC7B,qDAAqD;IACrD,KAAK,EAAE,aAAa,CAAA;IACpB,qBAAqB;IACrB,GAAG,EAAE,SAAS,CAAC,OAAO,CAAC,CAAA;CACxB;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS,CAY1F;AAED,uCAAuC;AACvC,MAAM,WAAW,gBAAgB,CAAC,CAAC;IACjC,qBAAqB;IACrB,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC,CAAA;IACjB,yDAAyD;IACzD,OAAO,EAAE,CAAC,CAAA;IACV,6EAA6E;IAC7E,OAAO,EAAE,aAAa,CAAC,WAAW,GAAG,SAAS,CAAC,CAAA;IAC/C,qEAAqE;IACrE,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAA;CAClC;AAED;;;;;;GAMG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAe9E"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createScope } from './generation.js';
|
|
2
|
+
/**
|
|
3
|
+
* Resolves a runnable id: a contribution with a `run`, or a frame-menu child addressed as
|
|
4
|
+
* `${itemId}:${childId}` (for example `acme:export:json`). Local ids never contain `:`, so a
|
|
5
|
+
* child id can never collide with a contribution id. Children share their item's owner scope.
|
|
6
|
+
* @param registry Host registry.
|
|
7
|
+
* @param id Qualified contribution id or frame-menu child id.
|
|
8
|
+
*/
|
|
9
|
+
export function resolveRunnable(registry, id) {
|
|
10
|
+
const entry = registry.get(id);
|
|
11
|
+
if (entry) {
|
|
12
|
+
const run = entry.contribution.run;
|
|
13
|
+
return typeof run === 'function' ? { entry, run: run } : undefined;
|
|
14
|
+
}
|
|
15
|
+
const separator = id.lastIndexOf(':');
|
|
16
|
+
const parent = separator > 0 ? registry.get(id.slice(0, separator)) : undefined;
|
|
17
|
+
if (parent?.contribution.kind !== 'frame-menu.item')
|
|
18
|
+
return undefined;
|
|
19
|
+
const childId = id.slice(separator + 1);
|
|
20
|
+
const child = parent.contribution.children?.find(item => item.id === childId);
|
|
21
|
+
return typeof child?.run === 'function' ? { entry: parent, run: child.run } : undefined;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Framework-neutral action runner core. The action runs under a fresh scope linked to every
|
|
25
|
+
* given signal; the promise doubles as pending state for the invoking surface.
|
|
26
|
+
* Settlement after any linked scope retired is discarded: no error, no diagnostic.
|
|
27
|
+
* The action scope retires once the action settles, releasing parent listeners.
|
|
28
|
+
* @param options Action, captured context, linked signals and error sink.
|
|
29
|
+
*/
|
|
30
|
+
export async function runAction(options) {
|
|
31
|
+
const scope = createScope(...options.signals);
|
|
32
|
+
if (!scope.active)
|
|
33
|
+
return;
|
|
34
|
+
try {
|
|
35
|
+
await options.run(options.context, { signal: scope.signal });
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
// A retired generation must not publish errors into the current one.
|
|
39
|
+
if (!scope.active)
|
|
40
|
+
return;
|
|
41
|
+
options.onError(error);
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
scope.retire();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ExtensionHost, ExtensionHostOptions } from '../types/host.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates the per-workbench extension host: installs plugins concurrently in config order,
|
|
4
|
+
* owns the registry, preferences and diagnostics, and tears everything down exactly once.
|
|
5
|
+
* Hosts never share state, even when created from the same definitions.
|
|
6
|
+
* @param options Plugins in config order and host services.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createExtensionHost(options: ExtensionHostOptions): ExtensionHost;
|
|
9
|
+
//# sourceMappingURL=create-host.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-host.d.ts","sourceRoot":"","sources":["../../src/host/create-host.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAuB,MAAM,kBAAkB,CAAA;AAoChG;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,oBAAoB,GAAG,aAAa,CAuJhF"}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { defineWorkbenchPlugin } from '../define.js';
|
|
2
|
+
import { PluginDefinitionError } from '../errors.js';
|
|
3
|
+
import { BUILTIN_NAMESPACE, parseQualifiedId } from '../ids.js';
|
|
4
|
+
import { resolveRunnable, runAction } from './actions.js';
|
|
5
|
+
import { createDiagnosticsLog } from './diagnostics.js';
|
|
6
|
+
import { createScope } from './generation.js';
|
|
7
|
+
import { installPlugin } from './install.js';
|
|
8
|
+
import { mountContribution } from './mount.js';
|
|
9
|
+
import { createPreferenceStore } from './preferences.js';
|
|
10
|
+
import { assertKnownKind, createRegistry, qualifyContribution } from './registry.js';
|
|
11
|
+
import { NOOP } from './resources.js';
|
|
12
|
+
/**
|
|
13
|
+
* Splits a config entry into definition and options.
|
|
14
|
+
* @param entry Bare definition or `{ definition, options }`.
|
|
15
|
+
*/
|
|
16
|
+
function normalizeEntry(entry) {
|
|
17
|
+
return typeof entry === 'object' && entry !== null && 'definition' in entry
|
|
18
|
+
? { definition: entry.definition, options: entry.options }
|
|
19
|
+
: { definition: entry, options: undefined };
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Best-effort plugin attribution for an invalid definition.
|
|
23
|
+
* @param error Validation error.
|
|
24
|
+
* @param definition Untrusted definition.
|
|
25
|
+
*/
|
|
26
|
+
function attributedId(error, definition) {
|
|
27
|
+
if (error instanceof PluginDefinitionError && error.pluginId !== undefined)
|
|
28
|
+
return error.pluginId;
|
|
29
|
+
const id = typeof definition === 'object' && definition !== null ? definition.id : undefined;
|
|
30
|
+
return typeof id === 'string' ? id : '<unknown>';
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates the per-workbench extension host: installs plugins concurrently in config order,
|
|
34
|
+
* owns the registry, preferences and diagnostics, and tears everything down exactly once.
|
|
35
|
+
* Hosts never share state, even when created from the same definitions.
|
|
36
|
+
* @param options Plugins in config order and host services.
|
|
37
|
+
*/
|
|
38
|
+
export function createExtensionHost(options) {
|
|
39
|
+
const { services } = options;
|
|
40
|
+
const listeners = new Set();
|
|
41
|
+
/** Notifies host subscribers; a throwing listener never blocks the others. */
|
|
42
|
+
function notify() {
|
|
43
|
+
for (const listener of [...listeners]) {
|
|
44
|
+
try {
|
|
45
|
+
listener();
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
queueMicrotask(() => {
|
|
49
|
+
throw error;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// Host generation: every plugin, built-in and action scope descends from it.
|
|
55
|
+
const scope = createScope();
|
|
56
|
+
const registry = createRegistry(notify);
|
|
57
|
+
const diagnostics = createDiagnosticsLog(notify);
|
|
58
|
+
let sequence = 0;
|
|
59
|
+
const internals = {
|
|
60
|
+
registry,
|
|
61
|
+
diagnostics,
|
|
62
|
+
scope,
|
|
63
|
+
context: services.context,
|
|
64
|
+
// Preference writes may flip toggle `pressed` states and badges: surfaces re-read on notification.
|
|
65
|
+
preferences: createPreferenceStore(services.storage, notify, { namespace: services.storageNamespace, signal: scope.signal }),
|
|
66
|
+
storageNamespace: services.storageNamespace,
|
|
67
|
+
nextSequence: () => sequence++,
|
|
68
|
+
// Plugin-internal state changes (`api.invalidate`) re-read badges, toggles and predicates.
|
|
69
|
+
notify,
|
|
70
|
+
};
|
|
71
|
+
const builtinIds = new Set();
|
|
72
|
+
/** Contributions whose `available` threw; hidden for good (no retry). */
|
|
73
|
+
const failedPredicates = new WeakSet();
|
|
74
|
+
const seen = new Set();
|
|
75
|
+
const plugins = [];
|
|
76
|
+
options.plugins.forEach((entry, index) => {
|
|
77
|
+
const { definition, options: pluginOptions } = normalizeEntry(entry);
|
|
78
|
+
try {
|
|
79
|
+
defineWorkbenchPlugin(definition);
|
|
80
|
+
if (seen.has(definition.id))
|
|
81
|
+
throw new PluginDefinitionError('duplicate plugin id; the first configured entry is kept', definition.id);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
diagnostics.record({ pluginId: attributedId(error, definition), phase: 'load', error });
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
seen.add(definition.id);
|
|
88
|
+
plugins.push(installPlugin(definition, pluginOptions, index, internals));
|
|
89
|
+
});
|
|
90
|
+
// Context changes may flip availability predicates: surfaces re-read on notification.
|
|
91
|
+
const stopContext = services.context.subscribe(notify);
|
|
92
|
+
const ready = Promise.all(plugins.map(plugin => plugin.settled)).then(() => { });
|
|
93
|
+
/**
|
|
94
|
+
* Evaluates `devOnly` and the `available` predicate of one contribution.
|
|
95
|
+
* @param contribution Committed contribution.
|
|
96
|
+
* @param context Surface context.
|
|
97
|
+
*/
|
|
98
|
+
function isAvailable(contribution, context) {
|
|
99
|
+
if (failedPredicates.has(contribution))
|
|
100
|
+
return false;
|
|
101
|
+
if (contribution.devOnly && context.source.mode !== 'dev')
|
|
102
|
+
return false;
|
|
103
|
+
if (!contribution.available)
|
|
104
|
+
return true;
|
|
105
|
+
try {
|
|
106
|
+
return Boolean(contribution.available(context));
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
failedPredicates.add(contribution);
|
|
110
|
+
diagnostics.record({ pluginId: contribution.pluginId, contributionId: contribution.id, phase: 'callback', error });
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Registers an app-owned `builtin:` contribution, committed and notified immediately.
|
|
116
|
+
* @param contribution Contribution with a local id.
|
|
117
|
+
*/
|
|
118
|
+
function registerBuiltin(contribution) {
|
|
119
|
+
if (!scope.active)
|
|
120
|
+
return NOOP;
|
|
121
|
+
assertKnownKind(contribution, BUILTIN_NAMESPACE);
|
|
122
|
+
const id = `${BUILTIN_NAMESPACE}:${String(contribution.id)}`;
|
|
123
|
+
if (!parseQualifiedId(id))
|
|
124
|
+
throw new Error(`[histoire] invalid built-in contribution id ${JSON.stringify(contribution.id)}`);
|
|
125
|
+
if (registry.get(id))
|
|
126
|
+
throw new Error(`[histoire] duplicate built-in contribution "${id}"`);
|
|
127
|
+
const order = typeof contribution.order === 'number' && Number.isFinite(contribution.order) ? contribution.order : 0;
|
|
128
|
+
const entry = {
|
|
129
|
+
contribution: qualifyContribution(contribution, BUILTIN_NAMESPACE, id),
|
|
130
|
+
rank: { builtin: true, order, configIndex: -1, sequence: sequence++ },
|
|
131
|
+
owner: scope.signal,
|
|
132
|
+
};
|
|
133
|
+
builtinIds.add(id);
|
|
134
|
+
registry.commit([entry]);
|
|
135
|
+
return () => {
|
|
136
|
+
// Identity check keeps a stale disposer from removing a re-registered built-in.
|
|
137
|
+
if (registry.get(id) !== entry)
|
|
138
|
+
return;
|
|
139
|
+
builtinIds.delete(id);
|
|
140
|
+
registry.remove([id]);
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
let disposal;
|
|
144
|
+
return {
|
|
145
|
+
ready,
|
|
146
|
+
contributions: kind => registry.contributions(kind),
|
|
147
|
+
available: (kind, context) => registry.contributions(kind).filter(contribution => isAvailable(contribution, context)),
|
|
148
|
+
registerBuiltin,
|
|
149
|
+
subscribe(listener) {
|
|
150
|
+
if (!scope.active)
|
|
151
|
+
return NOOP;
|
|
152
|
+
const entry = () => listener();
|
|
153
|
+
listeners.add(entry);
|
|
154
|
+
return () => listeners.delete(entry);
|
|
155
|
+
},
|
|
156
|
+
mount: (contributionId, container, context, mountOptions = {}) => mountContribution(registry.get(contributionId), contributionId, container, context, mountOptions, diagnostics.record),
|
|
157
|
+
reportViewError(contributionId, error, phase) {
|
|
158
|
+
const entry = registry.get(contributionId);
|
|
159
|
+
if (entry && scope.active)
|
|
160
|
+
diagnostics.record({ pluginId: entry.contribution.pluginId, contributionId, phase, error });
|
|
161
|
+
},
|
|
162
|
+
run(contributionId, context, runOptions) {
|
|
163
|
+
const runnable = resolveRunnable(registry, contributionId);
|
|
164
|
+
if (!runnable)
|
|
165
|
+
return Promise.reject(new Error(`[histoire] no runnable contribution "${contributionId}"`));
|
|
166
|
+
const { entry } = runnable;
|
|
167
|
+
return runAction({
|
|
168
|
+
run: runnable.run,
|
|
169
|
+
context,
|
|
170
|
+
// Plugin (or host) generation plus the caller's captured target scope.
|
|
171
|
+
signals: [entry.owner, runOptions?.signal],
|
|
172
|
+
onError: error => diagnostics.record({ pluginId: entry.contribution.pluginId, contributionId, phase: 'run', error }),
|
|
173
|
+
});
|
|
174
|
+
},
|
|
175
|
+
diagnostics: () => diagnostics.list(),
|
|
176
|
+
dispose() {
|
|
177
|
+
// Assigned before teardown so reentrant calls from plugin disposers return early.
|
|
178
|
+
if (disposal)
|
|
179
|
+
return disposal;
|
|
180
|
+
disposal = Promise.resolve();
|
|
181
|
+
stopContext();
|
|
182
|
+
// Retire first: no plugin can register while others tear down.
|
|
183
|
+
scope.retire();
|
|
184
|
+
registry.batch(() => {
|
|
185
|
+
for (const plugin of [...plugins].reverse())
|
|
186
|
+
plugin.dispose();
|
|
187
|
+
registry.remove([...builtinIds]);
|
|
188
|
+
builtinIds.clear();
|
|
189
|
+
});
|
|
190
|
+
listeners.clear();
|
|
191
|
+
return disposal;
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|