-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
154 additions
and
18 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
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
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
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
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,57 @@ | ||
import { QSubscribers } from '../../util/markers'; | ||
import type { VNode } from '../client/types'; | ||
import { vnode_getProp } from '../client/vnode'; | ||
import { EffectSubscriptionsProp, isSignal2, type Signal2 } from './v2-signal'; | ||
|
||
export abstract class Subscriber { | ||
$dependencies$: Subscriber[] | null = null; | ||
} | ||
|
||
export function isSubscriber(value: unknown): value is Subscriber { | ||
return value instanceof Subscriber; | ||
} | ||
|
||
export function clearVNodeDependencies(value: VNode): void { | ||
const effects = vnode_getProp<Subscriber[]>(value, QSubscribers, null); | ||
if (!effects) { | ||
return; | ||
} | ||
for (let i = effects.length - 1; i >= 0; i--) { | ||
const subscriber = effects[i]; | ||
const subscriptionRemoved = clearSubscriptions(subscriber, value); | ||
if (subscriptionRemoved) { | ||
effects.splice(i, 1); | ||
} | ||
} | ||
} | ||
|
||
export function clearSubscriberDependencies(value: Subscriber): void { | ||
if (value.$dependencies$) { | ||
for (let i = value.$dependencies$.length - 1; i >= 0; i--) { | ||
const subscriber = value.$dependencies$[i]; | ||
const subscriptionRemoved = clearSubscriptions(subscriber, value); | ||
if (subscriptionRemoved) { | ||
value.$dependencies$.splice(i, 1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function clearSubscriptions(subscriber: Subscriber, value: Subscriber | VNode): boolean { | ||
if (!isSignal2(subscriber)) { | ||
return false; | ||
} | ||
const effectSubscriptions = (subscriber as Signal2<unknown>).$effects$; | ||
if (!effectSubscriptions) { | ||
return false; | ||
} | ||
let subscriptionRemoved = false; | ||
for (let i = effectSubscriptions.length - 1; i >= 0; i--) { | ||
const effect = effectSubscriptions[i]; | ||
if (effect[EffectSubscriptionsProp.EFFECT] === value) { | ||
effectSubscriptions.splice(i, 1); | ||
subscriptionRemoved = true; | ||
} | ||
} | ||
return subscriptionRemoved; | ||
} |