(function (global) { 'use strict' const hasOwn = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key) const isObject = (value) => value && typeof value === 'object' && !Array.isArray(value) const copyList = (value) => Array.isArray(value) ? value.slice() : [] const normalize = (input) => { const source = isObject(input) ? input : {} return { nodes: copyList(source.nodes), edges: copyList(source.edges), groups: copyList(source.groups), rev: Number.isFinite(source.rev) ? source.rev : 0, updatedAt: Number.isFinite(source.updatedAt) ? source.updatedAt : 0, } } const cloneSnapshot = (state) => ({ nodes: state.nodes.slice(), edges: state.edges.slice(), groups: state.groups.slice(), rev: state.rev, updatedAt: state.updatedAt, }) const create = (options) => { const config = isObject(options) ? options : {} let state = normalize(config.initialState) let destroyed = false let revision = state.rev const listeners = [] const notify = (next, previous, meta) => { if (destroyed) return const current = listeners.slice() for (const listener of current) { try { listener(cloneSnapshot(next), cloneSnapshot(previous), meta || {}) } catch (e) { /* isolate subscribers */ } } } // v3.26.109 诊断:连线在没有任何「删除」动作的情况下变少时,留一条带调用栈的日志。 // 用户实测「生图出来后配置节点的连线自己断了」,光看代码找不到唯一路径,先把证据留下。 const noteEdgeDrop = (before, after, meta, nodesAfter) => { try { if (!Array.isArray(before) || !Array.isArray(after) || after.length >= before.length) return const kept = new Set(after.map((e) => e && e.id)) const gone = before.filter((e) => e && !kept.has(e.id)) if (!gone.length) return // 换画板/整表替换不是「连线自己断了」:旧边的两个端点在新画板里都找不到 ⇒ 直接跳过, // 否则每次切板都会刷一条 n=90/140 的假告警(2026-11-29 日志实测刷屏,干扰真问题定位)。 const alive = new Set((nodesAfter || []).map((n) => n && n.id)) if (alive.size && gone.every((e) => !alive.has(e.from) && !alive.has(e.to))) return const why = String((meta && (meta.reason || meta.kind)) || '') const list = gone.slice(0, 3).map((e) => String(e.id) + ':' + String(e.from) + '>' + String(e.to)).join(',') const rewired = before.filter((e) => { const n = after.find((x) => x && e && x.id === e.id); return n && (n.from !== e.from || n.to !== e.to) }) if (!gone.length && rewired.length) { const rl = rewired.slice(0, 3).map((e) => String(e.id) + ':' + String(e.from) + '>' + String(e.to) + '→' + (() => { const n = after.find((x) => x && x.id === e.id); return String(n.from) + '>' + String(n.to) })()).join(',') const rst = String(new Error('edge-rewire').stack || '').split('\n').slice(2, 5).map((x) => x.trim().replace(/\s+/g, ' ')).join(' <- ') const rline = 'edge-rewire n=' + rewired.length + ' ' + rl + ' | ' + rst const stw = global.IWBStorageFile if (stw && typeof stw.logLine === 'function') stw.logLine(rline) else if (global.console && global.console.warn) global.console.warn('[iwb] ' + rline) } const stack = String(new Error('edge-drop').stack || '').split('\n').slice(2, 6).map((x) => x.trim().replace(/\s+/g, ' ')).join(' <- ') const line = 'edge-drop n=' + gone.length + ' meta=' + (why || '-') + ' ' + list + ' | ' + stack const st = global.IWBStorageFile if (st && typeof st.logLine === 'function') st.logLine(line) else if (global.console && global.console.warn) global.console.warn('[iwb] ' + line) } catch (e) { /* 诊断失败不影响画布 */ } } const commit = (next, meta) => { if (destroyed) return cloneSnapshot(state) const previous = state try { const nx = normalize(next) || { nodes: [], edges: [] }; noteEdgeDrop((previous && previous.edges) || [], nx.edges || [], meta, nx.nodes) } catch (e) { /* ignore */ } state = normalize(next) revision += 1 state.rev = revision state.updatedAt = Date.now() const snapshot = cloneSnapshot(state) notify(state, previous, meta) if (typeof config.onChange === 'function') { try { config.onChange(snapshot, cloneSnapshot(previous), meta || {}) } catch (e) { /* isolate adapter */ } } return snapshot } return { getState() { return cloneSnapshot(state) }, replace(snapshot, meta) { return commit(snapshot, meta) }, patch(patch, meta) { const source = isObject(patch) ? patch : {} const next = { nodes: hasOwn(source, 'nodes') ? source.nodes : state.nodes, edges: hasOwn(source, 'edges') ? source.edges : state.edges, groups: hasOwn(source, 'groups') ? source.groups : state.groups, } return commit(next, meta) }, transaction(mutator, meta) { const draft = cloneSnapshot(state) if (typeof mutator === 'function') mutator(draft) return commit(draft, meta) }, subscribe(listener) { if (typeof listener !== 'function' || destroyed) return () => {} listeners.push(listener) return () => { const index = listeners.indexOf(listener) if (index >= 0) listeners.splice(index, 1) } }, destroy() { destroyed = true listeners.length = 0 }, get revision() { return revision }, } } global.IWBCanvasStore = { create, normalize } })(typeof window !== 'undefined' ? window : this)