Skip to content

Commit

Permalink
refactor: add "I" prefix to interfaces
Browse files Browse the repository at this point in the history
close #32
  • Loading branch information
johnsoncodehk committed Jan 10, 2025
1 parent 62e637b commit 014ec13
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 57 deletions.
10 changes: 5 additions & 5 deletions src/computed.ts
Original file line number Diff line number Diff line change
@@ -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<T>(getter: (cachedValue?: T) => T): Computed<T> {
Expand All @@ -11,12 +11,12 @@ export class Computed<T = any> implements IComputed, ISignal<T> {
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(
Expand Down
16 changes: 8 additions & 8 deletions src/effect.ts
Original file line number Diff line number Diff line change
@@ -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<T>(fn: () => T): T {
const prevSub = activeSub;
Expand All @@ -13,7 +13,7 @@ export function untrack<T>(fn: () => T): T {
}
}

export function setActiveSub(sub: Subscriber | undefined): void {
export function setActiveSub(sub: ISubscriber | undefined): void {
activeSub = sub;
}

Expand All @@ -23,16 +23,16 @@ export function effect<T>(fn: () => T): Effect<T> {
return e;
}

export class Effect<T = any> implements IEffect, Dependency {
export class Effect<T = any> 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(
Expand Down
8 changes: 4 additions & 4 deletions src/effectScope.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions src/signal.ts
Original file line number Diff line number Diff line change
@@ -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<T>(): Signal<T | undefined>;
Expand All @@ -9,10 +9,10 @@ export function signal<T>(oldValue?: T): Signal<T | undefined> {
return new Signal(oldValue);
}

export class Signal<T = any> implements Dependency, IWritableSignal<T> {
export class Signal<T = any> implements IDependency, IWritableSignal<T> {
// Dependency
subs: Link | undefined = undefined;
subsTail: Link | undefined = undefined;
subs: ILink | undefined = undefined;
subsTail: ILink | undefined = undefined;

constructor(
public currentValue: T
Expand Down
58 changes: 29 additions & 29 deletions src/system.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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!;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/unstable/asyncComputed.ts
Original file line number Diff line number Diff line change
@@ -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<T>(getter: (cachedValue?: T) => AsyncGenerator<Dependency, T>): AsyncComputed<T> {
export function asyncComputed<T>(getter: (cachedValue?: T) => AsyncGenerator<IDependency, T>): AsyncComputed<T> {
return new AsyncComputed<T>(getter);
}

Expand Down
4 changes: 2 additions & 2 deletions src/unstable/asyncEffect.ts
Original file line number Diff line number Diff line change
@@ -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<T>(fn: () => AsyncGenerator<Dependency, T>): AsyncEffect<T> {
export function asyncEffect<T>(fn: () => AsyncGenerator<IDependency, T>): AsyncEffect<T> {
const e = new AsyncEffect(fn);
e.run();
return e;
Expand Down
6 changes: 3 additions & 3 deletions src/unstable/asyncSystem.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
export async function asyncCheckDirty(link: ILink): Promise<boolean> {
let stack = 0;
let dirty: boolean;
let nextDep: Link | undefined;
let nextDep: ILink | undefined;

top: do {
dirty = false;
Expand Down

2 comments on commit 014ec13

@medz
Copy link
Contributor

@medz medz commented on 014ec13 Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂 This is not a good idea

@johnsoncodehk
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@medz I'll revisit it before releasing 1.0.

Please sign in to comment.