openvibers/openvibe.shared dock-chat-edits.js
dock-chat-edits.js
| 1 | /* Edit events for an open Dock DM, over Chat's socket: another of my devices (or this one) edited a message, and the |
| 2 | Dock's apply() redraws it. Loaded when the conversation opens. Editing itself is the Dock's own (4.220.0: the box is the |
| 3 | editor, a pencil at the bubble's corner starts it; no browser prompt). */ |
| 4 | (function (root) { |
| 5 | ; |
| 6 | root.OpenVibeDockChatEdits = function ({ id, apply, tokenOf, chatBase, active }) { |
| 7 | let socket = null, timer = null, stopped = false; |
| 8 | async function connect() { |
| 9 | if (stopped || !active() || !root.WebSocket) return; |
| 10 | const token = await tokenOf(); |
| 11 | if (!token || stopped || !active()) return; |
| 12 | const ws = new root.WebSocket(chatBase().replace(/^http/, 'ws') + '/ws/chat'); |
| 13 | socket = ws; |
| 14 | ws.onopen = () => ws.send(JSON.stringify({ type: 'join', token })); |
| 15 | ws.onmessage = (event) => { let frame; try { frame = JSON.parse(event.data); } catch { return; } if (frame.type === 'dm_edited' && String(frame.conversation_id) === id && active()) apply(frame.message); }; |
| 16 | ws.onclose = () => { if (socket === ws) socket = null; if (!stopped && active()) timer = setTimeout(connect, 3000); }; |
| 17 | } |
| 18 | connect().catch(() => {}); |
| 19 | return { stop() { stopped = true; clearTimeout(timer); if (socket) { socket.onclose = null; socket.close(); socket = null; } } }; |
| 20 | }; |
| 21 | })(typeof window === 'undefined' ? globalThis : window); |
| 22 |