feat: 支持了watcher的lazy以及timer和watcher的free形式

This commit is contained in:
Pan Qiancheng 2026-01-19 14:45:19 +08:00
parent 3331e6d66e
commit 89a2b0f414
5 changed files with 57 additions and 6 deletions

13
lib/types/Timer.d.ts vendored
View File

@ -8,19 +8,28 @@ type FreeRoutineFn<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncCont
socket: Namespace;
contextBuilder: () => Promise<Cxt>;
}) => Promise<OperationResult<ED>>;
type FreeTimerFn<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = (context: Cxt) => Promise<OperationResult<ED>>;
type BaseTimerFn<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = (context: Cxt) => Promise<OperationResult<ED>>;
type FreeTimerFn<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = (builder: () => Promise<Cxt>) => Promise<OperationResult<ED>>;
export type FreeRoutine<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = {
name: string;
routine: FreeRoutineFn<ED, Cxt>;
};
export type BaseTimer<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = {
type?: 'base';
name: string;
cron: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
timer: BaseTimerFn<ED, Cxt>;
singleton?: true;
};
export type FreeTimer<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = {
type: 'free';
name: string;
cron: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
timer: FreeTimerFn<ED, Cxt>;
singleton?: true;
};
export type Routine<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> = FreeRoutine<ED, Cxt> | Watcher<ED, T, Cxt>;
export type Timer<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> = FreeTimer<ED, Cxt> | Watcher<ED, T, Cxt> & {
export type Timer<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> = BaseTimer<ED, Cxt> | FreeTimer<ED, Cxt> | Watcher<ED, T, Cxt> & {
cron: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
};
export {};

View File

@ -4,14 +4,17 @@ import { EntityDict, OperationResult } from "./Entity";
import { EntityDict as BaseEntityDict } from '../base-app-domain';
type ActionData<ED extends EntityDict & BaseEntityDict, T extends keyof ED> = ED[T]['Update']['data'] | ED[T]['Remove']['data'];
export interface BBWatcher<ED extends EntityDict & BaseEntityDict, T extends keyof ED> {
type?: 'base';
name: string;
entity: T;
filter: ED[T]['Filter'] | (() => ED[T]['Filter']);
action: Omit<ED[T]['Action'], 'create' | ReadOnlyAction>;
actionData: ActionData<ED, T> | (() => Promise<ActionData<ED, T>>);
singleton?: true;
lazy?: true;
}
export interface WBWatcher<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> {
type?: 'base';
name: string;
entity: T;
filter: ED[T]['Filter'] | (() => Promise<ED[T]['Filter']>);
@ -19,6 +22,18 @@ export interface WBWatcher<ED extends EntityDict & BaseEntityDict, T extends key
fn: (context: Cxt, data: Partial<ED[T]['Schema']>[]) => Promise<OperationResult<ED>>;
forUpdate?: true;
singleton?: true;
lazy?: true;
}
export type Watcher<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> = BBWatcher<ED, T> | WBWatcher<ED, T, Cxt>;
export interface WBFreeWatcher<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> {
type: 'free';
name: string;
entity: T;
filter: ED[T]['Filter'] | (() => Promise<ED[T]['Filter']>);
projection: ED[T]['Projection'] | (() => Promise<ED[T]['Projection']>);
fn: (builder: () => Promise<Cxt>, data: Partial<ED[T]['Schema']>[]) => Promise<OperationResult<ED>>;
forUpdate?: true;
singleton?: true;
lazy?: true;
}
export type Watcher<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> = BBWatcher<ED, T> | WBWatcher<ED, T, Cxt> | WBFreeWatcher<ED, T, Cxt>;
export {};

View File

@ -2,3 +2,4 @@
Object.defineProperty(exports, "__esModule", { value: true });
;
;
;

View File

@ -13,14 +13,24 @@ type FreeRoutineFn<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncCont
}
) => Promise<OperationResult<ED>>;
type FreeTimerFn<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = (context: Cxt) => Promise<OperationResult<ED>>;
type BaseTimerFn<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = (context: Cxt) => Promise<OperationResult<ED>>;
type FreeTimerFn<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = (builder: () => Promise<Cxt>) => Promise<OperationResult<ED>>;
export type FreeRoutine<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = {
name: string;
routine: FreeRoutineFn<ED, Cxt>;
};
export type BaseTimer<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = {
type?: 'base';
name: string;
cron: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
timer: BaseTimerFn<ED, Cxt>;
singleton?: true; // 置singleton意味着在集群环境中只有一个进程会去执行
};
export type FreeTimer<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = {
type: 'free';
name: string;
cron: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
timer: FreeTimerFn<ED, Cxt>;
@ -28,6 +38,6 @@ export type FreeTimer<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncC
};
export type Routine<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> = FreeRoutine<ED, Cxt> | Watcher<ED, T, Cxt>;
export type Timer<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> = FreeTimer<ED, Cxt> | Watcher<ED, T, Cxt> & {
export type Timer<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> = BaseTimer<ED, Cxt> | FreeTimer<ED, Cxt> | Watcher<ED, T, Cxt> & {
cron: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
};

View File

@ -7,15 +7,18 @@ import { EntityDict as BaseEntityDict } from '../base-app-domain';
type ActionData<ED extends EntityDict & BaseEntityDict, T extends keyof ED> = ED[T]['Update']['data'] | ED[T]['Remove']['data'];
export interface BBWatcher<ED extends EntityDict & BaseEntityDict, T extends keyof ED> {
type?: 'base';
name: string;
entity: T;
filter: ED[T]['Filter'] | (() => ED[T]['Filter']);
action: Omit<ED[T]['Action'], 'create' | ReadOnlyAction>;
actionData: ActionData<ED, T> | (() => Promise<ActionData<ED, T>>);
singleton?: true; // 置singleton意味着在集群环境中只有一个进程会去执行
lazy?: true; // 置lazy意味着在启动时不会立即执行而是等到第一次调度时才执行
};
export interface WBWatcher<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> {
type?: 'base';
name: string;
entity: T;
filter: ED[T]['Filter'] | (() => Promise<ED[T]['Filter']>);
@ -23,6 +26,19 @@ export interface WBWatcher<ED extends EntityDict & BaseEntityDict, T extends key
fn: (context: Cxt, data: Partial<ED[T]['Schema']>[]) => Promise<OperationResult<ED>>;
forUpdate?: true;
singleton?: true; // 置singleton意味着在集群环境中只有一个进程会去执行
lazy?: true; // 置lazy意味着在启动时不会立即执行而是等到第一次调度时才执行
};
export type Watcher<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> = BBWatcher<ED, T> | WBWatcher<ED, T, Cxt>;
export interface WBFreeWatcher<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> {
type: 'free';
name: string;
entity: T;
filter: ED[T]['Filter'] | (() => Promise<ED[T]['Filter']>);
projection: ED[T]['Projection'] | (() => Promise<ED[T]['Projection']>);
fn: (builder: () => Promise<Cxt>, data: Partial<ED[T]['Schema']>[]) => Promise<OperationResult<ED>>;
forUpdate?: true;
singleton?: true; // 置singleton意味着在集群环境中只有一个进程会去执行
lazy?: true; // 置lazy意味着在启动时不会立即执行而是等到第一次调度时才执行
};
export type Watcher<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> = BBWatcher<ED, T> | WBWatcher<ED, T, Cxt> | WBFreeWatcher<ED, T, Cxt>;