Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make watchers clearable via $watch #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,46 @@ const useStore = defineStore('store', {
});
```

### 4. Clear the watcher

```ts
import { defineStore } from 'pinia';

const useStore = defineStore('store', {
state: () => ({
count: 0,
user: {
name: 'John',
age: 20,
},
}),

// PiniaPluginWatch
watch: {
// Watch `count`
count: (newValue, oldValue, onCleanup, store) => {
console.log('count changed', newValue, oldValue);
},

// Watch `user` and `user.name`
user: {
handler: (newValue, oldValue, onCleanup, store) => {
console.log('user changed', newValue, oldValue);
},
children: {
name: (newValue, oldValue, onCleanup, store) => {
console.log('user.name changed', newValue, oldValue);
},
},
},
},
});

const store = useStore();
store.$watch.clear('count'); // Clear the watcher for `count`
store.$watch.clear('user'); // Clear the watcher for `user`
```

For usage examples, see the [Usage](./USAGE.md) documentation.

## 🌮 API
Expand All @@ -92,6 +132,12 @@ For usage examples, see the [Usage](./USAGE.md) documentation.

The `watch` option is copied to the `$watch` property of the store.

### `store.$watch.clear`

- Type: `(key: string) => void`

The `clear` method allows you to remove a watcher from the store.

## License

[MIT](./LICENSE)
115 changes: 89 additions & 26 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('Option Store (Deep watch)', () => {
await nextTick();
expect(fooSpy).toHaveBeenCalledOnce(); // from #1
expect(barSpy).not.toBeCalled();
expect(bazSpy).not.toBeCalled();
expect(bazSpy).not toBeCalled();
});

it('$patch object', async () => {
Expand Down Expand Up @@ -139,6 +139,27 @@ describe('Option Store (Deep watch)', () => {
expect(barSpy).toHaveBeenCalledOnce(); // from #1
expect(bazSpy).toHaveBeenCalledOnce(); // from #1
});

it('clear watch', async () => {
const { store, fooSpy, barSpy, bazSpy } = useStore();

store.foo.bar.baz = 10; // #1
expect(store.foo.bar.baz).toBe(10);

await nextTick();
expect(fooSpy).toHaveBeenCalledOnce(); // from #1
expect(barSpy).toHaveBeenCalledOnce(); // from #1
expect(bazSpy).toHaveBeenCalledOnce(); // from #1

store.$watch.clear('foo'); // #2
store.foo.bar.baz = 20; // #3
expect(store.foo.bar.baz).toBe(20);

await nextTick();
expect(fooSpy).toHaveBeenCalledOnce(); // from #1
expect(barSpy).toHaveBeenCalledOnce(); // from #1
expect(bazSpy).toHaveBeenCalledOnce(); // from #1
});
});

const useStore = () => {
Expand Down Expand Up @@ -179,9 +200,9 @@ describe('Setup Store (Deep watch)', () => {
it('update baz', async () => {
const { fooSpy, barSpy, bazSpy, store } = useSetupStore();

expect(fooSpy).not.toBeCalled();
expect(barSpy).not.toBeCalled();
expect(bazSpy).not.toBeCalled();
expect(fooSpy).not toBeCalled();
expect(barSpy).not toBeCalled();
expect(bazSpy).not toBeCalled();

store.foo.bar.baz = 10; // #1
expect(store.foo.bar.baz).toBe(10);
Expand All @@ -203,7 +224,7 @@ describe('Setup Store (Deep watch)', () => {
await nextTick();
expect(fooSpy).toHaveBeenCalledOnce(); // from #1
expect(barSpy).toHaveBeenCalledOnce(); // from #1
expect(bazSpy).not.toBeCalled();
expect(bazSpy).not toBeCalled();
});

it('update foo', async () => {
Expand All @@ -218,8 +239,8 @@ describe('Setup Store (Deep watch)', () => {

await nextTick();
expect(fooSpy).toHaveBeenCalledOnce(); // from #1
expect(barSpy).not.toBeCalled();
expect(bazSpy).not.toBeCalled();
expect(barSpy).not toBeCalled();
expect(bazSpy).not toBeCalled();
});

it('$patch object', async () => {
Expand Down Expand Up @@ -300,6 +321,27 @@ describe('Setup Store (Deep watch)', () => {
expect(barSpy).toHaveBeenCalledOnce(); // from #1
expect(bazSpy).toHaveBeenCalledOnce(); // from #1
});

it('clear watch', async () => {
const { store, fooSpy, barSpy, bazSpy } = useSetupStore();

store.foo.bar.baz = 10; // #1
expect(store.foo.bar.baz).toBe(10);

await nextTick();
expect(fooSpy).toHaveBeenCalledOnce(); // from #1
expect(barSpy).toHaveBeenCalledOnce(); // from #1
expect(bazSpy).toHaveBeenCalledOnce(); // from #1

store.$watch.clear('foo'); // #2
store.foo.bar.baz = 20; // #3
expect(store.foo.bar.baz).toBe(20);

await nextTick();
expect(fooSpy).toHaveBeenCalledOnce(); // from #1
expect(barSpy).toHaveBeenCalledOnce(); // from #1
expect(bazSpy).toHaveBeenCalledOnce(); // from #1
});
});

const useSetupStore = () => {
Expand Down Expand Up @@ -356,17 +398,17 @@ describe('Option Store (Non-deep watch)', () => {
it('update baz', async () => {
const { fooSpy, barSpy, bazSpy, store } = useNonDeepWatchStore();

expect(fooSpy).not.toBeCalled();
expect(barSpy).not.toBeCalled();
expect(bazSpy).not.toBeCalled();
expect(fooSpy).not toBeCalled();
expect(barSpy).not toBeCalled();
expect(bazSpy).not toBeCalled();

store.foo.bar.baz = 10; // #1
expect(store.foo.bar.baz).toBe(10);

await nextTick();

expect(fooSpy).not.toBeCalled();
expect(barSpy).not.toBeCalled();
expect(fooSpy).not toBeCalled();
expect(barSpy).not toBeCalled();
expect(bazSpy).toHaveBeenCalledOnce(); // from #1
});

Expand All @@ -379,9 +421,9 @@ describe('Option Store (Non-deep watch)', () => {
};

await nextTick();
expect(fooSpy).not.toBeCalled();
expect(fooSpy).not toBeCalled();
expect(barSpy).toHaveBeenCalledOnce(); // from #1
expect(bazSpy).not.toBeCalled();
expect(bazSpy).not toBeCalled();
});

it('update foo', async () => {
Expand All @@ -396,8 +438,8 @@ describe('Option Store (Non-deep watch)', () => {

await nextTick();
expect(fooSpy).toHaveBeenCalledOnce(); // from #1
expect(barSpy).not.toBeCalled();
expect(bazSpy).not.toBeCalled();
expect(barSpy).not toBeCalled();
expect(bazSpy).not toBeCalled();
});

it('$patch object', async () => {
Expand All @@ -420,8 +462,8 @@ describe('Option Store (Non-deep watch)', () => {
expect(store.foo.bar.baz).toBe(20);

await nextTick();
expect(fooSpy).not.toBeCalled();
expect(barSpy).not.toBeCalled();
expect(fooSpy).not toBeCalled();
expect(barSpy).not toBeCalled();
expect(bazSpy).toHaveBeenCalledTimes(2); // from #1, #2
});

Expand All @@ -433,8 +475,8 @@ describe('Option Store (Non-deep watch)', () => {
expect(store.foo.bar.baz).toBe(10);

await nextTick();
expect(fooSpy).not.toBeCalled();
expect(barSpy).not.toBeCalled();
expect(fooSpy).not toBeCalled();
expect(barSpy).not toBeCalled();
expect(bazSpy).toHaveBeenCalledOnce(); // from #1

// #2
Expand All @@ -445,8 +487,8 @@ describe('Option Store (Non-deep watch)', () => {
expect(store.foo.bar.baz).toBe(20);

await nextTick();
expect(fooSpy).not.toBeCalled();
expect(barSpy).not.toBeCalled();
expect(fooSpy).not toBeCalled();
expect(barSpy).not toBeCalled();
expect(bazSpy).toHaveBeenCalledTimes(2); // from #1, #2
});

Expand All @@ -457,16 +499,37 @@ describe('Option Store (Non-deep watch)', () => {
expect(store.foo.bar.baz).toBe(10);

await nextTick();
expect(fooSpy).not.toBeCalled();
expect(barSpy).not.toBeCalled();
expect(fooSpy).not toBeCalled();
expect(barSpy).not toBeCalled();
expect(bazSpy).toHaveBeenCalledOnce(); // from #1

store.$reset(); // #2
expect(store.foo.bar.baz).toBe(1);

await nextTick();
expect(fooSpy).toHaveBeenCalledOnce(); // from #2
expect(barSpy).not.toBeCalled();
expect(barSpy).not toBeCalled();
expect(bazSpy).toHaveBeenCalledOnce(); // from #1
});

it('clear watch', async () => {
const { store, fooSpy, barSpy, bazSpy } = useNonDeepWatchStore();

store.foo.bar.baz = 10; // #1
expect(store.foo.bar.baz).toBe(10);

await nextTick();
expect(fooSpy).not toBeCalled();
expect(barSpy).not toBeCalled();
expect(bazSpy).toHaveBeenCalledOnce(); // from #1

store.$watch.clear('foo'); // #2
store.foo.bar.baz = 20; // #3
expect(store.foo.bar.baz).toBe(20);

await nextTick();
expect(fooSpy).not toBeCalled();
expect(barSpy).not toBeCalled();
expect(bazSpy).toHaveBeenCalledOnce(); // from #1
});
});
Expand Down Expand Up @@ -547,7 +610,7 @@ describe('Watch only some properties', () => {
store.countB = 2;

await nextTick();
expect(watch.countA).not.toBeCalled();
expect(watch.countA).not toBeCalled();
});

it('WatchObject with undefined handler', async () => {
Expand Down
38 changes: 36 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ declare module 'pinia' {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface PiniaCustomProperties<Id, S> {
$watch: Readonly<WatchOptions<Store, S>>;
$watch: Readonly<WatchOptions<Store, S>> & {
clear: (key: keyof S) => void;
};
}
}

Expand All @@ -49,7 +51,12 @@ export const WatchPiniaPlugin = ({

const { $state } = store;

store.$watch = watchMap;
store.$watch = {
...watchMap,
clear: (key: keyof typeof $state) => {
clearWatcher($state, watchMap, key);
},
};
watchState($state, watchMap, store);
};

Expand Down Expand Up @@ -113,6 +120,33 @@ const watchState = <S extends object>(
});
};

const clearWatcher = <S extends object>(
state: S,
watchMap: WatchOptions<Store, S>,
key: keyof S,
) => {
const maybeWatcher = watchMap[key];

if (!maybeWatcher) {
return;
}

if (isWatchHandler(maybeWatcher)) {
delete watchMap[key];
return;
}

if (isWatchHandlerObject<typeof state[key]>(maybeWatcher)) {
delete watchMap[key];
return;
}

const value = state[key];
if (isObject(value)) {
clearWatcher(value, maybeWatcher, key);
}
};

const isObject = (value: unknown): value is object =>
typeof value === 'object' && value !== null;

Expand Down
Loading