symbiote-ui 0.3.0-alpha.4
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/CHANGELOG.md +46 -0
- package/LICENSE +21 -0
- package/README.md +76 -0
- package/canvas/AutoLayout.js +731 -0
- package/canvas/Breadcrumb/Breadcrumb.css.js +75 -0
- package/canvas/Breadcrumb/Breadcrumb.js +96 -0
- package/canvas/Breadcrumb/Breadcrumb.tpl.js +7 -0
- package/canvas/CanvasConnectionRenderer.js +971 -0
- package/canvas/CanvasGraph/CanvasGraph.css.js +29 -0
- package/canvas/CanvasGraph/CanvasGraph.js +1697 -0
- package/canvas/CanvasGraph/CanvasGraphDrawState.js +280 -0
- package/canvas/CanvasGraph/CanvasGraphGeometry.js +194 -0
- package/canvas/CanvasViewport.js +550 -0
- package/canvas/ConnectionRenderer.js +1283 -0
- package/canvas/FlowSimulator.js +326 -0
- package/canvas/ForceLayout.js +226 -0
- package/canvas/ForceWorker.js +1303 -0
- package/canvas/FrameManager.js +223 -0
- package/canvas/GraphExplorerShell/GraphExplorerShell.css.js +136 -0
- package/canvas/GraphExplorerShell/GraphExplorerShell.js +129 -0
- package/canvas/GraphExplorerShell/GraphExplorerShell.tpl.js +12 -0
- package/canvas/GraphTabs/GraphTabs.css.js +101 -0
- package/canvas/GraphTabs/GraphTabs.js +189 -0
- package/canvas/GraphTabs/GraphTabs.tpl.js +12 -0
- package/canvas/LODManager.js +88 -0
- package/canvas/Minimap/Minimap.css.js +73 -0
- package/canvas/Minimap/Minimap.js +210 -0
- package/canvas/Minimap/Minimap.tpl.js +7 -0
- package/canvas/NodeCanvas/NodeCanvas.css.js +398 -0
- package/canvas/NodeCanvas/NodeCanvas.js +1499 -0
- package/canvas/NodeCanvas/NodeCanvas.tpl.js +22 -0
- package/canvas/NodeSearch/NodeSearch.css.js +97 -0
- package/canvas/NodeSearch/NodeSearch.js +140 -0
- package/canvas/NodeSearch/NodeSearch.tpl.js +25 -0
- package/canvas/NodeViewManager.js +748 -0
- package/canvas/PcbRouteDiagnostics.js +463 -0
- package/canvas/PcbRouter.js +1127 -0
- package/canvas/PinExpansion.js +134 -0
- package/canvas/PseudoConnection.js +84 -0
- package/canvas/SelectionSync.js +163 -0
- package/canvas/SubgraphManager.js +203 -0
- package/canvas/SubgraphRouter.js +452 -0
- package/canvas/ViewportActions.js +473 -0
- package/canvas/graph-explorer.js +339 -0
- package/canvas/graph-layout.js +148 -0
- package/canvas/graph-model.js +68 -0
- package/canvas/html-in-canvas.js +202 -0
- package/canvas/project-graph-builder.js +440 -0
- package/canvas/project-graph-model.js +183 -0
- package/chat/ChatComposer/ChatComposer.css.js +652 -0
- package/chat/ChatComposer/ChatComposer.js +304 -0
- package/chat/ChatList/ChatList.css.js +102 -0
- package/chat/ChatList/ChatList.js +99 -0
- package/chat/ChatList/ChatList.tpl.js +20 -0
- package/chat/ChatListItem/ChatListItem.css.js +117 -0
- package/chat/ChatListItem/ChatListItem.js +32 -0
- package/chat/ChatListItem/ChatListItem.tpl.js +17 -0
- package/chat/ChatMessageItem/ChatMessageItem.css.js +628 -0
- package/chat/ChatMessageItem/ChatMessageItem.js +156 -0
- package/chat/ChatSidebar/ChatSidebar.css.js +150 -0
- package/chat/ChatSidebar/ChatSidebar.js +230 -0
- package/chat/ChatSidebar/ChatSidebar.tpl.js +18 -0
- package/chat/ChatSidebar/constants.js +11 -0
- package/chat/ChatSidebarItem/ChatSidebarItem.css.js +445 -0
- package/chat/ChatSidebarItem/ChatSidebarItem.js +304 -0
- package/chat/ChatTranscript/ChatTranscript.css.js +90 -0
- package/chat/ChatTranscript/ChatTranscript.js +244 -0
- package/chat/chat-context.js +123 -0
- package/chat/message-model.js +156 -0
- package/cli.js +20 -0
- package/control/Button/Button.css.js +93 -0
- package/control/Button/Button.js +78 -0
- package/control/Button/Button.tpl.js +3 -0
- package/control/Field/Field.css.js +91 -0
- package/control/Field/Field.js +17 -0
- package/control/Field/Field.tpl.js +3 -0
- package/core/Connection.js +47 -0
- package/core/Editor.js +449 -0
- package/core/Frame.js +33 -0
- package/core/GraphMermaid.js +348 -0
- package/core/GraphText.js +228 -0
- package/core/Node.js +145 -0
- package/core/Portal.js +106 -0
- package/core/Socket.js +187 -0
- package/core/SubgraphNode.js +121 -0
- package/core/base-path.js +55 -0
- package/core/dom-utils.js +14 -0
- package/core/index.js +18 -0
- package/core/local-cache.js +26 -0
- package/core/state-sync.js +227 -0
- package/custom-elements.json +6380 -0
- package/discover.js +240 -0
- package/display/Badge/Badge.css.js +44 -0
- package/display/Badge/Badge.js +17 -0
- package/display/Badge/Badge.tpl.js +3 -0
- package/display/Banner/Banner.css.js +61 -0
- package/display/Banner/Banner.js +17 -0
- package/display/Banner/Banner.tpl.js +3 -0
- package/display/CodeBlock/CodeBlock.css.js +194 -0
- package/display/CodeBlock/CodeBlock.js +220 -0
- package/display/CodeBlock/CodeBlock.tpl.js +11 -0
- package/display/DataTable/DataTable.css.js +101 -0
- package/display/DataTable/DataTable.js +136 -0
- package/display/DataTable/DataTable.tpl.js +13 -0
- package/display/EmptyState/EmptyState.css.js +33 -0
- package/display/EmptyState/EmptyState.js +17 -0
- package/display/EmptyState/EmptyState.tpl.js +3 -0
- package/display/EventFeed/EventFeed.css.js +145 -0
- package/display/EventFeed/EventFeed.js +64 -0
- package/display/EventFeed/EventFeed.tpl.js +14 -0
- package/display/EventFeed/EventFeedItem.js +116 -0
- package/display/EventFeed/EventFeedItem.tpl.js +22 -0
- package/display/LoadingOverlay/LoadingOverlay.css.js +91 -0
- package/display/LoadingOverlay/LoadingOverlay.js +48 -0
- package/display/LoadingOverlay/LoadingOverlay.tpl.js +12 -0
- package/display/Metric/Metric.css.js +60 -0
- package/display/Metric/Metric.js +17 -0
- package/display/Metric/Metric.tpl.js +6 -0
- package/display/OutputGraphPreview/OutputGraphPreview.css.js +122 -0
- package/display/OutputGraphPreview/OutputGraphPreview.js +89 -0
- package/display/OutputGraphPreview/OutputGraphPreview.tpl.js +13 -0
- package/display/OutputListPreview/OutputListPreview.css.js +109 -0
- package/display/OutputListPreview/OutputListPreview.js +77 -0
- package/display/OutputListPreview/OutputListPreview.tpl.js +13 -0
- package/display/SourceEditor/SourceEditor.css.js +39 -0
- package/display/SourceEditor/SourceEditor.js +129 -0
- package/display/SourceEditor/SourceEditor.tpl.js +10 -0
- package/display/SourceViewer/SourceViewer.css.js +80 -0
- package/display/SourceViewer/SourceViewer.js +418 -0
- package/display/SourceViewer/SourceViewer.tpl.js +17 -0
- package/display/StatusRibbon/StatusRibbon.css.js +73 -0
- package/display/StatusRibbon/StatusRibbon.js +87 -0
- package/display/StatusRibbon/StatusRibbon.tpl.js +7 -0
- package/display/event-feed-adapter.js +72 -0
- package/display/format-utils.js +29 -0
- package/display/highlight.js +659 -0
- package/display/icons.js +37 -0
- package/display/markdown-formatter.js +60 -0
- package/display/network-approval-page.js +487 -0
- package/display/output-preview.js +261 -0
- package/effects/CellBg/CellBg.css.js +33 -0
- package/effects/CellBg/CellBg.js +410 -0
- package/effects/CellBg/CellBg.tpl.js +5 -0
- package/graph/canvas-adapter.js +223 -0
- package/graph/graph-algorithms.js +31 -0
- package/graph/index.js +46 -0
- package/graph/model.js +176 -0
- package/graph/project-graph-build.js +66 -0
- package/graph/project-graph-metadata.js +253 -0
- package/graph/project-package.js +128 -0
- package/graph/project-runtime.js +116 -0
- package/graph/project-transaction.js +284 -0
- package/graph/skeleton-utils.js +84 -0
- package/graph/theme-contract.js +36 -0
- package/graph/transaction-parser.js +56 -0
- package/icons/MaterialSymbols.js +69 -0
- package/icons/material-symbols-outlined-400.ttf +0 -0
- package/icons/material-symbols.css +24 -0
- package/index.js +95 -0
- package/inspector/InspectorPanel/InspectorPanel.css.js +375 -0
- package/inspector/InspectorPanel/InspectorPanel.js +368 -0
- package/inspector/InspectorPanel/InspectorPanel.tpl.js +96 -0
- package/inspector/TemplatePreview/TemplatePreview.css.js +104 -0
- package/inspector/TemplatePreview/TemplatePreview.js +145 -0
- package/inspector/TemplatePreview/TemplatePreview.tpl.js +33 -0
- package/interactions/ConnectFlow.js +304 -0
- package/interactions/Drag.js +104 -0
- package/interactions/Selector.js +133 -0
- package/interactions/SnapGrid.js +66 -0
- package/interactions/Zoom.js +139 -0
- package/layout/ActionZone/ActionZone.css.js +88 -0
- package/layout/ActionZone/ActionZone.js +261 -0
- package/layout/ActionZone/ActionZone.tpl.js +11 -0
- package/layout/CrossLayoutPortalBridge/CrossLayoutPortalBridge.js +255 -0
- package/layout/Layout/Layout.css.js +91 -0
- package/layout/Layout/Layout.js +637 -0
- package/layout/Layout/Layout.tpl.js +27 -0
- package/layout/LayoutNode/LayoutNode.css.js +302 -0
- package/layout/LayoutNode/LayoutNode.js +509 -0
- package/layout/LayoutNode/LayoutNode.tpl.js +39 -0
- package/layout/LayoutPreview/LayoutPreview.css.js +46 -0
- package/layout/LayoutPreview/LayoutPreview.js +102 -0
- package/layout/LayoutPreview/LayoutPreview.tpl.js +6 -0
- package/layout/LayoutRouter/LayoutRouter.js +274 -0
- package/layout/LayoutRouter/SectionRegistry.js +135 -0
- package/layout/LayoutRouter/routerSync.js +250 -0
- package/layout/LayoutSidebar/LayoutSidebar.css.js +411 -0
- package/layout/LayoutSidebar/LayoutSidebar.js +368 -0
- package/layout/LayoutSidebar/LayoutSidebar.tpl.js +26 -0
- package/layout/LayoutSidebar/SidebarSection.css.js +20 -0
- package/layout/LayoutSidebar/SidebarSection.js +184 -0
- package/layout/LayoutSidebar/SidebarSection.tpl.js +22 -0
- package/layout/LayoutTree.js +373 -0
- package/layout/PanelMenu/PanelMenu.css.js +43 -0
- package/layout/PanelMenu/PanelMenu.js +95 -0
- package/layout/PanelMenu/PanelMenu.tpl.js +17 -0
- package/layout/ProjectTabs/ProjectTabs.css.js +188 -0
- package/layout/ProjectTabs/ProjectTabs.js +77 -0
- package/layout/ProjectTabs/ProjectTabs.tpl.js +15 -0
- package/layout/index.js +40 -0
- package/list/ListDetailShell/ListDetailShell.css.js +128 -0
- package/list/ListDetailShell/ListDetailShell.js +72 -0
- package/list/ListDetailShell/ListDetailShell.tpl.js +36 -0
- package/list/ListItem/ListItem.css.js +111 -0
- package/list/ListItem/ListItem.js +66 -0
- package/list/ListItem/ListItem.tpl.js +18 -0
- package/locale/index.js +503 -0
- package/manifest/component-registry.js +2446 -0
- package/manifest/graph-schema.js +285 -0
- package/manifest/index.js +6 -0
- package/manifest/project-schema-catalog.js +246 -0
- package/manifest/rule-catalog.js +201 -0
- package/manifest/theme-catalog.js +2149 -0
- package/manifest/ui-schema-catalog.js +334 -0
- package/menu/ContextMenu/ContextMenu.css.js +61 -0
- package/menu/ContextMenu/ContextMenu.js +82 -0
- package/menu/ContextMenu/ContextMenu.tpl.js +19 -0
- package/navigation/QuickOpen/QuickOpen.css.js +92 -0
- package/navigation/QuickOpen/QuickOpen.js +185 -0
- package/navigation/QuickOpen/QuickOpen.tpl.js +15 -0
- package/navigation/quick-open-utils.js +101 -0
- package/node/CtrlItem/CtrlItem.css.js +41 -0
- package/node/CtrlItem/CtrlItem.js +24 -0
- package/node/CtrlItem/CtrlItem.tpl.js +17 -0
- package/node/GraphFrame/GraphFrame.css.js +66 -0
- package/node/GraphFrame/GraphFrame.js +32 -0
- package/node/GraphFrame/GraphFrame.tpl.js +13 -0
- package/node/GraphNode/GraphNode.css.js +815 -0
- package/node/GraphNode/GraphNode.js +173 -0
- package/node/GraphNode/GraphNode.tpl.js +33 -0
- package/node/NodeCallout/NodeCallout.css.js +91 -0
- package/node/NodeCallout/NodeCallout.js +281 -0
- package/node/NodeCallout/NodeCallout.tpl.js +8 -0
- package/node/NodeSocket/NodeSocket.css.js +68 -0
- package/node/NodeSocket/NodeSocket.js +26 -0
- package/node/NodeSocket/NodeSocket.tpl.js +7 -0
- package/node/PortItem/PortItem.css.js +93 -0
- package/node/PortItem/PortItem.js +87 -0
- package/node/PortItem/PortItem.tpl.js +10 -0
- package/package.json +165 -0
- package/palette/PaletteBrowser/PaletteBrowser.css.js +143 -0
- package/palette/PaletteBrowser/PaletteBrowser.js +152 -0
- package/palette/PaletteBrowser/PaletteBrowser.tpl.js +23 -0
- package/plugins/History.js +408 -0
- package/plugins/Readonly.js +60 -0
- package/rules/symbiote-3x.json +170 -0
- package/schemas/component-descriptor-v1.json +91 -0
- package/schemas/component-descriptor-v2.json +145 -0
- package/schemas/graph-model-v1.json +179 -0
- package/schemas/graph-v1.json +91 -0
- package/schemas/project-package-v1.json +102 -0
- package/schemas/project-transaction-v1.json +114 -0
- package/schemas/runtime-ui-v1.json +80 -0
- package/schemas/theme-rule-block-v1.json +73 -0
- package/shapes/CircleShape.js +79 -0
- package/shapes/CommentShape.js +35 -0
- package/shapes/DiamondShape.js +130 -0
- package/shapes/NodeShape.js +79 -0
- package/shapes/PillShape.js +91 -0
- package/shapes/RectShape.js +84 -0
- package/shapes/SVGShape.js +525 -0
- package/shapes/index.js +63 -0
- package/surface/Card/Card.css.js +57 -0
- package/surface/Card/Card.js +17 -0
- package/surface/Card/Card.tpl.js +3 -0
- package/themes/Palette.js +30 -0
- package/themes/Skin.js +113 -0
- package/themes/Theme.js +82 -0
- package/themes/carbon.js +135 -0
- package/themes/dark.js +140 -0
- package/themes/default-dark.js +714 -0
- package/themes/default-provider.css +635 -0
- package/themes/default-provider.js +718 -0
- package/themes/ebook.js +136 -0
- package/themes/grey.js +137 -0
- package/themes/light.js +139 -0
- package/themes/neon.js +138 -0
- package/themes/pcb.js +273 -0
- package/themes/synthwave.js +138 -0
- package/tokens/base.json +29 -0
- package/tokens/themes/carbon.json +11 -0
- package/tokens/themes/dark.json +12 -0
- package/tokens/themes/default-dark.json +1543 -0
- package/tokens/themes/default-provider.json +1543 -0
- package/tokens/themes/ebook.json +11 -0
- package/tokens/themes/grey.json +11 -0
- package/tokens/themes/light.json +12 -0
- package/tokens/themes/neon.json +11 -0
- package/tokens/themes/pcb.json +11 -0
- package/tokens/themes/synthwave.json +11 -0
- package/toolbar/QuickToolbar/QuickToolbar.css.js +152 -0
- package/toolbar/QuickToolbar/QuickToolbar.js +529 -0
- package/toolbar/QuickToolbar/QuickToolbar.tpl.js +34 -0
- package/tree/TreePanel/TreePanel.css.js +112 -0
- package/tree/TreePanel/TreePanel.js +147 -0
- package/tree/TreePanel/TreePanel.tpl.js +18 -0
- package/tree/TreeView/TreeView.css.js +122 -0
- package/tree/TreeView/TreeView.js +365 -0
- package/tree/TreeView/TreeView.tpl.js +10 -0
- package/ui/dialogs.js +221 -0
- package/ui/host-adapters.js +114 -0
- package/ui/index.js +660 -0
- package/ui/locale.js +50 -0
- package/ui/overlay-stack.js +89 -0
- package/ui/shared-styles.js +26 -0
- package/webmcp.js +37 -0
- package/xr/deep-graph.js +646 -0
- package/xr/emulation.js +198 -0
- package/xr/gesture.js +228 -0
- package/xr/html-canvas-renderer.js +472 -0
- package/xr/index.js +15 -0
- package/xr/layout-projection.js +1046 -0
- package/xr/panel-frame.js +128 -0
- package/xr/panel-host.js +267 -0
- package/xr/pointer.js +258 -0
- package/xr/scene-controller.js +242 -0
- package/xr/spatial-scene.js +212 -0
- package/xr/theme-bridge.js +105 -0
- package/xr/three-webxr-adapter.js +3439 -0
- package/xr/webgl-layer-renderer.js +419 -0
- package/xr/webxr.js +679 -0
- package/xr/workbench.js +516 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* State Sync — generic versioned state synchronization engine.
|
|
3
|
+
*
|
|
4
|
+
* Connects to a WebSocket endpoint, receives snapshots + delta patches,
|
|
5
|
+
* maintains a local mirror and emits fine-grained path-based events.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* const sync = createStateSync()
|
|
9
|
+
* sync.connect('wss://host/ws/state', { onMessage: (msg) => ... })
|
|
10
|
+
* sync.on('tasks', (tasks) => { ... })
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Create an independent state sync instance.
|
|
15
|
+
*
|
|
16
|
+
* @param {object} [config]
|
|
17
|
+
* @param {number} [config.reconnectMs=2000] - Reconnect delay in ms. Set 0 to disable.
|
|
18
|
+
* @returns {{ on, get, commit, connect, disconnect, __test }}
|
|
19
|
+
*/
|
|
20
|
+
export function createStateSync(config = {}) {
|
|
21
|
+
/** @type {WebSocket|null} */
|
|
22
|
+
let _ws = null
|
|
23
|
+
/** @type {number|null} */
|
|
24
|
+
let _reconnectTimer = null
|
|
25
|
+
/** @type {number} last known version */
|
|
26
|
+
let _version = 0
|
|
27
|
+
/** @type {object} local state mirror */
|
|
28
|
+
let _state = {}
|
|
29
|
+
/** @type {Map<string, Set<Function>>} */
|
|
30
|
+
const _listeners = new Map()
|
|
31
|
+
|
|
32
|
+
let _wsUrl = null
|
|
33
|
+
let _onMessage = null
|
|
34
|
+
let _reconnectMs = config.reconnectMs !== undefined ? config.reconnectMs : 2000
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Subscribe to changes on a specific path prefix.
|
|
38
|
+
* Callback fires with the value at that path.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} key - top-level key (e.g. 'tasks', 'chats', 'ui')
|
|
41
|
+
* @param {(value: any, key: string) => void} cb
|
|
42
|
+
* @returns {() => void} unsubscribe
|
|
43
|
+
*/
|
|
44
|
+
function on(key, cb) {
|
|
45
|
+
if (!_listeners.has(key)) _listeners.set(key, new Set())
|
|
46
|
+
_listeners.get(key).add(cb)
|
|
47
|
+
// Deliver current value immediately if available
|
|
48
|
+
if (_state[key] !== undefined) {
|
|
49
|
+
try { cb(_state[key], key) } catch (e) { console.error('[stateSync] initial delivery error:', e) }
|
|
50
|
+
}
|
|
51
|
+
return () => _listeners.get(key)?.delete(cb)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Get current value at path.
|
|
56
|
+
* @param {string} path - slash-separated path
|
|
57
|
+
* @returns {any}
|
|
58
|
+
*/
|
|
59
|
+
function get(path) {
|
|
60
|
+
let parts = path.split('/')
|
|
61
|
+
let obj = _state
|
|
62
|
+
for (let p of parts) {
|
|
63
|
+
if (obj == null) return undefined
|
|
64
|
+
obj = obj[p]
|
|
65
|
+
}
|
|
66
|
+
return obj
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Send a commit to the server.
|
|
71
|
+
* @param {Array<{op: string, path: string, value?: any}>} ops
|
|
72
|
+
* @param {string} [source='browser']
|
|
73
|
+
*/
|
|
74
|
+
function commit(ops, source = 'browser') {
|
|
75
|
+
if (!_ws || _ws.readyState !== WebSocket.OPEN) return
|
|
76
|
+
_ws.send(JSON.stringify({
|
|
77
|
+
method: 'commit',
|
|
78
|
+
params: { ops, source },
|
|
79
|
+
}))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Fire listeners for affected paths. */
|
|
83
|
+
function _notify(affectedPaths) {
|
|
84
|
+
let notified = new Set()
|
|
85
|
+
for (let path of affectedPaths) {
|
|
86
|
+
// Notify the top-level key
|
|
87
|
+
let topKey = path.split('/')[0]
|
|
88
|
+
if (!notified.has(topKey) && _listeners.has(topKey)) {
|
|
89
|
+
notified.add(topKey)
|
|
90
|
+
let val = _state[topKey]
|
|
91
|
+
for (let cb of _listeners.get(topKey)) {
|
|
92
|
+
try { cb(val, topKey) } catch (e) { console.error('[stateSync] listener error:', e) }
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Always notify '*' wildcard
|
|
97
|
+
if (_listeners.has('*')) {
|
|
98
|
+
for (let cb of _listeners.get('*')) {
|
|
99
|
+
try { cb(_state, '*') } catch (e) { console.error('[stateSync] wildcard listener error:', e) }
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Apply a snapshot (full state replace). */
|
|
105
|
+
function _applySnapshot(params) {
|
|
106
|
+
_state = params.state || {}
|
|
107
|
+
_version = params.v || 0
|
|
108
|
+
// Notify all known keys
|
|
109
|
+
let keys = new Set([...Object.keys(_state), ..._listeners.keys()])
|
|
110
|
+
_notify([...keys])
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Apply a delta patch. */
|
|
114
|
+
function _applyPatch(params) {
|
|
115
|
+
let { v, ops } = params
|
|
116
|
+
if (!ops) return
|
|
117
|
+
_version = v
|
|
118
|
+
let affected = []
|
|
119
|
+
for (let op of ops) {
|
|
120
|
+
let path = op.path
|
|
121
|
+
affected.push(path)
|
|
122
|
+
let parts = path.split('/')
|
|
123
|
+
if (op.op === 'delete' || op.op === 'del') {
|
|
124
|
+
// Delete
|
|
125
|
+
let obj = _state
|
|
126
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
127
|
+
if (obj == null) break
|
|
128
|
+
obj = obj[parts[i]]
|
|
129
|
+
}
|
|
130
|
+
if (obj) delete obj[parts[parts.length - 1]]
|
|
131
|
+
} else if (op.op === 'merge') {
|
|
132
|
+
let obj = _state
|
|
133
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
134
|
+
if (obj[parts[i]] == null) obj[parts[i]] = {}
|
|
135
|
+
obj = obj[parts[i]]
|
|
136
|
+
}
|
|
137
|
+
let key = parts[parts.length - 1]
|
|
138
|
+
let current = obj[key]
|
|
139
|
+
if (current && typeof current === 'object' && !Array.isArray(current)) {
|
|
140
|
+
obj[key] = { ...current, ...(op.value || {}) }
|
|
141
|
+
} else {
|
|
142
|
+
obj[key] = op.value
|
|
143
|
+
}
|
|
144
|
+
} else {
|
|
145
|
+
// Set
|
|
146
|
+
let obj = _state
|
|
147
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
148
|
+
if (obj[parts[i]] == null) obj[parts[i]] = {}
|
|
149
|
+
obj = obj[parts[i]]
|
|
150
|
+
}
|
|
151
|
+
obj[parts[parts.length - 1]] = op.value
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
_notify(affected)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Connect to a WebSocket endpoint.
|
|
159
|
+
*
|
|
160
|
+
* @param {string} [wsUrl] - WebSocket URL. Required on first call, stored for reconnects.
|
|
161
|
+
* @param {object} [options]
|
|
162
|
+
* @param {(msg: object) => void} [options.onMessage] - Hook called with every parsed message before internal processing.
|
|
163
|
+
* @param {number} [options.reconnectMs] - Override reconnect delay.
|
|
164
|
+
*/
|
|
165
|
+
function connect(wsUrl, options = {}) {
|
|
166
|
+
if (_ws) return
|
|
167
|
+
if (wsUrl) _wsUrl = wsUrl
|
|
168
|
+
if (options.onMessage) _onMessage = options.onMessage
|
|
169
|
+
if (options.reconnectMs !== undefined) _reconnectMs = options.reconnectMs
|
|
170
|
+
|
|
171
|
+
let url = _wsUrl
|
|
172
|
+
if (_version > 0) url += `${url.includes('?') ? '&' : '?'}since=${_version}`
|
|
173
|
+
|
|
174
|
+
_ws = new WebSocket(url)
|
|
175
|
+
|
|
176
|
+
_ws.onopen = () => {
|
|
177
|
+
if (_reconnectTimer) {
|
|
178
|
+
clearTimeout(_reconnectTimer)
|
|
179
|
+
_reconnectTimer = null
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
_ws.onmessage = ({ data }) => {
|
|
184
|
+
try {
|
|
185
|
+
let msg = JSON.parse(data)
|
|
186
|
+
if (_onMessage) _onMessage(msg)
|
|
187
|
+
if (msg.method === 'snapshot') _applySnapshot(msg.params)
|
|
188
|
+
else if (msg.method === 'patch') _applyPatch(msg.params)
|
|
189
|
+
} catch (e) {
|
|
190
|
+
console.error('[stateSync] parse error:', e)
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
_ws.onclose = () => {
|
|
195
|
+
_ws = null
|
|
196
|
+
if (_reconnectMs > 0) {
|
|
197
|
+
_reconnectTimer = setTimeout(() => connect(), _reconnectMs)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
_ws.onerror = () => {}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function disconnect() {
|
|
205
|
+
if (_reconnectTimer) {
|
|
206
|
+
clearTimeout(_reconnectTimer)
|
|
207
|
+
_reconnectTimer = null
|
|
208
|
+
}
|
|
209
|
+
if (_ws) {
|
|
210
|
+
_ws.close()
|
|
211
|
+
_ws = null
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
on,
|
|
217
|
+
get,
|
|
218
|
+
commit,
|
|
219
|
+
connect,
|
|
220
|
+
disconnect,
|
|
221
|
+
__test: {
|
|
222
|
+
applySnapshot: _applySnapshot,
|
|
223
|
+
applyPatch: _applyPatch,
|
|
224
|
+
getState: () => _state,
|
|
225
|
+
},
|
|
226
|
+
}
|
|
227
|
+
}
|