-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jindy
committed
Oct 12, 2022
1 parent
3715784
commit 9ec93c1
Showing
4 changed files
with
108 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { reactive } from "@guide-mini-vue/reactivity"; | ||
import { watchEffect } from "../src/apiWatch"; | ||
import { nextTick } from "../src/scheduler"; | ||
import { vi } from "vitest"; | ||
|
||
describe("api: watch", () => { | ||
it("effect", async () => { | ||
const state = reactive({ count: 0 }); | ||
let dummy; | ||
watchEffect(() => { | ||
dummy = state.count; | ||
}); | ||
expect(dummy).toBe(0); | ||
|
||
state.count++; | ||
await nextTick(); | ||
expect(dummy).toBe(1); | ||
}); | ||
|
||
it("stopping the watcher (effect)", async () => { | ||
const state = reactive({ count: 0 }); | ||
let dummy; | ||
const stop: any = watchEffect(() => { | ||
dummy = state.count; | ||
}); | ||
expect(dummy).toBe(0); | ||
|
||
stop(); | ||
state.count++; | ||
await nextTick(); | ||
// should not update | ||
expect(dummy).toBe(0); | ||
}); | ||
|
||
it("cleanup registration (effect)", async () => { | ||
const state = reactive({ count: 0 }); | ||
const cleanup = vi.fn(); | ||
let dummy; | ||
const stop: any = watchEffect((onCleanup) => { | ||
onCleanup(cleanup); | ||
dummy = state.count; | ||
}); | ||
expect(dummy).toBe(0); | ||
|
||
state.count++; | ||
await nextTick(); | ||
expect(cleanup).toHaveBeenCalledTimes(1); | ||
expect(dummy).toBe(1); | ||
|
||
stop(); | ||
expect(cleanup).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ReactiveEffect } from "../../reactivity/src/effect"; | ||
import { queuePreFlushCb } from "./scheduler"; | ||
export function watchEffect(source) { | ||
function job() { | ||
effect.run(); | ||
} | ||
let cleanup; | ||
const oncleanup = function (fn) { | ||
cleanup = effect.onStop = () => { | ||
fn(); | ||
}; | ||
}; | ||
function getter() { | ||
if (cleanup) cleanup(); | ||
source(oncleanup); | ||
} | ||
const effect = new ReactiveEffect(getter, () => { | ||
queuePreFlushCb(job); | ||
}); | ||
|
||
effect.run(); | ||
|
||
return () => { | ||
effect.stop(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,42 @@ | ||
const queue: any[] = [] | ||
let isFlushPending = false | ||
const p = Promise.resolve() | ||
const queue: any[] = []; | ||
const activePreFlushCbs: any[] = []; | ||
let isFlushPending = false; | ||
const p = Promise.resolve(); | ||
export function queueJobs(job) { | ||
if (!queue.includes(job)) { | ||
queue.push(job) | ||
queue.push(job); | ||
} | ||
queueFlush() | ||
queueFlush(); | ||
} | ||
|
||
export function nextTick(fn) { | ||
return fn ? p.then(fn) : p | ||
export function nextTick(fn?) { | ||
return fn ? p.then(fn) : p; | ||
} | ||
|
||
function queueFlush() { | ||
if (isFlushPending) return | ||
isFlushPending = true | ||
nextTick(flushJobs) | ||
if (isFlushPending) return; | ||
isFlushPending = true; | ||
nextTick(flushJobs); | ||
} | ||
|
||
function flushJobs() { | ||
isFlushPending = false | ||
let job | ||
isFlushPending = false; | ||
|
||
flushPrevFlushCbs(); | ||
|
||
let job; | ||
while ((job = queue.shift())) { | ||
job && job() | ||
job && job(); | ||
} | ||
} | ||
|
||
function flushPrevFlushCbs() { | ||
for (let i = 0; i < activePreFlushCbs.length; i++) { | ||
activePreFlushCbs[i](); | ||
} | ||
} | ||
|
||
export function queuePreFlushCb(cb) { | ||
activePreFlushCbs.push(cb); | ||
queueFlush(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters