@schema-element-editor/host-sdk 2.0.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -28,11 +28,12 @@ module.exports = __toCommonJS(index_exports);
28
28
  // src/react.ts
29
29
  var import_react = require("react");
30
30
 
31
- // src/core.ts
31
+ // src/constants.ts
32
32
  var DEFAULT_SOURCE_CONFIG = {
33
33
  contentSource: "schema-element-editor-content",
34
34
  hostSource: "schema-element-editor-host"
35
35
  };
36
+ var SDK_COORDINATOR_SOURCE = "schema-element-editor-sdk-coordinator";
36
37
  var DEFAULT_MESSAGE_TYPES = {
37
38
  getSchema: "GET_SCHEMA",
38
39
  updateSchema: "UPDATE_SCHEMA",
@@ -44,8 +45,239 @@ var DEFAULT_MESSAGE_TYPES = {
44
45
  stopRecording: "STOP_RECORDING",
45
46
  schemaPush: "SCHEMA_PUSH"
46
47
  };
48
+ var SDK_COORDINATION_MESSAGE_TYPES = {
49
+ register: "SDK_REGISTER",
50
+ unregister: "SDK_UNREGISTER",
51
+ query: "SDK_QUERY"
52
+ };
53
+
54
+ // src/coordinator.ts
55
+ var SdkCoordinator = class {
56
+ constructor(config) {
57
+ /**
58
+ * SDK 销毁标志
59
+ *
60
+ * 作用:防止在 cleanup() 执行期间,监听器移除前仍然响应请求
61
+ *
62
+ * 场景:cleanup() 中先调用 destroy()(设置 isDestroyed = true),
63
+ * 然后才移除 handleMessage 监听器。在这个微小的时间窗口内,
64
+ * 如果插件请求到达,没有 isDestroyed 检查会导致已销毁的 SDK 仍然响应。
65
+ */
66
+ this.isDestroyed = false;
67
+ /**
68
+ * 存储比当前 SDK 优先级更高的其他 SDK ID
69
+ * 按方法分类:如果某个方法的集合不为空,说明有更高优先级的 SDK 应该处理该方法
70
+ *
71
+ * 优化:只为当前 SDK 实现了的方法维护优先级集合
72
+ */
73
+ this.higherLevelSDKs = {};
74
+ /**
75
+ * 存储与当前 SDK 优先级相同的其他 SDK ID
76
+ * 按方法分类:用于判断是否需要在执行失败时跳过响应
77
+ *
78
+ * 当有同级竞争者时,SDK在执行失败/无数据时会跳过响应,让其他SDK有机会响应
79
+ */
80
+ this.sameLevelSDKs = {};
81
+ /**
82
+ * 处理 SDK 协调消息
83
+ */
84
+ this.handleCoordinationMessage = (event) => {
85
+ const isFromSelf = event.source === window;
86
+ const isFromParent = window !== window.top && event.source === window.parent;
87
+ if (!isFromSelf && !isFromParent) return;
88
+ const data = event.data;
89
+ if (!data || data.source !== SDK_COORDINATOR_SOURCE) return;
90
+ switch (data.type) {
91
+ case SDK_COORDINATION_MESSAGE_TYPES.query:
92
+ this.sendRegister();
93
+ break;
94
+ case SDK_COORDINATION_MESSAGE_TYPES.register:
95
+ this.handleSdkRegister(data.payload);
96
+ break;
97
+ case SDK_COORDINATION_MESSAGE_TYPES.unregister:
98
+ this.handleSdkUnregister(data.payload.sdkId);
99
+ break;
100
+ }
101
+ };
102
+ this.sdkId = config.sdkId || `sdk-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
103
+ this.messageSource = config.messageSource;
104
+ this.level = config.level ?? 0;
105
+ this.methodLevels = config.methodLevels ?? {};
106
+ this.implementedMethods = config.implementedMethods;
107
+ this.implementedMethods.forEach((method) => {
108
+ this.higherLevelSDKs[method] = /* @__PURE__ */ new Set();
109
+ this.sameLevelSDKs[method] = /* @__PURE__ */ new Set();
110
+ });
111
+ }
112
+ /**
113
+ * 初始化协调器
114
+ */
115
+ init() {
116
+ window.addEventListener("message", this.handleCoordinationMessage);
117
+ this.sendQuery();
118
+ this.sendRegister();
119
+ }
120
+ /**
121
+ * 销毁协调器
122
+ */
123
+ destroy() {
124
+ this.isDestroyed = true;
125
+ this.sendUnregister();
126
+ window.removeEventListener("message", this.handleCoordinationMessage);
127
+ Object.values(this.higherLevelSDKs).forEach((set) => set.clear());
128
+ Object.values(this.sameLevelSDKs).forEach((set) => set.clear());
129
+ }
130
+ /**
131
+ * 判断是否应该响应某个方法的请求
132
+ * @param method - 方法名
133
+ * @returns 是否应该响应
134
+ */
135
+ shouldRespond(method) {
136
+ if (this.isDestroyed) return false;
137
+ if (!this.implementedMethods.includes(method)) {
138
+ return false;
139
+ }
140
+ const higherSDKs = this.higherLevelSDKs[method];
141
+ return !higherSDKs || higherSDKs.size === 0;
142
+ }
143
+ /**
144
+ * 判断某个方法是否有相同优先级的竞争者
145
+ * @param method - 方法名
146
+ * @returns 是否存在同级竞争者
147
+ */
148
+ hasSameLevelCompetitors(method) {
149
+ const sameSDKs = this.sameLevelSDKs[method];
150
+ return sameSDKs ? sameSDKs.size > 0 : false;
151
+ }
152
+ /**
153
+ * 获取方法的优先级
154
+ */
155
+ getMethodLevel(method) {
156
+ return this.methodLevels[method] ?? this.level;
157
+ }
158
+ /**
159
+ * 处理其他 SDK 的注册
160
+ */
161
+ handleSdkRegister(info) {
162
+ if (info.sdkId === this.sdkId) return;
163
+ if (info.messageSource !== this.messageSource) return;
164
+ this.implementedMethods.forEach((method) => {
165
+ if (!info.implementedMethods.includes(method)) {
166
+ return;
167
+ }
168
+ const myLevel = this.getMethodLevel(method);
169
+ const otherLevel = info.methodLevels[method] ?? info.level;
170
+ if (otherLevel > myLevel) {
171
+ this.higherLevelSDKs[method].add(info.sdkId);
172
+ this.sameLevelSDKs[method].delete(info.sdkId);
173
+ } else if (otherLevel === myLevel) {
174
+ this.sameLevelSDKs[method].add(info.sdkId);
175
+ this.higherLevelSDKs[method].delete(info.sdkId);
176
+ } else {
177
+ this.higherLevelSDKs[method].delete(info.sdkId);
178
+ this.sameLevelSDKs[method].delete(info.sdkId);
179
+ }
180
+ });
181
+ }
182
+ /**
183
+ * 处理其他 SDK 的注销
184
+ */
185
+ handleSdkUnregister(sdkId) {
186
+ Object.values(this.higherLevelSDKs).forEach((set) => {
187
+ set.delete(sdkId);
188
+ });
189
+ Object.values(this.sameLevelSDKs).forEach((set) => {
190
+ set.delete(sdkId);
191
+ });
192
+ }
193
+ /**
194
+ * 发送查询消息
195
+ */
196
+ sendQuery() {
197
+ const message = {
198
+ source: SDK_COORDINATOR_SOURCE,
199
+ type: SDK_COORDINATION_MESSAGE_TYPES.query,
200
+ payload: { sdkId: this.sdkId }
201
+ };
202
+ this.postCoordinationMessage(message);
203
+ }
204
+ /**
205
+ * 发送注册消息
206
+ */
207
+ sendRegister() {
208
+ const message = {
209
+ source: SDK_COORDINATOR_SOURCE,
210
+ type: SDK_COORDINATION_MESSAGE_TYPES.register,
211
+ payload: {
212
+ sdkId: this.sdkId,
213
+ messageSource: this.messageSource,
214
+ level: this.level,
215
+ methodLevels: this.methodLevels,
216
+ implementedMethods: this.implementedMethods
217
+ }
218
+ };
219
+ this.postCoordinationMessage(message);
220
+ }
221
+ /**
222
+ * 发送注销消息
223
+ */
224
+ sendUnregister() {
225
+ const message = {
226
+ source: SDK_COORDINATOR_SOURCE,
227
+ type: SDK_COORDINATION_MESSAGE_TYPES.unregister,
228
+ payload: { sdkId: this.sdkId }
229
+ };
230
+ this.postCoordinationMessage(message);
231
+ }
232
+ /**
233
+ * 发送协调消息
234
+ */
235
+ postCoordinationMessage(message) {
236
+ window.postMessage(message, "*");
237
+ if (window.top && window.top !== window) {
238
+ window.top.postMessage(message, "*");
239
+ }
240
+ }
241
+ };
242
+
243
+ // src/bridge.ts
244
+ var METHOD_NAMES = {
245
+ GET_SCHEMA: "getSchema",
246
+ UPDATE_SCHEMA: "updateSchema",
247
+ CHECK_PREVIEW: "checkPreview",
248
+ RENDER_PREVIEW: "renderPreview",
249
+ CLEANUP_PREVIEW: "cleanupPreview",
250
+ START_RECORDING: "startRecording",
251
+ STOP_RECORDING: "stopRecording"
252
+ };
253
+ function shouldSkipFailedResponse(method, result) {
254
+ switch (method) {
255
+ case METHOD_NAMES.GET_SCHEMA:
256
+ return result.success === true && result.data === void 0;
257
+ case METHOD_NAMES.UPDATE_SCHEMA:
258
+ return result.success === false;
259
+ case METHOD_NAMES.RENDER_PREVIEW:
260
+ case METHOD_NAMES.CLEANUP_PREVIEW:
261
+ return result.success === false;
262
+ case METHOD_NAMES.CHECK_PREVIEW:
263
+ case METHOD_NAMES.START_RECORDING:
264
+ case METHOD_NAMES.STOP_RECORDING:
265
+ return false;
266
+ default:
267
+ return false;
268
+ }
269
+ }
47
270
  function createSchemaElementEditorBridge(config) {
48
- const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes } = config;
271
+ const {
272
+ getSchema,
273
+ updateSchema,
274
+ renderPreview,
275
+ sourceConfig,
276
+ messageTypes,
277
+ sdkId,
278
+ level,
279
+ methodLevels
280
+ } = config;
49
281
  const mergedSourceConfig = {
50
282
  ...DEFAULT_SOURCE_CONFIG,
51
283
  ...sourceConfig
@@ -54,9 +286,40 @@ function createSchemaElementEditorBridge(config) {
54
286
  ...DEFAULT_MESSAGE_TYPES,
55
287
  ...messageTypes
56
288
  };
289
+ const implementedMethods = [];
290
+ if (typeof getSchema === "function") implementedMethods.push(METHOD_NAMES.GET_SCHEMA);
291
+ if (typeof updateSchema === "function") implementedMethods.push(METHOD_NAMES.UPDATE_SCHEMA);
292
+ if (typeof renderPreview === "function") {
293
+ implementedMethods.push(
294
+ METHOD_NAMES.CHECK_PREVIEW,
295
+ METHOD_NAMES.RENDER_PREVIEW,
296
+ METHOD_NAMES.CLEANUP_PREVIEW
297
+ );
298
+ }
299
+ implementedMethods.push(METHOD_NAMES.START_RECORDING, METHOD_NAMES.STOP_RECORDING);
300
+ const coordinator = new SdkCoordinator({
301
+ sdkId,
302
+ messageSource: mergedSourceConfig.contentSource,
303
+ level,
304
+ methodLevels,
305
+ implementedMethods
306
+ });
307
+ coordinator.init();
57
308
  let previewCleanupFn = null;
58
309
  const recordingParams = /* @__PURE__ */ new Set();
59
310
  const currentConfig = { getSchema, updateSchema, renderPreview };
311
+ const methodTypeToName = {
312
+ [mergedMessageTypes.getSchema]: METHOD_NAMES.GET_SCHEMA,
313
+ [mergedMessageTypes.updateSchema]: METHOD_NAMES.UPDATE_SCHEMA,
314
+ [mergedMessageTypes.checkPreview]: METHOD_NAMES.CHECK_PREVIEW,
315
+ [mergedMessageTypes.renderPreview]: METHOD_NAMES.RENDER_PREVIEW,
316
+ [mergedMessageTypes.cleanupPreview]: METHOD_NAMES.CLEANUP_PREVIEW,
317
+ [mergedMessageTypes.startRecording]: METHOD_NAMES.START_RECORDING,
318
+ [mergedMessageTypes.stopRecording]: METHOD_NAMES.STOP_RECORDING
319
+ };
320
+ const getMethodNameByType = (type) => {
321
+ return methodTypeToName[type] ?? null;
322
+ };
60
323
  const sendResponse = (requestId, result) => {
61
324
  const message = {
62
325
  source: mergedSourceConfig.hostSource,
@@ -89,10 +352,18 @@ function createSchemaElementEditorBridge(config) {
89
352
  if (!event.data || event.data.source !== mergedSourceConfig.contentSource) return;
90
353
  const { type, payload, requestId } = event.data;
91
354
  if (!requestId) return;
355
+ const methodName = getMethodNameByType(type);
356
+ if (!methodName) return;
357
+ if (!coordinator.shouldRespond(methodName)) {
358
+ return;
359
+ }
92
360
  const { getSchema: getSchema2, updateSchema: updateSchema2, renderPreview: renderPreview2 } = currentConfig;
93
361
  let result;
94
362
  switch (type) {
95
363
  case mergedMessageTypes.getSchema: {
364
+ if (typeof getSchema2 !== "function") {
365
+ return;
366
+ }
96
367
  const params = String(payload?.params ?? "");
97
368
  try {
98
369
  const data = getSchema2(params);
@@ -106,6 +377,9 @@ function createSchemaElementEditorBridge(config) {
106
377
  break;
107
378
  }
108
379
  case mergedMessageTypes.updateSchema: {
380
+ if (typeof updateSchema2 !== "function") {
381
+ return;
382
+ }
109
383
  const schema = payload?.schema;
110
384
  const params = String(payload?.params ?? "");
111
385
  try {
@@ -178,11 +452,16 @@ function createSchemaElementEditorBridge(config) {
178
452
  default:
179
453
  return;
180
454
  }
455
+ const hasSameLevelSdks = coordinator.hasSameLevelCompetitors(methodName);
456
+ if (hasSameLevelSdks && shouldSkipFailedResponse(methodName, result)) {
457
+ return;
458
+ }
181
459
  sendResponse(requestId, result);
182
460
  };
183
461
  window.addEventListener("message", handleMessage);
184
462
  return {
185
463
  cleanup: () => {
464
+ coordinator.destroy();
186
465
  window.removeEventListener("message", handleMessage);
187
466
  if (previewCleanupFn) {
188
467
  previewCleanupFn();
@@ -198,7 +477,17 @@ function createSchemaElementEditorBridge(config) {
198
477
 
199
478
  // src/react.ts
200
479
  function useSchemaElementEditor(config) {
201
- const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes, enabled } = config;
480
+ const {
481
+ getSchema,
482
+ updateSchema,
483
+ renderPreview,
484
+ sourceConfig,
485
+ messageTypes,
486
+ enabled,
487
+ sdkId,
488
+ level,
489
+ methodLevels
490
+ } = config;
202
491
  const configRef = (0, import_react.useRef)({ getSchema, updateSchema, renderPreview });
203
492
  const bridgeRef = (0, import_react.useRef)(null);
204
493
  (0, import_react.useEffect)(() => {
@@ -206,18 +495,19 @@ function useSchemaElementEditor(config) {
206
495
  }, [getSchema, updateSchema, renderPreview]);
207
496
  (0, import_react.useEffect)(() => {
208
497
  if (enabled === false) {
209
- if (bridgeRef.current && typeof bridgeRef.current.cleanup === "function") {
210
- bridgeRef.current.cleanup();
211
- bridgeRef.current = null;
212
- }
213
498
  return;
214
499
  }
215
500
  const proxyConfig = {
216
- getSchema: (params) => configRef.current.getSchema(params),
217
- updateSchema: (schema, params) => configRef.current.updateSchema(schema, params),
501
+ // 外层检查确保初始时函数存在才创建代理
502
+ // 内层使用可选链安全访问,配合类型断言确保类型正确
503
+ getSchema: configRef.current.getSchema ? (params) => configRef.current.getSchema?.(params) : void 0,
504
+ updateSchema: configRef.current.updateSchema ? (schema, params) => configRef.current.updateSchema?.(schema, params) : void 0,
218
505
  renderPreview: configRef.current.renderPreview ? (schema, containerId) => configRef.current.renderPreview?.(schema, containerId) : void 0,
219
506
  sourceConfig,
220
- messageTypes
507
+ messageTypes,
508
+ sdkId,
509
+ level,
510
+ methodLevels
221
511
  };
222
512
  const bridge = createSchemaElementEditorBridge(proxyConfig);
223
513
  bridgeRef.current = bridge;
@@ -225,7 +515,7 @@ function useSchemaElementEditor(config) {
225
515
  bridge.cleanup();
226
516
  bridgeRef.current = null;
227
517
  };
228
- }, [enabled, configRef, sourceConfig, messageTypes]);
518
+ }, [enabled, configRef, sourceConfig, messageTypes, sdkId, level, methodLevels]);
229
519
  const push = (0, import_react.useCallback)((params, data) => {
230
520
  bridgeRef.current?.recording.push(params, data);
231
521
  }, []);
package/dist/index.d.cts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { useSchemaElementEditor } from './react.cjs';
2
- export { PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorBridge, SchemaElementEditorConfig, SchemaValue, createSchemaElementEditorBridge } from './core.cjs';
2
+ export { createSchemaElementEditorBridge } from './core.cjs';
3
+ export { M as MethodLevelConfig, P as PostMessageSourceConfig, a as PostMessageTypeConfig, c as SchemaElementEditorBridge, b as SchemaElementEditorConfig, d as SchemaElementEditorRecording, S as SchemaValue } from './types-D2ZJx8T_.cjs';
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { useSchemaElementEditor } from './react.js';
2
- export { PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorBridge, SchemaElementEditorConfig, SchemaValue, createSchemaElementEditorBridge } from './core.js';
2
+ export { createSchemaElementEditorBridge } from './core.js';
3
+ export { M as MethodLevelConfig, P as PostMessageSourceConfig, a as PostMessageTypeConfig, c as SchemaElementEditorBridge, b as SchemaElementEditorConfig, d as SchemaElementEditorRecording, S as SchemaValue } from './types-D2ZJx8T_.js';
package/dist/index.js CHANGED
@@ -1,9 +1,10 @@
1
+ import "./chunk-432BCQVI.js";
1
2
  import {
2
3
  useSchemaElementEditor
3
- } from "./chunk-3RNKTSDC.js";
4
+ } from "./chunk-Q6EJUFM6.js";
4
5
  import {
5
6
  createSchemaElementEditorBridge
6
- } from "./chunk-52EFKQHQ.js";
7
+ } from "./chunk-LJJYJHOZ.js";
7
8
  export {
8
9
  createSchemaElementEditorBridge,
9
10
  useSchemaElementEditor