Skip to content

Commit

Permalink
fix: type (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
yc-kanyun authored Dec 27, 2024
1 parent 2fa18dc commit a70f512
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
16 changes: 4 additions & 12 deletions packages/ccstate/src/core/__tests__/interceptor.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { expect, it, vi } from 'vitest';
import { computed, command, state } from '../signal/factory';
import type { CallbackFunc, Store, StoreInterceptor, StoreOptions } from '../../../types/core/store';
import type { CallbackFunc, SetArgs, Store, StoreInterceptor, StoreOptions } from '../../../types/core/store';
import { StoreImpl } from '../store/store';
import type { Command, Signal, Updater, State } from '../../../types/core/signal';
import type { Command, Signal, State } from '../../../types/core/signal';
import { delay } from 'signal-timers';

function createStoreForTest(options: StoreOptions): Store {
Expand Down Expand Up @@ -102,11 +102,7 @@ it('should intercept set', () => {

const store = createStoreForTest({
interceptor: {
set: <T, Args extends unknown[]>(
atom: State<T> | Command<T, Args>,
fn: () => T,
...args: Args | [T | Updater<T>]
) => {
set: <T, Args extends SetArgs<T, unknown[]>>(atom: State<T> | Command<T, Args>, fn: () => T, ...args: Args) => {
const ret = fn();
trace(atom, args, ret);
},
Expand All @@ -125,11 +121,7 @@ it('should intercept set hierarchy', () => {

const store = createStoreForTest({
interceptor: {
set: <T, Args extends unknown[]>(
atom: State<T> | Command<T, Args>,
fn: () => T,
...args: Args | [T | Updater<T>]
) => {
set: <T, Args extends SetArgs<T, unknown[]>>(atom: State<T> | Command<T, Args>, fn: () => T, ...args: Args) => {
const ret = fn();
trace(atom, args, ret);
},
Expand Down
11 changes: 6 additions & 5 deletions packages/ccstate/src/core/interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Command, Computed, Signal, State, Updater } from '../../types/core/signal';
import type { Command, Computed, Signal, State } from '../../types/core/signal';
import type {
CallbackFunc,
ComputedState,
Expand All @@ -8,6 +8,7 @@ import type {
InterceptorSet,
InterceptorSub,
InterceptorUnsub,
SetArgs,
} from '../../types/core/store';

type DataWithCalledState<T> =
Expand All @@ -19,11 +20,11 @@ type DataWithCalledState<T> =
data: T;
};

export function withSetInterceptor<T, Args extends unknown[]>(
export function withSetInterceptor<T, Args extends SetArgs<T, unknown[]>>(
fn: () => T | undefined,
writable$: State<T> | Command<T, Args>,
interceptor: InterceptorSet | undefined,
...args: [T | Updater<T>] | Args
...args: Args
): T | undefined {
if (!interceptor) {
return fn();
Expand All @@ -35,9 +36,9 @@ export function withSetInterceptor<T, Args extends unknown[]>(
return result.data;
};
if ('write' in writable$) {
interceptor(writable$, wrappedFn, ...(args as Args));
interceptor(writable$, wrappedFn, ...args);
} else {
interceptor(writable$, wrappedFn, args[0] as T | Updater<T>);
interceptor(writable$, wrappedFn, args[0]);
}

if (!result.called) {
Expand Down
20 changes: 14 additions & 6 deletions packages/ccstate/src/core/store/set.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import type { Command, State, Updater, Computed, Signal } from '../../../types/core/signal';
import type { Mutation, ReadComputed, StoreContext, StateState, StoreGet, StoreSet } from '../../../types/core/store';
import type {
Mutation,
ReadComputed,
StoreContext,
StateState,
StoreGet,
StoreSet,
SetArgs,
} from '../../../types/core/store';

function pushDirtyMarkers(signalState: StateState<unknown>, context: StoreContext, mutation: Mutation) {
let queue: Computed<unknown>[] = Array.from(signalState.mounted?.readDepts ?? []);
Expand Down Expand Up @@ -94,19 +102,19 @@ function innerSetState<T>(
return undefined;
}

export function set<T, Args extends unknown[]>(
export function set<T, Args extends SetArgs<T, unknown[]>>(
readComputed: ReadComputed,
writable$: State<T> | Command<T, Args>,
context: StoreContext,
mutation: Mutation,
...args: [T | Updater<T>] | Args
...args: Args
): undefined | T {
if ('read' in writable$) {
return;
}

if ('write' in writable$) {
return writable$.write(mutation.visitor, ...(args as Args));
return writable$.write(mutation.visitor, ...args);
}

innerSetState(readComputed, writable$, context, mutation, args[0]);
Expand Down Expand Up @@ -134,9 +142,9 @@ export function createMutation(context: StoreContext, get: StoreGet, set: StoreS
get: <T>(signal$: Signal<T>) => {
return get(signal$, context, mutation);
},
set: <T, Args extends unknown[]>(
set: <T, Args extends SetArgs<T, unknown[]>>(
signal$: State<T> | Command<T, Args>,
...args: [T | Updater<T>] | Args
...args: Args
): undefined | T => {
return set<T, Args>(signal$, context, ...args);
},
Expand Down
13 changes: 7 additions & 6 deletions packages/ccstate/src/core/store/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Command, Getter, Setter, Signal, State, Updater, Computed } from '../../../types/core/signal';
import type { Command, Getter, Setter, Signal, State, Computed } from '../../../types/core/signal';
import type {
StateMap,
Store,
Expand All @@ -12,6 +12,7 @@ import type {
SignalState,
StoreGet,
StoreSet,
SetArgs,
} from '../../../types/core/store';
import { evaluateComputed, tryGetCached } from '../signal/computed';
import { withComputedInterceptor, withGetInterceptor, withSetInterceptor } from '../interceptor';
Expand Down Expand Up @@ -89,10 +90,10 @@ const get: StoreGet = (signal, context, mutation) => {
);
};

const set: StoreSet = (<T, Args extends unknown[]>(
const set: StoreSet = <T, Args extends SetArgs<T, unknown[]>>(
atom: State<T> | Command<T, Args>,
context: StoreContext,
...args: [T | Updater<T>] | Args
...args: Args
): T | undefined => {
return withSetInterceptor<T, Args>(
() => {
Expand All @@ -110,7 +111,7 @@ const set: StoreSet = (<T, Args extends unknown[]>(
context.interceptor?.set,
...args,
);
}) as StoreSet;
};

export class StoreImpl implements Store {
protected readonly stateMap: StateMap = new WeakMap();
Expand All @@ -127,9 +128,9 @@ export class StoreImpl implements Store {
return get(atom, this.context);
};

set: Setter = <T, Args extends unknown[]>(
set: Setter = <T, Args extends SetArgs<T, unknown[]>>(
atom: State<T> | Command<T, Args>,
...args: [T | Updater<T>] | Args
...args: Args
): undefined | T => {
return set<T, Args>(atom, this.context, ...args);
};
Expand Down
8 changes: 5 additions & 3 deletions packages/ccstate/types/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type Subscribe = (
export type InterceptorGet = <T>(signal$: Signal<T>, fn: () => T) => void;
export interface InterceptorSet {
<T, Args extends unknown[]>(command$: Command<T, Args>, fn: () => T, ...args: Args): void;
<T>(value$: State<T>, fn: () => void, val: T | Updater<T>): void;
<T>(state$: State<T>, fn: () => void, val: T | Updater<T>): void;
}
export type InterceptorSub = <T>(signal$: Signal<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
export type InterceptorUnsub = <T>(signal$: Signal<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
Expand Down Expand Up @@ -83,10 +83,12 @@ export interface Mounted {
readDepts: Set<Computed<unknown>>;
}

export type StoreSet = <T, Args extends unknown[]>(
export type SetArgs<T, Args extends unknown[]> = [T | Updater<T>] | Args;

export type StoreSet = <T, Args extends SetArgs<T, unknown[]>>(
atom: State<T> | Command<T, Args>,
context: StoreContext,
...args: [T | Updater<T>] | Args
...args: Args
) => T | undefined;

export type StoreGet = <T>(signal: Signal<T>, context: StoreContext, mutation?: Mutation) => T;
Expand Down

0 comments on commit a70f512

Please sign in to comment.