@softwarity/livewire 0.1.0
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 +89 -0
- package/jest.config.js +11 -0
- package/ng-package.json +6 -0
- package/package.json +61 -0
- package/src/lib/live-indicator.component.ts +118 -0
- package/src/lib/live-list.ts +94 -0
- package/src/lib/live-topic.ts +83 -0
- package/src/lib/live-window.datasource.ts +322 -0
- package/src/lib/livewire.client.ts +215 -0
- package/src/lib/provide-livewire.ts +21 -0
- package/src/public-api.ts +20 -0
- package/test/end-to-end.spec.ts +166 -0
- package/test/live-window.datasource.spec.ts +221 -0
- package/test/setup.ts +6 -0
- package/tsconfig.json +18 -0
- package/tsconfig.spec.json +5 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
import { MockServer } from '@softwarity/livewire-mock';
|
|
3
|
+
import { LiveList } from '../src/lib/live-list';
|
|
4
|
+
import { LiveTopic, liveLabels } from '../src/lib/live-topic';
|
|
5
|
+
import { LiveWindowDataSource } from '../src/lib/live-window.datasource';
|
|
6
|
+
import { LivewireClient } from '../src/lib/livewire.client';
|
|
7
|
+
import { provideLivewire } from '../src/lib/provide-livewire';
|
|
8
|
+
import type { LiveRow, LiveWindow } from '@softwarity/livewire-protocol';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The client against the in-memory server: the whole loop, with nothing faked
|
|
12
|
+
* in between.
|
|
13
|
+
*
|
|
14
|
+
* The mock speaks the protocol and passes the conformance suite, so what this
|
|
15
|
+
* checks is the client's half - that it opens one socket, applies snapshots and
|
|
16
|
+
* patches, tells a screen when to resync, and closes what it opened.
|
|
17
|
+
*/
|
|
18
|
+
describe('the client, end to end', () => {
|
|
19
|
+
let server: MockServer;
|
|
20
|
+
let rows: LiveRow[];
|
|
21
|
+
|
|
22
|
+
function clientOf(): LivewireClient {
|
|
23
|
+
TestBed.resetTestingModule();
|
|
24
|
+
TestBed.configureTestingModule({
|
|
25
|
+
providers: [provideLivewire({ path: '', connect: () => server.connect() })],
|
|
26
|
+
});
|
|
27
|
+
return TestBed.inject(LivewireClient);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The mock opens on the next tick, as a socket does. */
|
|
31
|
+
const settle = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0));
|
|
32
|
+
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
rows = [
|
|
35
|
+
{ id: 'a', updatedAt: 'v1' },
|
|
36
|
+
{ id: 'b', updatedAt: 'v1' },
|
|
37
|
+
];
|
|
38
|
+
server = new MockServer();
|
|
39
|
+
server.register('rows', { windowFor: (): LiveWindow => ({ rows, total: rows.length }) });
|
|
40
|
+
server.register('kinds', {
|
|
41
|
+
windowFor: (): LiveWindow => ({ rows: [{ id: 'FPL', updatedAt: 'FPL', label: 'Flight plan' } as LiveRow] }),
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('reports the socket as up once it opens', async () => {
|
|
46
|
+
const client = clientOf();
|
|
47
|
+
client.watch('a', 'rows', null).subscribe();
|
|
48
|
+
await settle();
|
|
49
|
+
|
|
50
|
+
expect(client.live()).toBe(true);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('answers a snapshot, then a patch when the data moves', async () => {
|
|
54
|
+
const client = clientOf();
|
|
55
|
+
const seen: string[] = [];
|
|
56
|
+
client.watch('a', 'rows', null).subscribe((frame) => seen.push(frame.type));
|
|
57
|
+
await settle();
|
|
58
|
+
|
|
59
|
+
rows = [{ id: 'c', updatedAt: 'v1' }, ...rows];
|
|
60
|
+
server.touched();
|
|
61
|
+
|
|
62
|
+
expect(seen).toEqual(['snapshot', 'patch']);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('applies both into a list a screen can read', async () => {
|
|
66
|
+
const client = clientOf();
|
|
67
|
+
const list = new LiveList<LiveRow>();
|
|
68
|
+
client.watch<LiveRow>('a', 'rows', null).subscribe((frame) => list.apply(frame));
|
|
69
|
+
await settle();
|
|
70
|
+
|
|
71
|
+
rows = [{ id: 'c', updatedAt: 'v1' }, ...rows];
|
|
72
|
+
server.touched();
|
|
73
|
+
|
|
74
|
+
expect(list.rows().map((row) => row.id)).toEqual(['c', 'a', 'b']);
|
|
75
|
+
expect(list.total()).toBe(3);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('says nothing when the window did not move', async () => {
|
|
79
|
+
const client = clientOf();
|
|
80
|
+
const seen: string[] = [];
|
|
81
|
+
client.watch('a', 'rows', null).subscribe((frame) => seen.push(frame.type));
|
|
82
|
+
await settle();
|
|
83
|
+
|
|
84
|
+
server.touched();
|
|
85
|
+
server.touched();
|
|
86
|
+
|
|
87
|
+
expect(seen).toEqual(['snapshot']);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('opens one socket for two subscriptions', async () => {
|
|
91
|
+
let connections = 0;
|
|
92
|
+
const counting = new MockServer();
|
|
93
|
+
counting.register('rows', { windowFor: (): LiveWindow => ({ rows, total: rows.length }) });
|
|
94
|
+
TestBed.resetTestingModule();
|
|
95
|
+
TestBed.configureTestingModule({
|
|
96
|
+
providers: [
|
|
97
|
+
provideLivewire({
|
|
98
|
+
path: '',
|
|
99
|
+
connect: () => {
|
|
100
|
+
connections += 1;
|
|
101
|
+
return counting.connect();
|
|
102
|
+
},
|
|
103
|
+
}),
|
|
104
|
+
],
|
|
105
|
+
});
|
|
106
|
+
const client = TestBed.inject(LivewireClient);
|
|
107
|
+
|
|
108
|
+
client.watch('a', 'rows', null).subscribe();
|
|
109
|
+
client.watch('b', 'rows', null).subscribe();
|
|
110
|
+
await settle();
|
|
111
|
+
|
|
112
|
+
expect(connections).toBe(1);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('stops delivering once a screen unsubscribes', async () => {
|
|
116
|
+
const client = clientOf();
|
|
117
|
+
const seen: string[] = [];
|
|
118
|
+
const watching = client.watch('a', 'rows', null).subscribe((frame) => seen.push(frame.type));
|
|
119
|
+
await settle();
|
|
120
|
+
|
|
121
|
+
watching.unsubscribe();
|
|
122
|
+
rows = [{ id: 'c', updatedAt: 'v1' }, ...rows];
|
|
123
|
+
server.touched();
|
|
124
|
+
|
|
125
|
+
expect(seen).toEqual(['snapshot']);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('feeds a virtual-scroll source from end to end', async () => {
|
|
129
|
+
const client = clientOf();
|
|
130
|
+
const topic = new LiveTopic<LiveRow>(client, 'rows');
|
|
131
|
+
const source = new LiveWindowDataSource<LiveRow>();
|
|
132
|
+
source.reset((offset, limit) => topic.window({}, offset, limit));
|
|
133
|
+
await settle();
|
|
134
|
+
|
|
135
|
+
expect(source.length).toBe(2);
|
|
136
|
+
expect(source.at(0)).toEqual({ id: 'a', updatedAt: 'v1' });
|
|
137
|
+
|
|
138
|
+
rows = [{ id: 'c', updatedAt: 'v1' }, ...rows];
|
|
139
|
+
server.touched();
|
|
140
|
+
|
|
141
|
+
expect(source.at(0)).toEqual({ id: 'c', updatedAt: 'v1' });
|
|
142
|
+
expect(source.fresh('c')).toBe(true);
|
|
143
|
+
expect(source.fresh('a')).toBe(false);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('reads a short list as labels', async () => {
|
|
147
|
+
const client = clientOf();
|
|
148
|
+
const seen: { id: string; label: string }[][] = [];
|
|
149
|
+
liveLabels(client, 'kinds').subscribe((labels) => seen.push(labels));
|
|
150
|
+
await settle();
|
|
151
|
+
|
|
152
|
+
expect(seen.at(-1)).toEqual([{ id: 'FPL', label: 'Flight plan' }]);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('asks again for every open window when told to retry', async () => {
|
|
156
|
+
const client = clientOf();
|
|
157
|
+
const seen: string[] = [];
|
|
158
|
+
client.watch('a', 'rows', null).subscribe((frame) => seen.push(frame.type));
|
|
159
|
+
await settle();
|
|
160
|
+
|
|
161
|
+
client.retry();
|
|
162
|
+
|
|
163
|
+
// A resubscribe answers with a snapshot, whatever the window holds.
|
|
164
|
+
expect(seen).toEqual(['snapshot', 'snapshot']);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { Subject } from 'rxjs';
|
|
2
|
+
import { LiveWindowDataSource } from '../src/lib/live-window.datasource';
|
|
3
|
+
import type { CollectionViewer } from '@angular/cdk/collections';
|
|
4
|
+
import type { LiveRow, UpdateFrame } from '@softwarity/livewire-protocol';
|
|
5
|
+
|
|
6
|
+
function rows(from: number, count: number): LiveRow[] {
|
|
7
|
+
return Array.from({ length: count }, (_, index) => ({ id: `r${from + index}`, updatedAt: 'v1' }));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
describe('LiveWindowDataSource', () => {
|
|
11
|
+
let opened: { offset: number; limit: number }[];
|
|
12
|
+
let feed: Subject<UpdateFrame<LiveRow>>;
|
|
13
|
+
let source: LiveWindowDataSource<LiveRow>;
|
|
14
|
+
let viewChange: Subject<{ start: number; end: number }>;
|
|
15
|
+
|
|
16
|
+
function sourceOf(window?: number): void {
|
|
17
|
+
opened = [];
|
|
18
|
+
feed = new Subject<UpdateFrame<LiveRow>>();
|
|
19
|
+
source = new LiveWindowDataSource<LiveRow>(
|
|
20
|
+
() => undefined,
|
|
21
|
+
() => undefined,
|
|
22
|
+
window,
|
|
23
|
+
);
|
|
24
|
+
viewChange = new Subject<{ start: number; end: number }>();
|
|
25
|
+
source.connect({ viewChange } as unknown as CollectionViewer);
|
|
26
|
+
source.reset((offset, limit) => {
|
|
27
|
+
opened.push({ offset, limit });
|
|
28
|
+
return feed;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function answer(offset: number, total: number, held = 100): void {
|
|
33
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(offset, held), total, sequence: 1 });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
beforeEach(() => sourceOf());
|
|
37
|
+
|
|
38
|
+
it('opens one window, not one per page', () => {
|
|
39
|
+
expect(opened).toEqual([{ offset: 0, limit: 100 }]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The loop this guards against freezes the renderer: every publication makes
|
|
44
|
+
* the list longer, the viewport recalculates and emits a range, and anything
|
|
45
|
+
* that re-subscribes on that range publishes again.
|
|
46
|
+
*/
|
|
47
|
+
it('stays put while the view is inside the window', () => {
|
|
48
|
+
answer(0, 13_000);
|
|
49
|
+
opened.length = 0;
|
|
50
|
+
|
|
51
|
+
for (const total of [13_010, 13_020, 13_030]) {
|
|
52
|
+
viewChange.next({ start: 20, end: 78 });
|
|
53
|
+
answer(0, total);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
expect(opened).toEqual([]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('moves once the view walks out, and only then', () => {
|
|
60
|
+
answer(0, 13_000);
|
|
61
|
+
opened.length = 0;
|
|
62
|
+
|
|
63
|
+
viewChange.next({ start: 60, end: 98 });
|
|
64
|
+
expect(opened).toHaveLength(1);
|
|
65
|
+
expect(opened[0].offset).toBeGreaterThan(0);
|
|
66
|
+
|
|
67
|
+
const moved = opened[0].offset;
|
|
68
|
+
answer(moved, 13_000);
|
|
69
|
+
opened.length = 0;
|
|
70
|
+
viewChange.next({ start: moved + 20, end: moved + 78 });
|
|
71
|
+
|
|
72
|
+
expect(opened).toEqual([]);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* A viewport reduced to nothing - a drawer opening over it - emits an empty
|
|
77
|
+
* range. Taken at face value it moves the window to the top of the list, and
|
|
78
|
+
* the list behind the panel comes back blank.
|
|
79
|
+
*/
|
|
80
|
+
it('ignores an empty range rather than treating it as a position', () => {
|
|
81
|
+
viewChange.next({ start: 5000, end: 5058 });
|
|
82
|
+
const moved = opened.at(-1)?.offset ?? 0;
|
|
83
|
+
answer(moved, 13_000);
|
|
84
|
+
opened.length = 0;
|
|
85
|
+
|
|
86
|
+
viewChange.next({ start: 0, end: 0 });
|
|
87
|
+
|
|
88
|
+
expect(opened).toEqual([]);
|
|
89
|
+
expect(source.at(moved)).toBeDefined();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('moves the window to cover a row asked for outright', () => {
|
|
93
|
+
answer(0, 13_000);
|
|
94
|
+
opened.length = 0;
|
|
95
|
+
|
|
96
|
+
source.ensure(9000);
|
|
97
|
+
|
|
98
|
+
expect(opened).toHaveLength(1);
|
|
99
|
+
expect(opened[0].offset).toBeLessThanOrEqual(9000);
|
|
100
|
+
expect(opened[0].offset + 100).toBeGreaterThan(9000);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('places the rows it holds at their own offset in the published list', () => {
|
|
104
|
+
viewChange.next({ start: 5000, end: 5058 });
|
|
105
|
+
const moved = opened.at(-1)?.offset ?? 0;
|
|
106
|
+
answer(moved, 13_000);
|
|
107
|
+
|
|
108
|
+
expect(source.length).toBe(13_000);
|
|
109
|
+
expect(source.at(moved)).toEqual({ id: `r${moved}`, updatedAt: 'v1' });
|
|
110
|
+
expect(source.at(0)).toBeUndefined();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('carries the pivot through, whatever it means', () => {
|
|
114
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(0, 10), total: 500, pivot: 42, sequence: 1 });
|
|
115
|
+
|
|
116
|
+
expect(source.pivot).toBe(42);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('resyncs rather than applying a frame it cannot place', () => {
|
|
120
|
+
let drifted = 0;
|
|
121
|
+
opened = [];
|
|
122
|
+
feed = new Subject<UpdateFrame<LiveRow>>();
|
|
123
|
+
source = new LiveWindowDataSource<LiveRow>(
|
|
124
|
+
() => undefined,
|
|
125
|
+
() => (drifted += 1),
|
|
126
|
+
);
|
|
127
|
+
source.reset(() => feed);
|
|
128
|
+
|
|
129
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(0, 3), total: 3, sequence: 1 });
|
|
130
|
+
// Sequence 3 where 2 was due: a frame went missing.
|
|
131
|
+
feed.next({ id: 'w', type: 'patch', upserted: [], removed: [], order: ['r0'], total: 1, sequence: 3 });
|
|
132
|
+
|
|
133
|
+
expect(drifted).toBe(1);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* The mark that lights a row for a moment. The trap it has to avoid is
|
|
138
|
+
* lighting the whole screen: a snapshot is what arriving on the list and what
|
|
139
|
+
* scrolling both get back, and neither means anything arrived.
|
|
140
|
+
*/
|
|
141
|
+
describe('what counts as fresh', () => {
|
|
142
|
+
beforeEach(() => answer(0, 13_000));
|
|
143
|
+
|
|
144
|
+
it('marks nothing on a snapshot - a window that moved is not news', () => {
|
|
145
|
+
expect(source.fresh('r0')).toBe(false);
|
|
146
|
+
expect(source.fresh('r40')).toBe(false);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('marks the rows a patch actually changed, and only those', () => {
|
|
150
|
+
feed.next({
|
|
151
|
+
id: 'w',
|
|
152
|
+
type: 'patch',
|
|
153
|
+
upserted: [{ id: 'new1', updatedAt: 'v1' }],
|
|
154
|
+
removed: ['r99'],
|
|
155
|
+
order: ['new1', ...rows(0, 99).map((row) => row.id)],
|
|
156
|
+
total: 13_001,
|
|
157
|
+
sequence: 2,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
expect(source.fresh('new1')).toBe(true);
|
|
161
|
+
// Shifted down by one, unchanged, not in `upserted` - and not marked.
|
|
162
|
+
expect(source.fresh('r0')).toBe(false);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('says nothing about a row it was never told about', () => {
|
|
166
|
+
expect(source.fresh(undefined)).toBe(false);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* The flight list, whose rows are heavy enough that its window holds fifty.
|
|
172
|
+
*
|
|
173
|
+
* That is where a band of rows loaded by nothing showed up, between one
|
|
174
|
+
* window and the next: the offset was centred on the view and then rounded to
|
|
175
|
+
* a block, with nothing checking that the result still covered the rows being
|
|
176
|
+
* rendered. Rendered [90, 120) rounded to [100, 150) - and since that was
|
|
177
|
+
* already the offset, the ten rows above it stayed placeholders for good.
|
|
178
|
+
*/
|
|
179
|
+
describe('a window barely wider than the viewport', () => {
|
|
180
|
+
const WINDOW = 50;
|
|
181
|
+
|
|
182
|
+
beforeEach(() => {
|
|
183
|
+
sourceOf(WINDOW);
|
|
184
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(0, WINDOW), total: 13_000, sequence: 1 });
|
|
185
|
+
opened.length = 0;
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('always covers every rendered row, whatever the block rounding says', () => {
|
|
189
|
+
for (const start of [30, 55, 90, 91, 137, 4321]) {
|
|
190
|
+
const range = { start, end: start + 30 };
|
|
191
|
+
viewChange.next(range);
|
|
192
|
+
const offset = opened.at(-1)?.offset ?? 0;
|
|
193
|
+
expect(offset).toBeLessThanOrEqual(range.start);
|
|
194
|
+
expect(offset + WINDOW).toBeGreaterThanOrEqual(range.end);
|
|
195
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(offset, WINDOW), total: 13_000, sequence: 1 });
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('leaves no hole in the rows the viewport is showing', () => {
|
|
200
|
+
viewChange.next({ start: 90, end: 120 });
|
|
201
|
+
const offset = opened.at(-1)?.offset ?? 0;
|
|
202
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(offset, WINDOW), total: 13_000, sequence: 1 });
|
|
203
|
+
|
|
204
|
+
for (let index = 90; index < 120; index += 1) {
|
|
205
|
+
expect(source.at(index)).toBeDefined();
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('still stands still once it covers the view, however tight the fit', () => {
|
|
210
|
+
viewChange.next({ start: 90, end: 120 });
|
|
211
|
+
const offset = opened.at(-1)?.offset ?? 0;
|
|
212
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(offset, WINDOW), total: 13_000, sequence: 1 });
|
|
213
|
+
opened.length = 0;
|
|
214
|
+
|
|
215
|
+
viewChange.next({ start: 90, end: 120 });
|
|
216
|
+
viewChange.next({ start: 90, end: 120 });
|
|
217
|
+
|
|
218
|
+
expect(opened).toEqual([]);
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
});
|
package/test/setup.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Zoneless, deliberately: this library is written for applications that do not
|
|
2
|
+
// ship zone.js, and its one contract with change detection - read `revision()`
|
|
3
|
+
// in the template - only means anything there.
|
|
4
|
+
import { setupZonelessTestEnv } from 'jest-preset-angular/setup-env/zoneless';
|
|
5
|
+
|
|
6
|
+
setupZonelessTestEnv();
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ES2022",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": ["ES2022", "DOM"],
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"experimentalDecorators": true,
|
|
9
|
+
"importHelpers": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"noImplicitOverride": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"esModuleInterop": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true
|
|
15
|
+
},
|
|
16
|
+
"angularCompilerOptions": { "strictTemplates": true },
|
|
17
|
+
"include": ["src/**/*.ts"]
|
|
18
|
+
}
|