| 302 | * and creates all documents (process, states, transitions) in the model. |
| 303 | */ |
| 304 | export async function importProcess ( |
| 305 | masterTag: Ref<MasterTag>, |
| 306 | json: string, |
| 307 | bindings?: Record<string, string> |
| 308 | ): Promise<void> { |
| 309 | try { |
| 310 | const data = JSON.parse(json) as Doc[] |
| 311 | if (data.length === 0) return |
| 312 | const denormalizedData = denormalizeIds(data, masterTag, bindings ?? {}) |
| 313 | const client = getClient() |
| 314 | const m = client.getModel() |
| 315 | const apply = client.apply('Import process') |
| 316 | for (const elem of denormalizedData) { |
| 317 | if (elem._class === processPlugin.class.Process) { |
| 318 | ;(elem as any).masterTag = masterTag |
| 319 | if (bindings !== undefined) { |
| 320 | ;(elem as any).bindings = bindings |
| 321 | } |
| 322 | // Strip template metadata — slots will be re-detected for the new environment |
| 323 | delete (elem as any).requiredSlots |
| 324 | } |
| 325 | const existing = m.findObject(elem._id) |
| 326 | if (existing !== undefined) { |
| 327 | const newData = stripData(elem) |
| 328 | const oldData = stripData(existing) |
| 329 | if (!deepEqual(newData, oldData)) { |
| 330 | await apply.updateDoc(elem._class, existing.space, elem._id, newData) |
| 331 | } |
| 332 | continue |
| 333 | } |
| 334 | await apply.createDoc(elem._class, core.space.Model, stripData(elem), elem._id) |
| 335 | } |
| 336 | await apply.commit(true) |
| 337 | } catch (e) { |
| 338 | console.error('Failed to import process:', e) |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | // ─── ID Normalization ──────────────────────────────────────────────────────── |
| 343 | |