| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -230,7 +230,19 @@ function createWatcher( | |||
| 230 | 230 | ||
| 231 | 231 | // effect watch | |
| 232 | 232 | if (cb === null) { | |
| 233 | - const getter = () => (source as WatchEffect)(registerCleanup) | ||
| 233 | + let running = false | ||
| 234 | + const getter = () => { | ||
| 235 | + // preventing the watch callback being call in the same execution | ||
| 236 | + if (running) { | ||
| 237 | + return | ||
| 238 | + } | ||
| 239 | + try { | ||
| 240 | + running = true | ||
| 241 | + ;(source as WatchEffect)(registerCleanup) | ||
| 242 | + } finally { | ||
| 243 | + running = false | ||
| 244 | + } | ||
| 245 | + } | ||
| 234 | 246 | const watcher = createVueWatcher(vm, getter, noopFn, { | |
| 235 | 247 | deep: options.deep || false, | |
| 236 | 248 | sync: isSync, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -514,4 +514,22 @@ describe('api: watch', () => { | |||
| 514 | 514 | // expect(spy).toHaveBeenCalledTimes(1); | |
| 515 | 515 | // expect(warnSpy).toHaveBeenCalledWith(`"deep" option is only respected`); | |
| 516 | 516 | // }); | |
| 517 | + | ||
| 518 | + | ||
| 519 | + // #388 | ||
| 520 | + it('should not call the callback multiple times', () => { | ||
| 521 | + const data = ref([1, 1, 1, 1, 1]) | ||
| 522 | + const data2 = ref<number[]>([]) | ||
| 523 | + | ||
| 524 | + watchEffect( | ||
| 525 | + () => { | ||
| 526 | + data2.value = data.value.slice(1, 2) | ||
| 527 | + }, | ||
| 528 | + { | ||
| 529 | + flush: 'sync', | ||
| 530 | + } | ||
| 531 | + ) | ||
| 532 | + | ||
| 533 | + expect(data2.value).toMatchObject([1]) | ||
| 534 | + }) | ||
| 517 | 535 | }) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments