@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.
Files changed (162) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +182 -2
  3. package/dist/builtins.d.ts +13 -0
  4. package/dist/builtins.d.ts.map +1 -0
  5. package/dist/builtins.js +7 -0
  6. package/dist/define.d.ts +19 -0
  7. package/dist/define.d.ts.map +1 -0
  8. package/dist/define.js +43 -0
  9. package/dist/errors.d.ts +11 -0
  10. package/dist/errors.d.ts.map +1 -0
  11. package/dist/errors.js +14 -0
  12. package/dist/frame.d.ts +6 -0
  13. package/dist/frame.d.ts.map +1 -0
  14. package/dist/frame.js +3 -0
  15. package/dist/host/actions.d.ts +37 -0
  16. package/dist/host/actions.d.ts.map +1 -0
  17. package/dist/host/actions.js +47 -0
  18. package/dist/host/create-host.d.ts +9 -0
  19. package/dist/host/create-host.d.ts.map +1 -0
  20. package/dist/host/create-host.js +194 -0
  21. package/dist/host/diagnostics.d.ts +14 -0
  22. package/dist/host/diagnostics.d.ts.map +1 -0
  23. package/dist/host/diagnostics.js +26 -0
  24. package/dist/host/frames.d.ts +43 -0
  25. package/dist/host/frames.d.ts.map +1 -0
  26. package/dist/host/frames.js +63 -0
  27. package/dist/host/generation.d.ts +24 -0
  28. package/dist/host/generation.d.ts.map +1 -0
  29. package/dist/host/generation.js +38 -0
  30. package/dist/host/install.d.ts +43 -0
  31. package/dist/host/install.d.ts.map +1 -0
  32. package/dist/host/install.js +168 -0
  33. package/dist/host/manifest.d.ts +23 -0
  34. package/dist/host/manifest.d.ts.map +1 -0
  35. package/dist/host/manifest.js +37 -0
  36. package/dist/host/mount.d.ts +15 -0
  37. package/dist/host/mount.d.ts.map +1 -0
  38. package/dist/host/mount.js +57 -0
  39. package/dist/host/ordering.d.ts +20 -0
  40. package/dist/host/ordering.d.ts.map +1 -0
  41. package/dist/host/ordering.js +12 -0
  42. package/dist/host/plugin-api.d.ts +33 -0
  43. package/dist/host/plugin-api.d.ts.map +1 -0
  44. package/dist/host/plugin-api.js +113 -0
  45. package/dist/host/preferences.d.ts +57 -0
  46. package/dist/host/preferences.d.ts.map +1 -0
  47. package/dist/host/preferences.js +137 -0
  48. package/dist/host/registry.d.ts +43 -0
  49. package/dist/host/registry.d.ts.map +1 -0
  50. package/dist/host/registry.js +123 -0
  51. package/dist/host/resources.d.ts +20 -0
  52. package/dist/host/resources.d.ts.map +1 -0
  53. package/dist/host/resources.js +45 -0
  54. package/dist/ids.d.ts +32 -0
  55. package/dist/ids.d.ts.map +1 -0
  56. package/dist/ids.js +51 -0
  57. package/dist/index.d.ts +27 -0
  58. package/dist/index.d.ts.map +1 -0
  59. package/dist/index.js +16 -0
  60. package/dist/react/index.d.ts +19 -0
  61. package/dist/react/index.d.ts.map +1 -0
  62. package/dist/react/index.js +25 -0
  63. package/dist/render/keys.d.ts +7 -0
  64. package/dist/render/keys.d.ts.map +1 -0
  65. package/dist/render/keys.js +15 -0
  66. package/dist/render/mount-manager.d.ts +36 -0
  67. package/dist/render/mount-manager.d.ts.map +1 -0
  68. package/dist/render/mount-manager.js +128 -0
  69. package/dist/render/view-binding.d.ts +19 -0
  70. package/dist/render/view-binding.d.ts.map +1 -0
  71. package/dist/render/view-binding.js +7 -0
  72. package/dist/render/workbench-components.d.ts +39 -0
  73. package/dist/render/workbench-components.d.ts.map +1 -0
  74. package/dist/render/workbench-components.js +76 -0
  75. package/dist/session/context.d.ts +66 -0
  76. package/dist/session/context.d.ts.map +1 -0
  77. package/dist/session/context.js +173 -0
  78. package/dist/session/frames.d.ts +46 -0
  79. package/dist/session/frames.d.ts.map +1 -0
  80. package/dist/session/frames.js +68 -0
  81. package/dist/shortcuts.d.ts +94 -0
  82. package/dist/shortcuts.d.ts.map +1 -0
  83. package/dist/shortcuts.js +157 -0
  84. package/dist/styles.d.ts +3 -0
  85. package/dist/styles.d.ts.map +1 -0
  86. package/dist/styles.js +25 -0
  87. package/dist/svelte4/index.d.ts +27 -0
  88. package/dist/svelte4/index.d.ts.map +1 -0
  89. package/dist/svelte4/index.js +16 -0
  90. package/dist/svelte5/index.d.ts +21 -0
  91. package/dist/svelte5/index.d.ts.map +1 -0
  92. package/dist/svelte5/index.js +40 -0
  93. package/dist/types/actions.d.ts +12 -0
  94. package/dist/types/actions.d.ts.map +1 -0
  95. package/dist/types/actions.js +1 -0
  96. package/dist/types/base.d.ts +12 -0
  97. package/dist/types/base.d.ts.map +1 -0
  98. package/dist/types/base.js +1 -0
  99. package/dist/types/canvas.d.ts +150 -0
  100. package/dist/types/canvas.d.ts.map +1 -0
  101. package/dist/types/canvas.js +1 -0
  102. package/dist/types/channel.d.ts +28 -0
  103. package/dist/types/channel.d.ts.map +1 -0
  104. package/dist/types/channel.js +1 -0
  105. package/dist/types/commands.d.ts +35 -0
  106. package/dist/types/commands.d.ts.map +1 -0
  107. package/dist/types/commands.js +1 -0
  108. package/dist/types/common.d.ts +7 -0
  109. package/dist/types/common.d.ts.map +1 -0
  110. package/dist/types/common.js +1 -0
  111. package/dist/types/context.d.ts +72 -0
  112. package/dist/types/context.d.ts.map +1 -0
  113. package/dist/types/context.js +1 -0
  114. package/dist/types/contribution.d.ts +56 -0
  115. package/dist/types/contribution.d.ts.map +1 -0
  116. package/dist/types/contribution.js +1 -0
  117. package/dist/types/frame.d.ts +41 -0
  118. package/dist/types/frame.d.ts.map +1 -0
  119. package/dist/types/frame.js +1 -0
  120. package/dist/types/host.d.ts +123 -0
  121. package/dist/types/host.d.ts.map +1 -0
  122. package/dist/types/host.js +1 -0
  123. package/dist/types/index.d.ts +19 -0
  124. package/dist/types/index.d.ts.map +1 -0
  125. package/dist/types/index.js +1 -0
  126. package/dist/types/inspector.d.ts +38 -0
  127. package/dist/types/inspector.d.ts.map +1 -0
  128. package/dist/types/inspector.js +1 -0
  129. package/dist/types/menus.d.ts +71 -0
  130. package/dist/types/menus.d.ts.map +1 -0
  131. package/dist/types/menus.js +1 -0
  132. package/dist/types/plugin.d.ts +133 -0
  133. package/dist/types/plugin.d.ts.map +1 -0
  134. package/dist/types/plugin.js +1 -0
  135. package/dist/types/preferences.d.ts +31 -0
  136. package/dist/types/preferences.d.ts.map +1 -0
  137. package/dist/types/preferences.js +1 -0
  138. package/dist/types/services.d.ts +175 -0
  139. package/dist/types/services.d.ts.map +1 -0
  140. package/dist/types/services.js +1 -0
  141. package/dist/types/shell.d.ts +47 -0
  142. package/dist/types/shell.d.ts.map +1 -0
  143. package/dist/types/shell.js +1 -0
  144. package/dist/types/toolbar.d.ts +59 -0
  145. package/dist/types/toolbar.d.ts.map +1 -0
  146. package/dist/types/toolbar.js +1 -0
  147. package/dist/types/view.d.ts +82 -0
  148. package/dist/types/view.d.ts.map +1 -0
  149. package/dist/types/view.js +1 -0
  150. package/dist/vue/components.d.ts +122 -0
  151. package/dist/vue/components.d.ts.map +1 -0
  152. package/dist/vue/components.js +112 -0
  153. package/dist/vue/index.d.ts +21 -0
  154. package/dist/vue/index.d.ts.map +1 -0
  155. package/dist/vue/index.js +47 -0
  156. package/dist/vue/mount.d.ts +9 -0
  157. package/dist/vue/mount.d.ts.map +1 -0
  158. package/dist/vue/mount.js +32 -0
  159. package/dist/vue/panel-action.d.ts +60 -0
  160. package/dist/vue/panel-action.d.ts.map +1 -0
  161. package/dist/vue/panel-action.js +28 -0
  162. package/package.json +92 -3
@@ -0,0 +1,33 @@
1
+ import type { HistoireJsonValue } from '@histoire/protocol';
2
+ import type { Disposer } from '../types/common.js';
3
+ import type { WorkbenchContext } from '../types/context.js';
4
+ import type { WorkbenchContribution } from '../types/contribution.js';
5
+ import type { WorkbenchPluginApi } from '../types/plugin.js';
6
+ import type { PluginPreferences } from '../types/preferences.js';
7
+ /** Inputs of one plugin's API object. */
8
+ export interface PluginApiOptions {
9
+ /** Plugin id. */
10
+ id: string;
11
+ /** Browser-visible options. */
12
+ options: HistoireJsonValue | undefined;
13
+ /** Root context; wrapped so subscriptions are plugin-owned. */
14
+ context: WorkbenchContext;
15
+ /** Plugin preferences. */
16
+ preferences: PluginPreferences;
17
+ /** Plugin scope signal. */
18
+ signal: AbortSignal;
19
+ /** Owned registration. */
20
+ contribute: (contribution: WorkbenchContribution) => Disposer;
21
+ /** Attributed error report. */
22
+ reportError: (error: unknown) => void;
23
+ /** Ties a resource to the plugin lifetime. */
24
+ track: (release: () => void) => Disposer;
25
+ /** Notifies host subscribers so surfaces re-read plugin callbacks; inert after retirement. */
26
+ invalidate: () => void;
27
+ }
28
+ /**
29
+ * Builds `WorkbenchPluginApi`; typed helpers are sugar over `contribute`.
30
+ * @param options Plugin identity, services and ownership hooks.
31
+ */
32
+ export declare function createPluginApi(options: PluginApiOptions): WorkbenchPluginApi;
33
+ //# sourceMappingURL=plugin-api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin-api.d.ts","sourceRoot":"","sources":["../../src/host/plugin-api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAkB,MAAM,oBAAoB,CAAA;AAC3E,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AAC3D,OAAO,KAAK,EAAoB,qBAAqB,EAAE,MAAM,0BAA0B,CAAA;AACvF,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;AAC5D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAA;AAKhE,yCAAyC;AACzC,MAAM,WAAW,gBAAgB;IAC/B,iBAAiB;IACjB,EAAE,EAAE,MAAM,CAAA;IACV,+BAA+B;IAC/B,OAAO,EAAE,iBAAiB,GAAG,SAAS,CAAA;IACtC,+DAA+D;IAC/D,OAAO,EAAE,gBAAgB,CAAA;IACzB,0BAA0B;IAC1B,WAAW,EAAE,iBAAiB,CAAA;IAC9B,2BAA2B;IAC3B,MAAM,EAAE,WAAW,CAAA;IACnB,0BAA0B;IAC1B,UAAU,EAAE,CAAC,YAAY,EAAE,qBAAqB,KAAK,QAAQ,CAAA;IAC7D,+BAA+B;IAC/B,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAA;IACrC,8CAA8C;IAC9C,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,IAAI,KAAK,QAAQ,CAAA;IACxC,8FAA8F;IAC9F,UAAU,EAAE,MAAM,IAAI,CAAA;CACvB;AA4ED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,gBAAgB,GAAG,kBAAkB,CAoC7E"}
@@ -0,0 +1,113 @@
1
+ import { createPluginFrames } from './frames.js';
2
+ import { NOOP } from './resources.js';
3
+ /**
4
+ * Wraps the root context so `subscribe` (and `catalog.subscribe`, `tests.subscribe`) are owned
5
+ * by the plugin and released with it.
6
+ * The proxy targets an empty object so frozen contexts never violate proxy invariants;
7
+ * every other property reads through to the live context (getters stay fresh).
8
+ * @param context Root context.
9
+ * @param signal Plugin scope signal; subscriptions after retirement are refused.
10
+ * @param track Ties a subscription to the plugin lifetime.
11
+ */
12
+ function createOwnedContext(context, signal, track) {
13
+ const subscribe = listener => signal.aborted ? NOOP : track(context.subscribe(listener));
14
+ // Owned replacements of root context fields; every other field reads through.
15
+ const owned = new Map([
16
+ ['subscribe', subscribe],
17
+ ['catalog', createOwnedCatalog(context.catalog, signal, track)],
18
+ ['tests', context.tests && createOwnedTests(context.tests, signal, track)],
19
+ ]);
20
+ return new Proxy({}, {
21
+ get: (_, key) => owned.has(key) ? owned.get(key) : Reflect.get(context, key, context),
22
+ has: (_, key) => Reflect.has(context, key),
23
+ ownKeys: () => Reflect.ownKeys(context),
24
+ getOwnPropertyDescriptor(_, key) {
25
+ const descriptor = Reflect.getOwnPropertyDescriptor(context, key);
26
+ if (!descriptor)
27
+ return undefined;
28
+ if (owned.has(key))
29
+ return { value: owned.get(key), writable: false, enumerable: descriptor.enumerable, configurable: true };
30
+ return { ...descriptor, configurable: true };
31
+ },
32
+ });
33
+ }
34
+ /**
35
+ * Wraps the catalog service so its publication subscriptions are owned by the plugin.
36
+ * @param catalog Host catalog service.
37
+ * @param signal Plugin scope signal; subscriptions after retirement are refused.
38
+ * @param track Ties a subscription to the plugin lifetime.
39
+ */
40
+ function createOwnedCatalog(catalog, signal, track) {
41
+ const { subscribe } = catalog;
42
+ return Object.freeze({
43
+ list: () => catalog.list(),
44
+ getStory: (storyId) => catalog.getStory(storyId),
45
+ search: (query) => catalog.search(query),
46
+ // Hosts that cannot observe publications keep `subscribe` absent.
47
+ ...(subscribe ? { subscribe: (listener) => signal.aborted ? NOOP : track(subscribe.call(catalog, listener)) } : {}),
48
+ });
49
+ }
50
+ /**
51
+ * Wraps the tests service so its subscriptions are owned by the plugin; getters read through.
52
+ * @param tests Host tests service.
53
+ * @param signal Plugin scope signal; subscriptions after retirement are refused.
54
+ * @param track Ties a subscription to the plugin lifetime.
55
+ */
56
+ function createOwnedTests(tests, signal, track) {
57
+ return Object.freeze({
58
+ entries: () => tests.entries(),
59
+ get running() {
60
+ return tests.running;
61
+ },
62
+ get canRun() {
63
+ return tests.canRun;
64
+ },
65
+ get progress() {
66
+ return tests.progress;
67
+ },
68
+ get error() {
69
+ return tests.error;
70
+ },
71
+ runAll: () => tests.runAll(),
72
+ run: (target) => tests.run(target),
73
+ subscribe: (listener) => signal.aborted ? NOOP : track(tests.subscribe(listener)),
74
+ });
75
+ }
76
+ /**
77
+ * Builds `WorkbenchPluginApi`; typed helpers are sugar over `contribute`.
78
+ * @param options Plugin identity, services and ownership hooks.
79
+ */
80
+ export function createPluginApi(options) {
81
+ const { contribute } = options;
82
+ /**
83
+ * Helper adding contributions of one kind; `WorkbenchPluginApi` types each input shape.
84
+ * @param kind Contribution kind stamped on each input.
85
+ */
86
+ const add = (kind) => (input) => contribute({ ...input, kind });
87
+ return {
88
+ id: options.id,
89
+ options: options.options,
90
+ context: createOwnedContext(options.context, options.signal, options.track),
91
+ preferences: options.preferences,
92
+ signal: options.signal,
93
+ contribute,
94
+ inspector: { addTab: add('inspector.tab'), addSection: add('inspector.section') },
95
+ toolbar: { addItem: add('toolbar.item') },
96
+ commands: { add: add('command') },
97
+ shortcuts: { add: add('shortcut') },
98
+ menus: { addFrameAction: add('frame-menu.item'), addContextAction: add('context-menu.item') },
99
+ canvas: {
100
+ addOverlay: add('canvas.overlay'),
101
+ addFrameOverlay: add('canvas.frame-overlay'),
102
+ addFrameBadge: add('canvas.frame-badge'),
103
+ addTool: add('canvas.tool'),
104
+ addView: add('canvas.view'),
105
+ },
106
+ shell: { addPane: add('shell.pane') },
107
+ settings: { addSection: add('settings.section') },
108
+ home: { addSection: add('home.section') },
109
+ frames: createPluginFrames(options),
110
+ reportError: options.reportError,
111
+ invalidate: options.invalidate,
112
+ };
113
+ }
@@ -0,0 +1,57 @@
1
+ import type { HistoireJsonValue } from '@histoire/protocol';
2
+ import type { Disposer } from '../types/common.js';
3
+ import type { PluginPreferences, StorageLike } from '../types/preferences.js';
4
+ /** Listener of one fully namespaced preference key; never throws (errors are attributed). */
5
+ type PreferenceListener = (value: HistoireJsonValue) => void;
6
+ /**
7
+ * Host-wide preference backend over injected storage. A per-host memory overlay holds
8
+ * values when storage is absent or a write fails, so reads stay consistent and two hosts
9
+ * without storage never see each other's values.
10
+ */
11
+ export interface PreferenceStore {
12
+ /** Raw value of a full key, or `null`. */
13
+ read: (key: string) => string | null;
14
+ /** Writes a raw value of a full key. */
15
+ write: (key: string, raw: string) => void;
16
+ /** Listeners by full key. */
17
+ readonly listeners: Map<string, Set<PreferenceListener>>;
18
+ /** Host-level notification after any write (toolbar toggles and badges re-read plugin state). */
19
+ readonly changed: () => void;
20
+ }
21
+ /** Options of {@link createPreferenceStore}. */
22
+ export interface PreferenceStoreOptions {
23
+ /** Project namespace; external changes outside `<namespace>:plugin:` are ignored. */
24
+ namespace?: string;
25
+ /** Host lifetime; the storage change subscription is released when it aborts. */
26
+ signal?: AbortSignal;
27
+ }
28
+ /**
29
+ * Creates the preference backend of one host.
30
+ * @param storage Injected storage; memory only when absent.
31
+ * @param changed Called after every write, after key subscribers.
32
+ * @param options Namespace and lifetime used to observe changes made outside this host.
33
+ */
34
+ export declare function createPreferenceStore(storage: StorageLike | undefined, changed?: () => void, options?: PreferenceStoreOptions): PreferenceStore;
35
+ /** Inputs of one plugin's preferences. */
36
+ export interface PluginPreferencesOptions {
37
+ /** Host preference backend. */
38
+ store: PreferenceStore;
39
+ /** Owning plugin. */
40
+ pluginId: string;
41
+ /** Project namespace prepended to every key. */
42
+ namespace: string | undefined;
43
+ /** Whether the plugin generation is still current; writes from stale generations are dropped. */
44
+ isActive: () => boolean;
45
+ /** Ties a subscription to the plugin lifetime. */
46
+ track: (release: () => void) => Disposer;
47
+ /** Receives listener errors. */
48
+ onError: (error: unknown) => void;
49
+ }
50
+ /**
51
+ * Creates `api.preferences` for one plugin: JSON values under `plugin:<id>:`, optionally
52
+ * prefixed by the project namespace.
53
+ * @param options Backend, ownership and attribution.
54
+ */
55
+ export declare function createPluginPreferences(options: PluginPreferencesOptions): PluginPreferences;
56
+ export {};
57
+ //# sourceMappingURL=preferences.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"preferences.d.ts","sourceRoot":"","sources":["../../src/host/preferences.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAC3D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,KAAK,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAE7E,6FAA6F;AAC7F,KAAK,kBAAkB,GAAG,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAA;AAE5D;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,0CAA0C;IAC1C,IAAI,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAA;IACpC,wCAAwC;IACxC,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;IACzC,6BAA6B;IAC7B,QAAQ,CAAC,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,kBAAkB,CAAC,CAAC,CAAA;IACxD,iGAAiG;IACjG,QAAQ,CAAC,OAAO,EAAE,MAAM,IAAI,CAAA;CAC7B;AAED,gDAAgD;AAChD,MAAM,WAAW,sBAAsB;IACrC,qFAAqF;IACrF,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,iFAAiF;IACjF,MAAM,CAAC,EAAE,WAAW,CAAA;CACrB;AAUD;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,WAAW,GAAG,SAAS,EAAE,OAAO,GAAE,MAAM,IAAe,EAAE,OAAO,GAAE,sBAA2B,GAAG,eAAe,CAmE7J;AAED,0CAA0C;AAC1C,MAAM,WAAW,wBAAwB;IACvC,+BAA+B;IAC/B,KAAK,EAAE,eAAe,CAAA;IACtB,qBAAqB;IACrB,QAAQ,EAAE,MAAM,CAAA;IAChB,gDAAgD;IAChD,SAAS,EAAE,MAAM,GAAG,SAAS,CAAA;IAC7B,iGAAiG;IACjG,QAAQ,EAAE,MAAM,OAAO,CAAA;IACvB,kDAAkD;IAClD,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,IAAI,KAAK,QAAQ,CAAA;IACxC,gCAAgC;IAChC,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAA;CAClC;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,wBAAwB,GAAG,iBAAiB,CA2C5F"}
@@ -0,0 +1,137 @@
1
+ /**
2
+ * Key prefix shared by every plugin preference of one project namespace.
3
+ * @param namespace Project namespace, if any.
4
+ */
5
+ function preferencePrefix(namespace) {
6
+ return `${namespace ? `${namespace}:` : ''}plugin:`;
7
+ }
8
+ /**
9
+ * Creates the preference backend of one host.
10
+ * @param storage Injected storage; memory only when absent.
11
+ * @param changed Called after every write, after key subscribers.
12
+ * @param options Namespace and lifetime used to observe changes made outside this host.
13
+ */
14
+ export function createPreferenceStore(storage, changed = () => { }, options = {}) {
15
+ const memory = new Map();
16
+ const listeners = new Map();
17
+ const prefix = preferencePrefix(options.namespace);
18
+ /**
19
+ * Reads a raw value, the memory overlay first.
20
+ * @param key Full key.
21
+ */
22
+ function read(key) {
23
+ if (memory.has(key))
24
+ return memory.get(key);
25
+ try {
26
+ return storage?.getItem(key) ?? null;
27
+ }
28
+ catch {
29
+ return null;
30
+ }
31
+ }
32
+ /**
33
+ * Applies a change made outside this host (another tab): key subscribers receive the stored
34
+ * value, then host subscribers re-read. Removed or unreadable values only notify the host,
35
+ * since subscribers cannot know the fallback their `get` would use.
36
+ * @param key Changed full key, or `null` when the whole storage was cleared.
37
+ */
38
+ function external(key) {
39
+ if (key !== null && !key.startsWith(prefix))
40
+ return;
41
+ // The other writer's value supersedes values this host could not persist.
42
+ if (key === null)
43
+ memory.clear();
44
+ else
45
+ memory.delete(key);
46
+ for (const fullKey of key === null ? [...listeners.keys()] : [key]) {
47
+ const raw = read(fullKey);
48
+ if (raw === null)
49
+ continue;
50
+ let value;
51
+ try {
52
+ value = JSON.parse(raw);
53
+ }
54
+ catch {
55
+ continue;
56
+ }
57
+ for (const listener of [...listeners.get(fullKey) ?? []])
58
+ listener(value);
59
+ }
60
+ changed();
61
+ }
62
+ const { signal } = options;
63
+ if (storage?.subscribe && signal && !signal.aborted) {
64
+ const stop = storage.subscribe(external);
65
+ signal.addEventListener('abort', () => stop(), { once: true });
66
+ }
67
+ return {
68
+ read,
69
+ write(key, raw) {
70
+ try {
71
+ if (!storage)
72
+ throw new Error('no storage');
73
+ storage.setItem(key, raw);
74
+ memory.delete(key);
75
+ }
76
+ catch {
77
+ // Unavailable or full storage: keep the value for this host's lifetime.
78
+ memory.set(key, raw);
79
+ }
80
+ },
81
+ listeners,
82
+ changed,
83
+ };
84
+ }
85
+ /**
86
+ * Creates `api.preferences` for one plugin: JSON values under `plugin:<id>:`, optionally
87
+ * prefixed by the project namespace.
88
+ * @param options Backend, ownership and attribution.
89
+ */
90
+ export function createPluginPreferences(options) {
91
+ const { store, pluginId, namespace, isActive, track, onError } = options;
92
+ const prefix = `${preferencePrefix(namespace)}${pluginId}:`;
93
+ return {
94
+ get(key, fallback) {
95
+ const raw = store.read(prefix + key);
96
+ if (raw === null)
97
+ return fallback;
98
+ try {
99
+ return JSON.parse(raw);
100
+ }
101
+ catch {
102
+ return fallback;
103
+ }
104
+ },
105
+ set(key, value) {
106
+ if (!isActive())
107
+ return;
108
+ const fullKey = prefix + key;
109
+ store.write(fullKey, JSON.stringify(value));
110
+ for (const listener of [...store.listeners.get(fullKey) ?? []])
111
+ listener(value);
112
+ store.changed();
113
+ },
114
+ subscribe(key, listener) {
115
+ const fullKey = prefix + key;
116
+ let listeners = store.listeners.get(fullKey);
117
+ if (!listeners)
118
+ store.listeners.set(fullKey, listeners = new Set());
119
+ // Wrapper keeps duplicate subscriptions of the same function independent and attributes
120
+ // failures to this plugin, whichever host or tab wrote the value.
121
+ const entry = (value) => {
122
+ try {
123
+ listener(value);
124
+ }
125
+ catch (error) {
126
+ onError(error);
127
+ }
128
+ };
129
+ listeners.add(entry);
130
+ return track(() => {
131
+ listeners.delete(entry);
132
+ if (!listeners.size && store.listeners.get(fullKey) === listeners)
133
+ store.listeners.delete(fullKey);
134
+ });
135
+ },
136
+ };
137
+ }
@@ -0,0 +1,43 @@
1
+ import type { ContributionKind, QualifiedContribution, WorkbenchContribution } from '../types/contribution.js';
2
+ import type { ContributionRank } from './ordering.js';
3
+ /** One committed or provisional registration. */
4
+ export interface RegistryEntry {
5
+ /** Frozen qualified contribution exposed to surfaces. */
6
+ readonly contribution: QualifiedContribution;
7
+ /** Sort key. */
8
+ readonly rank: ContributionRank;
9
+ /** Owner scope signal; actions run under it. */
10
+ readonly owner: AbortSignal;
11
+ }
12
+ /** Per-host contribution registry keyed by qualified id, grouped by kind. */
13
+ export interface Registry {
14
+ /** Committed entry by qualified id. */
15
+ get: (id: string) => RegistryEntry | undefined;
16
+ /** Frozen ordered snapshot of one kind. */
17
+ contributions: <K extends ContributionKind>(kind: K) => ReadonlyArray<QualifiedContribution<K>>;
18
+ /** Exposes entries atomically; notifies once. Ids must be unique (checked by the owner). */
19
+ commit: (entries: ReadonlyArray<RegistryEntry>) => void;
20
+ /** Removes committed entries; notifies once if any was present. */
21
+ remove: (ids: Iterable<string>) => void;
22
+ /** Runs `fn` with notifications deferred to a single one after it returns. */
23
+ batch: (fn: () => void) => void;
24
+ }
25
+ /**
26
+ * Validates a raw contribution and builds its frozen qualified form.
27
+ * @param contribution Contribution as passed by a plugin or the app.
28
+ * @param namespace Plugin id, or `builtin`.
29
+ * @param qualifiedId Already validated `${namespace}:${id}`.
30
+ */
31
+ export declare function qualifyContribution(contribution: WorkbenchContribution, namespace: string, qualifiedId: string): QualifiedContribution;
32
+ /**
33
+ * Throws unless `contribution` is an object of a known kind.
34
+ * @param contribution Untrusted contribution value.
35
+ * @param pluginId Owner used for attribution.
36
+ */
37
+ export declare function assertKnownKind(contribution: unknown, pluginId: string): asserts contribution is WorkbenchContribution;
38
+ /**
39
+ * Creates an empty registry.
40
+ * @param notify Called after each commit or removal that changed the registry.
41
+ */
42
+ export declare function createRegistry(notify: () => void): Registry;
43
+ //# sourceMappingURL=registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/host/registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAA;AAC9G,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAA;AA2BrD,iDAAiD;AACjD,MAAM,WAAW,aAAa;IAC5B,yDAAyD;IACzD,QAAQ,CAAC,YAAY,EAAE,qBAAqB,CAAA;IAC5C,gBAAgB;IAChB,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAA;IAC/B,gDAAgD;IAChD,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAA;CAC5B;AAED,6EAA6E;AAC7E,MAAM,WAAW,QAAQ;IACvB,uCAAuC;IACvC,GAAG,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,aAAa,GAAG,SAAS,CAAA;IAC9C,2CAA2C;IAC3C,aAAa,EAAE,CAAC,CAAC,SAAS,gBAAgB,EAAE,IAAI,EAAE,CAAC,KAAK,aAAa,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,CAAA;IAC/F,4FAA4F;IAC5F,MAAM,EAAE,CAAC,OAAO,EAAE,aAAa,CAAC,aAAa,CAAC,KAAK,IAAI,CAAA;IACvD,mEAAmE;IACnE,MAAM,EAAE,CAAC,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,KAAK,IAAI,CAAA;IACvC,8EAA8E;IAC9E,KAAK,EAAE,CAAC,EAAE,EAAE,MAAM,IAAI,KAAK,IAAI,CAAA;CAChC;AAKD;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,YAAY,EAAE,qBAAqB,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,qBAAqB,CAYtI;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,IAAI,qBAAqB,CAKtH;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,IAAI,GAAG,QAAQ,CA8D3D"}
@@ -0,0 +1,123 @@
1
+ import { PluginDefinitionError } from '../errors.js';
2
+ import { parseQualifiedId } from '../ids.js';
3
+ import { compareRanks } from './ordering.js';
4
+ /**
5
+ * Runtime list of contribution kinds. `satisfies` keeps it in sync with `ContributionKindMap`:
6
+ * adding a kind to the type map fails compilation until it is listed here.
7
+ */
8
+ const KINDS = {
9
+ 'inspector.tab': true,
10
+ 'inspector.section': true,
11
+ 'toolbar.item': true,
12
+ 'command': true,
13
+ 'shortcut': true,
14
+ 'frame-menu.item': true,
15
+ 'context-menu.item': true,
16
+ 'canvas.overlay': true,
17
+ 'canvas.frame-overlay': true,
18
+ 'canvas.frame-badge': true,
19
+ 'canvas.tool': true,
20
+ 'canvas.view': true,
21
+ 'shell.pane': true,
22
+ 'settings.section': true,
23
+ 'home.section': true,
24
+ };
25
+ /** Shared frozen empty snapshot. */
26
+ const EMPTY = Object.freeze([]);
27
+ /**
28
+ * Validates a raw contribution and builds its frozen qualified form.
29
+ * @param contribution Contribution as passed by a plugin or the app.
30
+ * @param namespace Plugin id, or `builtin`.
31
+ * @param qualifiedId Already validated `${namespace}:${id}`.
32
+ */
33
+ export function qualifyContribution(contribution, namespace, qualifiedId) {
34
+ const qualified = { ...contribution, id: qualifiedId, pluginId: namespace, localId: contribution.id };
35
+ if ((qualified.kind === 'shell.pane' || qualified.kind === 'inspector.tab') && qualified.primaryCommand !== undefined) {
36
+ // Relations may precede their command registration, but must stay inside the owner namespace.
37
+ const input = qualified.primaryCommand;
38
+ const command = typeof input === 'string' ? parseQualifiedId(input.includes(':') ? input : `${namespace}:${input}`) : null;
39
+ if (!command || command.namespace !== namespace) {
40
+ throw new PluginDefinitionError(`invalid primary command ${JSON.stringify(input)}; expected a command id owned by "${namespace}"`, namespace);
41
+ }
42
+ qualified.primaryCommand = `${namespace}:${command.localId}`;
43
+ }
44
+ return Object.freeze(qualified);
45
+ }
46
+ /**
47
+ * Throws unless `contribution` is an object of a known kind.
48
+ * @param contribution Untrusted contribution value.
49
+ * @param pluginId Owner used for attribution.
50
+ */
51
+ export function assertKnownKind(contribution, pluginId) {
52
+ const kind = typeof contribution === 'object' && contribution !== null ? contribution.kind : undefined;
53
+ if (typeof kind !== 'string' || !Object.hasOwn(KINDS, kind)) {
54
+ throw new PluginDefinitionError(`unknown contribution kind ${JSON.stringify(kind)}`, pluginId);
55
+ }
56
+ }
57
+ /**
58
+ * Creates an empty registry.
59
+ * @param notify Called after each commit or removal that changed the registry.
60
+ */
61
+ export function createRegistry(notify) {
62
+ const entries = new Map();
63
+ const snapshots = new Map();
64
+ let batchDepth = 0;
65
+ let dirty = false;
66
+ /**
67
+ * Rebuilds snapshots of changed kinds so unchanged kinds keep their array identity.
68
+ * @param kinds Kinds touched by the change.
69
+ */
70
+ function changed(kinds) {
71
+ if (!kinds.size)
72
+ return;
73
+ for (const kind of kinds) {
74
+ const sorted = [...entries.values()]
75
+ .filter(entry => entry.contribution.kind === kind)
76
+ .sort((a, b) => compareRanks(a.rank, b.rank))
77
+ .map(entry => entry.contribution);
78
+ snapshots.set(kind, Object.freeze(sorted));
79
+ }
80
+ dirty = true;
81
+ flush();
82
+ }
83
+ /** Emits one notification outside batches. */
84
+ function flush() {
85
+ if (batchDepth || !dirty)
86
+ return;
87
+ dirty = false;
88
+ notify();
89
+ }
90
+ return {
91
+ get: id => entries.get(id),
92
+ contributions: kind => (snapshots.get(kind) ?? EMPTY),
93
+ commit(batch) {
94
+ const kinds = new Set();
95
+ for (const entry of batch) {
96
+ entries.set(entry.contribution.id, entry);
97
+ kinds.add(entry.contribution.kind);
98
+ }
99
+ changed(kinds);
100
+ },
101
+ remove(ids) {
102
+ const kinds = new Set();
103
+ for (const id of ids) {
104
+ const entry = entries.get(id);
105
+ if (!entry)
106
+ continue;
107
+ entries.delete(id);
108
+ kinds.add(entry.contribution.kind);
109
+ }
110
+ changed(kinds);
111
+ },
112
+ batch(fn) {
113
+ batchDepth++;
114
+ try {
115
+ fn();
116
+ }
117
+ finally {
118
+ batchDepth--;
119
+ flush();
120
+ }
121
+ },
122
+ };
123
+ }
@@ -0,0 +1,20 @@
1
+ import type { Disposer } from '../types/common.js';
2
+ /** Resources acquired by one owner, released exactly once. */
3
+ export interface ResourceSet {
4
+ /**
5
+ * Tracks a release callback. Once the set is closed, the callback runs immediately
6
+ * (late acquisitions are disposed on arrival) and a no-op disposer is returned.
7
+ * The returned disposer releases this resource early, exactly once.
8
+ */
9
+ add: (release: () => void) => Disposer;
10
+ /** Closes the set and releases every tracked resource in reverse acquisition order. */
11
+ close: () => void;
12
+ }
13
+ /** No-op disposer for registrations refused by a retired scope. */
14
+ export declare const NOOP: Disposer;
15
+ /**
16
+ * Creates a resource set.
17
+ * @param onError Receives errors thrown by release callbacks; release continues with the next one.
18
+ */
19
+ export declare function createResourceSet(onError: (error: unknown) => void): ResourceSet;
20
+ //# sourceMappingURL=resources.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resources.d.ts","sourceRoot":"","sources":["../../src/host/resources.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAElD,8DAA8D;AAC9D,MAAM,WAAW,WAAW;IAC1B;;;;OAIG;IACH,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,IAAI,KAAK,QAAQ,CAAA;IACtC,uFAAuF;IACvF,KAAK,EAAE,MAAM,IAAI,CAAA;CAClB;AAED,mEAAmE;AACnE,eAAO,MAAM,IAAI,EAAE,QAAmB,CAAA;AAEtC;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,GAAG,WAAW,CAqChF"}
@@ -0,0 +1,45 @@
1
+ /** No-op disposer for registrations refused by a retired scope. */
2
+ export const NOOP = () => { };
3
+ /**
4
+ * Creates a resource set.
5
+ * @param onError Receives errors thrown by release callbacks; release continues with the next one.
6
+ */
7
+ export function createResourceSet(onError) {
8
+ const tracked = new Set();
9
+ let closed = false;
10
+ /**
11
+ * Runs one release callback, routing its error.
12
+ * @param release Callback to run.
13
+ */
14
+ function run(release) {
15
+ try {
16
+ release();
17
+ }
18
+ catch (error) {
19
+ onError(error);
20
+ }
21
+ }
22
+ return {
23
+ add(release) {
24
+ if (closed) {
25
+ run(release);
26
+ return NOOP;
27
+ }
28
+ // Wrapper identity marks the entry; removing it first makes reentrant calls no-ops.
29
+ const entry = () => {
30
+ if (tracked.delete(entry))
31
+ run(release);
32
+ };
33
+ tracked.add(entry);
34
+ return entry;
35
+ },
36
+ close() {
37
+ if (closed)
38
+ return;
39
+ closed = true;
40
+ // Entries may release others reentrantly; each removes itself before running.
41
+ for (const entry of [...tracked].reverse())
42
+ entry();
43
+ },
44
+ };
45
+ }
package/dist/ids.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ /** Namespace reserved for built-in workbench contributions (`builtin:props`). */
2
+ export declare const BUILTIN_NAMESPACE = "builtin";
3
+ /** Parsed `${namespace}:${localId}` id. */
4
+ export interface ParsedQualifiedId {
5
+ /** Plugin id, or `builtin`. */
6
+ namespace: string;
7
+ /** Id local to the namespace. */
8
+ localId: string;
9
+ }
10
+ /**
11
+ * Whether `id` is usable as a plugin id (`[a-z][a-z0-9-]*`, at most 64 characters, not
12
+ * `builtin`). Same grammar as the Node descriptor and channel validation (`@histoire/protocol`).
13
+ * @param id Candidate value of any type.
14
+ */
15
+ export declare function isValidPluginId(id: unknown): id is string;
16
+ /**
17
+ * Throws an attributed error unless `id` is a valid plugin id.
18
+ * @param id Candidate value of any type.
19
+ */
20
+ export declare function assertPluginId(id: unknown): asserts id is string;
21
+ /**
22
+ * Qualifies a local contribution id as `${pluginId}:${localId}`.
23
+ * @param pluginId Owning plugin; `builtin` is rejected.
24
+ * @param localId Id as written by the plugin.
25
+ */
26
+ export declare function qualifyId(pluginId: string, localId: string): string;
27
+ /**
28
+ * Splits a qualified id. Returns `null` for unqualified or malformed ids.
29
+ * @param id Qualified id such as `acme:panel` or `builtin:props`.
30
+ */
31
+ export declare function parseQualifiedId(id: string): ParsedQualifiedId | null;
32
+ //# sourceMappingURL=ids.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ids.d.ts","sourceRoot":"","sources":["../src/ids.ts"],"names":[],"mappings":"AAGA,iFAAiF;AACjF,eAAO,MAAM,iBAAiB,YAAY,CAAA;AAK1C,2CAA2C;AAC3C,MAAM,WAAW,iBAAiB;IAChC,+BAA+B;IAC/B,SAAS,EAAE,MAAM,CAAA;IACjB,iCAAiC;IACjC,OAAO,EAAE,MAAM,CAAA;CAChB;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,EAAE,EAAE,OAAO,GAAG,EAAE,IAAI,MAAM,CAEzD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,OAAO,GAAG,OAAO,CAAC,EAAE,IAAI,MAAM,CAOhE;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAMnE;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,iBAAiB,GAAG,IAAI,CAOrE"}