Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"compile": "rm -rf dist && pnpm run '/^compile:.*/'",
"compile:esm": "tsc -p tsconfig.esm.json",
"compile:cjs": "tsc -p tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
"fix": "pnpm run fix:lint && pnpm run fix:format",
"fix:format": "prettier . --write",
"fix:lint": "eslint . --fix",
"test": "pnpm run '/^test:.*/'",
"test:format": "prettier -c .",
"test:lint": "eslint .",
Expand Down
21 changes: 8 additions & 13 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,18 @@ export function watch(fn: () => void): Unwatch {
};

const subscribeProxies = () => {
const rootTouchedProxies = new Set<ProxyObject>();
for (const p of touchedKeys.keys()) {
// FIXME this isn't very efficient.
if (Object.values(p).every((v) => !touchedKeys.has(v))) {
rootTouchedProxies.add(p);
}
}
for (const [p, unsub] of subscriptions) {
if (rootTouchedProxies.has(p)) {
rootTouchedProxies.delete(p);
} else {
if (!touchedKeys.has(p)) {
unsub();
subscriptions.delete(p);
}
}
for (const p of rootTouchedProxies) {
const unsub = subscribe(p, () => registerCallback(callback), true);
subscriptions.set(p, unsub);

for (const p of touchedKeys.keys()) {
if (!subscriptions.has(p)) {
const unsub = subscribe(p, () => registerCallback(callback), true);
subscriptions.set(p, unsub);
}
}
};

Expand Down
48 changes: 48 additions & 0 deletions tests/01_watch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,54 @@ describe('watch', () => {
expect(fn).toHaveBeenLastCalledWith([2]);
unwatch();
});

it('should watch primitive properties on parent when child is also watched', async () => {
const state = proxy({
count: 0,
nested: { text: 'hello' },
});
const fn = vi.fn();

const unwatch = watch(() => {
void state.count;
void state.nested.text;
fn();
});

expect(fn).toHaveBeenCalledTimes(1);

state.count++;
await new Promise<void>((r) => setTimeout(r));
expect(fn).toHaveBeenCalledTimes(2);

state.nested.text = 'world';
await new Promise<void>((r) => setTimeout(r));
expect(fn).toHaveBeenCalledTimes(3);

unwatch();
});

it('should handle object replacement when watching nested properties', async () => {
const state = proxy({
nested: { text: 'initial' },
});
const fn = vi.fn();

const unwatch = watch(() => {
fn(state.nested.text);
});

expect(fn).toHaveBeenCalledTimes(1);
expect(fn).toHaveBeenLastCalledWith('initial');

state.nested = { text: 'replaced' };
await new Promise<void>((r) => setTimeout(r));

expect(fn).toHaveBeenCalledTimes(2);
expect(fn).toHaveBeenLastCalledWith('replaced');

unwatch();
});
});

describe('watch with batch', () => {
Expand Down