wenay-react2 1.0.33 → 1.0.34
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/lib/common/api.d.ts +1 -0
- package/lib/common/api.js +3 -0
- package/lib/common/src/components/Toolbar/Toolbar.d.ts +105 -0
- package/lib/common/src/components/Toolbar/Toolbar.js +210 -0
- package/lib/common/src/components/Toolbar/index.d.ts +1 -0
- package/lib/common/src/components/Toolbar/index.js +1 -0
- package/lib/common/src/hooks/index.d.ts +2 -0
- package/lib/common/src/hooks/index.js +2 -0
- package/lib/common/src/hooks/useReorder.d.ts +49 -0
- package/lib/common/src/hooks/useReorder.js +134 -0
- package/lib/common/src/hooks/useReorderBoard.d.ts +61 -0
- package/lib/common/src/hooks/useReorderBoard.js +232 -0
- package/lib/common/src/hooks/useReplay.d.ts +63 -0
- package/lib/common/src/hooks/useReplay.js +128 -8
- package/lib/common/src/styles/tokens.d.ts +17 -0
- package/lib/common/src/styles/tokens.js +17 -0
- package/lib/style/style.css +128 -0
- package/lib/style/tokens.css +14 -0
- package/package.json +2 -2
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { useDraggableApi } from './useDraggable';
|
|
3
|
+
export function useReorderBoard(o) {
|
|
4
|
+
const oRef = useRef(o);
|
|
5
|
+
oRef.current = o;
|
|
6
|
+
const colEls = useRef(new Map());
|
|
7
|
+
const refCbs = useRef(new Map());
|
|
8
|
+
const [dragKey, setDragKey] = useState(null);
|
|
9
|
+
const geom = useRef(null);
|
|
10
|
+
const measureCache = useRef(null);
|
|
11
|
+
/** stable callback ref for a column div; columns can be added at runtime */
|
|
12
|
+
function columnRef(col) {
|
|
13
|
+
let cb = refCbs.current.get(col);
|
|
14
|
+
if (!cb) {
|
|
15
|
+
cb = el => { el ? colEls.current.set(col, el) : colEls.current.delete(col); };
|
|
16
|
+
refCbs.current.set(col, cb);
|
|
17
|
+
}
|
|
18
|
+
return cb;
|
|
19
|
+
}
|
|
20
|
+
const local = (v) => v / (geom.current?.scale ?? 1);
|
|
21
|
+
/** Nearest column by clamped distance to its rect, then the insertion index
|
|
22
|
+
* = how many of that column's items (start centers, excluding the dragged
|
|
23
|
+
* one) sit above the dragged center. Targeting always runs against the
|
|
24
|
+
* geometry measured at drag START (anti-oscillation, same as useReorder). */
|
|
25
|
+
function dragTarget(dx, dy) {
|
|
26
|
+
const g = geom.current;
|
|
27
|
+
if (!g)
|
|
28
|
+
return { col: '', index: 0 };
|
|
29
|
+
const cx = g.draggedCenter.x + dx, cy = g.draggedCenter.y + dy;
|
|
30
|
+
let best = null, bestD = Infinity;
|
|
31
|
+
for (const [col, r] of g.colRect) {
|
|
32
|
+
const px = Math.max(r.x, Math.min(r.x + r.w, cx));
|
|
33
|
+
const py = Math.max(r.y, Math.min(r.y + r.h, cy));
|
|
34
|
+
const d = (px - cx) ** 2 + (py - cy) ** 2;
|
|
35
|
+
if (d < bestD) {
|
|
36
|
+
bestD = d;
|
|
37
|
+
best = col;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (best == null)
|
|
41
|
+
return g.from;
|
|
42
|
+
let index = 0;
|
|
43
|
+
for (const c of geom.current.centers.get(best) ?? [])
|
|
44
|
+
if (c.key != dragKey && c.y < cy)
|
|
45
|
+
index++;
|
|
46
|
+
return { col: best, index };
|
|
47
|
+
}
|
|
48
|
+
/** Simulated commit: the dragged key removed from its column and inserted at
|
|
49
|
+
* over - preview and drop share this, so they agree by construction. */
|
|
50
|
+
function movedColumns(key, over) {
|
|
51
|
+
return oRef.current.columns.map(c => {
|
|
52
|
+
let items = c.items.filter(k => k != key);
|
|
53
|
+
if (c.key == over.col) {
|
|
54
|
+
const i = Math.max(0, Math.min(items.length, over.index));
|
|
55
|
+
items = items.slice(0, i).concat(key, items.slice(i));
|
|
56
|
+
}
|
|
57
|
+
return { key: c.key, items };
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/** FLIP across columns, all offset-based (transform/transition-immune, see
|
|
61
|
+
* useReorder): the dragged block leaves flow via display:none, survivors get
|
|
62
|
+
* their preview CSS `order`, and the landing slot becomes a real margin gap
|
|
63
|
+
* (draggedHeight + row-gap) on the neighbour - so the column's own CSS
|
|
64
|
+
* gravity decides who moves aside (flex-end pushes the blocks ABOVE up).
|
|
65
|
+
* One synchronous apply-read-revert, cached per target slot. */
|
|
66
|
+
function measured(key, over) {
|
|
67
|
+
const cacheKey = over.col + '#' + over.index;
|
|
68
|
+
if (measureCache.current?.key == cacheKey)
|
|
69
|
+
return measureCache.current.pos;
|
|
70
|
+
const g = geom.current;
|
|
71
|
+
const keyToEl = new Map();
|
|
72
|
+
for (const c of oRef.current.columns) {
|
|
73
|
+
const el = colEls.current.get(c.key);
|
|
74
|
+
if (!el)
|
|
75
|
+
continue;
|
|
76
|
+
const kids = el.children;
|
|
77
|
+
c.items.forEach((k, i) => { const kid = kids[i]; if (kid)
|
|
78
|
+
keyToEl.set(k, kid); });
|
|
79
|
+
}
|
|
80
|
+
const preview = movedColumns(key, over);
|
|
81
|
+
const saved = [];
|
|
82
|
+
const save = (el) => saved.push({ el, order: el.style.order, display: el.style.display, mt: el.style.marginTop, mb: el.style.marginBottom });
|
|
83
|
+
for (const pc of preview) {
|
|
84
|
+
pc.items.forEach((k, i) => {
|
|
85
|
+
const el = keyToEl.get(k);
|
|
86
|
+
if (!el)
|
|
87
|
+
return;
|
|
88
|
+
save(el);
|
|
89
|
+
if (k == key)
|
|
90
|
+
el.style.display = 'none';
|
|
91
|
+
else
|
|
92
|
+
el.style.order = String(i);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
const target = preview.find(c => c.key == over.col);
|
|
96
|
+
if (target) {
|
|
97
|
+
const gap = g.draggedSize.h + (g.rowGap.get(over.col) ?? 0);
|
|
98
|
+
const afterEl = keyToEl.get(target.items[over.index + 1] ?? '');
|
|
99
|
+
const beforeEl = keyToEl.get(target.items[over.index - 1] ?? '');
|
|
100
|
+
if (afterEl)
|
|
101
|
+
afterEl.style.marginTop = gap + 'px';
|
|
102
|
+
else if (beforeEl)
|
|
103
|
+
beforeEl.style.marginBottom = gap + 'px';
|
|
104
|
+
}
|
|
105
|
+
const pos = new Map();
|
|
106
|
+
for (const c of oRef.current.columns)
|
|
107
|
+
for (const k of c.items) {
|
|
108
|
+
if (k == key)
|
|
109
|
+
continue;
|
|
110
|
+
const el = keyToEl.get(k);
|
|
111
|
+
if (el)
|
|
112
|
+
pos.set(k, { x: el.offsetLeft, y: el.offsetTop });
|
|
113
|
+
}
|
|
114
|
+
saved.reverse().forEach(s => { s.el.style.order = s.order; s.el.style.display = s.display; s.el.style.marginTop = s.mt; s.el.style.marginBottom = s.mb; });
|
|
115
|
+
measureCache.current = { key: cacheKey, pos };
|
|
116
|
+
return pos;
|
|
117
|
+
}
|
|
118
|
+
const drag = useDraggableApi({ holdMs: o.holdMs ?? 0, onDragEnd: function commitBoard(final) {
|
|
119
|
+
const key = dragKey;
|
|
120
|
+
setDragKey(null);
|
|
121
|
+
measureCache.current = null;
|
|
122
|
+
const g = geom.current;
|
|
123
|
+
geom.current = null;
|
|
124
|
+
if (key == null || !g)
|
|
125
|
+
return;
|
|
126
|
+
geom.current = g; // dragTarget/local need it for this last computation
|
|
127
|
+
const over = dragTarget(local(final.x), local(final.y));
|
|
128
|
+
const next = movedColumns(key, over);
|
|
129
|
+
const cur = oRef.current.columns;
|
|
130
|
+
const committed = next.some((c, i) => c.items.length != cur[i].items.length || c.items.some((k, j) => k != cur[i].items[j]));
|
|
131
|
+
geom.current = null;
|
|
132
|
+
if (committed)
|
|
133
|
+
oRef.current.commit(next);
|
|
134
|
+
oRef.current.onDragEnd?.({ key, from: g.from, over, committed });
|
|
135
|
+
} });
|
|
136
|
+
function beginDrag(key, e) {
|
|
137
|
+
if (oRef.current.canDrag && !oRef.current.canDrag(key))
|
|
138
|
+
return false;
|
|
139
|
+
if (e.target.closest('input, button, select, textarea, a'))
|
|
140
|
+
return false;
|
|
141
|
+
const cols = oRef.current.columns;
|
|
142
|
+
let from = null;
|
|
143
|
+
for (const c of cols) {
|
|
144
|
+
const i = c.items.indexOf(key);
|
|
145
|
+
if (i != -1) {
|
|
146
|
+
from = { col: c.key, index: i };
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const fromEl = from && colEls.current.get(from.col);
|
|
151
|
+
if (!from || !fromEl)
|
|
152
|
+
return false;
|
|
153
|
+
const scale = fromEl.offsetWidth ? fromEl.getBoundingClientRect().width / fromEl.offsetWidth : 1;
|
|
154
|
+
const g = { scale, from, colRect: new Map(), centers: new Map(), startOffset: new Map(), rowGap: new Map(), draggedCenter: { x: 0, y: 0 }, draggedSize: { w: 0, h: 0 } };
|
|
155
|
+
for (const c of cols) {
|
|
156
|
+
const el = colEls.current.get(c.key);
|
|
157
|
+
if (!el)
|
|
158
|
+
continue;
|
|
159
|
+
const r = el.getBoundingClientRect();
|
|
160
|
+
g.colRect.set(c.key, { x: r.x / scale, y: r.y / scale, w: r.width / scale, h: r.height / scale });
|
|
161
|
+
g.rowGap.set(c.key, parseFloat(getComputedStyle(el).rowGap) || 0);
|
|
162
|
+
const kids = el.children;
|
|
163
|
+
const cs = [];
|
|
164
|
+
c.items.forEach((k, i) => {
|
|
165
|
+
const kid = kids[i];
|
|
166
|
+
if (!kid)
|
|
167
|
+
return;
|
|
168
|
+
const kr = kid.getBoundingClientRect();
|
|
169
|
+
cs.push({ key: k, y: (kr.y + kr.height / 2) / scale });
|
|
170
|
+
g.startOffset.set(k, { x: kid.offsetLeft, y: kid.offsetTop });
|
|
171
|
+
if (k == key) {
|
|
172
|
+
g.draggedCenter = { x: (kr.x + kr.width / 2) / scale, y: (kr.y + kr.height / 2) / scale };
|
|
173
|
+
g.draggedSize = { w: kid.offsetWidth, h: kid.offsetHeight };
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
g.centers.set(c.key, cs);
|
|
177
|
+
}
|
|
178
|
+
geom.current = g;
|
|
179
|
+
measureCache.current = null;
|
|
180
|
+
setDragKey(key);
|
|
181
|
+
oRef.current.onDragStart?.({ key, from });
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
const over = dragKey != null && geom.current
|
|
185
|
+
? dragTarget(local(drag.position.x), local(drag.position.y)) : null;
|
|
186
|
+
// callbacks ride effects (post-render), reading fresh options via oRef
|
|
187
|
+
const prevOverRef = useRef(null);
|
|
188
|
+
useEffect(() => {
|
|
189
|
+
if (dragKey == null || !over) {
|
|
190
|
+
prevOverRef.current = null;
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const prev = prevOverRef.current;
|
|
194
|
+
if (!prev || prev.col != over.col || prev.index != over.index) {
|
|
195
|
+
oRef.current.onOverChange?.({ key: dragKey, over, prev });
|
|
196
|
+
prevOverRef.current = { ...over };
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
const px = dragKey != null ? drag.position.x : 0, py = dragKey != null ? drag.position.y : 0;
|
|
200
|
+
useEffect(() => {
|
|
201
|
+
if (dragKey != null && over)
|
|
202
|
+
oRef.current.onDragMove?.({ key: dragKey, over, position: { x: px, y: py } });
|
|
203
|
+
}, [dragKey, px, py]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
204
|
+
function item(key) {
|
|
205
|
+
const active = dragKey != null;
|
|
206
|
+
const dragging = key == dragKey;
|
|
207
|
+
let style;
|
|
208
|
+
if (active && geom.current && over) {
|
|
209
|
+
if (dragging) {
|
|
210
|
+
style = { transform: `translate(${local(drag.position.x)}px, ${local(drag.position.y)}px)` };
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
const pos = measured(dragKey, over);
|
|
214
|
+
const a = geom.current.startOffset.get(key), b = pos.get(key);
|
|
215
|
+
if (a && b && (a.x != b.x || a.y != b.y))
|
|
216
|
+
style = { transform: `translate(${b.x - a.x}px, ${b.y - a.y}px)` };
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
props: {
|
|
221
|
+
onMouseDown(e) { if (e.button == 0 && beginDrag(key, e))
|
|
222
|
+
drag.props.onMouseDown(e); },
|
|
223
|
+
onTouchStart(e) { if (beginDrag(key, e))
|
|
224
|
+
drag.props.onTouchStart(e); },
|
|
225
|
+
},
|
|
226
|
+
style,
|
|
227
|
+
dragging,
|
|
228
|
+
active,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
return { columnRef, item, dragKey, over };
|
|
232
|
+
}
|
|
@@ -21,6 +21,20 @@ export type UseReplaySubscribeOptions = {
|
|
|
21
21
|
staleMs?: number;
|
|
22
22
|
/** Edge-triggered mirror of common2's onStale (fresh<->stale transitions only). Goes through a ref — a new identity does not resubscribe. */
|
|
23
23
|
onStale?: (info: StaleInfo) => void;
|
|
24
|
+
/**
|
|
25
|
+
* Lag policy of the wire subscription (frame model): 'queue' (default) — the socket buffers
|
|
26
|
+
* everything, nothing is ever skipped; 'frame' — rides the server's frameLine when the remote
|
|
27
|
+
* has one: on lag the server DROPS events for this client and recovers with a mini-frame.
|
|
28
|
+
* Without a frameLine (old server, in-proc line) 'frame' degrades to 'queue' in common2.
|
|
29
|
+
* This picks the wire surface, so changing it resubscribes (reconnects by tail under keepSeq).
|
|
30
|
+
*/
|
|
31
|
+
policy?: 'queue' | 'frame';
|
|
32
|
+
/**
|
|
33
|
+
* Opaque pass-through for the line's `frame` condenser on catch-up (which condensation rule
|
|
34
|
+
* this client wants). Captured at subscribe time through a ref — a new identity does not
|
|
35
|
+
* resubscribe; the next (re)subscribe reads the latest value.
|
|
36
|
+
*/
|
|
37
|
+
hint?: unknown;
|
|
24
38
|
};
|
|
25
39
|
export type ReplaySubscribeController = {
|
|
26
40
|
/** Keyframe/tail catch-up finished, events are live from here. */
|
|
@@ -64,6 +78,55 @@ export type StoreReplayMirrorController<T extends object> = StoreReplaySyncContr
|
|
|
64
78
|
* so reconnect is a journal tail, not a keyframe. A new `remote` recreates the store.
|
|
65
79
|
*/
|
|
66
80
|
export declare function useStoreReplayMirror<T extends object>(remote: ReplayRemote<[StorePatch]> | null | undefined, initial: T, options?: UseStoreReplaySyncOptions): StoreReplayMirrorController<T>;
|
|
81
|
+
export type UseReplayFrameOptions = {
|
|
82
|
+
/** Pull period, ms. Default 300. */
|
|
83
|
+
intervalMs?: number;
|
|
84
|
+
/**
|
|
85
|
+
* Start position: frame() brings the consumer from here to head. Omit = keyframe start:
|
|
86
|
+
* keyframe() is polled until the line has one (an empty line is "nothing yet", not an error).
|
|
87
|
+
* A sacred line (no keyframe) NEEDS an explicit since (0 = full tail) — omitted, it waits forever.
|
|
88
|
+
*/
|
|
89
|
+
since?: number;
|
|
90
|
+
/** Keep the last folded seq across remote-identity-stable resubscribes. Default true. */
|
|
91
|
+
keepSeq?: boolean;
|
|
92
|
+
/** false = stop pulling (and drop the timer). Default true. */
|
|
93
|
+
enabled?: boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Opaque pass-through for the line's `frame` condenser (which condensation rule this client
|
|
96
|
+
* wants). Read through a ref on EVERY pull — a new identity neither resubscribes nor is missed.
|
|
97
|
+
*/
|
|
98
|
+
hint?: unknown;
|
|
99
|
+
onSeq?: (seq: number) => void;
|
|
100
|
+
/** frame() failed (network, or a sacred line evicted past our seq — loud by design). Pulling STOPS until restart(). */
|
|
101
|
+
onError?: (e: unknown) => void;
|
|
102
|
+
};
|
|
103
|
+
export type ReplayFrameController = {
|
|
104
|
+
/** First successful pull finished (like replaySubscribe's ready — even if the line was still empty). */
|
|
105
|
+
readonly ready: boolean;
|
|
106
|
+
/** Last pull error; pulling is stopped while set (restart() clears and re-arms). */
|
|
107
|
+
readonly error: unknown;
|
|
108
|
+
/** Last folded seq (reconnect point). Getter — reading it does not re-render. */
|
|
109
|
+
seq(): number;
|
|
110
|
+
/** Pull out of schedule now; resolves after folding. hint omitted = the latest options.hint. */
|
|
111
|
+
pull(hint?: unknown): Promise<void>;
|
|
112
|
+
/** Re-arm after an error / jump: since omitted = continue from the current seq. */
|
|
113
|
+
restart(since?: number): void;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Pull a replay line at YOUR pace (the frame model of common2): a timer around
|
|
117
|
+
* `remote.frame(seq, hint)` — the server condenses the tail via the line's `frame` lambda
|
|
118
|
+
* (mini-frame), so a slow consumer never accumulates a backlog and never holds a live socket
|
|
119
|
+
* subscription. Complements useReplaySubscribe (push): use pull when the consumer wants its own
|
|
120
|
+
* cadence (e.g. 500ms UI refresh over a fast line) or a client-picked condensation rule (hint).
|
|
121
|
+
*
|
|
122
|
+
* Folding contract mirrors the push path: envelopes arrive seq-ascending, already-seen seq are
|
|
123
|
+
* skipped, a keyframe recovery is just an envelope of the same event type. Fresh start (no since)
|
|
124
|
+
* = keyframe start like replaySubscribe: keyframe() is polled until the line has one; a sacred
|
|
125
|
+
* line needs an explicit since. Overlapping pulls are never issued (a slow frame() call skips
|
|
126
|
+
* timer ticks). A remote without frame() (old server) fails loudly via onError — there is
|
|
127
|
+
* deliberately no tail fallback here (that is replaySubscribe's job).
|
|
128
|
+
*/
|
|
129
|
+
export declare function useReplayFrame<Z extends any[]>(remote: ReplayRemote<Z> | null | undefined, cb: (...event: Z) => void, options?: UseReplayFrameOptions): ReplayFrameController;
|
|
67
130
|
export type ReplayHistoryLike<Z extends any[]> = {
|
|
68
131
|
at(where?: {
|
|
69
132
|
seq?: number;
|
|
@@ -13,10 +13,14 @@ import { ObserveAll2, Replay } from "wenay-common2";
|
|
|
13
13
|
* outside via onSeq and pass it back as `since` (see the QA card for the pattern);
|
|
14
14
|
* - a different `remote` identity always starts from scratch (seq from another line is meaningless);
|
|
15
15
|
* - freshness (staleMs/onStale, controller.stale/lastTs()) is detected by wenay-common2;
|
|
16
|
-
* the hooks only mirror the fresh<->stale edges into React state
|
|
16
|
+
* the hooks only mirror the fresh<->stale edges into React state;
|
|
17
|
+
* - lag policy is the consumer's pick per subscription (frame model of common2 rev2):
|
|
18
|
+
* {policy: 'frame'} rides the server's frameLine when the remote has one (on lag the server
|
|
19
|
+
* drops and recovers with a mini-frame; old servers/in-proc lines degrade to 'queue');
|
|
20
|
+
* pull-at-own-pace is a separate hook (useReplayFrame) — a timer around remote.frame().
|
|
17
21
|
*
|
|
18
|
-
* Server-side parts of the stack (conflateReplay, archiveReplay
|
|
19
|
-
* and intentionally have no hooks here.
|
|
22
|
+
* Server-side parts of the stack (conflateReplay, archiveReplay, createRpcServerAuto replayOpts)
|
|
23
|
+
* are per-connection/per-process and intentionally have no hooks here.
|
|
20
24
|
*/
|
|
21
25
|
function useLatestRef(value) {
|
|
22
26
|
const ref = useRef(value);
|
|
@@ -30,9 +34,10 @@ function useLatestRef(value) {
|
|
|
30
34
|
* cb goes through a ref — a new cb identity does not resubscribe.
|
|
31
35
|
*/
|
|
32
36
|
export function useReplaySubscribe(remote, cb, options = {}) {
|
|
33
|
-
const { since, keepSeq = true, enabled = true, onSeq, onError, staleMs, onStale } = options;
|
|
37
|
+
const { since, keepSeq = true, enabled = true, onSeq, onError, staleMs, onStale, policy, hint } = options;
|
|
34
38
|
const cbRef = useLatestRef(cb);
|
|
35
39
|
const hooksRef = useLatestRef({ onSeq, onError, onStale });
|
|
40
|
+
const hintRef = useLatestRef(hint);
|
|
36
41
|
const seqRef = useRef(since);
|
|
37
42
|
const subRef = useRef(null);
|
|
38
43
|
const lastRemoteRef = useRef(undefined);
|
|
@@ -55,6 +60,8 @@ export function useReplaySubscribe(remote, cb, options = {}) {
|
|
|
55
60
|
setStale(false);
|
|
56
61
|
const off = Replay.replaySubscribe(remote, (...event) => cbRef.current(...event), {
|
|
57
62
|
since: seqRef.current,
|
|
63
|
+
policy,
|
|
64
|
+
hint: hintRef.current,
|
|
58
65
|
onSeq: seq => {
|
|
59
66
|
seqRef.current = seq;
|
|
60
67
|
hooksRef.current.onSeq?.(seq);
|
|
@@ -90,9 +97,10 @@ export function useReplaySubscribe(remote, cb, options = {}) {
|
|
|
90
97
|
seqRef.current = since;
|
|
91
98
|
};
|
|
92
99
|
// keepSeq/since are start-position config, not subscription identity — no resubscribe on change;
|
|
93
|
-
// staleMs is subscribe-time config in common2, so changing it resubscribes
|
|
100
|
+
// staleMs is subscribe-time config in common2, so changing it resubscribes;
|
|
101
|
+
// policy picks the wire surface (line vs frameLine), so it resubscribes too; hint rides a ref
|
|
94
102
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
95
|
-
}, [remote, enabled, epoch, staleMs]);
|
|
103
|
+
}, [remote, enabled, epoch, staleMs, policy]);
|
|
96
104
|
const seq = useCallback(() => subRef.current?.seq() ?? seqRef.current ?? -1, []);
|
|
97
105
|
const lastTs = useCallback(() => subRef.current?.lastTs() ?? 0, []);
|
|
98
106
|
const restart = useCallback((at) => {
|
|
@@ -109,8 +117,9 @@ export function useReplaySubscribe(remote, cb, options = {}) {
|
|
|
109
117
|
* (useStoreNode/useStoreSelect).
|
|
110
118
|
*/
|
|
111
119
|
export function useStoreReplaySync(store, remote, options = {}) {
|
|
112
|
-
const { since, keepSeq = true, enabled = true, onSeq, onError, staleMs, onStale } = options;
|
|
120
|
+
const { since, keepSeq = true, enabled = true, onSeq, onError, staleMs, onStale, policy, hint } = options;
|
|
113
121
|
const hooksRef = useLatestRef({ onSeq, onError, onStale });
|
|
122
|
+
const hintRef = useLatestRef(hint);
|
|
114
123
|
const seqRef = useRef(since);
|
|
115
124
|
const subRef = useRef(null);
|
|
116
125
|
const lastRemoteRef = useRef(undefined);
|
|
@@ -131,6 +140,8 @@ export function useStoreReplaySync(store, remote, options = {}) {
|
|
|
131
140
|
setStale(false); // see useReplaySubscribe: stale re-syncs from common2, no reset-to-false flicker
|
|
132
141
|
const off = ObserveAll2.syncStoreReplay(store, remote, {
|
|
133
142
|
since: seqRef.current,
|
|
143
|
+
policy,
|
|
144
|
+
hint: hintRef.current,
|
|
134
145
|
onSeq: seq => {
|
|
135
146
|
seqRef.current = seq;
|
|
136
147
|
hooksRef.current.onSeq?.(seq);
|
|
@@ -166,7 +177,7 @@ export function useStoreReplaySync(store, remote, options = {}) {
|
|
|
166
177
|
seqRef.current = since;
|
|
167
178
|
};
|
|
168
179
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
169
|
-
}, [store, remote, enabled, epoch, staleMs]);
|
|
180
|
+
}, [store, remote, enabled, epoch, staleMs, policy]);
|
|
170
181
|
const seq = useCallback(() => subRef.current?.seq() ?? seqRef.current ?? -1, []);
|
|
171
182
|
const lastTs = useCallback(() => subRef.current?.lastTs() ?? 0, []);
|
|
172
183
|
const restart = useCallback((at) => {
|
|
@@ -190,6 +201,115 @@ export function useStoreReplayMirror(remote, initial, options = {}) {
|
|
|
190
201
|
const sync = useStoreReplaySync(store, remote, options);
|
|
191
202
|
return useMemo(() => ({ ...sync, store }), [sync, store]);
|
|
192
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Pull a replay line at YOUR pace (the frame model of common2): a timer around
|
|
206
|
+
* `remote.frame(seq, hint)` — the server condenses the tail via the line's `frame` lambda
|
|
207
|
+
* (mini-frame), so a slow consumer never accumulates a backlog and never holds a live socket
|
|
208
|
+
* subscription. Complements useReplaySubscribe (push): use pull when the consumer wants its own
|
|
209
|
+
* cadence (e.g. 500ms UI refresh over a fast line) or a client-picked condensation rule (hint).
|
|
210
|
+
*
|
|
211
|
+
* Folding contract mirrors the push path: envelopes arrive seq-ascending, already-seen seq are
|
|
212
|
+
* skipped, a keyframe recovery is just an envelope of the same event type. Fresh start (no since)
|
|
213
|
+
* = keyframe start like replaySubscribe: keyframe() is polled until the line has one; a sacred
|
|
214
|
+
* line needs an explicit since. Overlapping pulls are never issued (a slow frame() call skips
|
|
215
|
+
* timer ticks). A remote without frame() (old server) fails loudly via onError — there is
|
|
216
|
+
* deliberately no tail fallback here (that is replaySubscribe's job).
|
|
217
|
+
*/
|
|
218
|
+
export function useReplayFrame(remote, cb, options = {}) {
|
|
219
|
+
const { intervalMs = 300, since, keepSeq = true, enabled = true, hint, onSeq, onError } = options;
|
|
220
|
+
const cbRef = useLatestRef(cb);
|
|
221
|
+
const hooksRef = useLatestRef({ onSeq, onError });
|
|
222
|
+
const hintRef = useLatestRef(hint);
|
|
223
|
+
const seqRef = useRef(since ?? -1);
|
|
224
|
+
const pullRef = useRef(null);
|
|
225
|
+
const lastRemoteRef = useRef(undefined);
|
|
226
|
+
const [ready, setReady] = useState(false);
|
|
227
|
+
const [error, setError] = useState(null);
|
|
228
|
+
const [epoch, setEpoch] = useState(0);
|
|
229
|
+
useEffect(() => {
|
|
230
|
+
if (!remote || !enabled)
|
|
231
|
+
return;
|
|
232
|
+
if (lastRemoteRef.current !== undefined && lastRemoteRef.current !== remote)
|
|
233
|
+
seqRef.current = since ?? -1; // a different line — old seq is meaningless
|
|
234
|
+
lastRemoteRef.current = remote;
|
|
235
|
+
let alive = true;
|
|
236
|
+
let stopped = false;
|
|
237
|
+
let timer = null;
|
|
238
|
+
let inflight = null;
|
|
239
|
+
setReady(false);
|
|
240
|
+
setError(null);
|
|
241
|
+
const fail = (e) => {
|
|
242
|
+
stopped = true;
|
|
243
|
+
if (timer) {
|
|
244
|
+
clearInterval(timer);
|
|
245
|
+
timer = null;
|
|
246
|
+
}
|
|
247
|
+
setError(e);
|
|
248
|
+
hooksRef.current.onError?.(e);
|
|
249
|
+
};
|
|
250
|
+
const pull = (hintArg) => {
|
|
251
|
+
if (inflight)
|
|
252
|
+
return inflight; // a slow frame() skips ticks, pulls never overlap
|
|
253
|
+
if (stopped || !alive)
|
|
254
|
+
return Promise.resolve();
|
|
255
|
+
const frame = remote.frame;
|
|
256
|
+
if (!frame) {
|
|
257
|
+
fail(new Error("useReplayFrame: remote has no frame() (old server) — use useReplaySubscribe"));
|
|
258
|
+
return Promise.resolve();
|
|
259
|
+
}
|
|
260
|
+
inflight = (async () => {
|
|
261
|
+
try {
|
|
262
|
+
// no position yet -> keyframe start, mirroring replaySubscribe's catch-up for since<0:
|
|
263
|
+
// frame(-1) has no tail to return and THROWS on a still-empty line, which is not an
|
|
264
|
+
// error here — poll keyframe() until the line has one, then switch to frame(seq)
|
|
265
|
+
const envs = seqRef.current < 0
|
|
266
|
+
? await Promise.resolve(remote.keyframe()).then(kf => kf ? [kf] : null)
|
|
267
|
+
: await frame(seqRef.current, hintArg !== undefined ? hintArg : hintRef.current);
|
|
268
|
+
if (!alive)
|
|
269
|
+
return;
|
|
270
|
+
for (const ev of envs ?? []) {
|
|
271
|
+
if (ev.seq <= seqRef.current)
|
|
272
|
+
continue;
|
|
273
|
+
seqRef.current = ev.seq;
|
|
274
|
+
cbRef.current(...ev.event);
|
|
275
|
+
hooksRef.current.onSeq?.(ev.seq);
|
|
276
|
+
}
|
|
277
|
+
setReady(true);
|
|
278
|
+
}
|
|
279
|
+
catch (e) {
|
|
280
|
+
if (alive)
|
|
281
|
+
fail(e);
|
|
282
|
+
}
|
|
283
|
+
finally {
|
|
284
|
+
inflight = null;
|
|
285
|
+
}
|
|
286
|
+
})();
|
|
287
|
+
return inflight;
|
|
288
|
+
};
|
|
289
|
+
pullRef.current = pull;
|
|
290
|
+
void pull();
|
|
291
|
+
if (!stopped)
|
|
292
|
+
timer = setInterval(() => { void pull(); }, intervalMs); // a sync throw in the first pull may already have stopped us
|
|
293
|
+
return () => {
|
|
294
|
+
alive = false;
|
|
295
|
+
pullRef.current = null;
|
|
296
|
+
if (timer)
|
|
297
|
+
clearInterval(timer);
|
|
298
|
+
if (!keepSeq)
|
|
299
|
+
seqRef.current = since ?? -1;
|
|
300
|
+
};
|
|
301
|
+
// since/keepSeq are start-position config, hint rides a ref — only the pace and identity resubscribe
|
|
302
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
303
|
+
}, [remote, enabled, epoch, intervalMs]);
|
|
304
|
+
const seq = useCallback(() => seqRef.current, []);
|
|
305
|
+
const pull = useCallback((hintArg) => pullRef.current?.(hintArg) ?? Promise.resolve(), []);
|
|
306
|
+
const restart = useCallback((at) => {
|
|
307
|
+
if (at !== undefined)
|
|
308
|
+
seqRef.current = at;
|
|
309
|
+
setEpoch(v => v + 1);
|
|
310
|
+
}, []);
|
|
311
|
+
return useMemo(() => ({ ready, error, seq, pull, restart }), [ready, error, seq, pull, restart]);
|
|
312
|
+
}
|
|
193
313
|
/**
|
|
194
314
|
* Time machine over `Replay.openHistory(storage, live?)`: scrubber/pause/resume for any replay line.
|
|
195
315
|
* `apply` folds ONE event into the consumer state (draw a frame, apply a store patch, ...) —
|
|
@@ -57,6 +57,23 @@ export declare const tokens: {
|
|
|
57
57
|
readonly navBg: "#17202e";
|
|
58
58
|
readonly navWidth: "168px";
|
|
59
59
|
};
|
|
60
|
+
/** Toolbar (createToolbar) chrome (--tb-*). Dark defaults; apps re-skin via :root[data-theme]. */
|
|
61
|
+
readonly tb: {
|
|
62
|
+
readonly bg: "transparent";
|
|
63
|
+
readonly border: "none";
|
|
64
|
+
readonly radius: "6px";
|
|
65
|
+
readonly gap: "2px";
|
|
66
|
+
/** var(--color-text-base) in CSS */
|
|
67
|
+
readonly itemColor: "#c4c4c4";
|
|
68
|
+
readonly itemHoverBg: "rgba(255, 255, 255, 0.08)";
|
|
69
|
+
readonly itemRadius: "6px";
|
|
70
|
+
/** var(--color-bg-dark) in CSS */
|
|
71
|
+
readonly popBg: "#131821";
|
|
72
|
+
/** var(--color-border-common) in CSS */
|
|
73
|
+
readonly popBorder: "1px solid rgb(50, 62, 71)";
|
|
74
|
+
readonly popRadius: "8px";
|
|
75
|
+
readonly popShadow: "0 8px 28px rgba(0, 0, 0, 0.5)";
|
|
76
|
+
};
|
|
60
77
|
readonly font: {
|
|
61
78
|
readonly family: "Roboto";
|
|
62
79
|
readonly sizeBase: "12px";
|
|
@@ -59,6 +59,23 @@ export const tokens = {
|
|
|
59
59
|
navBg: '#17202e',
|
|
60
60
|
navWidth: '168px',
|
|
61
61
|
},
|
|
62
|
+
/** Toolbar (createToolbar) chrome (--tb-*). Dark defaults; apps re-skin via :root[data-theme]. */
|
|
63
|
+
tb: {
|
|
64
|
+
bg: 'transparent',
|
|
65
|
+
border: 'none',
|
|
66
|
+
radius: '6px',
|
|
67
|
+
gap: '2px',
|
|
68
|
+
/** var(--color-text-base) in CSS */
|
|
69
|
+
itemColor: '#c4c4c4',
|
|
70
|
+
itemHoverBg: 'rgba(255, 255, 255, 0.08)',
|
|
71
|
+
itemRadius: '6px',
|
|
72
|
+
/** var(--color-bg-dark) in CSS */
|
|
73
|
+
popBg: '#131821',
|
|
74
|
+
/** var(--color-border-common) in CSS */
|
|
75
|
+
popBorder: '1px solid rgb(50, 62, 71)',
|
|
76
|
+
popRadius: '8px',
|
|
77
|
+
popShadow: '0 8px 28px rgba(0, 0, 0, 0.5)',
|
|
78
|
+
},
|
|
62
79
|
font: {
|
|
63
80
|
family: 'Roboto',
|
|
64
81
|
sizeBase: '12px',
|
package/lib/style/style.css
CHANGED
|
@@ -434,6 +434,134 @@ body {
|
|
|
434
434
|
top: 8px;
|
|
435
435
|
right: 8px;
|
|
436
436
|
}
|
|
437
|
+
/* Toolbar (createToolbar). Themed via --tb-* (tokens.css), dark defaults. */
|
|
438
|
+
.wenayTb {
|
|
439
|
+
display: inline-flex;
|
|
440
|
+
align-items: center;
|
|
441
|
+
gap: var(--tb-gap, 2px);
|
|
442
|
+
background: var(--tb-bg, transparent);
|
|
443
|
+
border: var(--tb-border, none);
|
|
444
|
+
border-radius: var(--tb-radius, 6px);
|
|
445
|
+
color: var(--tb-item-color, var(--color-text-base));
|
|
446
|
+
}
|
|
447
|
+
.wenayTbItem {
|
|
448
|
+
display: inline-flex;
|
|
449
|
+
align-items: center;
|
|
450
|
+
gap: 6px;
|
|
451
|
+
padding: 4px 6px;
|
|
452
|
+
border-radius: var(--tb-item-radius, 6px);
|
|
453
|
+
cursor: pointer;
|
|
454
|
+
user-select: none;
|
|
455
|
+
white-space: nowrap;
|
|
456
|
+
}
|
|
457
|
+
.wenayTbItem:hover {
|
|
458
|
+
background: var(--tb-item-hover-bg, rgba(255, 255, 255, 0.08));
|
|
459
|
+
}
|
|
460
|
+
.wenayTbIcon {
|
|
461
|
+
display: inline-flex;
|
|
462
|
+
align-items: center;
|
|
463
|
+
justify-content: center;
|
|
464
|
+
min-width: 16px;
|
|
465
|
+
}
|
|
466
|
+
/* The gear + its popover live in one positioned container (also the outside-click boundary) */
|
|
467
|
+
.wenayTbGear {
|
|
468
|
+
position: relative;
|
|
469
|
+
display: inline-flex;
|
|
470
|
+
}
|
|
471
|
+
.wenayTbPop {
|
|
472
|
+
position: absolute;
|
|
473
|
+
top: calc(100% + 6px);
|
|
474
|
+
right: 0;
|
|
475
|
+
z-index: var(--wenay-z-modal, 9999);
|
|
476
|
+
min-width: 240px;
|
|
477
|
+
padding: 10px;
|
|
478
|
+
background: var(--tb-pop-bg, var(--color-bg-dark));
|
|
479
|
+
border: var(--tb-pop-border, 1px solid var(--color-border-common));
|
|
480
|
+
border-radius: var(--tb-pop-radius, 8px);
|
|
481
|
+
box-shadow: var(--tb-pop-shadow, 0 8px 28px rgba(0, 0, 0, 0.5));
|
|
482
|
+
color: var(--color-text-base);
|
|
483
|
+
}
|
|
484
|
+
/* Bar popAlign='left' - for bars sitting at a container's LEFT edge (default 'right'
|
|
485
|
+
suits toolbars in a top-right corner) */
|
|
486
|
+
.wenayTbPopLeft {
|
|
487
|
+
right: auto;
|
|
488
|
+
left: 0;
|
|
489
|
+
}
|
|
490
|
+
/* The pure config editor (same element in the popover and in a settings section) */
|
|
491
|
+
.wenayTbSettings {
|
|
492
|
+
display: flex;
|
|
493
|
+
flex-direction: column;
|
|
494
|
+
gap: 10px;
|
|
495
|
+
font-size: 13px;
|
|
496
|
+
}
|
|
497
|
+
.wenayTbDensity {
|
|
498
|
+
display: inline-flex;
|
|
499
|
+
gap: 4px;
|
|
500
|
+
flex-wrap: wrap;
|
|
501
|
+
}
|
|
502
|
+
.wenayTbRows {
|
|
503
|
+
display: flex;
|
|
504
|
+
flex-direction: column;
|
|
505
|
+
gap: 2px;
|
|
506
|
+
}
|
|
507
|
+
.wenayTbRow {
|
|
508
|
+
display: flex;
|
|
509
|
+
align-items: center;
|
|
510
|
+
gap: 8px;
|
|
511
|
+
padding: 4px 6px;
|
|
512
|
+
border-radius: 6px;
|
|
513
|
+
}
|
|
514
|
+
.wenayTbRow:hover {
|
|
515
|
+
background: rgba(255, 255, 255, 0.05);
|
|
516
|
+
}
|
|
517
|
+
/* Non-fixed rows drag as a whole (mouse and touch) */
|
|
518
|
+
.wenayTbRowGrab {
|
|
519
|
+
cursor: grab;
|
|
520
|
+
touch-action: none; /* the browser must not hijack drags for scrolling */
|
|
521
|
+
user-select: none;
|
|
522
|
+
-webkit-user-select: none;
|
|
523
|
+
}
|
|
524
|
+
/* Displaced neighbours glide; the dragged row itself must NOT transition (it follows the pointer) */
|
|
525
|
+
.wenayTbRowShift {
|
|
526
|
+
transition: transform 0.12s ease;
|
|
527
|
+
}
|
|
528
|
+
.wenayTbRowDrag {
|
|
529
|
+
position: relative;
|
|
530
|
+
z-index: 1;
|
|
531
|
+
cursor: grabbing;
|
|
532
|
+
background: rgba(255, 255, 255, 0.12);
|
|
533
|
+
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
|
|
534
|
+
}
|
|
535
|
+
.wenayTbRowTitle {
|
|
536
|
+
flex: 1;
|
|
537
|
+
min-width: 0;
|
|
538
|
+
overflow: hidden;
|
|
539
|
+
text-overflow: ellipsis;
|
|
540
|
+
white-space: nowrap;
|
|
541
|
+
}
|
|
542
|
+
/* The settings-button toggle: set slightly apart from the item rows, never draggable */
|
|
543
|
+
.wenayTbRowMeta {
|
|
544
|
+
border-top: 1px solid rgba(255, 255, 255, 0.08);
|
|
545
|
+
margin-top: 4px;
|
|
546
|
+
border-radius: 0 0 6px 6px;
|
|
547
|
+
opacity: 0.85;
|
|
548
|
+
}
|
|
549
|
+
.wenayTbHandle {
|
|
550
|
+
display: inline-flex;
|
|
551
|
+
align-items: center;
|
|
552
|
+
cursor: grab;
|
|
553
|
+
user-select: none;
|
|
554
|
+
touch-action: none; /* the browser must not hijack vertical drags for scrolling */
|
|
555
|
+
padding: 0 4px;
|
|
556
|
+
opacity: 0.65;
|
|
557
|
+
}
|
|
558
|
+
.wenayTbHandle:hover,
|
|
559
|
+
.wenayTbHandle:focus {
|
|
560
|
+
opacity: 1;
|
|
561
|
+
outline: none;
|
|
562
|
+
color: #fff;
|
|
563
|
+
}
|
|
564
|
+
|
|
437
565
|
/* Minimal default section/segment buttons; apps override via sectionClassName /
|
|
438
566
|
sectionActiveClassName (SettingsDialog) and className / activeClassName (PlacementSetting) */
|
|
439
567
|
.wenayDlgSection,
|