Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(reactivity): ports alien-signals 1.0.0 #12570

Merged
merged 30 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1a24410
feat(reactivity): sync alien-signals 0.4.5
johnsoncodehk Dec 17, 2024
2b596f9
feat(reactivity): sync alien-signals 0.4.10
johnsoncodehk Dec 25, 2024
cfb16a0
refactor(reactivity): simplify flag checks in propagate function
johnsoncodehk Dec 25, 2024
20c4f6e
Update system.ts
johnsoncodehk Jan 2, 2025
0a5ac69
Update system.ts
johnsoncodehk Jan 2, 2025
da1a003
Update system.ts
johnsoncodehk Jan 2, 2025
b8b6912
Update system.ts
johnsoncodehk Jan 2, 2025
d69e942
Update system.ts
johnsoncodehk Jan 2, 2025
ce6a2ef
Sync https://github.com/stackblitz/alien-signals/pull/29
johnsoncodehk Jan 7, 2025
0072ab1
Sync
johnsoncodehk Jan 7, 2025
8c3eadb
Update link
johnsoncodehk Jan 7, 2025
6b0403b
Sync
johnsoncodehk Jan 8, 2025
eb2f8de
Update link
johnsoncodehk Jan 8, 2025
6dd0ec4
Sync
johnsoncodehk Jan 10, 2025
f87d863
Sync https://github.com/stackblitz/alien-signals/pull/33
johnsoncodehk Jan 10, 2025
d119b50
[autofix.ci] apply automated fixes
autofix-ci[bot] Jan 10, 2025
ba00468
Update system.ts
johnsoncodehk Jan 10, 2025
928ab51
Remove SubscriberFlags.Notified
johnsoncodehk Jan 10, 2025
78f4fdb
Sync v0.6.0
johnsoncodehk Jan 10, 2025
55ab1d5
Fix types
johnsoncodehk Jan 10, 2025
11b7f4d
Sync https://github.com/stackblitz/alien-signals/pull/34
johnsoncodehk Jan 11, 2025
853e825
Sync https://github.com/stackblitz/alien-signals/pull/35
johnsoncodehk Jan 12, 2025
1b49a9b
Minimize
johnsoncodehk Jan 12, 2025
d534df3
Minimize
johnsoncodehk Jan 12, 2025
01b49e7
Update system.ts
johnsoncodehk Jan 12, 2025
593998b
Sort code
johnsoncodehk Jan 12, 2025
9aba1ef
Revert tsconfig.json
johnsoncodehk Jan 12, 2025
acab975
Sync v1.0.0
johnsoncodehk Jan 14, 2025
a3b99ec
Update system.ts
johnsoncodehk Jan 14, 2025
eba03d5
Update computed.ts
johnsoncodehk Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,12 @@ describe('reactivity/computed', () => {
const c2 = computed(() => c1.value) as unknown as ComputedRefImpl

c2.value
expect(c1.flags & SubscriberFlags.Dirtys).toBe(0)
expect(c2.flags & SubscriberFlags.Dirtys).toBe(0)
expect(
c1.flags & (SubscriberFlags.Dirty | SubscriberFlags.PendingComputed),
).toBe(0)
expect(
c2.flags & (SubscriberFlags.Dirty | SubscriberFlags.PendingComputed),
).toBe(0)
})

it('should chained computeds dirtyLevel update with first computed effect', () => {
Expand Down
73 changes: 30 additions & 43 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ import {
type DebuggerEvent,
type DebuggerOptions,
activeSub,
activeTrackId,
nextTrackId,
setActiveSub,
} from './effect'
import { activeEffectScope } from './effectScope'
import type { Ref } from './ref'
import {
type Dependency,
type IComputed,
type Link,
type Subscriber,
SubscriberFlags,
checkDirty,
endTrack,
endTracking,
link,
startTrack,
processComputedUpdate,
startTracking,
updateDirtyFlag,
} from './system'
import { warn } from './warning'

Expand Down Expand Up @@ -54,22 +53,20 @@ 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 IComputed {
export class ComputedRefImpl<T = any> implements Dependency, Subscriber {
/**
* @internal
*/
_value: T | undefined = undefined
version = 0

// Dependency
subs: Link | undefined = undefined
subsTail: Link | undefined = undefined
lastTrackedId = 0

// Subscriber
deps: Link | undefined = undefined
depsTail: Link | undefined = undefined
flags: SubscriberFlags = SubscriberFlags.Dirty
flags: SubscriberFlags = SubscriberFlags.Computed | SubscriberFlags.Dirty

/**
* @internal
Expand All @@ -93,24 +90,20 @@ export class ComputedRefImpl<T = any> implements IComputed {
// for backwards compat
get _dirty(): boolean {
const flags = this.flags
if (flags & SubscriberFlags.Dirty) {
if (
flags & SubscriberFlags.Dirty ||
(flags & SubscriberFlags.PendingComputed &&
updateDirtyFlag(this, this.flags))
) {
return true
} else if (flags & SubscriberFlags.ToCheckDirty) {
if (checkDirty(this.deps!)) {
this.flags |= SubscriberFlags.Dirty
return true
} else {
this.flags &= ~SubscriberFlags.ToCheckDirty
return false
}
}
return false
}
set _dirty(v: boolean) {
if (v) {
this.flags |= SubscriberFlags.Dirty
} else {
this.flags &= ~SubscriberFlags.Dirtys
this.flags &= ~(SubscriberFlags.Dirty | SubscriberFlags.PendingComputed)
}
}

Expand All @@ -133,23 +126,20 @@ export class ComputedRefImpl<T = any> implements IComputed {
}

get value(): T {
if (this._dirty) {
this.update()
const flags = this.flags
if (flags & (SubscriberFlags.Dirty | SubscriberFlags.PendingComputed)) {
processComputedUpdate(this, flags)
}
if (activeTrackId !== 0 && this.lastTrackedId !== activeTrackId) {
if (activeSub !== undefined) {
if (__DEV__) {
onTrack(activeSub!, {
target: this,
type: TrackOpTypes.GET,
key: 'value',
})
}
this.lastTrackedId = activeTrackId
link(this, activeSub!).version = this.version
} else if (
activeEffectScope !== undefined &&
this.lastTrackedId !== activeEffectScope.trackId
) {
link(this, activeSub)
} else if (activeEffectScope !== undefined) {
link(this, activeEffectScope)
}
return this._value!
Expand All @@ -165,23 +155,20 @@ export class ComputedRefImpl<T = any> implements IComputed {

update(): boolean {
const prevSub = activeSub
const prevTrackId = activeTrackId
setActiveSub(this, nextTrackId())
startTrack(this)
const oldValue = this._value
let newValue: T
setActiveSub(this)
startTracking(this)
try {
newValue = this.fn(oldValue)
const oldValue = this._value
const newValue = this.fn(oldValue)
if (hasChanged(oldValue, newValue)) {
this._value = newValue
return true
}
return false
} finally {
setActiveSub(prevSub, prevTrackId)
endTrack(this)
setActiveSub(prevSub)
endTracking(this)
}
if (hasChanged(oldValue, newValue)) {
this._value = newValue
this.version++
return true
}
return false
}
}

Expand Down
17 changes: 8 additions & 9 deletions packages/reactivity/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,22 @@ export function setupOnTrigger(target: { new (...args: any[]): any }): void {
}

function setupFlagsHandler(target: Subscriber): void {
// @ts-expect-error
target._flags = target.flags
;(target as any)._flags = target.flags
Object.defineProperty(target, 'flags', {
get() {
// @ts-expect-error
return target._flags
return (target as any)._flags
},
set(value) {
if (
// @ts-expect-error
!(target._flags >> SubscriberFlags.DirtyFlagsIndex) &&
!!(value >> SubscriberFlags.DirtyFlagsIndex)
!(
(target as any)._flags &
(SubscriberFlags.PendingComputed | SubscriberFlags.Dirty)
) &&
!!(value & (SubscriberFlags.PendingComputed | SubscriberFlags.Dirty))
) {
onTrigger(this)
}
// @ts-expect-error
target._flags = value
;(target as any)._flags = value
},
})
}
22 changes: 9 additions & 13 deletions packages/reactivity/src/dep.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isArray, isIntegerKey, isMap, isSymbol } from '@vue/shared'
import { type TrackOpTypes, TriggerOpTypes } from './constants'
import { onTrack, triggerEventInfos } from './debug'
import { activeSub, activeTrackId } from './effect'
import { activeSub } from './effect'
import {
type Dependency,
type Link,
Expand All @@ -14,7 +14,6 @@ import {
class Dep implements Dependency {
_subs: Link | undefined = undefined
subsTail: Link | undefined = undefined
lastTrackedId = 0

constructor(
private map: KeyToDepMap,
Expand Down Expand Up @@ -62,7 +61,7 @@ export const ARRAY_ITERATE_KEY: unique symbol = Symbol(
* @param key - Identifier of the reactive property to track.
*/
export function track(target: object, type: TrackOpTypes, key: unknown): void {
if (activeTrackId > 0) {
if (activeSub !== undefined) {
let depsMap = targetMap.get(target)
if (!depsMap) {
targetMap.set(target, (depsMap = new Map()))
Expand All @@ -71,17 +70,14 @@ export function track(target: object, type: TrackOpTypes, key: unknown): void {
if (!dep) {
depsMap.set(key, (dep = new Dep(depsMap, key)))
}
if (dep.lastTrackedId !== activeTrackId) {
if (__DEV__) {
onTrack(activeSub!, {
target,
type,
key,
})
}
dep.lastTrackedId = activeTrackId
link(dep, activeSub!)
if (__DEV__) {
onTrack(activeSub!, {
target,
type,
key,
})
}
link(dep, activeSub!)
}
}

Expand Down
Loading
Loading