Skip to content

fix(watch): component should runCleanup() on unmount #298

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

Merged
merged 2 commits into from
Apr 24, 2020
Merged
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
18 changes: 16 additions & 2 deletions src/apis/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ function createVueWatcher(
return vm._watchers[index];
}

// We have to monkeypatch the teardown function so Vue will run
// runCleanup() when it tears down the watcher on unmmount.
function patchWatcherTeardown(watcher: VueWatcher, runCleanup: () => void) {
const _teardown = watcher.teardown;
watcher.teardown = function(...args) {
_teardown.apply(watcher, args);
runCleanup();
};
}

function createWatcher(
vm: ComponentInstance,
source: WatcherSource<unknown> | WatcherSource<unknown>[] | SimpleEffect,
Expand Down Expand Up @@ -188,6 +198,8 @@ function createWatcher(
before: runCleanup,
});

patchWatcherTeardown(watcher, runCleanup);

// enable the watcher update
watcher.lazy = false;

Expand All @@ -201,7 +213,6 @@ function createWatcher(

return () => {
watcher.teardown();
runCleanup();
};
}

Expand Down Expand Up @@ -240,9 +251,12 @@ function createWatcher(
sync: isSync,
});

// Once again, we have to hack the watcher for proper teardown
const watcher = vm._watchers[vm._watchers.length - 1];
patchWatcherTeardown(watcher, runCleanup);

return () => {
stop();
runCleanup();
};
}

Expand Down
24 changes: 22 additions & 2 deletions test/apis/watch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ describe('api/watch', () => {
});

it('should work', done => {
const onCleanupSpy = jest.fn();
const vm = new Vue({
setup() {
const a = ref(1);
watch(a, spy);
watch(a, (n, o, _onCleanup) => {
spy(n, o, _onCleanup);
_onCleanup(onCleanupSpy);
});
return {
a,
};
Expand All @@ -25,13 +29,21 @@ describe('api/watch', () => {
}).$mount();
expect(spy).toBeCalledTimes(1);
expect(spy).toHaveBeenLastCalledWith(1, undefined, anyFn);
expect(onCleanupSpy).toHaveBeenCalledTimes(0);
vm.a = 2;
vm.a = 3;
expect(spy).toBeCalledTimes(1);
waitForUpdate(() => {
expect(spy).toBeCalledTimes(2);
expect(spy).toHaveBeenLastCalledWith(3, 1, anyFn);
}).then(done);
expect(onCleanupSpy).toHaveBeenCalledTimes(1);

vm.$destroy();
})
.then(() => {
expect(onCleanupSpy).toHaveBeenCalledTimes(2);
})
.then(done);
});

it('basic usage(value wrapper)', done => {
Expand Down Expand Up @@ -349,11 +361,13 @@ describe('api/watch', () => {
let renderedText;
it('should work', done => {
let onCleanup;
const onCleanupSpy = jest.fn();
const vm = new Vue({
setup() {
const count = ref(0);
watchEffect(_onCleanup => {
onCleanup = _onCleanup;
_onCleanup(onCleanupSpy);
spy(count.value);
renderedText = vm.$el.textContent;
});
Expand All @@ -369,13 +383,19 @@ describe('api/watch', () => {
expect(spy).not.toHaveBeenCalled();
waitForUpdate(() => {
expect(onCleanup).toEqual(anyFn);
expect(onCleanupSpy).toHaveBeenCalledTimes(0);
expect(renderedText).toBe('0');
expect(spy).toHaveBeenLastCalledWith(0);
vm.count++;
})
.then(() => {
expect(renderedText).toBe('1');
expect(spy).toHaveBeenLastCalledWith(1);
expect(onCleanupSpy).toHaveBeenCalledTimes(1);
vm.$destroy();
})
.then(() => {
expect(onCleanupSpy).toHaveBeenCalledTimes(2);
})
.then(done);
});
Expand Down