Skip to content

Commit 769ea23

Browse files
authored
fix: apply push changes within push method execution (#37)
1 parent 43ff7dd commit 769ea23

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

src/view.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ class VueView<V> implements Output {
2121

2222
#state: State
2323

24-
#pendingChanges: Change[] = []
25-
2624
constructor(
2725
input: Input,
2826
onTransactionCommit: (cb: () => void) => void,
@@ -31,7 +29,6 @@ class VueView<V> implements Output {
3129
queryComplete: true | Promise<true>,
3230
) {
3331
this.#input = input
34-
onTransactionCommit(this.#onTransactionCommit)
3532
this.#format = format
3633
this.#onDestroy = onDestroy
3734
this.#state = reactive([
@@ -40,7 +37,9 @@ class VueView<V> implements Output {
4037
])
4138
input.setOutput(this)
4239

43-
this.#applyChanges(input.fetch({}), node => ({ type: 'add', node }))
40+
for (const node of input.fetch({})) {
41+
this.#applyChange({ type: 'add', node })
42+
}
4443

4544
if (queryComplete !== true) {
4645
void queryComplete.then(() => {
@@ -61,30 +60,18 @@ class VueView<V> implements Output {
6160
this.#onDestroy()
6261
}
6362

64-
#onTransactionCommit = () => {
65-
this.#applyChanges(this.#pendingChanges, c => c)
66-
}
67-
68-
#applyChanges<T>(changes: Iterable<T>, mapper: (v: T) => Change): void {
69-
try {
70-
for (const change of changes) {
71-
applyChange(
72-
this.#state[0],
73-
mapper(change),
74-
this.#input.getSchema(),
75-
'',
76-
this.#format,
77-
)
78-
}
79-
}
80-
finally {
81-
this.#pendingChanges = []
82-
}
63+
#applyChange(change: Change): void {
64+
applyChange(
65+
this.#state[0],
66+
change,
67+
this.#input.getSchema(),
68+
'',
69+
this.#format,
70+
)
8371
}
8472

8573
push(change: Change): void {
86-
// Delay setting the state until the transaction commit.
87-
this.#pendingChanges.push(change)
74+
this.#applyChange(change)
8875
}
8976
}
9077

0 commit comments

Comments
 (0)