@softwarity/livewire 0.1.0 → 0.2.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 +11 -7
- package/package.json +3 -3
- package/src/lib/live-window.datasource.ts +21 -10
- package/test/end-to-end.spec.ts +10 -2
- package/test/live-window.datasource.spec.ts +19 -6
- package/test/repaint.spec.ts +183 -0
- package/test/setup.ts +1 -1
package/README.md
CHANGED
|
@@ -22,8 +22,8 @@ export class MessagesService {
|
|
|
22
22
|
readonly source = new LiveWindowDataSource<MessageRow>(
|
|
23
23
|
(total) => this.total.set(total),
|
|
24
24
|
() => this.messages.resync(),
|
|
25
|
+
100,
|
|
25
26
|
);
|
|
26
|
-
readonly revision = this.source.revision;
|
|
27
27
|
|
|
28
28
|
constructor() {
|
|
29
29
|
effect(() => {
|
|
@@ -38,7 +38,7 @@ constructor() {
|
|
|
38
38
|
```
|
|
39
39
|
|
|
40
40
|
```html
|
|
41
|
-
<cdk-virtual-scroll-viewport [
|
|
41
|
+
<cdk-virtual-scroll-viewport [itemSize]="44">
|
|
42
42
|
<table mat-table [dataSource]="source">…</table>
|
|
43
43
|
</cdk-virtual-scroll-viewport>
|
|
44
44
|
|
|
@@ -49,11 +49,15 @@ constructor() {
|
|
|
49
49
|
|
|
50
50
|
Each of these cost a debugging session in the application this came from.
|
|
51
51
|
|
|
52
|
-
**1.
|
|
53
|
-
schedules no change detection at all in a
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
**1. Build the data source in the component that shows the list.** A row
|
|
53
|
+
arriving from a socket callback schedules no change detection at all in a
|
|
54
|
+
zoneless application: the field is right and the screen is wrong. Built in a
|
|
55
|
+
component field, the data source injects that view's `ChangeDetectorRef` and
|
|
56
|
+
calls `markForCheck()` on every publication — which marks the view dirty *and*
|
|
57
|
+
notifies the zoneless scheduler. Built outside an injection context it throws,
|
|
58
|
+
and that is the intended answer: a data source with no view to repaint has
|
|
59
|
+
nobody to answer. (`ApplicationRef.tick()` is the other way and the wrong one:
|
|
60
|
+
it throws when it lands inside a cycle already in progress.)
|
|
57
61
|
|
|
58
62
|
**2. The window size is a constant per screen, never derived from the
|
|
59
63
|
viewport.** Deriving it loops: publish → the viewport re-measures → a new window
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softwarity/livewire",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Live query synchronisation for Angular: subscribe to a query over one WebSocket, get its answer and every answer after it. Virtual-scroll data source included.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"angular",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"rxjs": "^7.5.0"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@softwarity/livewire-protocol": "0.
|
|
41
|
+
"@softwarity/livewire-protocol": "0.2.0",
|
|
42
42
|
"tslib": "^2.6.2"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
@@ -56,6 +56,6 @@
|
|
|
56
56
|
"rxjs": "^7.8.1",
|
|
57
57
|
"typescript": "~5.6.0",
|
|
58
58
|
"zone.js": "^0.15.0",
|
|
59
|
-
"@softwarity/livewire-mock": "0.
|
|
59
|
+
"@softwarity/livewire-mock": "0.2.0"
|
|
60
60
|
}
|
|
61
61
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DataSource } from '@angular/cdk/collections';
|
|
2
|
-
import {
|
|
2
|
+
import { ChangeDetectorRef, inject } from '@angular/core';
|
|
3
3
|
import { BehaviorSubject, Observable, Subscription } from 'rxjs';
|
|
4
4
|
import { LiveList } from './live-list';
|
|
5
5
|
import type { CollectionViewer, ListRange } from '@angular/cdk/collections';
|
|
@@ -63,19 +63,25 @@ const FRESH_MS = 1000;
|
|
|
63
63
|
export class LiveWindowDataSource<Row extends LiveRow> extends DataSource<Row | undefined> {
|
|
64
64
|
private readonly published = new BehaviorSubject<(Row | undefined)[]>([]);
|
|
65
65
|
private readonly viewing = new Subscription();
|
|
66
|
-
private readonly stamp = signal(0);
|
|
67
66
|
|
|
68
67
|
/**
|
|
69
|
-
*
|
|
68
|
+
* The view to repaint, taken from wherever this was built.
|
|
70
69
|
*
|
|
71
70
|
* Zoneless applications schedule nothing when a row arrives from a socket
|
|
72
71
|
* callback: the table is told its data changed but no pass is ever run, and
|
|
73
|
-
* the screen holds stale rows until something unrelated wakes Angular up.
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
72
|
+
* the screen holds stale rows until something unrelated wakes Angular up.
|
|
73
|
+
* `markForCheck()` marks the view dirty *and* notifies the zoneless
|
|
74
|
+
* scheduler, which is exactly the missing half.
|
|
75
|
+
*
|
|
76
|
+
* Injected rather than asked for, because there is nothing to decide: build
|
|
77
|
+
* this in a component field, as one does, and the injection context is right
|
|
78
|
+
* there. Built outside one it throws, and that is the point - a data source
|
|
79
|
+
* with no view to repaint has nobody to answer.
|
|
80
|
+
*
|
|
81
|
+
* (`ApplicationRef.tick()` is the other way and the wrong one: it throws when
|
|
82
|
+
* it lands inside a cycle already in progress.)
|
|
77
83
|
*/
|
|
78
|
-
readonly
|
|
84
|
+
private readonly repaint = inject(ChangeDetectorRef);
|
|
79
85
|
|
|
80
86
|
private list = new LiveList<Row>();
|
|
81
87
|
private watching: Subscription | null = null;
|
|
@@ -180,6 +186,11 @@ export class LiveWindowDataSource<Row extends LiveRow> extends DataSource<Row |
|
|
|
180
186
|
this.list = new LiveList<Row>();
|
|
181
187
|
this.recent.clear();
|
|
182
188
|
this.published.next([]);
|
|
189
|
+
// What is on the screen has just become the answer to a question nobody is
|
|
190
|
+
// asking any more, and the snapshot lands a moment later. Emptying it
|
|
191
|
+
// without saying so leaves the previous answer on display in between.
|
|
192
|
+
//
|
|
193
|
+
this.repaint.markForCheck();
|
|
183
194
|
this.follow();
|
|
184
195
|
}
|
|
185
196
|
|
|
@@ -300,7 +311,7 @@ export class LiveWindowDataSource<Row extends LiveRow> extends DataSource<Row |
|
|
|
300
311
|
for (const id of ids) {
|
|
301
312
|
this.recent.delete(id);
|
|
302
313
|
}
|
|
303
|
-
this.
|
|
314
|
+
this.repaint.markForCheck();
|
|
304
315
|
}, FRESH_MS);
|
|
305
316
|
}
|
|
306
317
|
|
|
@@ -317,6 +328,6 @@ export class LiveWindowDataSource<Row extends LiveRow> extends DataSource<Row |
|
|
|
317
328
|
rows[this.offset + index] = held[index];
|
|
318
329
|
}
|
|
319
330
|
this.published.next(rows);
|
|
320
|
-
this.
|
|
331
|
+
this.repaint.markForCheck();
|
|
321
332
|
}
|
|
322
333
|
}
|
package/test/end-to-end.spec.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ChangeDetectorRef } from '@angular/core';
|
|
1
2
|
import { TestBed } from '@angular/core/testing';
|
|
2
3
|
import { MockServer } from '@softwarity/livewire-mock';
|
|
3
4
|
import { LiveList } from '../src/lib/live-list';
|
|
@@ -22,7 +23,12 @@ describe('the client, end to end', () => {
|
|
|
22
23
|
function clientOf(): LivewireClient {
|
|
23
24
|
TestBed.resetTestingModule();
|
|
24
25
|
TestBed.configureTestingModule({
|
|
25
|
-
providers: [
|
|
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
|
+
],
|
|
26
32
|
});
|
|
27
33
|
return TestBed.inject(LivewireClient);
|
|
28
34
|
}
|
|
@@ -128,7 +134,9 @@ describe('the client, end to end', () => {
|
|
|
128
134
|
it('feeds a virtual-scroll source from end to end', async () => {
|
|
129
135
|
const client = clientOf();
|
|
130
136
|
const topic = new LiveTopic<LiveRow>(client, 'rows');
|
|
131
|
-
|
|
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>());
|
|
132
140
|
source.reset((offset, limit) => topic.window({}, offset, limit));
|
|
133
141
|
await settle();
|
|
134
142
|
|
|
@@ -1,8 +1,25 @@
|
|
|
1
|
+
import { ChangeDetectorRef } from '@angular/core';
|
|
2
|
+
import { TestBed } from '@angular/core/testing';
|
|
1
3
|
import { Subject } from 'rxjs';
|
|
2
4
|
import { LiveWindowDataSource } from '../src/lib/live-window.datasource';
|
|
3
5
|
import type { CollectionViewer } from '@angular/cdk/collections';
|
|
4
6
|
import type { LiveRow, UpdateFrame } from '@softwarity/livewire-protocol';
|
|
5
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
|
+
|
|
6
23
|
function rows(from: number, count: number): LiveRow[] {
|
|
7
24
|
return Array.from({ length: count }, (_, index) => ({ id: `r${from + index}`, updatedAt: 'v1' }));
|
|
8
25
|
}
|
|
@@ -16,11 +33,7 @@ describe('LiveWindowDataSource', () => {
|
|
|
16
33
|
function sourceOf(window?: number): void {
|
|
17
34
|
opened = [];
|
|
18
35
|
feed = new Subject<UpdateFrame<LiveRow>>();
|
|
19
|
-
source =
|
|
20
|
-
() => undefined,
|
|
21
|
-
() => undefined,
|
|
22
|
-
window,
|
|
23
|
-
);
|
|
36
|
+
source = build<LiveRow>(() => undefined, () => undefined, window);
|
|
24
37
|
viewChange = new Subject<{ start: number; end: number }>();
|
|
25
38
|
source.connect({ viewChange } as unknown as CollectionViewer);
|
|
26
39
|
source.reset((offset, limit) => {
|
|
@@ -120,7 +133,7 @@ describe('LiveWindowDataSource', () => {
|
|
|
120
133
|
let drifted = 0;
|
|
121
134
|
opened = [];
|
|
122
135
|
feed = new Subject<UpdateFrame<LiveRow>>();
|
|
123
|
-
source =
|
|
136
|
+
source = build<LiveRow>(
|
|
124
137
|
() => undefined,
|
|
125
138
|
() => (drifted += 1),
|
|
126
139
|
);
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { ChangeDetectorRef, Component } 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 { LiveRow, UpdateFrame } from '@softwarity/livewire-protocol';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* What repaints a zoneless screen when a frame lands in a socket callback.
|
|
9
|
+
*
|
|
10
|
+
* The data source injects the view's `ChangeDetectorRef` and calls
|
|
11
|
+
* `markForCheck()`, which marks the view dirty *and* notifies the zoneless
|
|
12
|
+
* scheduler. Nothing is asked of the screen.
|
|
13
|
+
*
|
|
14
|
+
* The component below reads **no signal at all** and uses no async pipe - it
|
|
15
|
+
* renders a plain field, filled from a subscription. That is deliberate: it
|
|
16
|
+
* strips out every other thing that could schedule a pass, so what is measured
|
|
17
|
+
* is the notification itself and nothing else.
|
|
18
|
+
*/
|
|
19
|
+
describe('repainting a zoneless screen', () => {
|
|
20
|
+
const feed = new Subject<UpdateFrame<LiveRow>>();
|
|
21
|
+
|
|
22
|
+
function frame(...ids: string[]): UpdateFrame<LiveRow> {
|
|
23
|
+
return {
|
|
24
|
+
id: 'w',
|
|
25
|
+
type: 'snapshot',
|
|
26
|
+
rows: ids.map((id) => ({ id, updatedAt: 'v1' })),
|
|
27
|
+
total: ids.length,
|
|
28
|
+
sequence: 1,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@Component({
|
|
33
|
+
selector: 'test-screen',
|
|
34
|
+
standalone: true,
|
|
35
|
+
template: `<span class="ids">{{ ids }}</span>`,
|
|
36
|
+
})
|
|
37
|
+
class ScreenComponent {
|
|
38
|
+
ids = '';
|
|
39
|
+
|
|
40
|
+
// A component field, which is an injection context - the data source takes
|
|
41
|
+
// this view's ChangeDetectorRef from it.
|
|
42
|
+
readonly source = new LiveWindowDataSource<LiveRow>();
|
|
43
|
+
|
|
44
|
+
constructor() {
|
|
45
|
+
this.source.changes.subscribe((rows) => (this.ids = rows.map((row) => row?.id ?? '-').join(',')));
|
|
46
|
+
this.source.reset(() => feed);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
it('repaints on a frame arriving from outside a change-detection cycle', async () => {
|
|
51
|
+
const fixture = TestBed.createComponent(ScreenComponent);
|
|
52
|
+
await fixture.whenStable();
|
|
53
|
+
|
|
54
|
+
// Straight into the data source, as a socket callback would.
|
|
55
|
+
feed.next(frame('a', 'b'));
|
|
56
|
+
await fixture.whenStable();
|
|
57
|
+
|
|
58
|
+
expect(fixture.nativeElement.querySelector('.ids').textContent).toBe('a,b');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('repaints again on every later frame, not only the first', async () => {
|
|
62
|
+
const fixture = TestBed.createComponent(ScreenComponent);
|
|
63
|
+
await fixture.whenStable();
|
|
64
|
+
|
|
65
|
+
for (const ids of [['a'], ['a', 'b'], ['a', 'b', 'c']]) {
|
|
66
|
+
feed.next(frame(...ids));
|
|
67
|
+
await fixture.whenStable();
|
|
68
|
+
expect(fixture.nativeElement.querySelector('.ids').textContent).toBe(ids.join(','));
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The rule made structural: there is no way to build one that cannot repaint.
|
|
74
|
+
* Left optional, the screens that forgot it would look right in development
|
|
75
|
+
* and hold stale rows in production.
|
|
76
|
+
*/
|
|
77
|
+
it('refuses to be built where there is no view to repaint', () => {
|
|
78
|
+
expect(() => new LiveWindowDataSource<LiveRow>()).toThrow();
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The other half of "it repaints": that it does so only when there is something
|
|
84
|
+
* to repaint.
|
|
85
|
+
*
|
|
86
|
+
* A pass asked for on every read rather than on every change is how a live
|
|
87
|
+
* screen becomes one nobody can use - and it would be invisible, since the
|
|
88
|
+
* screen would look right the whole time.
|
|
89
|
+
*/
|
|
90
|
+
describe('what asks for a repaint, and what does not', () => {
|
|
91
|
+
let asked: number;
|
|
92
|
+
let feed: Subject<UpdateFrame<LiveRow>>;
|
|
93
|
+
let source: LiveWindowDataSource<LiveRow>;
|
|
94
|
+
|
|
95
|
+
function rows(count: number): LiveRow[] {
|
|
96
|
+
return Array.from({ length: count }, (_, index) => ({ id: `r${index}`, updatedAt: 'v1' }));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
beforeEach(() => {
|
|
100
|
+
jest.useFakeTimers();
|
|
101
|
+
asked = 0;
|
|
102
|
+
// `ChangeDetectorRef` is a token like any other: provided here, the data
|
|
103
|
+
// source injects this instead of a view's, and the calls can be counted.
|
|
104
|
+
// Only `markForCheck` is ever called.
|
|
105
|
+
TestBed.configureTestingModule({
|
|
106
|
+
providers: [{ provide: ChangeDetectorRef, useValue: { markForCheck: () => (asked += 1) } }],
|
|
107
|
+
});
|
|
108
|
+
feed = new Subject<UpdateFrame<LiveRow>>();
|
|
109
|
+
source = TestBed.runInInjectionContext(() => new LiveWindowDataSource<LiveRow>());
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
afterEach(() => jest.useRealTimers());
|
|
113
|
+
|
|
114
|
+
it('asks once when the list is emptied for a new question', () => {
|
|
115
|
+
source.reset(() => feed);
|
|
116
|
+
|
|
117
|
+
expect(asked).toBe(1);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('asks once per frame it could apply, and no more', () => {
|
|
121
|
+
source.reset(() => feed);
|
|
122
|
+
asked = 0;
|
|
123
|
+
|
|
124
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(3), total: 3, sequence: 1 });
|
|
125
|
+
expect(asked).toBe(1);
|
|
126
|
+
|
|
127
|
+
feed.next({
|
|
128
|
+
id: 'w',
|
|
129
|
+
type: 'patch',
|
|
130
|
+
upserted: [{ id: 'new', updatedAt: 'v1' }],
|
|
131
|
+
removed: [],
|
|
132
|
+
order: ['new', 'r0', 'r1', 'r2'],
|
|
133
|
+
total: 4,
|
|
134
|
+
sequence: 2,
|
|
135
|
+
});
|
|
136
|
+
expect(asked).toBe(2);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
/** A frame it cannot place changes nothing on the screen. It resyncs instead. */
|
|
140
|
+
it('asks for nothing on a frame it had to reject', () => {
|
|
141
|
+
source.reset(() => feed);
|
|
142
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(3), total: 3, sequence: 1 });
|
|
143
|
+
asked = 0;
|
|
144
|
+
|
|
145
|
+
// Sequence 3 where 2 was due.
|
|
146
|
+
feed.next({ id: 'w', type: 'patch', upserted: [], removed: [], order: ['r0'], total: 1, sequence: 3 });
|
|
147
|
+
|
|
148
|
+
expect(asked).toBe(0);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
/** Moving the window is a question, not an answer. The snapshot repaints. */
|
|
152
|
+
it('asks for nothing when the window moves, only when its answer lands', () => {
|
|
153
|
+
source.reset(() => feed);
|
|
154
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(100), total: 13_000, sequence: 1 });
|
|
155
|
+
asked = 0;
|
|
156
|
+
|
|
157
|
+
source.ensure(9000);
|
|
158
|
+
|
|
159
|
+
expect(asked).toBe(0);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
/** The marks have to come off, and taking them off is a change like any other. */
|
|
163
|
+
it('asks once more when the fresh marks expire, and then stops', () => {
|
|
164
|
+
source.reset(() => feed);
|
|
165
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(3), total: 3, sequence: 1 });
|
|
166
|
+
feed.next({
|
|
167
|
+
id: 'w',
|
|
168
|
+
type: 'patch',
|
|
169
|
+
upserted: [{ id: 'new', updatedAt: 'v1' }],
|
|
170
|
+
removed: [],
|
|
171
|
+
order: ['new', 'r0', 'r1', 'r2'],
|
|
172
|
+
total: 4,
|
|
173
|
+
sequence: 2,
|
|
174
|
+
});
|
|
175
|
+
asked = 0;
|
|
176
|
+
|
|
177
|
+
jest.advanceTimersByTime(1000);
|
|
178
|
+
expect(asked).toBe(1);
|
|
179
|
+
|
|
180
|
+
jest.advanceTimersByTime(10_000);
|
|
181
|
+
expect(asked).toBe(1);
|
|
182
|
+
});
|
|
183
|
+
});
|
package/test/setup.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Zoneless, deliberately: this library is written for applications that do not
|
|
2
|
-
// ship zone.js, and its one contract with change detection -
|
|
2
|
+
// ship zone.js, and its one contract with change detection - `markForCheck()`
|
|
3
3
|
// in the template - only means anything there.
|
|
4
4
|
import { setupZonelessTestEnv } from 'jest-preset-angular/setup-env/zoneless';
|
|
5
5
|
|