fix: streaming polish — bind diff, state snapshot, dead code cleanup

- bind: input handler now emits _streamDiff so typed text syncs
- Peers broadcast full state snapshot on WS open (late joiners sync)
- Removed dead _handleRemoteInput 0x31 case (old source/receiver model)
- Preserved scroll (0x50), pointer, and key handlers
This commit is contained in:
enzotar 2026-02-25 21:55:49 -08:00
parent 8c9f5e8bfb
commit 0290ed464a

View file

@ -456,10 +456,10 @@ impl JsEmitter {
"DS.effect(() => {{ {}.value = {}.value; }});",
node_var, signal_name
));
// Input -> signal
// Input -> signal (with streaming diff)
self.emit_line(&format!(
"{}.addEventListener('input', (e) => {{ {}.value = e.target.value; DS.flush(); }});",
node_var, signal_name
"{}.addEventListener('input', (e) => {{ {}.value = e.target.value; DS._streamDiff(\"{}\", e.target.value); DS.flush(); }});",
node_var, signal_name, signal_name
));
}
"class" => {
@ -1867,7 +1867,16 @@ const DS = (() => {
}
};
_streamWs.onclose = function() { setTimeout(function() { _initStream(url, mode); }, 2000); };
_streamWs.onopen = function() {
console.log('[ds-stream] Peer connected:', peerUrl);
// Broadcast full state snapshot so other peers can sync
var fullState = { _pid: _peerId };
for (var name in _signalRegistry) {
var sig = _signalRegistry[name];
fullState[name] = (typeof sig === 'object' && sig !== null && '_value' in sig) ? sig._value : sig;
}
_streamSend(0x31, 0, new TextEncoder().encode(JSON.stringify(fullState)));
};
}
function _streamSend(type, flags, payload) {
@ -1936,16 +1945,6 @@ const DS = (() => {
case 0x50:
emit('remote_scroll', { dx: view.getInt16(0, true), dy: view.getInt16(2, true) });
break;
case 0x31: {
// Signal diff from a receiver — apply locally AND rebroadcast to all receivers
var json = new TextDecoder().decode(payload);
_applyRemoteDiff(json);
// Rebroadcast via source WS so all other receivers get it
if (_streamWs && _streamWs.readyState === 1) {
_streamSend(0x31, 0, payload);
}
break;
}
}
}