Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
76 changes: 38 additions & 38 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
type General = any;
type GeneralObject = object;
type GeneralConstructor = new (...args: Array<General>) => General;
type ModuleConstructor<C extends object> = new (context: C) => General;

type GeneralModuleInput = ReadonlyArray<GeneralConstructor> | ReadonlyArray<GeneralObject>;
type GeneralModuleInput = ReadonlyArray<GeneralConstructor> | ReadonlyArray<object>;

type IsPlainObject<T> = T extends object
? T extends Function | Date | RegExp | Array<any> | Map<any, any> | Set<any>
Expand All @@ -14,7 +13,7 @@ type IsPlainObject<T> = T extends object
type ShallowMerge<A, B> =
IsPlainObject<A> extends true ? (IsPlainObject<B> extends true ? Omit<A, keyof B> & B : B) : B;

type Keys<T> = T extends any ? keyof T : never;
type Keys<T> = T extends General ? keyof T : never;

type InstanceEach<T extends GeneralModuleInput> =
T extends ReadonlyArray<GeneralConstructor> ? { [K in keyof T]: InstanceType<T[K]> } : T;
Expand Down Expand Up @@ -63,13 +62,15 @@ type MergeResult<
> = MergeObjects<[Pr, ...PickEach<InstanceEach<M>, K>, Po]>;

export type Context<
M extends ReadonlyArray<ModuleConstructor<General>>,
M extends GeneralModuleInput,
K extends Keys<InstanceEach<M>[number]>,
Pr extends object = {},
Po extends object = {},
> = MergeResult<M, K, Pr, Po> & {
__modules__: WeakMap<M[number], InstanceType<M[number]>>;
__getModule__: <C extends M[number]>(ctor: C) => InstanceType<C>;
__modules__: WeakMap<M[number], InstanceEach<M>[number]>;
__getModule__: <C extends new (ctx: General) => InstanceEach<M>[number]>(
ctor: C,
) => InstanceType<C>;
__addModule__: <N extends ModuleConstructor<Context<[...M, N], K, Pr, Po>>>(
newModule: N,
) => Context<[...M, N], K, Pr, Po>;
Expand Down
6 changes: 6 additions & 0 deletions src/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type Ref<T> = {
(newValue: T): void;
subscribe(func: RefMatchingFunc<T>, options?: { immediate?: boolean }): () => void;
unsubscribe(func: RefMatchingFunc<T>): void;
clear(): void;
};
type RefOptions<T> = { equals?: (newValue: T, oldValue: T) => boolean };

Expand All @@ -28,6 +29,7 @@ export function ref<T>(initial: T, options?: RefOptions<T>): Ref<T> {
if (ops?.immediate) callback(value, value);
return () => result.unsubscribe(callback);
};
result.clear = subs.clear;
result.unsubscribe = (callback) => subs.delete(callback);
return result;
}
Expand All @@ -38,6 +40,7 @@ export type Hook<Args extends GeneralArray = []> = {
(...args: Args): void;
subscribe(callback: HookMatchingFunc<Args>): () => void;
unsubscribe(callback: HookMatchingFunc<Args>): void;
clear(): void;
};

export function hook<Args extends GeneralArray = []>(): Hook<Args> {
Expand All @@ -50,6 +53,7 @@ export function hook<Args extends GeneralArray = []>(): Hook<Args> {
return () => result.unsubscribe(callback);
};
result.unsubscribe = (callback) => subs.delete(callback);
result.clear = subs.clear;
return result;
}

Expand All @@ -60,6 +64,7 @@ export type Computed<T> = {
subscribe(func: RefMatchingFunc<T>, options?: { immediate?: boolean }): () => void;
unsubscribe(func: RefMatchingFunc<T>): void;
dispose(): void;
clear(): void;
};
type ComputedOptions<T> = {
equals?: (newValue: T, oldValue: T) => boolean;
Expand Down Expand Up @@ -103,5 +108,6 @@ export function computed<T>(getter: () => T, options?: ComputedOptions<T>): Comp
result.dispose = () => {
while (cleanup.length) cleanup.pop()!();
};
result.clear = subs.clear;
return result;
}