Skip to content

Commit

Permalink
fix(ref): merge review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbinBaauw committed Aug 20, 2020
1 parent 58195f0 commit 4da3bf0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
13 changes: 5 additions & 8 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ export interface WritableComputedOptions<T> {
set: ComputedSetter<T>
}

class _ComputedRef<T> {
class ComputedRefImpl<T> {
private _value!: T
private _dirty = true

public readonly effect: ReactiveEffect<T>;
public readonly effect: ReactiveEffect<T>

public [ReactiveFlags.IS_READONLY]: boolean
public readonly __v_isRef = true;
public readonly [ReactiveFlags.IS_READONLY]: boolean

constructor(
getter: ComputedGetter<T>,
Expand Down Expand Up @@ -58,10 +59,6 @@ class _ComputedRef<T> {
set value(newValue: T) {
this._setter(newValue)
}

get __v_isRef() {
return true
}
}

export function computed<T>(getter: ComputedGetter<T>): ComputedRef<T>
Expand All @@ -86,7 +83,7 @@ export function computed<T>(
setter = getterOrOptions.set
}

return new _ComputedRef(
return new ComputedRefImpl(
getter,
setter,
isFunction(getterOrOptions) || !getterOrOptions.set
Expand Down
30 changes: 12 additions & 18 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ export function shallowRef(value?: unknown) {
return createRef(value, true)
}

class _Ref<T> {
class RefImpl<T> {
private _value: T

public readonly __v_isRef = true

constructor(private _rawValue: T, private readonly _shallow = false) {
this._value = _shallow ? _rawValue : convert(_rawValue)
}
Expand All @@ -63,17 +65,13 @@ class _Ref<T> {
trigger(toRaw(this), TriggerOpTypes.SET, 'value', newVal)
}
}

get __v_isRef() {
return true
}
}

function createRef(rawValue: unknown, shallow = false) {
if (isRef(rawValue)) {
return rawValue
}
return new _Ref(rawValue, shallow)
return new RefImpl(rawValue, shallow)
}

export function triggerRef(ref: Ref) {
Expand Down Expand Up @@ -113,10 +111,12 @@ export type CustomRefFactory<T> = (
set: (value: T) => void
}

class _CustomRef<T> {
class CustomRefImpl<T> {
private readonly _get: ReturnType<CustomRefFactory<T>>['get']
private readonly _set: ReturnType<CustomRefFactory<T>>['set']

public readonly __v_isRef = true

constructor(factory: CustomRefFactory<T>) {
const { get, set } = factory(
() => track(this, TrackOpTypes.GET, 'value'),
Expand All @@ -133,14 +133,10 @@ class _CustomRef<T> {
set value(newVal) {
this._set(newVal)
}

get __v_isRef() {
return true
}
}

export function customRef<T>(factory: CustomRefFactory<T>): Ref<T> {
return new _CustomRef(factory) as any
return new CustomRefImpl(factory) as any
}

export function toRefs<T extends object>(object: T): ToRefs<T> {
Expand All @@ -154,7 +150,9 @@ export function toRefs<T extends object>(object: T): ToRefs<T> {
return ret
}

class _ObjectRef<T extends object, K extends keyof T> {
class ObjectRefImpl<T extends object, K extends keyof T> {
public readonly __v_isRef = true

constructor(private readonly _object: T, private readonly _key: K) {}

get value() {
Expand All @@ -164,17 +162,13 @@ class _ObjectRef<T extends object, K extends keyof T> {
set value(newVal) {
this._object[this._key] = newVal
}

get __v_isRef() {
return true
}
}

export function toRef<T extends object, K extends keyof T>(
object: T,
key: K
): Ref<T[K]> {
return new _ObjectRef(object, key) as any
return new ObjectRefImpl(object, key) as any
}

// corner case when use narrows type
Expand Down

0 comments on commit 4da3bf0

Please sign in to comment.