From 014ec13c2e961165be6198dbcede96b17c13a91d Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Fri, 10 Jan 2025 21:07:36 +0800 Subject: [PATCH] refactor: add "I" prefix to interfaces close #32 --- src/computed.ts | 10 +++--- src/effect.ts | 16 +++++----- src/effectScope.ts | 8 ++--- src/signal.ts | 8 ++--- src/system.ts | 58 +++++++++++++++++------------------ src/unstable/asyncComputed.ts | 4 +-- src/unstable/asyncEffect.ts | 4 +-- src/unstable/asyncSystem.ts | 6 ++-- 8 files changed, 57 insertions(+), 57 deletions(-) diff --git a/src/computed.ts b/src/computed.ts index fca42de..a897afe 100644 --- a/src/computed.ts +++ b/src/computed.ts @@ -1,6 +1,6 @@ import { activeSub, setActiveSub } from './effect.js'; import { activeEffectScope } from './effectScope.js'; -import { endTrack, IComputed, isDirty, Link, link, shallowPropagate, startTrack, SubscriberFlags } from './system.js'; +import { endTrack, IComputed, ILink, isDirty, link, shallowPropagate, startTrack, SubscriberFlags } from './system.js'; import type { ISignal } from './types.js'; export function computed(getter: (cachedValue?: T) => T): Computed { @@ -11,12 +11,12 @@ export class Computed implements IComputed, ISignal { currentValue: T | undefined = undefined; // Dependency - subs: Link | undefined = undefined; - subsTail: Link | undefined = undefined; + subs: ILink | undefined = undefined; + subsTail: ILink | undefined = undefined; // Subscriber - deps: Link | undefined = undefined; - depsTail: Link | undefined = undefined; + deps: ILink | undefined = undefined; + depsTail: ILink | undefined = undefined; flags: SubscriberFlags = SubscriberFlags.Dirty; constructor( diff --git a/src/effect.ts b/src/effect.ts index 3377381..c59f79b 100644 --- a/src/effect.ts +++ b/src/effect.ts @@ -1,7 +1,7 @@ import { activeEffectScope } from './effectScope.js'; -import { Dependency, endTrack, runInnerEffects, IEffect, isDirty, link, Link, startTrack, Subscriber, SubscriberFlags } from './system.js'; +import { IDependency, IEffect, ILink, ISubscriber, SubscriberFlags, endTrack, isDirty, link, runInnerEffects, startTrack } from './system.js'; -export let activeSub: Subscriber | undefined; +export let activeSub: ISubscriber | undefined; export function untrack(fn: () => T): T { const prevSub = activeSub; @@ -13,7 +13,7 @@ export function untrack(fn: () => T): T { } } -export function setActiveSub(sub: Subscriber | undefined): void { +export function setActiveSub(sub: ISubscriber | undefined): void { activeSub = sub; } @@ -23,16 +23,16 @@ export function effect(fn: () => T): Effect { return e; } -export class Effect implements IEffect, Dependency { +export class Effect implements IEffect, IDependency { nextNotify: IEffect | undefined = undefined; // Dependency - subs: Link | undefined = undefined; - subsTail: Link | undefined = undefined; + subs: ILink | undefined = undefined; + subsTail: ILink | undefined = undefined; // Subscriber - deps: Link | undefined = undefined; - depsTail: Link | undefined = undefined; + deps: ILink | undefined = undefined; + depsTail: ILink | undefined = undefined; flags: SubscriberFlags = SubscriberFlags.Dirty; constructor( diff --git a/src/effectScope.ts b/src/effectScope.ts index 412b34e..8d1d085 100644 --- a/src/effectScope.ts +++ b/src/effectScope.ts @@ -1,4 +1,4 @@ -import { endTrack, runInnerEffects, Link, startTrack, Subscriber, SubscriberFlags } from './system.js'; +import { endTrack, ILink, ISubscriber, runInnerEffects, startTrack, SubscriberFlags } from './system.js'; export let activeEffectScope: EffectScope | undefined = undefined; @@ -20,10 +20,10 @@ export function effectScope(): EffectScope { return new EffectScope(); } -export class EffectScope implements Subscriber { +export class EffectScope implements ISubscriber { // Subscriber - deps: Link | undefined = undefined; - depsTail: Link | undefined = undefined; + deps: ILink | undefined = undefined; + depsTail: ILink | undefined = undefined; flags: SubscriberFlags = SubscriberFlags.None; notify(): void { diff --git a/src/signal.ts b/src/signal.ts index 17d4a01..1bafd9c 100644 --- a/src/signal.ts +++ b/src/signal.ts @@ -1,6 +1,6 @@ import { batchDepth } from './batch.js'; import { activeSub } from './effect.js'; -import { Dependency, drainQueuedEffects, link, Link, propagate } from './system.js'; +import { IDependency, ILink, drainQueuedEffects, link, propagate } from './system.js'; import type { IWritableSignal } from './types.js'; export function signal(): Signal; @@ -9,10 +9,10 @@ export function signal(oldValue?: T): Signal { return new Signal(oldValue); } -export class Signal implements Dependency, IWritableSignal { +export class Signal implements IDependency, IWritableSignal { // Dependency - subs: Link | undefined = undefined; - subsTail: Link | undefined = undefined; + subs: ILink | undefined = undefined; + subsTail: ILink | undefined = undefined; constructor( public currentValue: T diff --git a/src/system.ts b/src/system.ts index 6ec1fe1..fffc9fa 100644 --- a/src/system.ts +++ b/src/system.ts @@ -1,32 +1,32 @@ -export interface IEffect extends Subscriber { +export interface IEffect extends ISubscriber { nextNotify: IEffect | undefined; notify(): void; } -export interface IComputed extends Dependency, Subscriber { +export interface IComputed extends IDependency, ISubscriber { update(): boolean; } -export interface Dependency { - subs: Link | undefined; - subsTail: Link | undefined; +export interface IDependency { + subs: ILink | undefined; + subsTail: ILink | undefined; } -export interface Subscriber { +export interface ISubscriber { flags: SubscriberFlags; - deps: Link | undefined; - depsTail: Link | undefined; + deps: ILink | undefined; + depsTail: ILink | undefined; } -export interface Link { - dep: Dependency | IComputed | (Dependency & IEffect); - sub: Subscriber | IComputed | (Dependency & IEffect) | IEffect; +export interface ILink { + dep: IDependency | IComputed | (IDependency & IEffect); + sub: ISubscriber | IComputed | (IDependency & IEffect) | IEffect; // Reuse to link prev stack in checkDirty // Reuse to link prev stack in propagate - prevSub: Link | undefined; - nextSub: Link | undefined; + prevSub: ILink | undefined; + nextSub: ILink | undefined; // Reuse to link next released link in linkPool - nextDep: Link | undefined; + nextDep: ILink | undefined; } export const enum SubscriberFlags { @@ -41,7 +41,7 @@ export const enum SubscriberFlags { let queuedEffects: IEffect | undefined; let queuedEffectsTail: IEffect | undefined; -let linkPool: Link | undefined; +let linkPool: ILink | undefined; export function drainQueuedEffects(): void { while (queuedEffects !== undefined) { @@ -58,7 +58,7 @@ export function drainQueuedEffects(): void { } } -export function link(dep: Dependency, sub: Subscriber): void { +export function link(dep: IDependency, sub: ISubscriber): void { const currentDep = sub.depsTail; if ( currentDep !== undefined @@ -87,8 +87,8 @@ export function link(dep: Dependency, sub: Subscriber): void { linkNewDep(dep, sub, nextDep, currentDep); } -function linkNewDep(dep: Dependency, sub: Subscriber, nextDep: Link | undefined, depsTail: Link | undefined): void { - let newLink: Link; +function linkNewDep(dep: IDependency, sub: ISubscriber, nextDep: ILink | undefined, depsTail: ILink | undefined): void { + let newLink: ILink; if (linkPool !== undefined) { newLink = linkPool; @@ -125,7 +125,7 @@ function linkNewDep(dep: Dependency, sub: Subscriber, nextDep: Link | undefined, } // See https://github.com/stackblitz/alien-signals#about-propagate-and-checkdirty-functions -export function propagate(link: Link): void { +export function propagate(link: ILink): void { let targetFlag = SubscriberFlags.Dirty; let subs = link; let stack = 0; @@ -149,11 +149,11 @@ export function propagate(link: Link): void { && isValidLink(link, sub) && ( sub.flags = subFlags | SubscriberFlags.Recursed | targetFlag, - (sub as Dependency).subs !== undefined + (sub as IDependency).subs !== undefined ) ) ) { - const subSubs = (sub as Dependency).subs; + const subSubs = (sub as IDependency).subs; if (subSubs !== undefined) { if (subSubs.nextSub !== undefined) { subSubs.prevSub = subs; @@ -214,7 +214,7 @@ export function propagate(link: Link): void { } while (true); } -export function shallowPropagate(link: Link): void { +export function shallowPropagate(link: ILink): void { do { const updateSub = link.sub; const updateSubFlags = updateSub.flags; @@ -225,7 +225,7 @@ export function shallowPropagate(link: Link): void { } while (link !== undefined); } -function isValidLink(subLink: Link, sub: Subscriber): boolean { +function isValidLink(subLink: ILink, sub: ISubscriber): boolean { const depsTail = sub.depsTail; if (depsTail !== undefined) { let link = sub.deps!; @@ -243,7 +243,7 @@ function isValidLink(subLink: Link, sub: Subscriber): boolean { } // See https://github.com/stackblitz/alien-signals#about-propagate-and-checkdirty-functions -export function checkDirty(link: Link): boolean { +export function checkDirty(link: ILink): boolean { let stack = 0; let dirty: boolean; @@ -320,12 +320,12 @@ export function checkDirty(link: Link): boolean { } while (true); } -export function startTrack(sub: Subscriber): void { +export function startTrack(sub: ISubscriber): void { sub.depsTail = undefined; sub.flags = (sub.flags & ~(SubscriberFlags.Recursed | SubscriberFlags.Notified)) | SubscriberFlags.Tracking; } -export function endTrack(sub: Subscriber): void { +export function endTrack(sub: ISubscriber): void { const depsTail = sub.depsTail; if (depsTail !== undefined) { const nextDep = depsTail.nextDep; @@ -340,7 +340,7 @@ export function endTrack(sub: Subscriber): void { sub.flags &= ~SubscriberFlags.Tracking; } -function clearTrack(link: Link): void { +function clearTrack(link: ILink): void { do { const dep = link.dep; const nextDep = link.nextDep; @@ -386,7 +386,7 @@ function clearTrack(link: Link): void { } while (link !== undefined); } -export function isDirty(sub: Subscriber, flags: SubscriberFlags): boolean { +export function isDirty(sub: ISubscriber, flags: SubscriberFlags): boolean { if (flags & SubscriberFlags.Dirty) { return true; } else if (flags & SubscriberFlags.ToCheckDirty) { @@ -400,7 +400,7 @@ export function isDirty(sub: Subscriber, flags: SubscriberFlags): boolean { return false; } -export function runInnerEffects(link: Link): void { +export function runInnerEffects(link: ILink): void { do { const dep = link.dep; if ('notify' in dep) { diff --git a/src/unstable/asyncComputed.ts b/src/unstable/asyncComputed.ts index 4532071..39cab36 100644 --- a/src/unstable/asyncComputed.ts +++ b/src/unstable/asyncComputed.ts @@ -1,8 +1,8 @@ import { Computed } from '../computed.js'; -import { Dependency, endTrack, link, shallowPropagate, startTrack, SubscriberFlags } from '../system.js'; +import { endTrack, IDependency, link, shallowPropagate, startTrack, SubscriberFlags } from '../system.js'; import { asyncCheckDirty } from './asyncSystem.js'; -export function asyncComputed(getter: (cachedValue?: T) => AsyncGenerator): AsyncComputed { +export function asyncComputed(getter: (cachedValue?: T) => AsyncGenerator): AsyncComputed { return new AsyncComputed(getter); } diff --git a/src/unstable/asyncEffect.ts b/src/unstable/asyncEffect.ts index 6a3b3ce..f4d8756 100644 --- a/src/unstable/asyncEffect.ts +++ b/src/unstable/asyncEffect.ts @@ -1,8 +1,8 @@ import { Effect } from '../effect.js'; -import { Dependency, endTrack, link, startTrack, SubscriberFlags } from '../system.js'; +import { endTrack, IDependency, link, startTrack, SubscriberFlags } from '../system.js'; import { asyncCheckDirty } from './asyncSystem.js'; -export function asyncEffect(fn: () => AsyncGenerator): AsyncEffect { +export function asyncEffect(fn: () => AsyncGenerator): AsyncEffect { const e = new AsyncEffect(fn); e.run(); return e; diff --git a/src/unstable/asyncSystem.ts b/src/unstable/asyncSystem.ts index 7a9771d..177a702 100644 --- a/src/unstable/asyncSystem.ts +++ b/src/unstable/asyncSystem.ts @@ -1,9 +1,9 @@ -import { IComputed, Link, shallowPropagate, SubscriberFlags } from "../system"; +import { IComputed, ILink, shallowPropagate, SubscriberFlags } from "../system"; -export async function asyncCheckDirty(link: Link): Promise { +export async function asyncCheckDirty(link: ILink): Promise { let stack = 0; let dirty: boolean; - let nextDep: Link | undefined; + let nextDep: ILink | undefined; top: do { dirty = false;