@dxyl/utils 1.3.1 → 1.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxyl/utils",
3
- "version": "1.3.1",
3
+ "version": "1.3.4",
4
4
  "description": "Collected many useful mathematical tools",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1,19 +1,23 @@
1
- export interface IDisposable {
1
+ interface IDispose {
2
+ dispose: () => void;
3
+ }
4
+ export interface IDisposable extends IDispose {
2
5
  isDisposed(): boolean;
3
- dispose(): void;
4
6
  disposeLater(): void;
5
7
  }
6
- type DPRegisterOptions = {
7
- dispose?: () => void;
8
+ type DPRegisterOptions<T> = {
9
+ dispose?: (obj: T) => void;
8
10
  };
9
- export declare const addDisposable: (target: IDisposable) => void;
11
+ export declare const addDisposable: (target: IDispose) => void;
10
12
  export declare class DisposableManager {
11
- static add: (target: IDisposable) => void;
12
- static mixin(target: any, options?: DPRegisterOptions): void;
13
+ static add: (target: IDispose) => void;
14
+ static mixin<T>(target: {
15
+ new (...args: any[]): T;
16
+ }, options?: DPRegisterOptions<T>): void;
13
17
  private disposables;
14
18
  private persistentDisposables;
15
- add(disposable: IDisposable): void;
16
- addPersistent(disposable: IDisposable): void;
19
+ add(disposable: IDispose): void;
20
+ addPersistent(disposable: IDispose): void;
17
21
  destroy(): void;
18
22
  dispose(): void;
19
23
  run(fn: () => void): void;
@@ -2,18 +2,20 @@ export type PluginServiceOPtions = {
2
2
  plugins?: IPlugin[];
3
3
  presets?: IPreset[];
4
4
  };
5
- export type PluginContext<HookList extends HookListType = any, HookMethods extends Record<string, IMethod> = any> = {
5
+ export type PluginContext<Ctx = any, HookList extends HookListType = any, HookMethods extends Record<string, IMethod> = any> = {
6
6
  pluginName: string;
7
+ ctx: Ctx;
7
8
  register<K extends keyof HookList>(hook: IHook<K, HookList[K]>): void;
8
9
  registerMethod<K extends keyof HookMethods>(name: K, fn?: IMethod<Parameters<HookMethods[K]>, ReturnType<HookMethods[K]>>): void;
9
- } & PluginMethods<HookMethods>;
10
+ } & PluginMethods<HookMethods> & PluginService<Ctx, HookList, HookMethods>;
10
11
  export type PluginMethods<HookMethods extends Record<string, IMethod> = any> = {
11
12
  [k in Exclude<keyof HookMethods, 'pluginName' | 'register' | 'registerMethod'>]: IMethod<Parameters<HookMethods[k]>, ReturnType<HookMethods[k]>>;
12
13
  };
13
- export type IPlugin<HookList extends HookListType = any, HookMethods extends Record<string, IMethod> = any> = {
14
+ export type IPlugin<Ctx = any, HookList extends HookListType = any, HookMethods extends Record<string, IMethod> = any> = {
14
15
  name: string;
15
16
  config?: any;
16
- apply: (api: PluginContext<HookList, HookMethods>, config?: any) => void;
17
+ apply: (api: PluginContext<Ctx, HookList, HookMethods>, config?: any) => void;
18
+ dispose?: (api: PluginContext<Ctx, HookList, HookMethods>) => void;
17
19
  };
18
20
  export type IPreset = Omit<IPlugin, 'apply'> & {
19
21
  apply: (api: PluginContext, config?: any) => ({
@@ -41,25 +43,27 @@ export type HookOpts<T> = {
41
43
  };
42
44
  export type IMethod<T extends any = any, R = any> = (...args: T extends Array<any> ? T : [T]) => R;
43
45
  export type HookListType = Record<string, any>;
44
- export declare class PluginService<HookList extends HookListType, HookMethods extends Record<string, IMethod> = any> {
46
+ export declare class PluginService<Ctx, HookList extends HookListType, HookMethods extends Record<string, IMethod> = any> {
45
47
  config?: PluginServiceOPtions;
46
48
  private hooks;
47
49
  private methods;
48
50
  private plugins;
49
51
  private extraPresets;
50
52
  private extraPlugins;
51
- constructor(config?: PluginServiceOPtions);
53
+ context: Ctx;
54
+ constructor(context: Ctx, config?: PluginServiceOPtions);
52
55
  initPresetsAndPlugins(config: PluginServiceOPtions): void;
53
56
  private resolvePresets;
54
57
  private resolvePlugins;
55
58
  private getApplyMethods;
56
59
  applyMethods<K extends Extract<keyof HookMethods, string>>(name: K, ...args: Parameters<HookMethods[K]> extends Array<any> ? Parameters<HookMethods[K]> : [Parameters<HookMethods[K]>]): ReturnType<HookMethods[K]>;
57
- private initPluginContext;
60
+ private getPluginContext;
58
61
  private initPreset;
59
62
  private initPlugin;
60
63
  registerPlugin(plugin: IPlugin | IPreset): void;
61
64
  register(hook: IHook): void;
62
65
  registerMethod<K>(name: string, fn?: IMethod): void;
63
66
  applyPlugins<K extends Extract<keyof HookList, string> = Extract<keyof HookList, string>, T = HookList[K]>(inOpts: HookOpts<K> | K): Promise<Exclude<T, void> extends never ? void : T>;
64
- destroy(): void;
67
+ uninstallPlugin(plugin: IPlugin | IPreset): void;
68
+ dispose(): void;
65
69
  }
@@ -0,0 +1,22 @@
1
+ import { ReactiveNode } from './system';
2
+ export declare function getActiveSub(): ReactiveNode | undefined;
3
+ export declare function setActiveSub(sub?: ReactiveNode): ReactiveNode;
4
+ export declare function getBatchDepth(): number;
5
+ export declare function startBatch(): void;
6
+ export declare function endBatch(): void;
7
+ export declare function isSignal(fn: () => void): boolean;
8
+ export declare function isComputed(fn: () => void): boolean;
9
+ export declare function isEffect(fn: () => void): boolean;
10
+ export declare function isEffectScope(fn: () => void): boolean;
11
+ export declare function signal<T>(): {
12
+ (): T | undefined;
13
+ (value: T | undefined): void;
14
+ };
15
+ export declare function signal<T>(initialValue: T): {
16
+ (): T;
17
+ (value: T): void;
18
+ };
19
+ export declare function computed<T>(getter: (previousValue?: T) => T): () => T;
20
+ export declare function effect(fn: () => void): () => void;
21
+ export declare function effectScope(fn: () => void): () => void;
22
+ export declare function trigger(fn: () => void): void;
@@ -0,0 +1,36 @@
1
+ export interface ReactiveNode {
2
+ deps?: Link;
3
+ depsTail?: Link;
4
+ subs?: Link;
5
+ subsTail?: Link;
6
+ flags: ReactiveFlags;
7
+ }
8
+ export interface Link {
9
+ version: number;
10
+ dep: ReactiveNode;
11
+ sub: ReactiveNode;
12
+ prevSub: Link | undefined;
13
+ nextSub: Link | undefined;
14
+ prevDep: Link | undefined;
15
+ nextDep: Link | undefined;
16
+ }
17
+ export declare const enum ReactiveFlags {
18
+ None = 0,
19
+ Mutable = 1,
20
+ Watching = 2,
21
+ RecursedCheck = 4,
22
+ Recursed = 8,
23
+ Dirty = 16,
24
+ Pending = 32
25
+ }
26
+ export declare function createReactiveSystem({ update, notify, unwatched, }: {
27
+ update(sub: ReactiveNode): boolean;
28
+ notify(sub: ReactiveNode): void;
29
+ unwatched(sub: ReactiveNode): void;
30
+ }): {
31
+ link: (dep: ReactiveNode, sub: ReactiveNode, version: number) => void;
32
+ unlink: (link: Link, sub?: ReactiveNode) => Link | undefined;
33
+ propagate: (link: Link) => void;
34
+ checkDirty: (link: Link, sub: ReactiveNode) => boolean;
35
+ shallowPropagate: (link: Link) => void;
36
+ };
@@ -1,8 +1,3 @@
1
- /**
2
- * @desc 信息
3
- * @ref preact-signals
4
-
5
- */
6
1
  declare const BRAND_SYMBOL: unique symbol;
7
2
  type Node = {
8
3
  _source: Signal;
@@ -51,14 +46,19 @@ declare class Signal<T = any> {
51
46
  _node?: Node;
52
47
  /** @internal */
53
48
  _targets?: Node;
54
- constructor(value?: T);
49
+ constructor(value?: T, options?: SignalOptions<T>);
55
50
  /** @internal */
56
51
  _refresh(): boolean;
57
52
  /** @internal */
58
53
  _subscribe(node: Node): void;
59
54
  /** @internal */
60
55
  _unsubscribe(node: Node): void;
56
+ /** @internal */
57
+ _watched?(this: Signal<T>): void;
58
+ /** @internal */
59
+ _unwatched?(this: Signal<T>): void;
61
60
  subscribe(fn: (value: T) => void): () => void;
61
+ name?: string;
62
62
  valueOf(): T;
63
63
  toString(): string;
64
64
  toJSON(): T;
@@ -67,26 +67,35 @@ declare class Signal<T = any> {
67
67
  get value(): T;
68
68
  set value(value: T);
69
69
  }
70
+ export interface SignalOptions<T = any> {
71
+ watched?: (this: Signal<T>) => void;
72
+ unwatched?: (this: Signal<T>) => void;
73
+ name?: string;
74
+ }
70
75
  /** @internal */
71
- declare function Signal(this: Signal, value?: unknown): void;
76
+ declare function Signal(this: Signal, value?: unknown, options?: SignalOptions): void;
72
77
  /**
73
78
  * Create a new plain signal.
74
79
  *
75
80
  * @param value The initial value for the signal.
76
81
  * @returns A new signal.
77
82
  */
78
- export declare function signal<T>(value: T): Signal<T>;
83
+ export declare function signal<T>(value: T, options?: SignalOptions<T>): Signal<T>;
79
84
  export declare function signal<T = undefined>(): Signal<T | undefined>;
85
+ /**
86
+ * The base class for computed signals.
87
+ */
80
88
  declare class Computed<T = any> extends Signal<T> {
81
89
  _fn: () => T;
82
90
  _sources?: Node;
83
91
  _globalVersion: number;
84
92
  _flags: number;
85
- constructor(fn: () => T);
93
+ constructor(fn: () => T, options?: SignalOptions<T>);
86
94
  _notify(): void;
87
95
  get value(): T;
88
96
  }
89
- declare function Computed(this: Computed, fn: () => unknown): void;
97
+ /** @internal */
98
+ declare function Computed(this: Computed, fn: () => unknown, options?: SignalOptions): void;
90
99
  declare namespace Computed {
91
100
  var prototype: Computed<any>;
92
101
  }
@@ -111,21 +120,32 @@ interface ReadonlySignal<T = any> {
111
120
  * @param fn The effect callback.
112
121
  * @returns A new read-only signal.
113
122
  */
114
- declare function computed<T>(fn: () => T): ReadonlySignal<T>;
115
- type EffectFn = () => void | (() => void);
123
+ declare function computed<T>(fn: () => T, options?: SignalOptions<T>): ReadonlySignal<T>;
124
+ type EffectFn = ((this: {
125
+ dispose: () => void;
126
+ }) => void | (() => void)) | (() => void | (() => void));
127
+ /**
128
+ * The base class for reactive effects.
129
+ */
116
130
  declare class Effect {
117
131
  _fn?: EffectFn;
118
132
  _cleanup?: () => void;
119
133
  _sources?: Node;
120
134
  _nextBatchedEffect?: Effect;
121
135
  _flags: number;
122
- constructor(fn: EffectFn);
136
+ name?: string;
137
+ constructor(fn: EffectFn, options?: EffectOptions);
123
138
  _callback(): void;
124
139
  _start(): () => void;
125
140
  _notify(): void;
126
141
  _dispose(): void;
142
+ dispose(): void;
127
143
  }
128
- declare function Effect(this: Effect, fn: EffectFn): void;
144
+ export interface EffectOptions {
145
+ name?: string;
146
+ }
147
+ /** @internal */
148
+ declare function Effect(this: Effect, fn: EffectFn, options?: EffectOptions): void;
129
149
  /**
130
150
  * Create an effect to run arbitrary code in response to signal changes.
131
151
  *
@@ -139,6 +159,5 @@ declare function Effect(this: Effect, fn: EffectFn): void;
139
159
  * @param fn The effect callback.
140
160
  * @returns A function for disposing the effect.
141
161
  */
142
- declare function effect(fn: EffectFn): () => void;
143
- export { computed, effect, batch, untracked, Signal };
144
- export type { ReadonlySignal };
162
+ declare function effect(fn: EffectFn, options?: EffectOptions): () => void;
163
+ export { computed, effect, batch, untracked, Signal, ReadonlySignal, Effect, Computed, };
package/types/index.d.ts CHANGED
@@ -19,6 +19,7 @@ export * as reactivity from './data/reactivity';
19
19
  export * as signals from './data/signals';
20
20
  export * as redux from './data/redux';
21
21
  export * as mobx from './data/mobx';
22
+ export * as alienSignals from './data/alien-signals';
22
23
  export * as tapable from './tapable';
23
24
  export { Options } from './Options';
24
25
  export { default as compose } from './compose';