Skip to content

Commit 046a0fb

Browse files
committed
perf(zero-solid): use reconcile
1 parent b01351f commit 046a0fb

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

packages/zero-solid/src/solid-view.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
createStore,
3-
produce,
3+
reconcile,
44
type SetStoreFunction,
55
type Store,
66
} from 'solid-js/store';
@@ -31,11 +31,10 @@ export class SolidView<V> implements Output {
3131
readonly #format: Format;
3232
readonly #onDestroy: () => void;
3333

34+
#draftState: State;
3435
#state: Store<State>;
3536
#setState: SetStoreFunction<State>;
3637

37-
#pendingChanges: Change[] = [];
38-
3938
constructor(
4039
input: Input,
4140
onTransactionCommit: (cb: () => void) => void,
@@ -47,13 +46,29 @@ export class SolidView<V> implements Output {
4746
onTransactionCommit(this.#onTransactionCommit);
4847
this.#format = format;
4948
this.#onDestroy = onDestroy;
49+
this.#draftState = [
50+
{'': format.singular ? undefined : []},
51+
queryComplete === true ? complete : unknown,
52+
];
5053
[this.#state, this.#setState] = createStore<State>([
5154
{'': format.singular ? undefined : []},
5255
queryComplete === true ? complete : unknown,
5356
]);
5457
input.setOutput(this);
5558

56-
this.#applyChanges(input.fetch({}), node => ({type: 'add', node}));
59+
for (const node of input.fetch({})) {
60+
applyChange(
61+
this.#draftState[0],
62+
{
63+
type: 'add',
64+
node,
65+
},
66+
this.#input.getSchema(),
67+
'',
68+
this.#format,
69+
);
70+
}
71+
this.#setState(reconcile(this.#draftState));
5772

5873
if (queryComplete !== true) {
5974
void queryComplete.then(() => {
@@ -75,32 +90,17 @@ export class SolidView<V> implements Output {
7590
}
7691

7792
#onTransactionCommit = () => {
78-
this.#applyChanges(this.#pendingChanges, c => c);
93+
this.#setState(reconcile(this.#draftState));
7994
};
8095

81-
#applyChanges<T>(changes: Iterable<T>, mapper: (v: T) => Change): void {
82-
try {
83-
this.#setState(
84-
produce((draftState: State) => {
85-
for (const change of changes) {
86-
applyChange(
87-
draftState[0],
88-
mapper(change),
89-
this.#input.getSchema(),
90-
'',
91-
this.#format,
92-
);
93-
}
94-
}),
95-
);
96-
} finally {
97-
this.#pendingChanges = [];
98-
}
99-
}
100-
10196
push(change: Change): void {
102-
// Delay setting the state until the transaction commit.
103-
this.#pendingChanges.push(change);
97+
applyChange(
98+
this.#draftState[0],
99+
change,
100+
this.#input.getSchema(),
101+
'',
102+
this.#format,
103+
);
104104
}
105105
}
106106

packages/zero/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rocicorp/zero",
3-
"version": "0.13.2025013101",
3+
"version": "0.13.2025013103",
44
"scripts": {
55
"build": "rm -rf out && npm run build-server && npm run build-client",
66
"build-client": "tsc -p tsconfig.client.json && tsc-alias -p tsconfig.client.json && npx tsx tool/build.js",

0 commit comments

Comments
 (0)