@deviltea/widget-core 0.0.1 → 0.0.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.
@@ -1,4 +1,4 @@
1
- import { a as isCompiledDependency, o as readCompiledBlueprint, t as readRuntimeInternals } from "../internals-DXFEmv8H.mjs";
1
+ import { a as isCompiledDependency, d as createReadonlyMap, o as readCompiledBlueprint, t as readRuntimeInternals, u as readWidgetPluginDefinition } from "../internals-B67fyYoz.mjs";
2
2
  //#region src/inspection/blueprint.ts
3
3
  /**
4
4
  * Projects a compiler-owned `BlueprintDependencyReference` into an independent, deep-frozen structural
@@ -93,23 +93,36 @@ function buildSemanticSlots(compiledNode) {
93
93
  }));
94
94
  return Object.freeze(result);
95
95
  }
96
- function buildStateMembers(compiledNode) {
96
+ function buildStateMembers$1(compiledNode) {
97
97
  const result = [];
98
- for (const name of compiledNode.state.keys()) result.push(Object.freeze({
98
+ for (const [name, member] of compiledNode.state) result.push(Object.freeze({
99
99
  type: "state",
100
- name
100
+ name,
101
+ authorWritable: member.definition.authorWritable === true
101
102
  }));
102
103
  return Object.freeze(result);
103
104
  }
104
- function buildPropertyMembers(compiledNode) {
105
+ function buildPropertyMembers$1(compiledNode) {
105
106
  const result = [];
106
107
  for (const [name, member] of compiledNode.properties) result.push(Object.freeze({
107
108
  type: "property",
108
109
  name,
110
+ valueContractId: member.definition.valueContract?.id ?? null,
109
111
  dependencies: flattenDependencies(member.deps)
110
112
  }));
111
113
  return Object.freeze(result);
112
114
  }
115
+ function buildEventMembers$1(compiledNode) {
116
+ const definitions = readWidgetPluginDefinition(compiledNode.plugin).events;
117
+ if (definitions === null) return Object.freeze([]);
118
+ const result = [];
119
+ for (const [name, definition] of definitions) result.push(Object.freeze({
120
+ type: "event",
121
+ name,
122
+ description: definition.description
123
+ }));
124
+ return Object.freeze(result);
125
+ }
113
126
  function buildMethodMembers(compiledNode) {
114
127
  const result = [];
115
128
  for (const [name, member] of compiledNode.methods) result.push(Object.freeze({
@@ -145,10 +158,12 @@ function buildNode(compiledNode, nodeId) {
145
158
  sourceSlots,
146
159
  resolved: true,
147
160
  capabilities: buildCapabilities(compiledNode),
161
+ config: compiledNode.plugin.config,
148
162
  semanticSlots: buildSemanticSlots(compiledNode),
149
- state: buildStateMembers(compiledNode),
150
- properties: buildPropertyMembers(compiledNode),
151
- methods: buildMethodMembers(compiledNode)
163
+ state: buildStateMembers$1(compiledNode),
164
+ properties: buildPropertyMembers$1(compiledNode),
165
+ methods: buildMethodMembers(compiledNode),
166
+ events: buildEventMembers$1(compiledNode)
152
167
  });
153
168
  }
154
169
  /**
@@ -207,6 +222,56 @@ function inspectBlueprint(blueprint) {
207
222
  return built;
208
223
  }
209
224
  //#endregion
225
+ //#region src/inspection/plugin.ts
226
+ const inspectionCache = /* @__PURE__ */ new WeakMap();
227
+ function buildStateMembers(plugin) {
228
+ const definitions = readWidgetPluginDefinition(plugin).state;
229
+ if (definitions === null) return null;
230
+ const members = [];
231
+ for (const [name, definition] of definitions) members.push([name, Object.freeze({
232
+ type: "state",
233
+ name,
234
+ authorWritable: definition.authorWritable === true
235
+ })]);
236
+ return createReadonlyMap(members);
237
+ }
238
+ function buildPropertyMembers(plugin) {
239
+ const definitions = readWidgetPluginDefinition(plugin).properties;
240
+ if (definitions === null) return null;
241
+ const members = [];
242
+ for (const [name, definition] of definitions) members.push([name, Object.freeze({
243
+ type: "property",
244
+ name,
245
+ valueContractId: definition.valueContract?.id ?? null
246
+ })]);
247
+ return createReadonlyMap(members);
248
+ }
249
+ function buildEventMembers(plugin) {
250
+ const definitions = readWidgetPluginDefinition(plugin).events;
251
+ if (definitions === null) return null;
252
+ const members = [];
253
+ for (const [name, definition] of definitions) members.push([name, Object.freeze({
254
+ type: "event",
255
+ name,
256
+ description: definition.description
257
+ })]);
258
+ return createReadonlyMap(members);
259
+ }
260
+ /**
261
+ * Returns an identity-stable, readonly/passive projection for the exact completed Plugin object.
262
+ */
263
+ function inspectPlugin(plugin) {
264
+ const existing = inspectionCache.get(plugin);
265
+ if (existing !== void 0) return existing;
266
+ const inspection = Object.freeze({
267
+ state: buildStateMembers(plugin),
268
+ properties: buildPropertyMembers(plugin),
269
+ events: buildEventMembers(plugin)
270
+ });
271
+ inspectionCache.set(plugin, inspection);
272
+ return inspection;
273
+ }
274
+ //#endregion
210
275
  //#region src/inspection/runtime.ts
211
276
  function buildStateInspection(context, primitive) {
212
277
  return Object.freeze({
@@ -228,9 +293,16 @@ function buildPropertyInspection(context, primitive) {
228
293
  }
229
294
  });
230
295
  }
296
+ function buildEventInspection(context, primitive) {
297
+ return Object.freeze({ subscribe(listener) {
298
+ context.assertActive();
299
+ return context.registerSubscription(primitive.inspection.subscribe(listener));
300
+ } });
301
+ }
231
302
  function buildRuntimeWidgetInspection(context, nodeId, blueprintNode, entry) {
232
303
  const stateFacades = /* @__PURE__ */ new Map();
233
304
  const propertyFacades = /* @__PURE__ */ new Map();
305
+ const eventFacades = /* @__PURE__ */ new Map();
234
306
  function getState(key) {
235
307
  const primitive = entry.state.get(key);
236
308
  if (primitive === void 0) return null;
@@ -241,6 +313,16 @@ function buildRuntimeWidgetInspection(context, nodeId, blueprintNode, entry) {
241
313
  }
242
314
  return facade;
243
315
  }
316
+ function getEvent(name) {
317
+ const primitive = entry.events.get(name);
318
+ if (primitive === void 0) return null;
319
+ let facade = eventFacades.get(name);
320
+ if (facade === void 0) {
321
+ facade = buildEventInspection(context, primitive);
322
+ eventFacades.set(name, facade);
323
+ }
324
+ return facade;
325
+ }
244
326
  function getProperty(name) {
245
327
  const primitive = entry.properties.get(name);
246
328
  if (primitive === void 0) return null;
@@ -255,7 +337,8 @@ function buildRuntimeWidgetInspection(context, nodeId, blueprintNode, entry) {
255
337
  nodeId,
256
338
  blueprintNode,
257
339
  getState,
258
- getProperty
340
+ getProperty,
341
+ getEvent
259
342
  });
260
343
  }
261
344
  function buildRuntimeInspection(runtime) {
@@ -293,4 +376,4 @@ function inspectRuntime(runtime) {
293
376
  return built;
294
377
  }
295
378
  //#endregion
296
- export { inspectBlueprint, inspectRuntime };
379
+ export { inspectBlueprint, inspectPlugin, inspectRuntime };
@@ -0,0 +1,12 @@
1
+ import { Lt as RuntimeWidget, Xt as WidgetSystemRuntime, cr as WidgetMemberKey, on as AnyWidgetPluginTuple } from "../dep-CzOpc4xk.mjs";
2
+ //#region src/integration/index.d.ts
3
+ type WidgetIntegrationErrorCode = 'runtime-widget-mismatch';
4
+ declare class WidgetIntegrationError extends Error {
5
+ readonly code: WidgetIntegrationErrorCode;
6
+ readonly name = "WidgetIntegrationError";
7
+ constructor(code: WidgetIntegrationErrorCode, message?: string);
8
+ }
9
+ type WidgetIntegrationEventEmitter = Readonly<Record<WidgetMemberKey, (...args: readonly unknown[]) => void>>;
10
+ declare function getWidgetEventEmitter<Plugins extends AnyWidgetPluginTuple>(runtime: WidgetSystemRuntime<Plugins>, widget: RuntimeWidget<Plugins>): WidgetIntegrationEventEmitter | null;
11
+ //#endregion
12
+ export { WidgetIntegrationError, WidgetIntegrationErrorCode, WidgetIntegrationEventEmitter, getWidgetEventEmitter };
@@ -0,0 +1,26 @@
1
+ import { t as buildEventEmitter } from "../event-BMif_vRJ.mjs";
2
+ import { o as readCompiledBlueprint, t as readRuntimeInternals, u as readWidgetPluginDefinition } from "../internals-B67fyYoz.mjs";
3
+ //#region src/integration/index.ts
4
+ var WidgetIntegrationError = class extends Error {
5
+ code;
6
+ name = "WidgetIntegrationError";
7
+ constructor(code, message = "The RuntimeWidget does not belong to the supplied WidgetSystemRuntime instance.") {
8
+ super(message);
9
+ this.code = code;
10
+ }
11
+ };
12
+ function getWidgetEventEmitter(runtime, widget) {
13
+ const erasedWidget = widget;
14
+ if (runtime.getWidget(erasedWidget.id) !== widget) throw new WidgetIntegrationError("runtime-widget-mismatch");
15
+ const compiled = readCompiledBlueprint(runtime.blueprint);
16
+ const nodeId = compiled.nodeIdByPublicNode.get(erasedWidget.blueprint);
17
+ if (nodeId === void 0) throw new Error("The RuntimeWidget is not backed by the supplied Runtime Blueprint.");
18
+ const node = compiled.nodes[nodeId];
19
+ if (node === void 0 || !node.resolved) throw new Error("The RuntimeWidget is not backed by a resolved Runtime node.");
20
+ if (readWidgetPluginDefinition(node.plugin).events === null) return null;
21
+ const entry = readRuntimeInternals(runtime).registry.get(nodeId);
22
+ if (entry === void 0) throw new Error("The RuntimeWidget event implementation was not found in the supplied Runtime.");
23
+ return buildEventEmitter(entry.events);
24
+ }
25
+ //#endregion
26
+ export { WidgetIntegrationError, getWidgetEventEmitter };
@@ -0,0 +1,195 @@
1
+ //#region src/readonly-map.ts
2
+ /** Creates a map-shaped, runtime immutable projection without exposing mutating Map methods. */
3
+ function createReadonlyMap(entries) {
4
+ const map = new Map(entries);
5
+ const result = {
6
+ get size() {
7
+ return map.size;
8
+ },
9
+ get(key) {
10
+ return map.get(key);
11
+ },
12
+ has(key) {
13
+ return map.has(key);
14
+ },
15
+ keys() {
16
+ return map.keys();
17
+ },
18
+ values() {
19
+ return map.values();
20
+ },
21
+ entries() {
22
+ return map.entries();
23
+ },
24
+ forEach(callbackfn, thisArg) {
25
+ map.forEach((value, key) => callbackfn.call(thisArg, value, key, result));
26
+ },
27
+ [Symbol.iterator]() {
28
+ return map[Symbol.iterator]();
29
+ }
30
+ };
31
+ return Object.freeze(result);
32
+ }
33
+ //#endregion
34
+ //#region src/plugin.ts
35
+ /**
36
+ * Framework-private brand carrying the plugin's phantom interfaces and its erased definition record.
37
+ */
38
+ const widgetPluginBrand = Symbol("@deviltea/widget-core:widget-plugin");
39
+ /**
40
+ * Declares a stable semantic value-contract id while retaining the associated TypeScript value type for
41
+ * Plugin-author checking. Core does not interpret, register, validate, or schema-check contract ids.
42
+ */
43
+ function createWidgetValueContract(id) {
44
+ return Object.freeze({ id });
45
+ }
46
+ const WIDGET_CONFIG_JSON_SCHEMA_DIALECT = "https://json-schema.org/draft/2020-12/schema";
47
+ /**
48
+ * Reads the erased definition record of a plugin.
49
+ */
50
+ function readWidgetPluginDefinition(plugin) {
51
+ return plugin[widgetPluginBrand].definition;
52
+ }
53
+ /**
54
+ * Builds the section keyed-chain object.
55
+ *
56
+ * Declared member keys exist only at the type level, so every string member access resolves to a
57
+ * consuming function; the returned chain object is the same object by identity.
58
+ */
59
+ function createSection(sink) {
60
+ const section = new Proxy({}, { get(_target, key) {
61
+ if (typeof key !== "string") return void 0;
62
+ return (definition) => {
63
+ sink.set(key, definition);
64
+ return section;
65
+ };
66
+ } });
67
+ return section;
68
+ }
69
+ /**
70
+ * Starts the plugin builder for one plugin `type`.
71
+ *
72
+ * Phase order is `interfaces -> config? -> slots? -> state? -> properties? -> methods? -> events? -> done()`;
73
+ * a phase exists only when its capability is declared.
74
+ */
75
+ function createWidgetPlugin(type) {
76
+ const draft = {
77
+ description: "",
78
+ config: null,
79
+ slots: null,
80
+ validateStructure: null,
81
+ state: null,
82
+ properties: null,
83
+ methods: null,
84
+ events: null
85
+ };
86
+ const builder = {
87
+ description(description) {
88
+ draft.description = description;
89
+ return builder;
90
+ },
91
+ interfaces: () => builder,
92
+ config(definition) {
93
+ draft.config = Object.freeze({
94
+ ...definition,
95
+ schema: definition.schema ?? null
96
+ });
97
+ return builder;
98
+ },
99
+ slots(definitions, validateStructure) {
100
+ draft.slots = new Map(Object.entries(definitions));
101
+ draft.validateStructure = validateStructure ?? null;
102
+ return builder;
103
+ },
104
+ state(build) {
105
+ const members = /* @__PURE__ */ new Map();
106
+ draft.state = members;
107
+ build(createSection(members));
108
+ return builder;
109
+ },
110
+ properties(build) {
111
+ const members = /* @__PURE__ */ new Map();
112
+ draft.properties = members;
113
+ build(createSection(members));
114
+ return builder;
115
+ },
116
+ methods(build) {
117
+ const members = /* @__PURE__ */ new Map();
118
+ draft.methods = members;
119
+ build(createSection(members));
120
+ return builder;
121
+ },
122
+ events(build) {
123
+ const members = /* @__PURE__ */ new Map();
124
+ draft.events = members;
125
+ build(createSection(members));
126
+ return builder;
127
+ },
128
+ done() {
129
+ const definition = Object.freeze({
130
+ type,
131
+ description: draft.description,
132
+ config: draft.config,
133
+ slots: draft.slots,
134
+ validateStructure: draft.validateStructure,
135
+ state: draft.state,
136
+ properties: draft.properties,
137
+ methods: draft.methods,
138
+ events: draft.events
139
+ });
140
+ const capabilities = Object.freeze({
141
+ config: definition.config !== null,
142
+ slots: definition.slots !== null,
143
+ state: definition.state !== null,
144
+ properties: definition.properties !== null,
145
+ methods: definition.methods !== null,
146
+ events: definition.events !== null
147
+ });
148
+ const config = definition.config === null ? null : Object.freeze({
149
+ description: definition.config.description,
150
+ schema: definition.config.schema
151
+ });
152
+ const descriptions = Object.freeze({
153
+ config: config?.description ?? null,
154
+ slots: definition.slots === null ? null : createReadonlyMap([...definition.slots].map(([key, slot]) => [key, slot.description]))
155
+ });
156
+ return Object.freeze({
157
+ type,
158
+ description: draft.description,
159
+ capabilities,
160
+ config,
161
+ descriptions,
162
+ [widgetPluginBrand]: Object.freeze({ definition })
163
+ });
164
+ }
165
+ };
166
+ return builder;
167
+ }
168
+ //#endregion
169
+ //#region src/internal/contract.ts
170
+ const compiledDependencyBrand = Symbol("@deviltea/widget-core:compiled-dependency");
171
+ function isCompiledDependency(value) {
172
+ return typeof value === "object" && value !== null && compiledDependencyBrand in value;
173
+ }
174
+ const blueprintInternals = Symbol("@deviltea/widget-core:blueprint-internals");
175
+ /**
176
+ * Reads the compiled snapshot carried by a Blueprint.
177
+ */
178
+ function readCompiledBlueprint(blueprint) {
179
+ const compiled = blueprint[blueprintInternals];
180
+ if (compiled === void 0) throw new Error("The blueprint was not produced by this widget core build.");
181
+ return compiled;
182
+ }
183
+ //#endregion
184
+ //#region src/runtime/internals.ts
185
+ const runtimeInternals = Symbol("@deviltea/widget-core:runtime-internals");
186
+ /**
187
+ * Reads the internals carried by a Runtime produced by `createWidgetSystemRuntime`.
188
+ */
189
+ function readRuntimeInternals(runtime) {
190
+ const internals = runtime[runtimeInternals];
191
+ if (internals === void 0) throw new Error("The runtime was not produced by this widget core build.");
192
+ return internals;
193
+ }
194
+ //#endregion
195
+ export { isCompiledDependency as a, createWidgetPlugin as c, createReadonlyMap as d, compiledDependencyBrand as i, createWidgetValueContract as l, runtimeInternals as n, readCompiledBlueprint as o, blueprintInternals as r, WIDGET_CONFIG_JSON_SCHEMA_DIALECT as s, readRuntimeInternals as t, readWidgetPluginDefinition as u };
package/package.json CHANGED
@@ -4,17 +4,17 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.0.1",
7
+ "version": "0.0.2",
8
8
  "author": "DevilTea <ch19980814@gmail.com>",
9
9
  "license": "MIT",
10
- "homepage": "https://github.com/DevilTea/deviltea-labs/tree/main/packages/widget/core#readme",
10
+ "homepage": "https://github.com/DevilTea/widget/tree/main/packages/core#readme",
11
11
  "repository": {
12
12
  "type": "git",
13
- "url": "git+https://github.com/DevilTea/deviltea-labs.git",
14
- "directory": "packages/widget/core"
13
+ "url": "git+https://github.com/DevilTea/widget.git",
14
+ "directory": "packages/core"
15
15
  },
16
16
  "bugs": {
17
- "url": "https://github.com/DevilTea/deviltea-labs/issues"
17
+ "url": "https://github.com/DevilTea/widget/issues"
18
18
  },
19
19
  "keywords": [
20
20
  "widget",
@@ -32,6 +32,10 @@
32
32
  "./inspection": {
33
33
  "types": "./dist/inspection/index.d.mts",
34
34
  "import": "./dist/inspection/index.mjs"
35
+ },
36
+ "./integration": {
37
+ "types": "./dist/integration/index.d.mts",
38
+ "import": "./dist/integration/index.mjs"
35
39
  }
36
40
  },
37
41
  "main": "dist/index.mjs",
@@ -44,9 +48,11 @@
44
48
  "node": "^22.14.0 || ^24.0.0"
45
49
  },
46
50
  "dependencies": {
47
- "alien-signals": "3.2.1"
51
+ "alien-signals": "3.2.1",
52
+ "json-schema-typed": "^8.0.2"
48
53
  },
49
54
  "devDependencies": {
55
+ "@deviltea/tsconfig": "1.0.0",
50
56
  "tsdown": "^0.22.3",
51
57
  "vitest": "^4.1.9"
52
58
  },
@@ -1,27 +0,0 @@
1
- //#region src/internal/contract.ts
2
- const compiledDependencyBrand = Symbol("@deviltea/widget-core:compiled-dependency");
3
- function isCompiledDependency(value) {
4
- return typeof value === "object" && value !== null && compiledDependencyBrand in value;
5
- }
6
- const blueprintInternals = Symbol("@deviltea/widget-core:blueprint-internals");
7
- /**
8
- * Reads the compiled snapshot carried by a Blueprint.
9
- */
10
- function readCompiledBlueprint(blueprint) {
11
- const compiled = blueprint[blueprintInternals];
12
- if (compiled === void 0) throw new Error("The blueprint was not produced by this widget core build.");
13
- return compiled;
14
- }
15
- //#endregion
16
- //#region src/runtime/internals.ts
17
- const runtimeInternals = Symbol("@deviltea/widget-core:runtime-internals");
18
- /**
19
- * Reads the internals carried by a Runtime produced by `createWidgetSystemRuntime`.
20
- */
21
- function readRuntimeInternals(runtime) {
22
- const internals = runtime[runtimeInternals];
23
- if (internals === void 0) throw new Error("The runtime was not produced by this widget core build.");
24
- return internals;
25
- }
26
- //#endregion
27
- export { isCompiledDependency as a, compiledDependencyBrand as i, runtimeInternals as n, readCompiledBlueprint as o, blueprintInternals as r, readRuntimeInternals as t };