Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Nov 5, 2024
1 parent 657603d commit aecd460
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 726 deletions.
3 changes: 2 additions & 1 deletion packages/reactivity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
},
"homepage": "https://github.com/vuejs/core/tree/main/packages/reactivity#readme",
"dependencies": {
"@vue/shared": "workspace:*"
"@vue/shared": "workspace:*",
"alien-signals": "file:/Users/johnsonchu/Desktop/GitHub/refs"
}
}
123 changes: 7 additions & 116 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import { isFunction } from '@vue/shared'
import {
type DebuggerEvent,
type DebuggerOptions,
EffectFlags,
type Subscriber,
activeSub,
batch,
refreshComputed,
} from './effect'
import { Computed } from 'alien-signals'
import type { Ref } from './ref'
import { warn } from './warning'
import { Dep, type Link, globalVersion } from './dep'
import { ReactiveFlags, TrackOpTypes } from './constants'

declare const ComputedRefSymbol: unique symbol
declare const WritableComputedRefSymbol: unique symbol
Expand Down Expand Up @@ -44,104 +34,16 @@ export interface WritableComputedOptions<T, S = T> {
* @private exported by @vue/reactivity for Vue core use, but not exported from
* the main vue package
*/
export class ComputedRefImpl<T = any> implements Subscriber {
/**
* @internal
*/
_value: any = undefined
/**
* @internal
*/
readonly dep: Dep = new Dep(this)
/**
* @internal
*/
readonly __v_isRef = true
// TODO isolatedDeclarations ReactiveFlags.IS_REF
/**
* @internal
*/
readonly __v_isReadonly: boolean
// TODO isolatedDeclarations ReactiveFlags.IS_READONLY
// A computed is also a subscriber that tracks other deps
/**
* @internal
*/
deps?: Link = undefined
/**
* @internal
*/
depsTail?: Link = undefined
/**
* @internal
*/
flags: EffectFlags = EffectFlags.DIRTY
/**
* @internal
*/
globalVersion: number = globalVersion - 1
/**
* @internal
*/
isSSR: boolean
/**
* @internal
*/
next?: Subscriber = undefined

// for backwards compat
effect: this = this
// dev only
onTrack?: (event: DebuggerEvent) => void
// dev only
onTrigger?: (event: DebuggerEvent) => void

/**
* Dev only
* @internal
*/
_warnRecursive?: boolean

export class ComputedRefImpl<T = any> extends Computed {
constructor(
public fn: ComputedGetter<T>,
fn: ComputedGetter<T>,
private readonly setter: ComputedSetter<T> | undefined,
isSSR: boolean,
) {
this[ReactiveFlags.IS_READONLY] = !setter
this.isSSR = isSSR
}

/**
* @internal
*/
notify(): true | void {
this.flags |= EffectFlags.DIRTY
if (
!(this.flags & EffectFlags.NOTIFIED) &&
// avoid infinite self recursion
activeSub !== this
) {
batch(this, true)
return true
} else if (__DEV__) {
// TODO warn
}
super(fn)
}

get value(): T {
const link = __DEV__
? this.dep.track({
target: this,
type: TrackOpTypes.GET,
key: 'value',
})
: this.dep.track()
refreshComputed(this)
// sync version after evaluation
if (link) {
link.version = this.dep.version
}
return this._value
return this.get()
}

set value(newValue) {
Expand Down Expand Up @@ -186,18 +88,12 @@ export class ComputedRefImpl<T = any> implements Subscriber {
* @param debugOptions - For debugging. See {@link https://vuejs.org/guide/extras/reactivity-in-depth.html#computed-debugging}.
* @see {@link https://vuejs.org/api/reactivity-core.html#computed}
*/
export function computed<T>(
getter: ComputedGetter<T>,
debugOptions?: DebuggerOptions,
): ComputedRef<T>
export function computed<T>(getter: ComputedGetter<T>): ComputedRef<T>
export function computed<T, S = T>(
options: WritableComputedOptions<T, S>,
debugOptions?: DebuggerOptions,
): WritableComputedRef<T, S>
export function computed<T>(
getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>,
debugOptions?: DebuggerOptions,
isSSR = false,
) {
let getter: ComputedGetter<T>
let setter: ComputedSetter<T> | undefined
Expand All @@ -209,12 +105,7 @@ export function computed<T>(
setter = getterOrOptions.set
}

const cRef = new ComputedRefImpl(getter, setter, isSSR)

if (__DEV__ && debugOptions && !isSSR) {
cRef.onTrack = debugOptions.onTrack
cRef.onTrigger = debugOptions.onTrigger
}
const cRef = new ComputedRefImpl(getter, setter)

return cRef as any
}
58 changes: 0 additions & 58 deletions packages/reactivity/src/dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import type { ComputedRefImpl } from './computed'
import { type TrackOpTypes, TriggerOpTypes } from './constants'
import {
type DebuggerEventExtraInfo,
EffectFlags,
type Subscriber,
activeSub,
endBatch,
shouldTrack,
startBatch,
Expand All @@ -16,50 +13,6 @@ import {
* This is used to give computed a fast path to avoid re-compute when nothing
* has changed.
*/
export let globalVersion = 0

/**
* Represents a link between a source (Dep) and a subscriber (Effect or Computed).
* Deps and subs have a many-to-many relationship - each link between a
* dep and a sub is represented by a Link instance.
*
* A Link is also a node in two doubly-linked lists - one for the associated
* sub to track all its deps, and one for the associated dep to track all its
* subs.
*
* @internal
*/
export class Link {
/**
* - Before each effect run, all previous dep links' version are reset to -1
* - During the run, a link's version is synced with the source dep on access
* - After the run, links with version -1 (that were never used) are cleaned
* up
*/
version: number

/**
* Pointers for doubly-linked lists
*/
nextDep?: Link
prevDep?: Link
nextSub?: Link
prevSub?: Link
prevActiveLink?: Link

constructor(
public sub: Subscriber,
public dep: Dep,
) {
this.version = dep.version
this.nextDep =
this.prevDep =
this.nextSub =
this.prevSub =
this.prevActiveLink =
undefined
}
}

/**
* @internal
Expand Down Expand Up @@ -144,17 +97,6 @@ export class Dep {
}
}

if (__DEV__ && activeSub.onTrack) {
activeSub.onTrack(
extend(
{
effect: activeSub,
},
debugInfo,
),
)
}

return link
}

Expand Down
Loading

0 comments on commit aecd460

Please sign in to comment.