forked from milomg/js-reactivity-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vueReactivity.ts
55 lines (53 loc) · 1.06 KB
/
vueReactivity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {
computed,
effectScope,
shallowRef,
effect,
ReactiveEffect,
} from "@vue/reactivity";
import { ReactiveFramework } from "../util/reactiveFramework";
let scheduled = [] as ReactiveEffect[];
let batching = false;
export const vueReactivityFramework: ReactiveFramework = {
name: "Vue",
signal: (initial) => {
const data = shallowRef(initial);
return {
read: () => data.value as any,
write: (v) => (data.value = v as any),
};
},
computed: (fn) => {
const c = computed(fn);
return {
read: () => c.value,
};
},
effect: (fn) => {
let t = effect(fn, {
scheduler: () => {
scheduled.push(t.effect);
},
});
},
// withBatch: (fn) => fn(),
withBatch: (fn) => {
if (batching) {
fn();
} else {
batching = true;
fn();
while (scheduled.length) {
scheduled.pop()!.run();
}
batching = false;
}
},
// withBuild: (fn) => fn()
withBuild: (fn) => {
const e = effectScope();
const r = e.run(fn)!;
e.stop();
return r;
},
};