@softwarity/livewire 0.2.0 → 0.2.2
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/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 -333
- package/src/lib/livewire.client.ts +0 -215
- package/test/end-to-end.spec.ts +0 -174
- package/test/live-window.datasource.spec.ts +0 -234
- package/test/repaint.spec.ts +0 -183
- package/test/setup.ts +0 -6
- package/tsconfig.json +0 -18
- package/tsconfig.spec.json +0 -5
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
import { DOCUMENT } from '@angular/common';
|
|
2
|
-
import { Inject, Injectable, InjectionToken, Optional, signal } from '@angular/core';
|
|
3
|
-
import { Observable, Subject } from 'rxjs';
|
|
4
|
-
import { filter } from 'rxjs/operators';
|
|
5
|
-
import { SUBSCRIBE_EVENT, UNSUBSCRIBE_EVENT, UPDATE_EVENT } from '@softwarity/livewire-protocol';
|
|
6
|
-
import type { Envelope, JsonValue, LiveRow, UpdateFrame } from '@softwarity/livewire-protocol';
|
|
7
|
-
|
|
8
|
-
export interface LivewireConfig {
|
|
9
|
-
/** Where the socket answers. Root-relative, so a gateway can route it. */
|
|
10
|
-
path: string;
|
|
11
|
-
|
|
12
|
-
/** How long a dropped socket waits before trying again. */
|
|
13
|
-
reconnectMs?: number;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* How to open the connection.
|
|
17
|
-
*
|
|
18
|
-
* Absent, a real `WebSocket` against `path` on the page's own host. Supplied,
|
|
19
|
-
* anything of that shape - which is how the in-memory server is dropped in
|
|
20
|
-
* for a demo or a test with no backend at all.
|
|
21
|
-
*/
|
|
22
|
-
connect?: () => LivewireSocket;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** The little of a `WebSocket` this client uses. */
|
|
26
|
-
export interface LivewireSocket {
|
|
27
|
-
send(frame: string): void;
|
|
28
|
-
close(code?: number, reason?: string): void;
|
|
29
|
-
onmessage?: ((frame: string) => void) | null;
|
|
30
|
-
onclose?: ((code: number, reason: string) => void) | null;
|
|
31
|
-
onopen?: (() => void) | null;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export const LIVEWIRE_CONFIG = new InjectionToken<LivewireConfig>('LIVEWIRE_CONFIG');
|
|
35
|
-
|
|
36
|
-
const DEFAULT_RECONNECT_MS = 3000;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* The one socket, shared by every live list.
|
|
40
|
-
*
|
|
41
|
-
* One connection per tab, not one per screen: one handshake, one place where
|
|
42
|
-
* authorisation happens, one reconnection to get right. Subscriptions are told
|
|
43
|
-
* apart by an id the caller picks, so several lists can be open at once.
|
|
44
|
-
*/
|
|
45
|
-
@Injectable({ providedIn: 'root' })
|
|
46
|
-
export class LivewireClient {
|
|
47
|
-
/** Whether the socket is currently carrying anything. */
|
|
48
|
-
readonly live = signal(false);
|
|
49
|
-
|
|
50
|
-
private socket: LivewireSocket | null = null;
|
|
51
|
-
|
|
52
|
-
/** The wait before the next attempt, held so `retry` can cut it short. */
|
|
53
|
-
private retrying: ReturnType<typeof setTimeout> | null = null;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Everything the socket delivers, for the watchers to filter.
|
|
57
|
-
*
|
|
58
|
-
* A plain Subject between the socket and the screens, and that indirection is
|
|
59
|
-
* the whole point. `rxjs/webSocket` ties the connection to its subscriber
|
|
60
|
-
* count: it closes when the last one leaves and reopens on the next, so a
|
|
61
|
-
* list scrolling out of view took the connection down with it and the next
|
|
62
|
-
* one opened another - nine sockets for one list, each losing the snapshot
|
|
63
|
-
* sent to the one before.
|
|
64
|
-
*/
|
|
65
|
-
private readonly incoming = new Subject<Envelope<unknown>>();
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* What to re-send when a socket comes back.
|
|
69
|
-
*
|
|
70
|
-
* A reconnected socket knows nothing about what the screens were watching a
|
|
71
|
-
* second ago, and the screens have no reason to find out that it dropped.
|
|
72
|
-
*/
|
|
73
|
-
private readonly open = new Map<string, unknown>();
|
|
74
|
-
|
|
75
|
-
constructor(
|
|
76
|
-
@Inject(LIVEWIRE_CONFIG) private readonly config: LivewireConfig,
|
|
77
|
-
@Optional() @Inject(DOCUMENT) private readonly document: Document | null,
|
|
78
|
-
) {}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Watches one topic, and keeps watching it across reconnections.
|
|
82
|
-
*
|
|
83
|
-
* The returned stream starts with a snapshot and continues with patches -
|
|
84
|
-
* applying them is `LiveList`'s job. Unsubscribing closes the subscription
|
|
85
|
-
* server-side; the socket itself stays up for the other lists.
|
|
86
|
-
*/
|
|
87
|
-
watch<Row extends LiveRow>(id: string, topic: string, query: JsonValue | null): Observable<UpdateFrame<Row>> {
|
|
88
|
-
return new Observable<UpdateFrame<Row>>((subscriber) => {
|
|
89
|
-
this.connect();
|
|
90
|
-
const asked = { id, topic, query: query ?? undefined };
|
|
91
|
-
this.open.set(id, asked);
|
|
92
|
-
this.send(SUBSCRIBE_EVENT, asked);
|
|
93
|
-
|
|
94
|
-
const frames = this.incoming
|
|
95
|
-
.pipe(
|
|
96
|
-
filter((envelope): envelope is Envelope<UpdateFrame<Row>> => {
|
|
97
|
-
return envelope.event === UPDATE_EVENT && (envelope.data as UpdateFrame<Row>)?.id === id;
|
|
98
|
-
}),
|
|
99
|
-
)
|
|
100
|
-
.subscribe((envelope) => subscriber.next(envelope.data));
|
|
101
|
-
|
|
102
|
-
return () => {
|
|
103
|
-
frames.unsubscribe();
|
|
104
|
-
// The socket stays up for the other lists; only this window closes.
|
|
105
|
-
this.open.delete(id);
|
|
106
|
-
this.send(UNSUBSCRIBE_EVENT, { id });
|
|
107
|
-
};
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Asks for a window again, and gets a snapshot back.
|
|
113
|
-
*
|
|
114
|
-
* A gap in the sequence means the two sides disagree about what was sent.
|
|
115
|
-
* Asking again is cheap; carrying on would show a list nobody can trust.
|
|
116
|
-
*/
|
|
117
|
-
resync(id: string): void {
|
|
118
|
-
const asked = this.open.get(id);
|
|
119
|
-
if (asked) {
|
|
120
|
-
this.send(SUBSCRIBE_EVENT, asked);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* What a reader can do about it when something looks off.
|
|
126
|
-
*
|
|
127
|
-
* Two situations, one gesture, because from their side they are the same
|
|
128
|
-
* complaint - the screen may not be showing what is true. Down: the
|
|
129
|
-
* reconnection delay is skipped and the socket is opened now. Up: every open
|
|
130
|
-
* window is asked for again, and each answers with a snapshot.
|
|
131
|
-
*/
|
|
132
|
-
retry(): void {
|
|
133
|
-
if (this.socket) {
|
|
134
|
-
for (const asked of this.open.values()) {
|
|
135
|
-
this.send(SUBSCRIBE_EVENT, asked);
|
|
136
|
-
}
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
if (this.retrying !== null) {
|
|
140
|
-
clearTimeout(this.retrying);
|
|
141
|
-
this.retrying = null;
|
|
142
|
-
}
|
|
143
|
-
this.connect();
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Opens the one socket, once, and keeps it open.
|
|
148
|
-
*
|
|
149
|
-
* A plain `WebSocket` rather than `rxjs/webSocket`, deliberately - see the
|
|
150
|
-
* note on `incoming`. What this needs is one socket for as long as the tab
|
|
151
|
-
* lives, which is easier to write outright than to talk an operator chain
|
|
152
|
-
* into.
|
|
153
|
-
*/
|
|
154
|
-
private connect(): void {
|
|
155
|
-
if (this.socket) {
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
const socket = this.config.connect ? this.config.connect() : this.openWebSocket();
|
|
159
|
-
this.socket = socket;
|
|
160
|
-
|
|
161
|
-
socket.onopen = (): void => {
|
|
162
|
-
this.live.set(true);
|
|
163
|
-
// Whatever the screens were watching, asked for again. A reconnected
|
|
164
|
-
// socket knows nothing about them, and on the first open this is what
|
|
165
|
-
// sends the subscriptions queued before it came up.
|
|
166
|
-
for (const asked of this.open.values()) {
|
|
167
|
-
this.send(SUBSCRIBE_EVENT, asked);
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
socket.onmessage = (frame: string): void => {
|
|
172
|
-
try {
|
|
173
|
-
this.incoming.next(JSON.parse(frame) as Envelope<unknown>);
|
|
174
|
-
} catch (error) {
|
|
175
|
-
console.error('[livewire] unreadable frame', error);
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
// A dropped socket is the normal case, not a fault: a gateway restart, a
|
|
180
|
-
// laptop that slept. Reconnecting is this client's job so no screen has to
|
|
181
|
-
// know it happened.
|
|
182
|
-
socket.onclose = (): void => {
|
|
183
|
-
this.live.set(false);
|
|
184
|
-
this.socket = null;
|
|
185
|
-
this.retrying = setTimeout(() => {
|
|
186
|
-
this.retrying = null;
|
|
187
|
-
this.connect();
|
|
188
|
-
}, this.config.reconnectMs ?? DEFAULT_RECONNECT_MS);
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
private openWebSocket(): LivewireSocket {
|
|
193
|
-
const location = this.document?.location;
|
|
194
|
-
const scheme = location?.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
195
|
-
const native = new WebSocket(`${scheme}//${location?.host ?? ''}${this.config.path}`);
|
|
196
|
-
// Adapted rather than used directly: a `MessageEvent` is the browser's
|
|
197
|
-
// shape, and everything else here - the in-memory server, the tests -
|
|
198
|
-
// speaks in frames.
|
|
199
|
-
const socket: LivewireSocket = {
|
|
200
|
-
send: (frame) => native.send(frame),
|
|
201
|
-
close: (code, reason) => native.close(code, reason),
|
|
202
|
-
};
|
|
203
|
-
native.onopen = () => socket.onopen?.();
|
|
204
|
-
native.onmessage = (event: MessageEvent) => socket.onmessage?.(String(event.data));
|
|
205
|
-
native.onclose = (event: CloseEvent) => socket.onclose?.(event.code, event.reason);
|
|
206
|
-
native.onerror = () => console.error('[livewire] socket error');
|
|
207
|
-
return socket;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
private send(event: string, data: unknown): void {
|
|
211
|
-
// Anything sent before the socket is up is not queued here: `onopen` sends
|
|
212
|
-
// every open subscription, which is the same set and a shorter path.
|
|
213
|
-
this.socket?.send(JSON.stringify({ event, data }));
|
|
214
|
-
}
|
|
215
|
-
}
|
package/test/end-to-end.spec.ts
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import { ChangeDetectorRef } from '@angular/core';
|
|
2
|
-
import { TestBed } from '@angular/core/testing';
|
|
3
|
-
import { MockServer } from '@softwarity/livewire-mock';
|
|
4
|
-
import { LiveList } from '../src/lib/live-list';
|
|
5
|
-
import { LiveTopic, liveLabels } from '../src/lib/live-topic';
|
|
6
|
-
import { LiveWindowDataSource } from '../src/lib/live-window.datasource';
|
|
7
|
-
import { LivewireClient } from '../src/lib/livewire.client';
|
|
8
|
-
import { provideLivewire } from '../src/lib/provide-livewire';
|
|
9
|
-
import type { LiveRow, LiveWindow } from '@softwarity/livewire-protocol';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* The client against the in-memory server: the whole loop, with nothing faked
|
|
13
|
-
* in between.
|
|
14
|
-
*
|
|
15
|
-
* The mock speaks the protocol and passes the conformance suite, so what this
|
|
16
|
-
* checks is the client's half - that it opens one socket, applies snapshots and
|
|
17
|
-
* patches, tells a screen when to resync, and closes what it opened.
|
|
18
|
-
*/
|
|
19
|
-
describe('the client, end to end', () => {
|
|
20
|
-
let server: MockServer;
|
|
21
|
-
let rows: LiveRow[];
|
|
22
|
-
|
|
23
|
-
function clientOf(): LivewireClient {
|
|
24
|
-
TestBed.resetTestingModule();
|
|
25
|
-
TestBed.configureTestingModule({
|
|
26
|
-
providers: [
|
|
27
|
-
provideLivewire({ path: '', connect: () => server.connect() }),
|
|
28
|
-
// The data source repaints a view; here there is none, so the ref is a
|
|
29
|
-
// stub. `repaint.spec.ts` is where the repainting itself is checked.
|
|
30
|
-
{ provide: ChangeDetectorRef, useValue: { markForCheck: () => undefined } },
|
|
31
|
-
],
|
|
32
|
-
});
|
|
33
|
-
return TestBed.inject(LivewireClient);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** The mock opens on the next tick, as a socket does. */
|
|
37
|
-
const settle = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0));
|
|
38
|
-
|
|
39
|
-
beforeEach(() => {
|
|
40
|
-
rows = [
|
|
41
|
-
{ id: 'a', updatedAt: 'v1' },
|
|
42
|
-
{ id: 'b', updatedAt: 'v1' },
|
|
43
|
-
];
|
|
44
|
-
server = new MockServer();
|
|
45
|
-
server.register('rows', { windowFor: (): LiveWindow => ({ rows, total: rows.length }) });
|
|
46
|
-
server.register('kinds', {
|
|
47
|
-
windowFor: (): LiveWindow => ({ rows: [{ id: 'FPL', updatedAt: 'FPL', label: 'Flight plan' } as LiveRow] }),
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('reports the socket as up once it opens', async () => {
|
|
52
|
-
const client = clientOf();
|
|
53
|
-
client.watch('a', 'rows', null).subscribe();
|
|
54
|
-
await settle();
|
|
55
|
-
|
|
56
|
-
expect(client.live()).toBe(true);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('answers a snapshot, then a patch when the data moves', async () => {
|
|
60
|
-
const client = clientOf();
|
|
61
|
-
const seen: string[] = [];
|
|
62
|
-
client.watch('a', 'rows', null).subscribe((frame) => seen.push(frame.type));
|
|
63
|
-
await settle();
|
|
64
|
-
|
|
65
|
-
rows = [{ id: 'c', updatedAt: 'v1' }, ...rows];
|
|
66
|
-
server.touched();
|
|
67
|
-
|
|
68
|
-
expect(seen).toEqual(['snapshot', 'patch']);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('applies both into a list a screen can read', async () => {
|
|
72
|
-
const client = clientOf();
|
|
73
|
-
const list = new LiveList<LiveRow>();
|
|
74
|
-
client.watch<LiveRow>('a', 'rows', null).subscribe((frame) => list.apply(frame));
|
|
75
|
-
await settle();
|
|
76
|
-
|
|
77
|
-
rows = [{ id: 'c', updatedAt: 'v1' }, ...rows];
|
|
78
|
-
server.touched();
|
|
79
|
-
|
|
80
|
-
expect(list.rows().map((row) => row.id)).toEqual(['c', 'a', 'b']);
|
|
81
|
-
expect(list.total()).toBe(3);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('says nothing when the window did not move', async () => {
|
|
85
|
-
const client = clientOf();
|
|
86
|
-
const seen: string[] = [];
|
|
87
|
-
client.watch('a', 'rows', null).subscribe((frame) => seen.push(frame.type));
|
|
88
|
-
await settle();
|
|
89
|
-
|
|
90
|
-
server.touched();
|
|
91
|
-
server.touched();
|
|
92
|
-
|
|
93
|
-
expect(seen).toEqual(['snapshot']);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('opens one socket for two subscriptions', async () => {
|
|
97
|
-
let connections = 0;
|
|
98
|
-
const counting = new MockServer();
|
|
99
|
-
counting.register('rows', { windowFor: (): LiveWindow => ({ rows, total: rows.length }) });
|
|
100
|
-
TestBed.resetTestingModule();
|
|
101
|
-
TestBed.configureTestingModule({
|
|
102
|
-
providers: [
|
|
103
|
-
provideLivewire({
|
|
104
|
-
path: '',
|
|
105
|
-
connect: () => {
|
|
106
|
-
connections += 1;
|
|
107
|
-
return counting.connect();
|
|
108
|
-
},
|
|
109
|
-
}),
|
|
110
|
-
],
|
|
111
|
-
});
|
|
112
|
-
const client = TestBed.inject(LivewireClient);
|
|
113
|
-
|
|
114
|
-
client.watch('a', 'rows', null).subscribe();
|
|
115
|
-
client.watch('b', 'rows', null).subscribe();
|
|
116
|
-
await settle();
|
|
117
|
-
|
|
118
|
-
expect(connections).toBe(1);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('stops delivering once a screen unsubscribes', async () => {
|
|
122
|
-
const client = clientOf();
|
|
123
|
-
const seen: string[] = [];
|
|
124
|
-
const watching = client.watch('a', 'rows', null).subscribe((frame) => seen.push(frame.type));
|
|
125
|
-
await settle();
|
|
126
|
-
|
|
127
|
-
watching.unsubscribe();
|
|
128
|
-
rows = [{ id: 'c', updatedAt: 'v1' }, ...rows];
|
|
129
|
-
server.touched();
|
|
130
|
-
|
|
131
|
-
expect(seen).toEqual(['snapshot']);
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it('feeds a virtual-scroll source from end to end', async () => {
|
|
135
|
-
const client = clientOf();
|
|
136
|
-
const topic = new LiveTopic<LiveRow>(client, 'rows');
|
|
137
|
-
// Built in an injection context, as it is in a component field: the data
|
|
138
|
-
// source takes the view it repaints from there.
|
|
139
|
-
const source = TestBed.runInInjectionContext(() => new LiveWindowDataSource<LiveRow>());
|
|
140
|
-
source.reset((offset, limit) => topic.window({}, offset, limit));
|
|
141
|
-
await settle();
|
|
142
|
-
|
|
143
|
-
expect(source.length).toBe(2);
|
|
144
|
-
expect(source.at(0)).toEqual({ id: 'a', updatedAt: 'v1' });
|
|
145
|
-
|
|
146
|
-
rows = [{ id: 'c', updatedAt: 'v1' }, ...rows];
|
|
147
|
-
server.touched();
|
|
148
|
-
|
|
149
|
-
expect(source.at(0)).toEqual({ id: 'c', updatedAt: 'v1' });
|
|
150
|
-
expect(source.fresh('c')).toBe(true);
|
|
151
|
-
expect(source.fresh('a')).toBe(false);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it('reads a short list as labels', async () => {
|
|
155
|
-
const client = clientOf();
|
|
156
|
-
const seen: { id: string; label: string }[][] = [];
|
|
157
|
-
liveLabels(client, 'kinds').subscribe((labels) => seen.push(labels));
|
|
158
|
-
await settle();
|
|
159
|
-
|
|
160
|
-
expect(seen.at(-1)).toEqual([{ id: 'FPL', label: 'Flight plan' }]);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
it('asks again for every open window when told to retry', async () => {
|
|
164
|
-
const client = clientOf();
|
|
165
|
-
const seen: string[] = [];
|
|
166
|
-
client.watch('a', 'rows', null).subscribe((frame) => seen.push(frame.type));
|
|
167
|
-
await settle();
|
|
168
|
-
|
|
169
|
-
client.retry();
|
|
170
|
-
|
|
171
|
-
// A resubscribe answers with a snapshot, whatever the window holds.
|
|
172
|
-
expect(seen).toEqual(['snapshot', 'snapshot']);
|
|
173
|
-
});
|
|
174
|
-
});
|
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import { ChangeDetectorRef } from '@angular/core';
|
|
2
|
-
import { TestBed } from '@angular/core/testing';
|
|
3
|
-
import { Subject } from 'rxjs';
|
|
4
|
-
import { LiveWindowDataSource } from '../src/lib/live-window.datasource';
|
|
5
|
-
import type { CollectionViewer } from '@angular/cdk/collections';
|
|
6
|
-
import type { LiveRow, UpdateFrame } from '@softwarity/livewire-protocol';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* The data source injects the view it repaints, so it is built in an injection
|
|
10
|
-
* context here as it is in a component field. Nothing in this file is about
|
|
11
|
-
* repainting - `repaint.spec.ts` covers that - so the ref is a stub.
|
|
12
|
-
*/
|
|
13
|
-
function build<Row extends LiveRow>(...args: ConstructorParameters<typeof LiveWindowDataSource>): LiveWindowDataSource<Row> {
|
|
14
|
-
return TestBed.runInInjectionContext(() => new LiveWindowDataSource<Row>(...args));
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
beforeEach(() =>
|
|
18
|
-
TestBed.configureTestingModule({
|
|
19
|
-
providers: [{ provide: ChangeDetectorRef, useValue: { markForCheck: () => undefined } }],
|
|
20
|
-
}),
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
function rows(from: number, count: number): LiveRow[] {
|
|
24
|
-
return Array.from({ length: count }, (_, index) => ({ id: `r${from + index}`, updatedAt: 'v1' }));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
describe('LiveWindowDataSource', () => {
|
|
28
|
-
let opened: { offset: number; limit: number }[];
|
|
29
|
-
let feed: Subject<UpdateFrame<LiveRow>>;
|
|
30
|
-
let source: LiveWindowDataSource<LiveRow>;
|
|
31
|
-
let viewChange: Subject<{ start: number; end: number }>;
|
|
32
|
-
|
|
33
|
-
function sourceOf(window?: number): void {
|
|
34
|
-
opened = [];
|
|
35
|
-
feed = new Subject<UpdateFrame<LiveRow>>();
|
|
36
|
-
source = build<LiveRow>(() => undefined, () => undefined, window);
|
|
37
|
-
viewChange = new Subject<{ start: number; end: number }>();
|
|
38
|
-
source.connect({ viewChange } as unknown as CollectionViewer);
|
|
39
|
-
source.reset((offset, limit) => {
|
|
40
|
-
opened.push({ offset, limit });
|
|
41
|
-
return feed;
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function answer(offset: number, total: number, held = 100): void {
|
|
46
|
-
feed.next({ id: 'w', type: 'snapshot', rows: rows(offset, held), total, sequence: 1 });
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
beforeEach(() => sourceOf());
|
|
50
|
-
|
|
51
|
-
it('opens one window, not one per page', () => {
|
|
52
|
-
expect(opened).toEqual([{ offset: 0, limit: 100 }]);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* The loop this guards against freezes the renderer: every publication makes
|
|
57
|
-
* the list longer, the viewport recalculates and emits a range, and anything
|
|
58
|
-
* that re-subscribes on that range publishes again.
|
|
59
|
-
*/
|
|
60
|
-
it('stays put while the view is inside the window', () => {
|
|
61
|
-
answer(0, 13_000);
|
|
62
|
-
opened.length = 0;
|
|
63
|
-
|
|
64
|
-
for (const total of [13_010, 13_020, 13_030]) {
|
|
65
|
-
viewChange.next({ start: 20, end: 78 });
|
|
66
|
-
answer(0, total);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
expect(opened).toEqual([]);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('moves once the view walks out, and only then', () => {
|
|
73
|
-
answer(0, 13_000);
|
|
74
|
-
opened.length = 0;
|
|
75
|
-
|
|
76
|
-
viewChange.next({ start: 60, end: 98 });
|
|
77
|
-
expect(opened).toHaveLength(1);
|
|
78
|
-
expect(opened[0].offset).toBeGreaterThan(0);
|
|
79
|
-
|
|
80
|
-
const moved = opened[0].offset;
|
|
81
|
-
answer(moved, 13_000);
|
|
82
|
-
opened.length = 0;
|
|
83
|
-
viewChange.next({ start: moved + 20, end: moved + 78 });
|
|
84
|
-
|
|
85
|
-
expect(opened).toEqual([]);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* A viewport reduced to nothing - a drawer opening over it - emits an empty
|
|
90
|
-
* range. Taken at face value it moves the window to the top of the list, and
|
|
91
|
-
* the list behind the panel comes back blank.
|
|
92
|
-
*/
|
|
93
|
-
it('ignores an empty range rather than treating it as a position', () => {
|
|
94
|
-
viewChange.next({ start: 5000, end: 5058 });
|
|
95
|
-
const moved = opened.at(-1)?.offset ?? 0;
|
|
96
|
-
answer(moved, 13_000);
|
|
97
|
-
opened.length = 0;
|
|
98
|
-
|
|
99
|
-
viewChange.next({ start: 0, end: 0 });
|
|
100
|
-
|
|
101
|
-
expect(opened).toEqual([]);
|
|
102
|
-
expect(source.at(moved)).toBeDefined();
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('moves the window to cover a row asked for outright', () => {
|
|
106
|
-
answer(0, 13_000);
|
|
107
|
-
opened.length = 0;
|
|
108
|
-
|
|
109
|
-
source.ensure(9000);
|
|
110
|
-
|
|
111
|
-
expect(opened).toHaveLength(1);
|
|
112
|
-
expect(opened[0].offset).toBeLessThanOrEqual(9000);
|
|
113
|
-
expect(opened[0].offset + 100).toBeGreaterThan(9000);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it('places the rows it holds at their own offset in the published list', () => {
|
|
117
|
-
viewChange.next({ start: 5000, end: 5058 });
|
|
118
|
-
const moved = opened.at(-1)?.offset ?? 0;
|
|
119
|
-
answer(moved, 13_000);
|
|
120
|
-
|
|
121
|
-
expect(source.length).toBe(13_000);
|
|
122
|
-
expect(source.at(moved)).toEqual({ id: `r${moved}`, updatedAt: 'v1' });
|
|
123
|
-
expect(source.at(0)).toBeUndefined();
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it('carries the pivot through, whatever it means', () => {
|
|
127
|
-
feed.next({ id: 'w', type: 'snapshot', rows: rows(0, 10), total: 500, pivot: 42, sequence: 1 });
|
|
128
|
-
|
|
129
|
-
expect(source.pivot).toBe(42);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it('resyncs rather than applying a frame it cannot place', () => {
|
|
133
|
-
let drifted = 0;
|
|
134
|
-
opened = [];
|
|
135
|
-
feed = new Subject<UpdateFrame<LiveRow>>();
|
|
136
|
-
source = build<LiveRow>(
|
|
137
|
-
() => undefined,
|
|
138
|
-
() => (drifted += 1),
|
|
139
|
-
);
|
|
140
|
-
source.reset(() => feed);
|
|
141
|
-
|
|
142
|
-
feed.next({ id: 'w', type: 'snapshot', rows: rows(0, 3), total: 3, sequence: 1 });
|
|
143
|
-
// Sequence 3 where 2 was due: a frame went missing.
|
|
144
|
-
feed.next({ id: 'w', type: 'patch', upserted: [], removed: [], order: ['r0'], total: 1, sequence: 3 });
|
|
145
|
-
|
|
146
|
-
expect(drifted).toBe(1);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* The mark that lights a row for a moment. The trap it has to avoid is
|
|
151
|
-
* lighting the whole screen: a snapshot is what arriving on the list and what
|
|
152
|
-
* scrolling both get back, and neither means anything arrived.
|
|
153
|
-
*/
|
|
154
|
-
describe('what counts as fresh', () => {
|
|
155
|
-
beforeEach(() => answer(0, 13_000));
|
|
156
|
-
|
|
157
|
-
it('marks nothing on a snapshot - a window that moved is not news', () => {
|
|
158
|
-
expect(source.fresh('r0')).toBe(false);
|
|
159
|
-
expect(source.fresh('r40')).toBe(false);
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it('marks the rows a patch actually changed, and only those', () => {
|
|
163
|
-
feed.next({
|
|
164
|
-
id: 'w',
|
|
165
|
-
type: 'patch',
|
|
166
|
-
upserted: [{ id: 'new1', updatedAt: 'v1' }],
|
|
167
|
-
removed: ['r99'],
|
|
168
|
-
order: ['new1', ...rows(0, 99).map((row) => row.id)],
|
|
169
|
-
total: 13_001,
|
|
170
|
-
sequence: 2,
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
expect(source.fresh('new1')).toBe(true);
|
|
174
|
-
// Shifted down by one, unchanged, not in `upserted` - and not marked.
|
|
175
|
-
expect(source.fresh('r0')).toBe(false);
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
it('says nothing about a row it was never told about', () => {
|
|
179
|
-
expect(source.fresh(undefined)).toBe(false);
|
|
180
|
-
});
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* The flight list, whose rows are heavy enough that its window holds fifty.
|
|
185
|
-
*
|
|
186
|
-
* That is where a band of rows loaded by nothing showed up, between one
|
|
187
|
-
* window and the next: the offset was centred on the view and then rounded to
|
|
188
|
-
* a block, with nothing checking that the result still covered the rows being
|
|
189
|
-
* rendered. Rendered [90, 120) rounded to [100, 150) - and since that was
|
|
190
|
-
* already the offset, the ten rows above it stayed placeholders for good.
|
|
191
|
-
*/
|
|
192
|
-
describe('a window barely wider than the viewport', () => {
|
|
193
|
-
const WINDOW = 50;
|
|
194
|
-
|
|
195
|
-
beforeEach(() => {
|
|
196
|
-
sourceOf(WINDOW);
|
|
197
|
-
feed.next({ id: 'w', type: 'snapshot', rows: rows(0, WINDOW), total: 13_000, sequence: 1 });
|
|
198
|
-
opened.length = 0;
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it('always covers every rendered row, whatever the block rounding says', () => {
|
|
202
|
-
for (const start of [30, 55, 90, 91, 137, 4321]) {
|
|
203
|
-
const range = { start, end: start + 30 };
|
|
204
|
-
viewChange.next(range);
|
|
205
|
-
const offset = opened.at(-1)?.offset ?? 0;
|
|
206
|
-
expect(offset).toBeLessThanOrEqual(range.start);
|
|
207
|
-
expect(offset + WINDOW).toBeGreaterThanOrEqual(range.end);
|
|
208
|
-
feed.next({ id: 'w', type: 'snapshot', rows: rows(offset, WINDOW), total: 13_000, sequence: 1 });
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
it('leaves no hole in the rows the viewport is showing', () => {
|
|
213
|
-
viewChange.next({ start: 90, end: 120 });
|
|
214
|
-
const offset = opened.at(-1)?.offset ?? 0;
|
|
215
|
-
feed.next({ id: 'w', type: 'snapshot', rows: rows(offset, WINDOW), total: 13_000, sequence: 1 });
|
|
216
|
-
|
|
217
|
-
for (let index = 90; index < 120; index += 1) {
|
|
218
|
-
expect(source.at(index)).toBeDefined();
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
it('still stands still once it covers the view, however tight the fit', () => {
|
|
223
|
-
viewChange.next({ start: 90, end: 120 });
|
|
224
|
-
const offset = opened.at(-1)?.offset ?? 0;
|
|
225
|
-
feed.next({ id: 'w', type: 'snapshot', rows: rows(offset, WINDOW), total: 13_000, sequence: 1 });
|
|
226
|
-
opened.length = 0;
|
|
227
|
-
|
|
228
|
-
viewChange.next({ start: 90, end: 120 });
|
|
229
|
-
viewChange.next({ start: 90, end: 120 });
|
|
230
|
-
|
|
231
|
-
expect(opened).toEqual([]);
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
});
|