(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 */ } } } const commit = (next, meta) => { if (destroyed) return cloneSnapshot(state) const previous = state 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)