@softwarity/livewire 0.1.0 → 0.1.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 +16 -7
- package/package.json +3 -3
- package/src/lib/live-window.datasource.ts +47 -5
- package/test/repaint.spec.ts +226 -0
package/README.md
CHANGED
|
@@ -22,8 +22,9 @@ export class MessagesService {
|
|
|
22
22
|
readonly source = new LiveWindowDataSource<MessageRow>(
|
|
23
23
|
(total) => this.total.set(total),
|
|
24
24
|
() => this.messages.resync(),
|
|
25
|
+
100,
|
|
26
|
+
inject(ChangeDetectorRef), // what repaints a zoneless screen - see rule 1
|
|
25
27
|
);
|
|
26
|
-
readonly revision = this.source.revision;
|
|
27
28
|
|
|
28
29
|
constructor() {
|
|
29
30
|
effect(() => {
|
|
@@ -38,7 +39,7 @@ constructor() {
|
|
|
38
39
|
```
|
|
39
40
|
|
|
40
41
|
```html
|
|
41
|
-
<cdk-virtual-scroll-viewport [
|
|
42
|
+
<cdk-virtual-scroll-viewport [itemSize]="44">
|
|
42
43
|
<table mat-table [dataSource]="source">…</table>
|
|
43
44
|
</cdk-virtual-scroll-viewport>
|
|
44
45
|
|
|
@@ -49,11 +50,19 @@ constructor() {
|
|
|
49
50
|
|
|
50
51
|
Each of these cost a debugging session in the application this came from.
|
|
51
52
|
|
|
52
|
-
**1.
|
|
53
|
-
schedules no change detection at all in a zoneless application
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
**1. Say that something arrived.** A row arriving from a socket callback
|
|
54
|
+
schedules no change detection at all in a zoneless application: the field is
|
|
55
|
+
right and the screen is wrong. Two ways to say it, and either is enough.
|
|
56
|
+
|
|
57
|
+
- **Hand the data source a `ChangeDetectorRef`** (above). It calls
|
|
58
|
+
`markForCheck()` on every publication, which marks the view dirty *and*
|
|
59
|
+
notifies the zoneless scheduler. The template then says only what it shows.
|
|
60
|
+
- **Or read `revision()` in the template** — `[attr.data-revision]="source.revision()"`
|
|
61
|
+
on the viewport is enough. Use this when the source is built outside an
|
|
62
|
+
injection context, where there is no `ChangeDetectorRef` to inject.
|
|
63
|
+
|
|
64
|
+
`ApplicationRef.tick()` is the third way and the wrong one: it throws when it
|
|
65
|
+
lands inside a cycle already in progress.
|
|
57
66
|
|
|
58
67
|
**2. The window size is a constant per screen, never derived from the
|
|
59
68
|
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.1.
|
|
3
|
+
"version": "0.1.1",
|
|
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.1.
|
|
41
|
+
"@softwarity/livewire-protocol": "0.1.1",
|
|
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.1.
|
|
59
|
+
"@softwarity/livewire-mock": "0.1.1"
|
|
60
60
|
}
|
|
61
61
|
}
|
|
@@ -2,6 +2,7 @@ import { DataSource } from '@angular/cdk/collections';
|
|
|
2
2
|
import { signal } from '@angular/core';
|
|
3
3
|
import { BehaviorSubject, Observable, Subscription } from 'rxjs';
|
|
4
4
|
import { LiveList } from './live-list';
|
|
5
|
+
import type { ChangeDetectorRef } from '@angular/core';
|
|
5
6
|
import type { CollectionViewer, ListRange } from '@angular/cdk/collections';
|
|
6
7
|
import type { LiveRow, UpdateFrame } from '@softwarity/livewire-protocol';
|
|
7
8
|
|
|
@@ -70,10 +71,13 @@ export class LiveWindowDataSource<Row extends LiveRow> extends DataSource<Row |
|
|
|
70
71
|
*
|
|
71
72
|
* Zoneless applications schedule nothing when a row arrives from a socket
|
|
72
73
|
* 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
|
-
*
|
|
74
|
+
* the screen holds stale rows until something unrelated wakes Angular up.
|
|
75
|
+
*
|
|
76
|
+
* Two ways to say it properly, and this is the one that needs nothing from
|
|
77
|
+
* the caller: a signal the view reads. Hand a `ChangeDetectorRef` to the
|
|
78
|
+
* constructor instead and the repaint is asked for here, leaving the template
|
|
79
|
+
* to say only what it shows. (`ApplicationRef.tick()` is the third way and the
|
|
80
|
+
* wrong one - it throws when it lands inside a cycle already in progress.)
|
|
77
81
|
*/
|
|
78
82
|
readonly revision = this.stamp.asReadonly();
|
|
79
83
|
|
|
@@ -95,6 +99,24 @@ export class LiveWindowDataSource<Row extends LiveRow> extends DataSource<Row |
|
|
|
95
99
|
private readonly onDrift: () => void = () => undefined,
|
|
96
100
|
/** Rows per window - see `DEFAULT_WINDOW` for how to choose one. */
|
|
97
101
|
private readonly window: number = DEFAULT_WINDOW,
|
|
102
|
+
/**
|
|
103
|
+
* The view to repaint when something arrives. Optional, and the difference
|
|
104
|
+
* it makes is on the screen's side, not here.
|
|
105
|
+
*
|
|
106
|
+
* `markForCheck()` marks the view dirty *and* notifies the zoneless
|
|
107
|
+
* scheduler, so a frame landing in a socket callback gets a pass run for it.
|
|
108
|
+
* Given one, a template needs no `revision()`; given none, it does.
|
|
109
|
+
*
|
|
110
|
+
* ```ts
|
|
111
|
+
* readonly source = new LiveWindowDataSource<Row>(
|
|
112
|
+
* (total) => this.total.set(total),
|
|
113
|
+
* () => this.topic.resync(),
|
|
114
|
+
* 100,
|
|
115
|
+
* inject(ChangeDetectorRef),
|
|
116
|
+
* );
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
private readonly repaint?: ChangeDetectorRef,
|
|
98
120
|
) {
|
|
99
121
|
super();
|
|
100
122
|
}
|
|
@@ -180,6 +202,14 @@ export class LiveWindowDataSource<Row extends LiveRow> extends DataSource<Row |
|
|
|
180
202
|
this.list = new LiveList<Row>();
|
|
181
203
|
this.recent.clear();
|
|
182
204
|
this.published.next([]);
|
|
205
|
+
// What is on the screen has just become the answer to a question nobody is
|
|
206
|
+
// asking any more, and the snapshot lands a moment later. Emptying it
|
|
207
|
+
// without saying so leaves the previous answer on display in between.
|
|
208
|
+
//
|
|
209
|
+
// `markForCheck` and not `touched`: a screen calls this from an `effect`,
|
|
210
|
+
// and Angular 18 - which this package still supports - refuses a signal
|
|
211
|
+
// write from inside one.
|
|
212
|
+
this.repaint?.markForCheck();
|
|
183
213
|
this.follow();
|
|
184
214
|
}
|
|
185
215
|
|
|
@@ -300,7 +330,7 @@ export class LiveWindowDataSource<Row extends LiveRow> extends DataSource<Row |
|
|
|
300
330
|
for (const id of ids) {
|
|
301
331
|
this.recent.delete(id);
|
|
302
332
|
}
|
|
303
|
-
this.
|
|
333
|
+
this.touched();
|
|
304
334
|
}, FRESH_MS);
|
|
305
335
|
}
|
|
306
336
|
|
|
@@ -317,6 +347,18 @@ export class LiveWindowDataSource<Row extends LiveRow> extends DataSource<Row |
|
|
|
317
347
|
rows[this.offset + index] = held[index];
|
|
318
348
|
}
|
|
319
349
|
this.published.next(rows);
|
|
350
|
+
this.touched();
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Says that the screen has something new to show.
|
|
355
|
+
*
|
|
356
|
+
* Both ways at once, and they do not overlap: the signal serves a template
|
|
357
|
+
* that reads `revision()`, and `markForCheck()` serves one that does not.
|
|
358
|
+
* Whichever the screen uses, the other costs a counter.
|
|
359
|
+
*/
|
|
360
|
+
private touched(): void {
|
|
320
361
|
this.stamp.update((count) => count + 1);
|
|
362
|
+
this.repaint?.markForCheck();
|
|
321
363
|
}
|
|
322
364
|
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { ChangeDetectorRef, Component, inject } 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
|
+
* Two ways, and the point of this file is that the second one works: a signal
|
|
11
|
+
* the template reads, or a `ChangeDetectorRef` handed to the data source.
|
|
12
|
+
*
|
|
13
|
+
* The templates here read **no signal at all** and use no async pipe - they
|
|
14
|
+
* render a plain field, filled from a subscription. That is deliberate: it
|
|
15
|
+
* strips out every other thing that could schedule a pass, so what the test
|
|
16
|
+
* measures is the notification itself and nothing else.
|
|
17
|
+
*/
|
|
18
|
+
describe('repainting a zoneless screen', () => {
|
|
19
|
+
const feed = new Subject<UpdateFrame<LiveRow>>();
|
|
20
|
+
|
|
21
|
+
function frame(...ids: string[]): UpdateFrame<LiveRow> {
|
|
22
|
+
return {
|
|
23
|
+
id: 'w',
|
|
24
|
+
type: 'snapshot',
|
|
25
|
+
rows: ids.map((id) => ({ id, updatedAt: 'v1' })),
|
|
26
|
+
total: ids.length,
|
|
27
|
+
sequence: 1,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
@Component({
|
|
32
|
+
selector: 'test-told',
|
|
33
|
+
standalone: true,
|
|
34
|
+
template: `<span class="ids">{{ ids }}</span>`,
|
|
35
|
+
})
|
|
36
|
+
class ToldComponent {
|
|
37
|
+
ids = '';
|
|
38
|
+
|
|
39
|
+
readonly source = new LiveWindowDataSource<LiveRow>(
|
|
40
|
+
() => undefined,
|
|
41
|
+
() => undefined,
|
|
42
|
+
100,
|
|
43
|
+
inject(ChangeDetectorRef),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
constructor() {
|
|
47
|
+
this.source.changes.subscribe((rows) => (this.ids = rows.map((row) => row?.id ?? '-').join(',')));
|
|
48
|
+
this.source.reset(() => feed);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
@Component({
|
|
53
|
+
selector: 'test-silent',
|
|
54
|
+
standalone: true,
|
|
55
|
+
template: `<span class="ids">{{ ids }}</span>`,
|
|
56
|
+
})
|
|
57
|
+
class SilentComponent {
|
|
58
|
+
ids = '';
|
|
59
|
+
|
|
60
|
+
readonly source = new LiveWindowDataSource<LiveRow>();
|
|
61
|
+
|
|
62
|
+
constructor() {
|
|
63
|
+
this.source.changes.subscribe((rows) => (this.ids = rows.map((row) => row?.id ?? '-').join(',')));
|
|
64
|
+
this.source.reset(() => feed);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
it('repaints on its own when it was given a ChangeDetectorRef', async () => {
|
|
69
|
+
const fixture = TestBed.createComponent(ToldComponent);
|
|
70
|
+
await fixture.whenStable();
|
|
71
|
+
|
|
72
|
+
// Straight into the data source, as a socket callback would - nothing here
|
|
73
|
+
// runs inside a change-detection cycle.
|
|
74
|
+
feed.next(frame('a', 'b'));
|
|
75
|
+
await fixture.whenStable();
|
|
76
|
+
|
|
77
|
+
expect(fixture.nativeElement.querySelector('.ids').textContent).toBe('a,b');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The other half of the claim, and the reason the rule exists: without either
|
|
82
|
+
* notification the field is right and the screen is wrong. A test that only
|
|
83
|
+
* checked the first case would pass just as well if something else in the
|
|
84
|
+
* fixture were scheduling the pass.
|
|
85
|
+
*/
|
|
86
|
+
it('does not, when it was given neither a ChangeDetectorRef nor a revision() read', async () => {
|
|
87
|
+
const fixture = TestBed.createComponent(SilentComponent);
|
|
88
|
+
await fixture.whenStable();
|
|
89
|
+
|
|
90
|
+
feed.next(frame('a', 'b'));
|
|
91
|
+
await fixture.whenStable();
|
|
92
|
+
|
|
93
|
+
expect(fixture.componentInstance.ids).toBe('a,b');
|
|
94
|
+
expect(fixture.nativeElement.querySelector('.ids').textContent).toBe('');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('repaints again on every later frame, not only the first', async () => {
|
|
98
|
+
const fixture = TestBed.createComponent(ToldComponent);
|
|
99
|
+
await fixture.whenStable();
|
|
100
|
+
|
|
101
|
+
for (const ids of [['a'], ['a', 'b'], ['a', 'b', 'c']]) {
|
|
102
|
+
feed.next(frame(...ids));
|
|
103
|
+
await fixture.whenStable();
|
|
104
|
+
expect(fixture.nativeElement.querySelector('.ids').textContent).toBe(ids.join(','));
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The other half of "it repaints": that it does so only when there is something
|
|
111
|
+
* to repaint.
|
|
112
|
+
*
|
|
113
|
+
* A pass asked for on every read rather than on every change is how a live
|
|
114
|
+
* screen becomes one nobody can use - and it would be invisible, since the
|
|
115
|
+
* screen would look right the whole time.
|
|
116
|
+
*/
|
|
117
|
+
describe('what asks for a repaint, and what does not', () => {
|
|
118
|
+
let asked: number;
|
|
119
|
+
let repaint: ChangeDetectorRef;
|
|
120
|
+
let feed: Subject<UpdateFrame<LiveRow>>;
|
|
121
|
+
let source: LiveWindowDataSource<LiveRow>;
|
|
122
|
+
|
|
123
|
+
function rows(count: number): LiveRow[] {
|
|
124
|
+
return Array.from({ length: count }, (_, index) => ({ id: `r${index}`, updatedAt: 'v1' }));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
beforeEach(() => {
|
|
128
|
+
jest.useFakeTimers();
|
|
129
|
+
asked = 0;
|
|
130
|
+
// Only `markForCheck` is ever called; the rest of the interface is not
|
|
131
|
+
// worth stubbing to count one thing.
|
|
132
|
+
repaint = { markForCheck: () => (asked += 1) } as unknown as ChangeDetectorRef;
|
|
133
|
+
feed = new Subject<UpdateFrame<LiveRow>>();
|
|
134
|
+
source = new LiveWindowDataSource<LiveRow>(
|
|
135
|
+
() => undefined,
|
|
136
|
+
() => undefined,
|
|
137
|
+
100,
|
|
138
|
+
repaint,
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
afterEach(() => jest.useRealTimers());
|
|
143
|
+
|
|
144
|
+
it('asks once when the list is emptied for a new question', () => {
|
|
145
|
+
source.reset(() => feed);
|
|
146
|
+
|
|
147
|
+
expect(asked).toBe(1);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* A screen calls `reset` from an `effect`, and Angular 18 refuses a signal
|
|
152
|
+
* write from inside one. So emptying the list asks for a pass without
|
|
153
|
+
* touching `revision` - which stays where the frames bump it.
|
|
154
|
+
*/
|
|
155
|
+
it('empties the list without writing a signal', () => {
|
|
156
|
+
const before = source.revision();
|
|
157
|
+
|
|
158
|
+
source.reset(() => feed);
|
|
159
|
+
|
|
160
|
+
expect(source.revision()).toBe(before);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('asks once per frame it could apply, and no more', () => {
|
|
164
|
+
source.reset(() => feed);
|
|
165
|
+
asked = 0;
|
|
166
|
+
|
|
167
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(3), total: 3, sequence: 1 });
|
|
168
|
+
expect(asked).toBe(1);
|
|
169
|
+
|
|
170
|
+
feed.next({
|
|
171
|
+
id: 'w',
|
|
172
|
+
type: 'patch',
|
|
173
|
+
upserted: [{ id: 'new', updatedAt: 'v1' }],
|
|
174
|
+
removed: [],
|
|
175
|
+
order: ['new', 'r0', 'r1', 'r2'],
|
|
176
|
+
total: 4,
|
|
177
|
+
sequence: 2,
|
|
178
|
+
});
|
|
179
|
+
expect(asked).toBe(2);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
/** A frame it cannot place changes nothing on the screen. It resyncs instead. */
|
|
183
|
+
it('asks for nothing on a frame it had to reject', () => {
|
|
184
|
+
source.reset(() => feed);
|
|
185
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(3), total: 3, sequence: 1 });
|
|
186
|
+
asked = 0;
|
|
187
|
+
|
|
188
|
+
// Sequence 3 where 2 was due.
|
|
189
|
+
feed.next({ id: 'w', type: 'patch', upserted: [], removed: [], order: ['r0'], total: 1, sequence: 3 });
|
|
190
|
+
|
|
191
|
+
expect(asked).toBe(0);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
/** Moving the window is a question, not an answer. The snapshot repaints. */
|
|
195
|
+
it('asks for nothing when the window moves, only when its answer lands', () => {
|
|
196
|
+
source.reset(() => feed);
|
|
197
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(100), total: 13_000, sequence: 1 });
|
|
198
|
+
asked = 0;
|
|
199
|
+
|
|
200
|
+
source.ensure(9000);
|
|
201
|
+
|
|
202
|
+
expect(asked).toBe(0);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
/** The marks have to come off, and taking them off is a change like any other. */
|
|
206
|
+
it('asks once more when the fresh marks expire, and then stops', () => {
|
|
207
|
+
source.reset(() => feed);
|
|
208
|
+
feed.next({ id: 'w', type: 'snapshot', rows: rows(3), total: 3, sequence: 1 });
|
|
209
|
+
feed.next({
|
|
210
|
+
id: 'w',
|
|
211
|
+
type: 'patch',
|
|
212
|
+
upserted: [{ id: 'new', updatedAt: 'v1' }],
|
|
213
|
+
removed: [],
|
|
214
|
+
order: ['new', 'r0', 'r1', 'r2'],
|
|
215
|
+
total: 4,
|
|
216
|
+
sequence: 2,
|
|
217
|
+
});
|
|
218
|
+
asked = 0;
|
|
219
|
+
|
|
220
|
+
jest.advanceTimersByTime(1000);
|
|
221
|
+
expect(asked).toBe(1);
|
|
222
|
+
|
|
223
|
+
jest.advanceTimersByTime(10_000);
|
|
224
|
+
expect(asked).toBe(1);
|
|
225
|
+
});
|
|
226
|
+
});
|