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 #5

Open
Gumball12 opened this issue Apr 1, 2024 · 1 comment
Open

Make watchers clearable via $watch #5

Gumball12 opened this issue Apr 1, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@Gumball12
Copy link
Owner

const useStore = defineStore('store', {
  state: () => ({
    countA: 1,
    countB: 1,
  }),
  watch: {
    countA: () => {
      console.log('changed');
    },
  },
});

const store = useStore();
store.countA = 2; // "changed"

store.$watch.countA(); // clear watch.countA
store.countA = 3; // nothing
@Gumball12 Gumball12 added the enhancement New feature or request label Apr 1, 2024
@Gumball12 Gumball12 self-assigned this Apr 1, 2024
@Gumball12
Copy link
Owner Author

✨✨ Here's an AI-assisted sketch of how you might approach this issue saved by @Gumball12 using Copilot Workspace v0.22

Topic

Can watchers be cleared via the $watch method?

Before
  • No, watchers cannot be cleared via the $watch method.
  • The current implementation in src/index.ts does not provide a mechanism to clear watchers.
  • The $watch property in the store is a readonly reference to the watch handlers defined in the store options.
  • There is no functionality in the code to remove or clear a watcher once it has been set.
  • The tests in __tests__/index.test.ts do not include any scenarios for clearing watchers via the $watch method.
After
  • Yes, watchers can now be cleared via the $watch method.
  • Added a method to clear watchers in src/index.ts.
    • Implemented a function to remove watchers from the store.
    • Updated the $watch property to include a method for clearing watchers.
  • Updated tests in __tests__/index.test.ts to include scenarios for clearing watchers via the $watch method.
    • Added tests to verify that watchers can be cleared and do not trigger after being cleared.
  • Updated documentation in README.md to include instructions on how to clear watchers via the $watch method.
Plan
  • src/index.ts (CHANGE)
    • Add a method to clear watchers in the WatchPiniaPlugin function
    • Implement a function to remove watchers from the store
    • Update the $watch property to include a method for clearing watchers
  • __tests__/index.test.ts (CHANGE)
    • Add tests to verify that watchers can be cleared via the $watch method
    • Add tests to verify that cleared watchers do not trigger after being cleared
  • README.md (CHANGE)
    • Update documentation to include instructions on how to clear watchers via the $watch method
Sketch of implementation

View the changes

Details

Code analyzed at 18313d9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant