Skip to content

Commit 0431dd4

Browse files
committed
fix: remove dirty tracking redundancy
1 parent 30bfb64 commit 0431dd4

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

src/array.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ test('reactivity (proxy)', () => {
131131
const proxy = new Proxy();
132132
expect(dirties).toEqual(0);
133133
proxy.setAt(0, {x: 3});
134-
expect(dirties).toEqual(2);
134+
expect(dirties).toEqual(1);
135135
proxy.setAt(1, {x: 4});
136-
expect(dirties).toEqual(4);
136+
expect(dirties).toEqual(2);
137137
proxy[0].x = 5;
138-
expect(dirties).toEqual(5);
138+
expect(dirties).toEqual(3);
139139
proxy.setAt(1, proxy[0]);
140-
expect(dirties).toEqual(6);
140+
expect(dirties).toEqual(4);
141141
});
142142

143143
test('disabled diff (proxy)', () => {

src/object.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ registry.object = class extends ProxyProperty {
201201
}`;
202202
}).join('\n')
203203
}
204-
this[SetWithDefaults](property.defaultValue);
205204
}
206205
[Set](value) {
207206
if (!value) return;
@@ -215,16 +214,39 @@ registry.object = class extends ProxyProperty {
215214
if (value) {
216215
${
217216
Object.keys(properties).map((key) => `{
218-
this['${key}'] = '${key}' in value
219-
? value['${key}']
220-
: properties['${key}'].defaultValue;
217+
let localValue;
218+
if ('${key}' in value) {
219+
localValue = value['${key}'];
220+
}
221+
else if (property.defaultValue && '${key}' in property.defaultValue) {
222+
localValue = property.defaultValue['${key}'];
223+
}
224+
else {
225+
localValue = properties['${key}'].defaultValue;
226+
}
227+
${
228+
properties[key] instanceof ProxyProperty
229+
? `this['${key}'][SetWithDefaults](localValue);`
230+
: `this['${key}'] = localValue;`
231+
}
221232
}`).join('\n')
222233
}
223234
}
224235
else {
225236
${
226237
Object.keys(properties).map((key) => `{
227-
this['${key}'] = properties['${key}'].defaultValue;
238+
let localValue;
239+
if (property.defaultValue && '${key}' in property.defaultValue) {
240+
localValue = property.defaultValue['${key}'];
241+
}
242+
else {
243+
localValue = properties['${key}'].defaultValue;
244+
}
245+
${
246+
properties[key] instanceof ProxyProperty
247+
? `this['${key}'][SetWithDefaults](localValue);`
248+
: `this['${key}'] = localValue;`
249+
}
228250
}`).join('\n')
229251
}
230252
}
@@ -276,20 +298,6 @@ registry.object = class extends ProxyProperty {
276298
return (blueprint.Proxy ?? ((C) => C))(
277299
codegen(`
278300
return class MappedProxy extends Proxy {
279-
${
280-
views.dirty
281-
? `
282-
constructor(dataIndex, dirtyIndex) {
283-
super(dataIndex, dirtyIndex);
284-
let bit = this[DirtyOffset];
285-
for (let i = 0; i < property.dirtyWidth; ++i) {
286-
views.dirty[bit >> 3] |= 1 << (bit & 7);
287-
bit += 1;
288-
}
289-
}
290-
`
291-
: ''
292-
}
293301
${(() => {
294302
let dataIndex = 0;
295303
let dirtyIndex = 0;

src/object.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {expect, test} from 'vitest';
22

33
import './object.js';
44
import './primitives.js';
5-
import {Diff, Set, ToJSON, ToJSONWithoutDefaults} from './proxy.js';
5+
import {Diff, Set, SetWithDefaults, ToJSON, ToJSONWithoutDefaults} from './proxy.js';
66
import {registry} from './register.js';
77

88
test('concrete', () => {
@@ -94,6 +94,7 @@ test('mapped onDirty', () => {
9494
const Proxy = property.map({data, dirty, onDirty: () => { dirties += 1; }});
9595
expect(dirties).toEqual(0);
9696
const proxy = new Proxy(0);
97+
proxy[SetWithDefaults]();
9798
expect(dirties).toEqual(4);
9899
dirties = 0;
99100
proxy.x = 2;
@@ -220,5 +221,6 @@ test('default value', () => {
220221
});
221222
const Proxy = property.concrete();
222223
const proxy = new Proxy(0);
224+
proxy[SetWithDefaults]();
223225
expect(proxy[ToJSON]()).toEqual({x: 1, y: 2, z: 3, a: 0});
224226
});

0 commit comments

Comments
 (0)