@softwarity/livewire 0.1.1 → 0.2.1
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/README.md +9 -14
- package/fesm2022/softwarity-livewire.mjs +723 -0
- package/fesm2022/softwarity-livewire.mjs.map +1 -0
- package/index.d.ts +5 -0
- package/lib/live-indicator.component.d.ts +36 -0
- package/lib/live-list.d.ts +36 -0
- package/lib/live-topic.d.ts +51 -0
- package/lib/live-window.datasource.d.ts +122 -0
- package/lib/livewire.client.d.ts +99 -0
- package/{src/lib/provide-livewire.ts → lib/provide-livewire.d.ts} +1 -5
- package/package.json +15 -25
- package/{src/public-api.ts → public-api.d.ts} +1 -13
- package/jest.config.js +0 -11
- package/ng-package.json +0 -6
- package/src/lib/live-indicator.component.ts +0 -118
- package/src/lib/live-list.ts +0 -94
- package/src/lib/live-topic.ts +0 -83
- package/src/lib/live-window.datasource.ts +0 -364
- package/src/lib/livewire.client.ts +0 -215
- package/test/end-to-end.spec.ts +0 -166
- package/test/live-window.datasource.spec.ts +0 -221
- package/test/repaint.spec.ts +0 -226
- package/test/setup.ts +0 -6
- package/tsconfig.json +0 -18
- package/tsconfig.spec.json +0 -5
|
@@ -0,0 +1,723 @@
|
|
|
1
|
+
import { DOCUMENT } from '@angular/common';
|
|
2
|
+
import * as i0 from '@angular/core';
|
|
3
|
+
import { InjectionToken, signal, Inject, Optional, Injectable, inject, ChangeDetectorRef, computed, ChangeDetectionStrategy, Component } from '@angular/core';
|
|
4
|
+
import { Subject, Observable, BehaviorSubject, Subscription } from 'rxjs';
|
|
5
|
+
import { filter, map } from 'rxjs/operators';
|
|
6
|
+
import { SUBSCRIBE_EVENT, UPDATE_EVENT, UNSUBSCRIBE_EVENT } from '@softwarity/livewire-protocol';
|
|
7
|
+
import { DataSource } from '@angular/cdk/collections';
|
|
8
|
+
|
|
9
|
+
const LIVEWIRE_CONFIG = new InjectionToken('LIVEWIRE_CONFIG');
|
|
10
|
+
const DEFAULT_RECONNECT_MS = 3000;
|
|
11
|
+
/**
|
|
12
|
+
* The one socket, shared by every live list.
|
|
13
|
+
*
|
|
14
|
+
* One connection per tab, not one per screen: one handshake, one place where
|
|
15
|
+
* authorisation happens, one reconnection to get right. Subscriptions are told
|
|
16
|
+
* apart by an id the caller picks, so several lists can be open at once.
|
|
17
|
+
*/
|
|
18
|
+
class LivewireClient {
|
|
19
|
+
config;
|
|
20
|
+
document;
|
|
21
|
+
/** Whether the socket is currently carrying anything. */
|
|
22
|
+
live = signal(false);
|
|
23
|
+
socket = null;
|
|
24
|
+
/** The wait before the next attempt, held so `retry` can cut it short. */
|
|
25
|
+
retrying = null;
|
|
26
|
+
/**
|
|
27
|
+
* Everything the socket delivers, for the watchers to filter.
|
|
28
|
+
*
|
|
29
|
+
* A plain Subject between the socket and the screens, and that indirection is
|
|
30
|
+
* the whole point. `rxjs/webSocket` ties the connection to its subscriber
|
|
31
|
+
* count: it closes when the last one leaves and reopens on the next, so a
|
|
32
|
+
* list scrolling out of view took the connection down with it and the next
|
|
33
|
+
* one opened another - nine sockets for one list, each losing the snapshot
|
|
34
|
+
* sent to the one before.
|
|
35
|
+
*/
|
|
36
|
+
incoming = new Subject();
|
|
37
|
+
/**
|
|
38
|
+
* What to re-send when a socket comes back.
|
|
39
|
+
*
|
|
40
|
+
* A reconnected socket knows nothing about what the screens were watching a
|
|
41
|
+
* second ago, and the screens have no reason to find out that it dropped.
|
|
42
|
+
*/
|
|
43
|
+
open = new Map();
|
|
44
|
+
constructor(config, document) {
|
|
45
|
+
this.config = config;
|
|
46
|
+
this.document = document;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Watches one topic, and keeps watching it across reconnections.
|
|
50
|
+
*
|
|
51
|
+
* The returned stream starts with a snapshot and continues with patches -
|
|
52
|
+
* applying them is `LiveList`'s job. Unsubscribing closes the subscription
|
|
53
|
+
* server-side; the socket itself stays up for the other lists.
|
|
54
|
+
*/
|
|
55
|
+
watch(id, topic, query) {
|
|
56
|
+
return new Observable((subscriber) => {
|
|
57
|
+
this.connect();
|
|
58
|
+
const asked = { id, topic, query: query ?? undefined };
|
|
59
|
+
this.open.set(id, asked);
|
|
60
|
+
this.send(SUBSCRIBE_EVENT, asked);
|
|
61
|
+
const frames = this.incoming
|
|
62
|
+
.pipe(filter((envelope) => {
|
|
63
|
+
return envelope.event === UPDATE_EVENT && envelope.data?.id === id;
|
|
64
|
+
}))
|
|
65
|
+
.subscribe((envelope) => subscriber.next(envelope.data));
|
|
66
|
+
return () => {
|
|
67
|
+
frames.unsubscribe();
|
|
68
|
+
// The socket stays up for the other lists; only this window closes.
|
|
69
|
+
this.open.delete(id);
|
|
70
|
+
this.send(UNSUBSCRIBE_EVENT, { id });
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Asks for a window again, and gets a snapshot back.
|
|
76
|
+
*
|
|
77
|
+
* A gap in the sequence means the two sides disagree about what was sent.
|
|
78
|
+
* Asking again is cheap; carrying on would show a list nobody can trust.
|
|
79
|
+
*/
|
|
80
|
+
resync(id) {
|
|
81
|
+
const asked = this.open.get(id);
|
|
82
|
+
if (asked) {
|
|
83
|
+
this.send(SUBSCRIBE_EVENT, asked);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* What a reader can do about it when something looks off.
|
|
88
|
+
*
|
|
89
|
+
* Two situations, one gesture, because from their side they are the same
|
|
90
|
+
* complaint - the screen may not be showing what is true. Down: the
|
|
91
|
+
* reconnection delay is skipped and the socket is opened now. Up: every open
|
|
92
|
+
* window is asked for again, and each answers with a snapshot.
|
|
93
|
+
*/
|
|
94
|
+
retry() {
|
|
95
|
+
if (this.socket) {
|
|
96
|
+
for (const asked of this.open.values()) {
|
|
97
|
+
this.send(SUBSCRIBE_EVENT, asked);
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (this.retrying !== null) {
|
|
102
|
+
clearTimeout(this.retrying);
|
|
103
|
+
this.retrying = null;
|
|
104
|
+
}
|
|
105
|
+
this.connect();
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Opens the one socket, once, and keeps it open.
|
|
109
|
+
*
|
|
110
|
+
* A plain `WebSocket` rather than `rxjs/webSocket`, deliberately - see the
|
|
111
|
+
* note on `incoming`. What this needs is one socket for as long as the tab
|
|
112
|
+
* lives, which is easier to write outright than to talk an operator chain
|
|
113
|
+
* into.
|
|
114
|
+
*/
|
|
115
|
+
connect() {
|
|
116
|
+
if (this.socket) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const socket = this.config.connect ? this.config.connect() : this.openWebSocket();
|
|
120
|
+
this.socket = socket;
|
|
121
|
+
socket.onopen = () => {
|
|
122
|
+
this.live.set(true);
|
|
123
|
+
// Whatever the screens were watching, asked for again. A reconnected
|
|
124
|
+
// socket knows nothing about them, and on the first open this is what
|
|
125
|
+
// sends the subscriptions queued before it came up.
|
|
126
|
+
for (const asked of this.open.values()) {
|
|
127
|
+
this.send(SUBSCRIBE_EVENT, asked);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
socket.onmessage = (frame) => {
|
|
131
|
+
try {
|
|
132
|
+
this.incoming.next(JSON.parse(frame));
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
console.error('[livewire] unreadable frame', error);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
// A dropped socket is the normal case, not a fault: a gateway restart, a
|
|
139
|
+
// laptop that slept. Reconnecting is this client's job so no screen has to
|
|
140
|
+
// know it happened.
|
|
141
|
+
socket.onclose = () => {
|
|
142
|
+
this.live.set(false);
|
|
143
|
+
this.socket = null;
|
|
144
|
+
this.retrying = setTimeout(() => {
|
|
145
|
+
this.retrying = null;
|
|
146
|
+
this.connect();
|
|
147
|
+
}, this.config.reconnectMs ?? DEFAULT_RECONNECT_MS);
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
openWebSocket() {
|
|
151
|
+
const location = this.document?.location;
|
|
152
|
+
const scheme = location?.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
153
|
+
const native = new WebSocket(`${scheme}//${location?.host ?? ''}${this.config.path}`);
|
|
154
|
+
// Adapted rather than used directly: a `MessageEvent` is the browser's
|
|
155
|
+
// shape, and everything else here - the in-memory server, the tests -
|
|
156
|
+
// speaks in frames.
|
|
157
|
+
const socket = {
|
|
158
|
+
send: (frame) => native.send(frame),
|
|
159
|
+
close: (code, reason) => native.close(code, reason),
|
|
160
|
+
};
|
|
161
|
+
native.onopen = () => socket.onopen?.();
|
|
162
|
+
native.onmessage = (event) => socket.onmessage?.(String(event.data));
|
|
163
|
+
native.onclose = (event) => socket.onclose?.(event.code, event.reason);
|
|
164
|
+
native.onerror = () => console.error('[livewire] socket error');
|
|
165
|
+
return socket;
|
|
166
|
+
}
|
|
167
|
+
send(event, data) {
|
|
168
|
+
// Anything sent before the socket is up is not queued here: `onopen` sends
|
|
169
|
+
// every open subscription, which is the same set and a shorter path.
|
|
170
|
+
this.socket?.send(JSON.stringify({ event, data }));
|
|
171
|
+
}
|
|
172
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: LivewireClient, deps: [{ token: LIVEWIRE_CONFIG }, { token: DOCUMENT, optional: true }], target: i0.ɵɵFactoryTarget.Injectable });
|
|
173
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: LivewireClient, providedIn: 'root' });
|
|
174
|
+
}
|
|
175
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: LivewireClient, decorators: [{
|
|
176
|
+
type: Injectable,
|
|
177
|
+
args: [{ providedIn: 'root' }]
|
|
178
|
+
}], ctorParameters: () => [{ type: undefined, decorators: [{
|
|
179
|
+
type: Inject,
|
|
180
|
+
args: [LIVEWIRE_CONFIG]
|
|
181
|
+
}] }, { type: Document, decorators: [{
|
|
182
|
+
type: Optional
|
|
183
|
+
}, {
|
|
184
|
+
type: Inject,
|
|
185
|
+
args: [DOCUMENT]
|
|
186
|
+
}] }] });
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Wires the one socket into an application.
|
|
190
|
+
*
|
|
191
|
+
* ```ts
|
|
192
|
+
* providers: [provideLivewire({ path: '/my-service/ws' })]
|
|
193
|
+
* ```
|
|
194
|
+
*
|
|
195
|
+
* For a demo or a test with no backend, hand it a connection instead:
|
|
196
|
+
*
|
|
197
|
+
* ```ts
|
|
198
|
+
* const server = new MockServer().register('rows', { windowFor: () => window });
|
|
199
|
+
* provideLivewire({ path: '', connect: () => server.connect() });
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
function provideLivewire(config) {
|
|
203
|
+
return [{ provide: LIVEWIRE_CONFIG, useValue: config }];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* A list kept in step with what the server pushes.
|
|
208
|
+
*
|
|
209
|
+
* Snapshot then patches, applied and nothing more: what belongs in the list and
|
|
210
|
+
* in what order is decided by the source's query, and a client that recomputed
|
|
211
|
+
* either from its own idea of the filters would be a second implementation free
|
|
212
|
+
* to disagree.
|
|
213
|
+
*/
|
|
214
|
+
class LiveList {
|
|
215
|
+
held = signal([]);
|
|
216
|
+
length = signal(0);
|
|
217
|
+
marked = signal(null);
|
|
218
|
+
/** The list as it stands. Ordered by the server, never re-sorted here. */
|
|
219
|
+
rows = this.held.asReadonly();
|
|
220
|
+
/** What the list is a page of, for a paged topic. Its own length otherwise. */
|
|
221
|
+
total = this.length.asReadonly();
|
|
222
|
+
/**
|
|
223
|
+
* An index in the whole list the server pointed at, if it pointed at one.
|
|
224
|
+
*
|
|
225
|
+
* Meaningless here on purpose: the library carries the number and the screen
|
|
226
|
+
* decides what it means - a boundary, a cursor, the first unread row.
|
|
227
|
+
*/
|
|
228
|
+
pivot = this.marked.asReadonly();
|
|
229
|
+
sequence = 0;
|
|
230
|
+
/**
|
|
231
|
+
* Applies one frame. Answers false when the two sides have drifted, which is
|
|
232
|
+
* the caller's cue to ask for the window again.
|
|
233
|
+
*/
|
|
234
|
+
apply(update) {
|
|
235
|
+
if (update.type === 'error') {
|
|
236
|
+
console.error(`Livewire subscription '${update.id}' refused: ${update.reason}`);
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
if (update.type === 'snapshot') {
|
|
240
|
+
this.sequence = update.sequence;
|
|
241
|
+
this.held.set(update.rows);
|
|
242
|
+
this.length.set(update.total ?? update.rows.length);
|
|
243
|
+
this.marked.set(update.pivot ?? null);
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
// On one socket nothing is lost and nothing overtakes, so a gap means the
|
|
247
|
+
// two sides disagree about what was sent. Asking again is cheap; carrying
|
|
248
|
+
// on would show a list nobody can reproduce.
|
|
249
|
+
if (update.sequence !== this.sequence + 1) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
const rebuilt = this.rebuild(update);
|
|
253
|
+
if (!rebuilt) {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
this.sequence = update.sequence;
|
|
257
|
+
this.held.set(rebuilt);
|
|
258
|
+
this.length.set(update.total ?? rebuilt.length);
|
|
259
|
+
this.marked.set(update.pivot ?? null);
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
/** Forgets what it holds - a fresh subscription answers with a snapshot. */
|
|
263
|
+
reset() {
|
|
264
|
+
this.sequence = 0;
|
|
265
|
+
this.held.set([]);
|
|
266
|
+
this.length.set(0);
|
|
267
|
+
this.marked.set(null);
|
|
268
|
+
}
|
|
269
|
+
/** Null when the patch names a row neither side holds - the caller resyncs. */
|
|
270
|
+
rebuild(update) {
|
|
271
|
+
const byId = new Map(this.held().map((row) => [row.id, row]));
|
|
272
|
+
for (const id of update.removed) {
|
|
273
|
+
byId.delete(id);
|
|
274
|
+
}
|
|
275
|
+
for (const row of update.upserted) {
|
|
276
|
+
byId.set(row.id, row);
|
|
277
|
+
}
|
|
278
|
+
const next = [];
|
|
279
|
+
for (const id of update.order) {
|
|
280
|
+
const row = byId.get(id);
|
|
281
|
+
if (!row) {
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
next.push(row);
|
|
285
|
+
}
|
|
286
|
+
return next;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* One screen's live window on one topic.
|
|
292
|
+
*
|
|
293
|
+
* The bookkeeping every feature service would otherwise repeat: the counter,
|
|
294
|
+
* the id it last opened, and the resync that has to name it. Small, and worth
|
|
295
|
+
* having once - the id rule below is the kind of thing that is right in one
|
|
296
|
+
* copy and wrong in the next.
|
|
297
|
+
*/
|
|
298
|
+
class LiveTopic {
|
|
299
|
+
client;
|
|
300
|
+
topic;
|
|
301
|
+
/** Counts subscriptions, so no two ever share an id. */
|
|
302
|
+
opened = 0;
|
|
303
|
+
/** The id of the current window, for `resync`. */
|
|
304
|
+
watching = null;
|
|
305
|
+
constructor(client, topic) {
|
|
306
|
+
this.client = client;
|
|
307
|
+
this.topic = topic;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* The rows the screen is showing, and again whenever they change.
|
|
311
|
+
*
|
|
312
|
+
* One subscription for the screen, not one per page: the window is part of
|
|
313
|
+
* the question, which is what makes a paged list pushable at all. The server
|
|
314
|
+
* does not send an insertion for the client to place - it sends what rows
|
|
315
|
+
* `[offset, offset+limit)` now hold, so three rows arriving at the top shift
|
|
316
|
+
* no index. The window simply holds something else than it did.
|
|
317
|
+
*/
|
|
318
|
+
window(query, offset, limit) {
|
|
319
|
+
// Unique per subscription. Reusing an id means a patch still in flight from
|
|
320
|
+
// the window before lands on the new one, whose sequence starts over - it
|
|
321
|
+
// reads as a gap, resyncs, and nothing publishes in the meantime.
|
|
322
|
+
this.watching = `${this.topic}:${(this.opened += 1)}`;
|
|
323
|
+
return this.client.watch(this.watching, this.topic, { ...query, offset, limit });
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* The same, for a list the server sends whole.
|
|
327
|
+
*
|
|
328
|
+
* A short list is bounded by its filters rather than by a scroll position, so
|
|
329
|
+
* there is no offset to send. Everything around it is unchanged: one live
|
|
330
|
+
* subscription at a time, a fresh id each time, and a resync that names it.
|
|
331
|
+
*/
|
|
332
|
+
open(query) {
|
|
333
|
+
this.watching = `${this.topic}:${(this.opened += 1)}`;
|
|
334
|
+
return this.client.watch(this.watching, this.topic, query);
|
|
335
|
+
}
|
|
336
|
+
/** Asks the server for the window again - answered with a snapshot. */
|
|
337
|
+
resync() {
|
|
338
|
+
if (this.watching) {
|
|
339
|
+
this.client.resync(this.watching);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* A short list, kept in step: the values that actually exist.
|
|
345
|
+
*
|
|
346
|
+
* Not a constant in the screen - offering a filter for something an
|
|
347
|
+
* installation has never seen answers nothing. These lists are small and
|
|
348
|
+
* unpaged, so there is one subscription per topic and its id is the topic.
|
|
349
|
+
*/
|
|
350
|
+
function liveLabels(client, topic, query = null) {
|
|
351
|
+
const list = new LiveList();
|
|
352
|
+
return client.watch(topic, topic, query).pipe(filter((update) => list.apply(update)),
|
|
353
|
+
// A topic that sends no label is a list of bare values - the value is its
|
|
354
|
+
// own label.
|
|
355
|
+
map(() => list.rows().map((row) => ({ id: row.id, label: row.label ?? row.id }))));
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* How many rows a window holds when the caller does not say.
|
|
360
|
+
*
|
|
361
|
+
* Fixed per screen, never derived from the viewport: a window sized from the
|
|
362
|
+
* rendered range would change with it - 58 rows here, 59 there - and every
|
|
363
|
+
* change is a new question, whose answer republishes, which makes the viewport
|
|
364
|
+
* emit again. That loop freezes the renderer outright. Only the offset moves.
|
|
365
|
+
*
|
|
366
|
+
* The number is a transport budget, not a display choice. Some proxies silently
|
|
367
|
+
* drop a WebSocket frame past ~64 kB and close the connection rather than the
|
|
368
|
+
* frame, leaving the screen empty with no error anywhere. The rule is
|
|
369
|
+
* `rows x bytes-per-row < the ceiling you tested`, and the bytes differ per
|
|
370
|
+
* screen. Measure before raising it: the failure gives no clue at all.
|
|
371
|
+
*/
|
|
372
|
+
const DEFAULT_WINDOW = 100;
|
|
373
|
+
/**
|
|
374
|
+
* How far the offset is rounded.
|
|
375
|
+
*
|
|
376
|
+
* Without it every scrolled pixel is a new question. Rounding means the
|
|
377
|
+
* subscription only moves when the view has genuinely left what it was reading.
|
|
378
|
+
*/
|
|
379
|
+
const BLOCK = 50;
|
|
380
|
+
/**
|
|
381
|
+
* How close the view may come to an edge before the window is moved.
|
|
382
|
+
*
|
|
383
|
+
* The hysteresis is what keeps this from running away: every publication makes
|
|
384
|
+
* the list longer, the viewport recalculates and emits a range, and recentring
|
|
385
|
+
* on each of those re-subscribes - which republishes, which emits again.
|
|
386
|
+
*/
|
|
387
|
+
const MARGIN = 10;
|
|
388
|
+
/**
|
|
389
|
+
* How long a row that just changed stays marked.
|
|
390
|
+
*
|
|
391
|
+
* The `.fresh` styling on the consuming screen should run for exactly this
|
|
392
|
+
* long. Shorter here cuts the animation off mid-way; longer leaves the row
|
|
393
|
+
* plain while it is still marked.
|
|
394
|
+
*/
|
|
395
|
+
const FRESH_MS = 1000;
|
|
396
|
+
/**
|
|
397
|
+
* One live window of a long list, as a CDK data source.
|
|
398
|
+
*
|
|
399
|
+
* The screen watches one thing: the rows around what it is showing. Scroll and
|
|
400
|
+
* the window moves; something changes and the server pushes what that window
|
|
401
|
+
* now holds. There is no page cache and no second question in flight - the
|
|
402
|
+
* window *is* the question, which is what lets a paged list be pushed at all.
|
|
403
|
+
*
|
|
404
|
+
* The array handed to the viewport is the full length the server reports, with
|
|
405
|
+
* a hole wherever the window does not reach. That is what makes the scrollbar
|
|
406
|
+
* tell the truth from the first frame: its height is the whole list, not the
|
|
407
|
+
* part being watched.
|
|
408
|
+
*/
|
|
409
|
+
class LiveWindowDataSource extends DataSource {
|
|
410
|
+
onTotal;
|
|
411
|
+
onDrift;
|
|
412
|
+
window;
|
|
413
|
+
published = new BehaviorSubject([]);
|
|
414
|
+
viewing = new Subscription();
|
|
415
|
+
/**
|
|
416
|
+
* The view to repaint, taken from wherever this was built.
|
|
417
|
+
*
|
|
418
|
+
* Zoneless applications schedule nothing when a row arrives from a socket
|
|
419
|
+
* callback: the table is told its data changed but no pass is ever run, and
|
|
420
|
+
* the screen holds stale rows until something unrelated wakes Angular up.
|
|
421
|
+
* `markForCheck()` marks the view dirty *and* notifies the zoneless
|
|
422
|
+
* scheduler, which is exactly the missing half.
|
|
423
|
+
*
|
|
424
|
+
* Injected rather than asked for, because there is nothing to decide: build
|
|
425
|
+
* this in a component field, as one does, and the injection context is right
|
|
426
|
+
* there. Built outside one it throws, and that is the point - a data source
|
|
427
|
+
* with no view to repaint has nobody to answer.
|
|
428
|
+
*
|
|
429
|
+
* (`ApplicationRef.tick()` is the other way and the wrong one: it throws when
|
|
430
|
+
* it lands inside a cycle already in progress.)
|
|
431
|
+
*/
|
|
432
|
+
repaint = inject(ChangeDetectorRef);
|
|
433
|
+
list = new LiveList();
|
|
434
|
+
watching = null;
|
|
435
|
+
/** The rows a patch touched a moment ago - see `fresh`. */
|
|
436
|
+
recent = new Set();
|
|
437
|
+
/** Where the current subscription starts. Its length is always `window`. */
|
|
438
|
+
offset = 0;
|
|
439
|
+
open = null;
|
|
440
|
+
range = { start: 0, end: 0 };
|
|
441
|
+
constructor(onTotal = () => undefined,
|
|
442
|
+
/** Told when a patch cannot be applied, so the caller can ask again. */
|
|
443
|
+
onDrift = () => undefined,
|
|
444
|
+
/** Rows per window - see `DEFAULT_WINDOW` for how to choose one. */
|
|
445
|
+
window = DEFAULT_WINDOW) {
|
|
446
|
+
super();
|
|
447
|
+
this.onTotal = onTotal;
|
|
448
|
+
this.onDrift = onDrift;
|
|
449
|
+
this.window = window;
|
|
450
|
+
}
|
|
451
|
+
connect(viewer) {
|
|
452
|
+
// Kept because a data source is asked for it, but not relied on: inside a
|
|
453
|
+
// `mat-table` the viewer is the table, whose `viewChange` stays silent
|
|
454
|
+
// while the CDK viewport does the scrolling. `track` is what actually
|
|
455
|
+
// reports where the eye is.
|
|
456
|
+
this.viewing.add(viewer.viewChange.subscribe((range) => this.moveTo(range)));
|
|
457
|
+
return this.published;
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Follows a viewport's rendered range.
|
|
461
|
+
*
|
|
462
|
+
* The screen hands this over explicitly because the `CollectionViewer` a
|
|
463
|
+
* table passes to `connect` does not carry it. Without this the window stays
|
|
464
|
+
* where it started and everything below the first screen is a placeholder
|
|
465
|
+
* that never fills.
|
|
466
|
+
*/
|
|
467
|
+
track(ranges) {
|
|
468
|
+
this.viewing.add(ranges.subscribe((range) => this.moveTo(range)));
|
|
469
|
+
}
|
|
470
|
+
disconnect() {
|
|
471
|
+
this.viewing.unsubscribe();
|
|
472
|
+
this.watching?.unsubscribe();
|
|
473
|
+
}
|
|
474
|
+
/** How long the list is, as the server last reported it. */
|
|
475
|
+
get length() {
|
|
476
|
+
return this.list.total();
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* The index the server pointed at, in the whole list rather than in the page.
|
|
480
|
+
*
|
|
481
|
+
* Whatever it means is the screen's business. It survives the window moving,
|
|
482
|
+
* which is the point: a boundary the reader has scrolled away from is still
|
|
483
|
+
* where it was.
|
|
484
|
+
*/
|
|
485
|
+
get pivot() {
|
|
486
|
+
return this.list.pivot();
|
|
487
|
+
}
|
|
488
|
+
/** Every republication, for a caller waiting on one row rather than on the viewport. */
|
|
489
|
+
get changes() {
|
|
490
|
+
return this.published.asObservable();
|
|
491
|
+
}
|
|
492
|
+
/** The row at that index, when the window reaches it. */
|
|
493
|
+
at(index) {
|
|
494
|
+
return this.published.value[index];
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Whether this row arrived, or changed, a moment ago.
|
|
498
|
+
*
|
|
499
|
+
* Only patches mark anything: a snapshot is the answer to "what does this
|
|
500
|
+
* window hold", which is what arriving on the screen and what scrolling both
|
|
501
|
+
* ask, and lighting up a whole page every time the window moves says nothing.
|
|
502
|
+
*
|
|
503
|
+
* A patch's `upserted` is exactly the rows whose version changed, so a row
|
|
504
|
+
* that merely shifted because three arrived above it is not marked. The one
|
|
505
|
+
* approximation: for a window part-way down the list, a row pushed into it
|
|
506
|
+
* from just outside is new to this client without being new to the list.
|
|
507
|
+
*/
|
|
508
|
+
fresh(id) {
|
|
509
|
+
return id !== undefined && this.recent.has(id);
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Starts again on a new question.
|
|
513
|
+
*
|
|
514
|
+
* A different question has a different answer, and a row kept from the
|
|
515
|
+
* previous one would be an answer to neither.
|
|
516
|
+
*/
|
|
517
|
+
reset(open) {
|
|
518
|
+
this.open = open;
|
|
519
|
+
this.range = { start: 0, end: BLOCK };
|
|
520
|
+
this.offset = -1;
|
|
521
|
+
this.list = new LiveList();
|
|
522
|
+
this.recent.clear();
|
|
523
|
+
this.published.next([]);
|
|
524
|
+
// What is on the screen has just become the answer to a question nobody is
|
|
525
|
+
// asking any more, and the snapshot lands a moment later. Emptying it
|
|
526
|
+
// without saying so leaves the previous answer on display in between.
|
|
527
|
+
//
|
|
528
|
+
this.repaint.markForCheck();
|
|
529
|
+
this.follow();
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Makes sure the window covers one row, wherever the viewport currently is.
|
|
533
|
+
*
|
|
534
|
+
* Stepping through a detail panel walks past the edge of what is loaded, and
|
|
535
|
+
* the scroll that follows only reports where it lands - too late for the row
|
|
536
|
+
* the panel is already opening on.
|
|
537
|
+
*/
|
|
538
|
+
ensure(index) {
|
|
539
|
+
if (index >= this.offset && index < this.offset + this.window) {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
// The offset is computed here rather than through `range`: that field is
|
|
543
|
+
// where the viewport is, and writing a made-up position into it means the
|
|
544
|
+
// next real range disagrees, moves the window back, and the two take turns
|
|
545
|
+
// until the renderer gives up.
|
|
546
|
+
//
|
|
547
|
+
// Clamped the way `follow` clamps, and for the same reason: rounding to a
|
|
548
|
+
// block can land beside the very row that was asked for.
|
|
549
|
+
const centred = Math.round((index - this.window / 2) / BLOCK) * BLOCK;
|
|
550
|
+
const wanted = Math.max(0, Math.min(index, Math.max(centred, index - this.window + 1)));
|
|
551
|
+
if (wanted === this.offset) {
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
this.offset = wanted;
|
|
555
|
+
this.subscribe();
|
|
556
|
+
}
|
|
557
|
+
moveTo(range) {
|
|
558
|
+
// An empty range says nothing about where the eye is, and taking it at face
|
|
559
|
+
// value moves the window to the top of the list. A viewport emits one every
|
|
560
|
+
// time it is hidden or resized to nothing - which is what opening a drawer
|
|
561
|
+
// over it does, and why the list behind came back blank.
|
|
562
|
+
if (range.end <= range.start) {
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
this.range = range;
|
|
566
|
+
this.follow();
|
|
567
|
+
}
|
|
568
|
+
/** Moves the window if the view has walked out of what it covers. */
|
|
569
|
+
follow() {
|
|
570
|
+
if (!this.open) {
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
const { start, end } = this.range;
|
|
574
|
+
// What is left once the rendered rows are covered, and therefore how much
|
|
575
|
+
// of it can be spent as hysteresis on each side. Asking for the full MARGIN
|
|
576
|
+
// regardless leaves a band of rows loaded by nothing: a window barely wider
|
|
577
|
+
// than the viewport cannot both cover it and keep ten rows in hand.
|
|
578
|
+
const spare = Math.max(0, this.window - (end - start));
|
|
579
|
+
const margin = Math.min(MARGIN, Math.floor(spare / 2));
|
|
580
|
+
if (this.offset >= 0 && start >= this.offset + margin && end <= this.offset + this.window - margin) {
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
// Centred on the view and rounded to a block - then pulled back until it
|
|
584
|
+
// covers the rendered rows. Covering them is the requirement; the block is
|
|
585
|
+
// only a way of not moving on every scrolled pixel, and rounding to one is
|
|
586
|
+
// exactly how a window settles beside the rows it was meant to hold:
|
|
587
|
+
// rendered [90, 120) rounded to [100, 150), leaving 90 to 99 in no window
|
|
588
|
+
// at all - and, the offset being unchanged, leaving them there.
|
|
589
|
+
const middle = (start + end) / 2;
|
|
590
|
+
const centred = Math.round((middle - this.window / 2) / BLOCK) * BLOCK;
|
|
591
|
+
// Never past the first rendered row, never so far back that the last one
|
|
592
|
+
// falls out. When the viewport renders more rows than the window holds
|
|
593
|
+
// there is no such position: the first row wins, which keeps the hole below
|
|
594
|
+
// the fold rather than above it.
|
|
595
|
+
const wanted = Math.max(0, Math.min(start, Math.max(centred, end - this.window)));
|
|
596
|
+
if (wanted === this.offset) {
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
this.offset = wanted;
|
|
600
|
+
this.subscribe();
|
|
601
|
+
}
|
|
602
|
+
subscribe() {
|
|
603
|
+
if (!this.open) {
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
this.watching?.unsubscribe();
|
|
607
|
+
// A fresh list per subscription: the server answers a new window with a
|
|
608
|
+
// snapshot, and rows from the previous one are at different offsets. The
|
|
609
|
+
// marks go with them - they belonged to the window being left.
|
|
610
|
+
this.list = new LiveList();
|
|
611
|
+
this.recent.clear();
|
|
612
|
+
this.watching = this.open(this.offset, this.window).subscribe({
|
|
613
|
+
next: (update) => {
|
|
614
|
+
if (!this.list.apply(update)) {
|
|
615
|
+
this.onDrift();
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
if (update.type === 'patch') {
|
|
619
|
+
this.mark(update.upserted.map((row) => row.id));
|
|
620
|
+
}
|
|
621
|
+
this.onTotal(this.list.total());
|
|
622
|
+
this.publish();
|
|
623
|
+
},
|
|
624
|
+
error: (error) => console.error('[livewire] window failed', error),
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
mark(ids) {
|
|
628
|
+
if (ids.length === 0) {
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
for (const id of ids) {
|
|
632
|
+
this.recent.add(id);
|
|
633
|
+
}
|
|
634
|
+
// One timer per wave, clearing its own ids: a single shared timer would be
|
|
635
|
+
// pushed back by every new arrival, so under a busy feed nothing would ever
|
|
636
|
+
// stop being marked. The repaint is explicit because a zoneless application
|
|
637
|
+
// schedules nothing for a set mutated outside the view.
|
|
638
|
+
setTimeout(() => {
|
|
639
|
+
for (const id of ids) {
|
|
640
|
+
this.recent.delete(id);
|
|
641
|
+
}
|
|
642
|
+
this.repaint.markForCheck();
|
|
643
|
+
}, FRESH_MS);
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* The whole length, holes and all.
|
|
647
|
+
*
|
|
648
|
+
* A new array each time: the viewport compares references to know something
|
|
649
|
+
* changed, and the rows themselves are shared rather than copied.
|
|
650
|
+
*/
|
|
651
|
+
publish() {
|
|
652
|
+
const rows = new Array(this.list.total());
|
|
653
|
+
const held = this.list.rows();
|
|
654
|
+
for (let index = 0; index < held.length; index += 1) {
|
|
655
|
+
rows[this.offset + index] = held[index];
|
|
656
|
+
}
|
|
657
|
+
this.published.next(rows);
|
|
658
|
+
this.repaint.markForCheck();
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/** How long the indicator acknowledges a click before going back to its state. */
|
|
663
|
+
const ACKNOWLEDGE_MS = 700;
|
|
664
|
+
/**
|
|
665
|
+
* Whether the socket is up, and the one thing to do about it when it is not.
|
|
666
|
+
*
|
|
667
|
+
* That and nothing else - not whether anything is arriving on it, which is a
|
|
668
|
+
* different question and one this would answer badly: a quiet feed at three in
|
|
669
|
+
* the morning is not a fault.
|
|
670
|
+
*
|
|
671
|
+
* Clicking it retries. Down, that opens the socket now instead of waiting out
|
|
672
|
+
* the reconnection delay; up, it asks every open window for a fresh snapshot.
|
|
673
|
+
* It is deliberately not a refresh button on the list: on a live screen a
|
|
674
|
+
* control offering to fetch it again implies that the rest of the time you are
|
|
675
|
+
* looking at something stale.
|
|
676
|
+
*
|
|
677
|
+
* Colours come from CSS custom properties, so a host theme dresses it without
|
|
678
|
+
* this package knowing anything about the theme:
|
|
679
|
+
*
|
|
680
|
+
* ```css
|
|
681
|
+
* lw-live-indicator { --lw-live: #1b7f3b; --lw-down: #b26a00; }
|
|
682
|
+
* ```
|
|
683
|
+
*/
|
|
684
|
+
class LiveIndicatorComponent {
|
|
685
|
+
client = inject(LivewireClient);
|
|
686
|
+
live = this.client.live;
|
|
687
|
+
/**
|
|
688
|
+
* Set for a moment after a click, and read for nothing but the animation.
|
|
689
|
+
*
|
|
690
|
+
* A resync on a list that has not moved changes no pixel, so without this the
|
|
691
|
+
* gesture looks like it did nothing at all.
|
|
692
|
+
*/
|
|
693
|
+
asked = signal(false);
|
|
694
|
+
label = computed(() => (this.live() ? LIVE : DOWN));
|
|
695
|
+
retry() {
|
|
696
|
+
this.client.retry();
|
|
697
|
+
this.asked.set(true);
|
|
698
|
+
setTimeout(() => this.asked.set(false), ACKNOWLEDGE_MS);
|
|
699
|
+
}
|
|
700
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: LiveIndicatorComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
|
|
701
|
+
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "19.2.25", type: LiveIndicatorComponent, isStandalone: true, selector: "lw-live-indicator", ngImport: i0, template: `
|
|
702
|
+
<button type="button" class="lw-indicator" [class.lw-down]="!live()" [attr.aria-label]="label()" [title]="label()" (click)="retry()">
|
|
703
|
+
<span class="lw-dot" [class.lw-beat]="live() && !asked()" [class.lw-turn]="asked()"></span>
|
|
704
|
+
</button>
|
|
705
|
+
`, isInline: true, styles: [":host{display:inline-flex}.lw-indicator{display:inline-flex;align-items:center;padding:0;border:none;background:none;cursor:pointer;color:var(--lw-live, currentColor)}.lw-indicator.lw-down{color:var(--lw-down, #b26a00)}.lw-indicator:focus-visible{outline:2px solid currentColor;outline-offset:2px;border-radius:50%}.lw-dot{display:block;width:.6rem;height:.6rem;border-radius:50%;background:currentColor}.lw-dot.lw-beat{animation:lw-beat 1s ease-in-out infinite}.lw-dot.lw-turn{animation:lw-turn .5s ease-out}@keyframes lw-beat{0%,90%{transform:scale(1)}45%{transform:scale(1.35)}}@keyframes lw-turn{0%{transform:scale(1.6);opacity:.4}}@media(prefers-reduced-motion:reduce){.lw-dot.lw-beat,.lw-dot.lw-turn{animation:none}}\n"], changeDetection: i0.ChangeDetectionStrategy.OnPush });
|
|
706
|
+
}
|
|
707
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: LiveIndicatorComponent, decorators: [{
|
|
708
|
+
type: Component,
|
|
709
|
+
args: [{ selector: 'lw-live-indicator', changeDetection: ChangeDetectionStrategy.OnPush, standalone: true, template: `
|
|
710
|
+
<button type="button" class="lw-indicator" [class.lw-down]="!live()" [attr.aria-label]="label()" [title]="label()" (click)="retry()">
|
|
711
|
+
<span class="lw-dot" [class.lw-beat]="live() && !asked()" [class.lw-turn]="asked()"></span>
|
|
712
|
+
</button>
|
|
713
|
+
`, styles: [":host{display:inline-flex}.lw-indicator{display:inline-flex;align-items:center;padding:0;border:none;background:none;cursor:pointer;color:var(--lw-live, currentColor)}.lw-indicator.lw-down{color:var(--lw-down, #b26a00)}.lw-indicator:focus-visible{outline:2px solid currentColor;outline-offset:2px;border-radius:50%}.lw-dot{display:block;width:.6rem;height:.6rem;border-radius:50%;background:currentColor}.lw-dot.lw-beat{animation:lw-beat 1s ease-in-out infinite}.lw-dot.lw-turn{animation:lw-turn .5s ease-out}@keyframes lw-beat{0%,90%{transform:scale(1)}45%{transform:scale(1.35)}}@keyframes lw-turn{0%{transform:scale(1.6);opacity:.4}}@media(prefers-reduced-motion:reduce){.lw-dot.lw-beat,.lw-dot.lw-turn{animation:none}}\n"] }]
|
|
714
|
+
}] });
|
|
715
|
+
const LIVE = 'Live - click to ask for the data again';
|
|
716
|
+
const DOWN = 'Disconnected - reconnecting. Click to try now';
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* Generated bundle index. Do not edit.
|
|
720
|
+
*/
|
|
721
|
+
|
|
722
|
+
export { LIVEWIRE_CONFIG, LiveIndicatorComponent, LiveList, LiveTopic, LiveWindowDataSource, LivewireClient, liveLabels, provideLivewire };
|
|
723
|
+
//# sourceMappingURL=softwarity-livewire.mjs.map
|