-
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.
实现ref,isRef,unRef,proxyRefs,computed
- Loading branch information
jindy
committed
Jun 7, 2022
1 parent
0b29f82
commit 6df8018
Showing
6 changed files
with
161 additions
and
20 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,28 @@ | ||
import { ReactiveEffect } from "./effect" | ||
|
||
class ComputedImpl { | ||
private _getter: any | ||
private _dirty: boolean = true | ||
private _value: any | ||
private _effect: ReactiveEffect | ||
constructor(getter) { | ||
this._getter = getter | ||
|
||
this._effect = new ReactiveEffect(getter, () => { | ||
if (!this._dirty) { | ||
this._dirty = true | ||
} | ||
}) | ||
} | ||
get value() { | ||
if (this._dirty) { | ||
this._dirty = false | ||
this._value = this._effect.run() | ||
} | ||
return this._value | ||
} | ||
} | ||
|
||
export function computed(getter) { | ||
return new ComputedImpl(getter) | ||
} |
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
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,31 +1,64 @@ | ||
import { hasChange } from "./../shared/index" | ||
import { isTracking, trackEffect, triggerEffect } from "./effect" | ||
import { hasChanged, isObject } from "./../shared/index" | ||
import { isTracking, trackEffects, triggerEffects } from "./effect" | ||
import { reactive } from "./reactive" | ||
|
||
class RefImpl { | ||
private _value: any | ||
public dep | ||
private _rawValue: any | ||
public __v_isRef = true | ||
constructor(value) { | ||
this._value = value | ||
this._rawValue = value | ||
this._value = convert(value) | ||
this.dep = new Set() | ||
} | ||
get value() { | ||
trackRefValue(this) | ||
return this._value | ||
} | ||
set value(newValue) { | ||
if (hasChange(newValue, this._value)) { | ||
this._value = newValue | ||
triggerEffect(this.dep) | ||
if (hasChanged(newValue, this._rawValue)) { | ||
this._rawValue = newValue | ||
this._value = convert(newValue) | ||
triggerEffects(this.dep) | ||
} | ||
} | ||
} | ||
|
||
function convert(value) { | ||
return isObject(value) ? reactive(value) : value | ||
} | ||
|
||
function trackRefValue(ref) { | ||
if (isTracking()) { | ||
trackEffect(ref.dep) | ||
trackEffects(ref.dep) | ||
} | ||
} | ||
|
||
export function ref(value) { | ||
return new RefImpl(value) | ||
} | ||
|
||
export function isRef(ref) { | ||
return !!ref.__v_isRef | ||
} | ||
|
||
// 判断ref 是否为 ref.value | ||
export function unRef(ref) { | ||
return isRef(ref) ? ref.value : ref | ||
} | ||
|
||
export function proxyRefs(objectWithRefs) { | ||
return new Proxy(objectWithRefs, { | ||
get(target, key) { | ||
return unRef(Reflect.get(target, key)) | ||
}, | ||
set(target, key, value) { | ||
if (isRef(target(key)) && !isRef(value)) { | ||
return (target[key].value = value) | ||
} else { | ||
return Reflect.set(target, key, value) | ||
} | ||
}, | ||
}) | ||
} |
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,42 @@ | ||
import { reactive } from "../reactive" | ||
import { computed } from "../computed" | ||
|
||
describe("computed", () => { | ||
it("happy path", () => { | ||
const user = reactive({ | ||
age: 1, | ||
}) | ||
|
||
const age = computed(() => { | ||
return user.age | ||
}) | ||
|
||
expect(age.value).toBe(1) | ||
}) | ||
|
||
it("should computed lazily", () => { | ||
const value = reactive({ foo: 1 }) | ||
const getter = jest.fn(() => { | ||
return value.foo | ||
}) | ||
const cValue = computed(getter) | ||
|
||
// lazy | ||
expect(getter).not.toHaveBeenCalled() | ||
|
||
expect(cValue.value).toBe(1) | ||
expect(getter).toHaveBeenCalledTimes(1) | ||
|
||
cValue.value | ||
expect(getter).toHaveBeenCalledTimes(1) | ||
|
||
value.foo = 2 | ||
expect(getter).toHaveBeenCalledTimes(1) | ||
|
||
expect(cValue.value).toBe(2) | ||
expect(getter).toHaveBeenCalledTimes(2) | ||
|
||
cValue.value | ||
expect(getter).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
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