forked from mengyxu/noob-components
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2.2 KiB
2.2 KiB
useRefreshFlags
Auto-refresh flag management with Vuex store integration.
Purpose
Watches refresh flags in Vuex store and triggers callbacks when matching flags are set. Used for coordinated auto-refresh across components.
Signature
export function useRefreshFlags(
flags: string[],
cb: () => void | Promise<void>,
match?: "any" | "all",
token?: string
): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
flags |
string[] |
required | Flag names to watch |
cb |
() => void | Promise<void> |
required | Callback when flag matches |
match |
"any" | "all" |
"any" |
Match any flag or all flags |
token |
string |
route.path |
Unique token for this watcher |
Match Behavior
"any": Trigger when any flag is set and this token hasn't consumed it"all": Trigger when all flags are set and this token hasn't consumed them
Vuex Store Structure
Expects state.refreshFlags to be a Map structure:
state.refreshFlags: Map<string, Set<string>>
// flag name -> Set of tokens that have consumed it
Usage Pattern
// Example: plugs/composables/useRefreshFlags.ts
import { useRefreshFlags } from "plugs/composables";
// Watch for any of these flags
useRefreshFlags(["userUpdated", "roleChanged"], async () => {
await query();
});
// Watch for all flags
useRefreshFlags(["fetchUsers", "fetchDepartments"], async () => {
await query();
}, "all");
// With custom token
useRefreshFlags(["refresh"], () => {
query();
}, "any", "my-unique-component-id");
Key Implementation Details
- Token auto-detection: Uses
route.pathas default token if not provided - Flag consumption: After callback executes, calls
unsetRefreshFlagVuex action to mark flags as consumed by this token - Reactive tracking: Explicitly accesses
state.refreshFlagsto ensure Vue tracks the dependency
Dependencies
vue-watchvue-router-useRoutevuex-useStore
Anti-patterns
- Do not use the same token for multiple independent watchers; each logical unit should have unique token
- Do not forget to dispatch
unsetRefreshFlagfor consumed flags (handled automatically)