Skip to content

Commit ef8d43e

Browse files
committed
ref(vuebugger): minimize watch load by only watching the state when enabled
1 parent c721a27 commit ef8d43e

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

packages/vuebugger/src/debug.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,34 @@ export const debug = <T extends Record<string, any>>(
8888
{ immediate: true, deep: true },
8989
)
9090
} else {
91+
let stateScope:
92+
| ReturnType<typeof effectScope>
93+
| undefined
94+
9195
watch(
92-
[() => toValue(options.enable), () => state],
93-
([isActive]) => {
94-
if (isActive) upsert(entry)
95-
else remove(entry)
96+
() => toValue(options.enable),
97+
(isActive) => {
98+
if (isActive) {
99+
stateScope = effectScope()
100+
stateScope.run(() => {
101+
watch(
102+
() => state,
103+
(value) => {
104+
upsert({ ...entry, debugState: value })
105+
},
106+
{ immediate: true, deep: true },
107+
)
108+
})
109+
} else {
110+
stateScope?.stop()
111+
stateScope = undefined
112+
remove(entry)
113+
}
96114
},
97-
{ immediate: true, deep: true },
115+
{ immediate: true },
98116
)
117+
118+
onScopeDispose(() => stateScope?.stop())
99119
}
100120
})
101121

0 commit comments

Comments
 (0)