as-model 0.2.8 → 0.3.0

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.
Files changed (2) hide show
  1. package/index.d.ts +345 -325
  2. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1,325 +1,345 @@
1
- declare interface ModelInstance {
2
- [key: string]: any;
3
- [key: number]: any;
4
- }
5
-
6
- export declare type FieldStructure<R = any> = {
7
- callback: () => R;
8
- deps: any[] | undefined;
9
- identifier: (d: any) => d is FieldStructure<R>;
10
- value: R;
11
- get: () => R;
12
- };
13
-
14
- export declare type MethodStructure<
15
- R extends (...args: any[]) => any = (...args: any[]) => any
16
- > = R & {
17
- identifier: (d: any) => d is MethodStructure;
18
- };
19
-
20
- declare type ValidInstance<S, T extends ModelInstance> = {
21
- [K in keyof T]: T[K] extends
22
- | ((...args: any[]) => S)
23
- | (((...args: any[]) => any) & { identifier: (d: any) => boolean })
24
- ? T[K]
25
- : T[K] extends (...args: any[]) => any
26
- ? never
27
- : T[K];
28
- };
29
-
30
- export declare type Model<S, T extends ModelInstance> = (
31
- state: S
32
- ) => ValidInstance<S, T>;
33
-
34
- export declare type Action<S = any, T extends ModelInstance = ModelInstance> = {
35
- type: null | string;
36
- method: null | ((...args: any[]) => any);
37
- params?: any[];
38
- state: S;
39
- prevState?: S;
40
- instance: T;
41
- prevInstance?: T;
42
- };
43
-
44
- export interface Token {
45
- isDifferent: (token: Token) => boolean;
46
- value: unknown;
47
- }
48
-
49
- export declare type Dispatch = (action: Action) => any;
50
-
51
- export declare interface Key<
52
- S = any,
53
- T extends ModelInstance = any,
54
- R extends (instance: () => T) => any = (instance: () => T) => T
55
- > extends Model<S, T> {
56
- (s: S): T;
57
- source: Model<S, T>;
58
- selector: R;
59
- modelKeyIdentifier: () => boolean;
60
- [k: string]: any;
61
- defaultState?: S;
62
- }
63
-
64
- declare interface UpdaterStore<
65
- S = any,
66
- T extends ModelInstance = ModelInstance
67
- > {
68
- getState: () => { state: S; instance: T };
69
- dispatch: (action: Action) => void;
70
- }
71
-
72
- export declare type MiddleWare = (
73
- store: UpdaterStore
74
- ) => (next: Dispatch) => (action: Action) => void;
75
-
76
- export declare interface Config {
77
- middleWares?: MiddleWare[];
78
- controlled?: boolean;
79
- notify?: (
80
- notifier: (action: Action) => { errors: any[] | undefined },
81
- action: Action
82
- ) => any;
83
- }
84
-
85
- /** createStore * */
86
-
87
- export declare interface StoreIndex<
88
- S = any,
89
- T extends ModelInstance = any,
90
- R extends (instance: () => T) => any = (instance: () => T) => T
91
- > {
92
- key: Key<S, T, R>;
93
- }
94
-
95
- export declare interface Store<
96
- S = any,
97
- T extends ModelInstance = any,
98
- R extends (instance: () => T) => any = (instance: () => T) => T
99
- > extends StoreIndex<S, T, R> {
100
- subscribe: (dispatcher: Dispatch) => () => void;
101
- getToken: () => Token;
102
- getInstance: () => ReturnType<R>;
103
- getStoreInstance: () => T;
104
- update: (args?: {
105
- model?: Model<S, T>;
106
- key?: Key<S, T, R>;
107
- initialState?: S;
108
- state?: S;
109
- }) => void;
110
- destroy: () => void;
111
- payload: <P>(
112
- callback?: (payload: P | undefined) => P | undefined
113
- ) => P | undefined;
114
- isDestroyed: () => boolean;
115
- extends: <E extends Record<string, any>>(e: E) => Store<S, T, R> & E;
116
- }
117
-
118
- export declare function createStore<
119
- S,
120
- T extends ModelInstance,
121
- D extends S,
122
- R extends (instance: () => T) => any = (instance: () => T) => T
123
- >(
124
- model: Model<S, T> | Key<S, T, R> | ModelUsage<S, T, R>,
125
- state?: D
126
- ): Store<S, T, R>;
127
-
128
- /** createKey * */
129
-
130
- export declare interface ModelKey<
131
- S = any,
132
- T extends ModelInstance = any,
133
- R extends (instance: () => T) => any = (instance: () => T) => T
134
- > extends Key<S, T, R> {
135
- (s: S): T;
136
- createStore: <D extends S>(state?: D) => Store<S, T, R>;
137
- extends: <E extends Record<string, any>>(e: E) => ModelKey<S, T, R> & E;
138
- }
139
-
140
- export declare function createKey<
141
- S,
142
- T extends ModelInstance,
143
- D extends S,
144
- R extends (instance: () => T) => any = (instance: () => T) => T
145
- >(model: Model<S, T> | ModelUsage<S, T, R>, state?: D): ModelKey<S, T, R>;
146
-
147
- /** createStores * */
148
-
149
- export declare interface StoreCollection {
150
- find: <
151
- S,
152
- T extends ModelInstance,
153
- R extends (instance: () => T) => any = (instance: () => T) => T
154
- >(
155
- key: Key<S, T, R> | StoreIndex<S, T, R>
156
- ) => Store<S, T, R> | null;
157
- update: (...keys: (ModelKey | StoreIndex)[]) => void;
158
- keys: () => Key[];
159
- destroy: () => void;
160
- }
161
-
162
- export declare function createStores(
163
- ...modelKeys: (ModelKey | StoreIndex)[]
164
- ): StoreCollection;
165
-
166
- /** model API * */
167
-
168
- export declare interface ModelUsage<
169
- S,
170
- T extends ModelInstance,
171
- R extends (instance: () => T) => any = (instance: () => T) => T
172
- > {
173
- (s: S): ValidInstance<S, T>;
174
- createKey: <D extends S>(state?: D) => ModelKey<S, T, R>;
175
- createStore: <D extends S>(state?: D) => Store<S, T, R>;
176
- produce: <C extends (instance: () => T) => any = (instance: () => T) => T>(
177
- s: C
178
- ) => ModelUsage<S, T, C>;
179
- wrapper: R;
180
- extends: <E extends Record<string, any>>(e: E) => ModelUsage<S, T, R> & E;
181
- }
182
-
183
- // eslint-disable-next-line @typescript-eslint/naming-convention
184
- export declare interface model {
185
- <
186
- S,
187
- T extends ModelInstance,
188
- R extends (instance: () => T) => any = (instance: () => T) => T
189
- >(
190
- modelFn: Model<S, T>,
191
- s?: R
192
- ): ModelUsage<S, T, R>;
193
- createField: <P extends () => any>(
194
- callback: P,
195
- deps?: any[]
196
- ) => FieldStructure<ReturnType<P>>;
197
- createMethod: <P extends (...args: any[]) => any = (...args: any[]) => any>(
198
- method: P
199
- ) => MethodStructure<P>;
200
- }
201
-
202
- /** createSignal * */
203
-
204
- export interface SignalOptions {
205
- cutOff?: boolean;
206
- }
207
-
208
- // eslint-disable-next-line @typescript-eslint/no-redeclare
209
- declare interface SignalStore<
210
- S = any,
211
- T extends ModelInstance = any,
212
- R extends (instance: () => T) => any = (instance: () => T) => any
213
- > extends StoreIndex<S, T, R> {
214
- getToken: () => Token;
215
- subscribe: (dispatcher: Dispatch) => () => void;
216
- getSignal: () => {
217
- (options?: SignalOptions): ReturnType<R>;
218
- startStatistics: () => void;
219
- stopStatistics: () => void;
220
- subscribe: (dispatcher: Dispatch) => () => void;
221
- store: Store<S, T, R>;
222
- };
223
- }
224
-
225
- export declare function createSignal<
226
- S,
227
- T extends ModelInstance,
228
- R extends (instance: () => T) => any = (instance: () => T) => T
229
- >(store: Store<S, T, R>): SignalStore<S, T, R>;
230
-
231
- /** createSelector * */
232
-
233
- declare type SelectMethod<
234
- T extends ModelInstance = any,
235
- R extends (instance: () => T) => any = (instance: () => T) => T
236
- > = {
237
- (): ReturnType<R>;
238
- <C extends (instance: () => ReturnType<R>) => any>(
239
- selector: C
240
- ): ReturnType<C>;
241
- <C extends (instance: () => ReturnType<R>) => any>(
242
- selector?: C
243
- ): ReturnType<R> | ReturnType<C>;
244
- };
245
-
246
- // eslint-disable-next-line @typescript-eslint/no-redeclare
247
- declare interface SelectorStore<
248
- S = any,
249
- T extends ModelInstance = any,
250
- R extends (instance: () => T) => any = (instance: () => T) => T
251
- > extends StoreIndex<S, T, R> {
252
- getToken: () => Token;
253
- subscribe: (dispatcher: Dispatch) => () => void;
254
- select: SelectMethod<T, R>;
255
- }
256
-
257
- declare interface SelectorOption<T = any> {
258
- equality?: (current: T, next: T) => boolean;
259
- }
260
-
261
- export declare function createSelector<
262
- S,
263
- T extends ModelInstance,
264
- R extends (instance: () => T) => any = (instance: () => T) => T
265
- >(store: Store<S, T, R>, opts?: SelectorOption): SelectorStore<S, T, R>;
266
-
267
- /** config * */
268
- export declare function config(configuration: Config): {
269
- createStore: <
270
- S,
271
- T extends ModelInstance,
272
- D extends S,
273
- R extends (instance: () => T) => any = (instance: () => T) => T
274
- >(
275
- model: Model<S, T> | Key<S, T, R> | ModelUsage<S, T, R>,
276
- state?: D
277
- ) => Store<S, T, R>;
278
- createKey: <
279
- S,
280
- T extends ModelInstance,
281
- D extends S,
282
- R extends (instance: () => T) => any = (instance: () => T) => T
283
- >(
284
- model: Model<S, T> | ModelUsage<S, T, R>,
285
- state?: D
286
- ) => ModelKey<S, T, R>;
287
- createStores: (...modelKeys: (ModelKey | StoreIndex)[]) => StoreCollection;
288
- model: model;
289
- };
290
-
291
- /** validations * */
292
- export declare const validations: {
293
- isInstanceFromNoStateModel: (instance: any) => boolean;
294
- isModelKey: <
295
- S,
296
- T extends ModelInstance,
297
- R extends (ins: () => T) => any = (ins: () => T) => T
298
- >(
299
- data: any
300
- ) => data is ModelKey<S, T, R>;
301
- isModelStore: <
302
- S,
303
- T extends ModelInstance,
304
- R extends (ins: () => T) => any = (ins: () => T) => T
305
- >(
306
- data: any
307
- ) => data is Store<S, T, R>;
308
- isModelUsage: <
309
- S,
310
- T extends ModelInstance,
311
- R extends (ins: () => T) => any = (ins: () => T) => T
312
- >(
313
- data: any
314
- ) => data is ModelUsage<S, T, R>;
315
- isStoreIndex: <
316
- S,
317
- T extends ModelInstance,
318
- R extends (ins: () => T) => any = (ins: () => T) => T
319
- >(
320
- data: any
321
- ) => data is StoreIndex<S, T, R>;
322
- };
323
-
324
- /** tools * */
325
- export declare function shallowEqual(prev: any, current: any): boolean;
1
+ declare interface ModelInstance {
2
+ [key: string]: any;
3
+ [key: number]: any;
4
+ }
5
+
6
+ export declare type FieldStructure<R = any> = {
7
+ callback: () => R;
8
+ deps: any[] | undefined;
9
+ identifier: (d: any) => d is FieldStructure<R>;
10
+ value: R;
11
+ get: () => R;
12
+ };
13
+
14
+ export declare type MethodStructure<
15
+ R extends (...args: any[]) => any = (...args: any[]) => any
16
+ > = R & {
17
+ identifier: (d: any) => d is MethodStructure;
18
+ };
19
+
20
+ declare type ValidInstance<S, T extends ModelInstance> = {
21
+ [K in keyof T]: T[K] extends
22
+ | ((...args: any[]) => S)
23
+ | (((...args: any[]) => any) & { identifier: (d: any) => boolean })
24
+ ? T[K]
25
+ : T[K] extends (...args: any[]) => any
26
+ ? never
27
+ : T[K];
28
+ };
29
+
30
+ export declare type Model<S = any, T extends ModelInstance = ModelInstance> = (
31
+ state: S
32
+ ) => ValidInstance<S, T>;
33
+
34
+ export type PickState<R extends Model> = R extends (state: infer S) => any
35
+ ? S
36
+ : never;
37
+
38
+ export type Instance<R extends Model> = R extends (state: any) => infer T
39
+ ? T
40
+ : never;
41
+
42
+ export declare type Action<S = any, T extends ModelInstance = ModelInstance> = {
43
+ type: null | string;
44
+ method: null | ((...args: any[]) => any);
45
+ params?: any[];
46
+ state: S;
47
+ prevState?: S;
48
+ instance: T;
49
+ prevInstance?: T;
50
+ };
51
+
52
+ export interface Token {
53
+ isDifferent: (token: Token) => boolean;
54
+ value: unknown;
55
+ }
56
+
57
+ export declare type Dispatch = (action: Action) => any;
58
+
59
+ export declare interface Key<
60
+ S = any,
61
+ T extends ModelInstance = any,
62
+ R extends undefined | ((instance: () => T) => any) = undefined
63
+ > extends Model<S, T> {
64
+ (s: S): T;
65
+ source: Model<S, T>;
66
+ wrapper: R;
67
+ modelKeyIdentifier: () => boolean;
68
+ [k: string]: any;
69
+ defaultState?: S;
70
+ }
71
+
72
+ declare interface UpdaterStore<
73
+ S = any,
74
+ T extends ModelInstance = ModelInstance
75
+ > {
76
+ getState: () => { state: S; instance: T };
77
+ dispatch: (action: Action) => void;
78
+ }
79
+
80
+ export declare type MiddleWare = (
81
+ store: UpdaterStore
82
+ ) => (next: Dispatch) => (action: Action) => void;
83
+
84
+ export declare interface Config {
85
+ middleWares?: MiddleWare[];
86
+ controlled?: boolean;
87
+ notify?: (
88
+ notifier: (action: Action) => { errors: any[] | undefined },
89
+ action: Action
90
+ ) => any;
91
+ }
92
+
93
+ /** createStore * */
94
+
95
+ export declare interface StoreIndex<
96
+ S = any,
97
+ T extends ModelInstance = any,
98
+ R extends undefined | ((instance: () => T) => any) = undefined
99
+ > {
100
+ key: Key<S, T, R>;
101
+ }
102
+
103
+ export declare interface Store<
104
+ S = any,
105
+ T extends ModelInstance = any,
106
+ R extends undefined | ((instance: () => T) => any) = undefined
107
+ > extends StoreIndex<S, T, R> {
108
+ subscribe: (dispatcher: Dispatch) => () => void;
109
+ getToken: () => Token;
110
+ getInstance: () => R extends undefined ? T : ReturnType<R>;
111
+ getStoreInstance: () => T;
112
+ update: (args?: {
113
+ model?: Model<S, T>;
114
+ key?: Key<S, T, R>;
115
+ initialState?: S;
116
+ state?: S;
117
+ }) => void;
118
+ destroy: () => void;
119
+ payload: <P>(
120
+ callback?: (payload: P | undefined) => P | undefined
121
+ ) => P | undefined;
122
+ isDestroyed: () => boolean;
123
+ extends: <E extends Record<string, any>>(e: E) => Store<S, T, R> & E;
124
+ }
125
+
126
+ export declare function createStore<
127
+ S,
128
+ T extends ModelInstance,
129
+ D extends S,
130
+ R extends undefined | ((instance: () => T) => any) = undefined
131
+ >(
132
+ model: Model<S, T> | Key<S, T, R> | ModelUsage<Model<S, T>, R>,
133
+ state?: D
134
+ ): Store<S, T, R>;
135
+
136
+ /** createKey * */
137
+
138
+ export declare interface ModelKey<
139
+ S = any,
140
+ T extends ModelInstance = any,
141
+ R extends undefined | ((instance: () => T) => any) = undefined
142
+ > extends Key<S, T, R> {
143
+ (s: S): T;
144
+ createStore: <D extends S>(state?: D) => Store<S, T, R>;
145
+ extends: <E extends Record<string, any>>(e: E) => ModelKey<S, T, R> & E;
146
+ }
147
+
148
+ export declare function createKey<
149
+ S,
150
+ T extends ModelInstance,
151
+ D extends S,
152
+ R extends undefined | ((instance: () => T) => any) = undefined
153
+ >(
154
+ model: Model<S, T> | ModelUsage<Model<S, T>, R>,
155
+ state?: D
156
+ ): ModelKey<S, T, R>;
157
+
158
+ /** createStores * */
159
+
160
+ export declare interface StoreCollection {
161
+ find: <
162
+ S,
163
+ T extends ModelInstance,
164
+ R extends undefined | ((instance: () => T) => any) = undefined
165
+ >(
166
+ key: Key<S, T, R> | StoreIndex<S, T, R>
167
+ ) => Store<S, T, R> | null;
168
+ update: (...keys: (ModelKey | StoreIndex)[]) => void;
169
+ keys: () => Key[];
170
+ destroy: () => void;
171
+ }
172
+
173
+ export declare function createStores(
174
+ ...modelKeys: (ModelKey | StoreIndex)[]
175
+ ): StoreCollection;
176
+
177
+ /** model API * */
178
+
179
+ export declare type ModelUsage<
180
+ M extends Model,
181
+ R extends undefined | ((instance: () => Instance<M>) => any) = undefined
182
+ > = M & {
183
+ createKey: <D extends PickState<M>>(
184
+ state?: D
185
+ ) => ModelKey<PickState<M>, Instance<M>, R>;
186
+ createStore: <D extends PickState<M>>(
187
+ state?: D
188
+ ) => Store<PickState<M>, Instance<M>, R>;
189
+ produce: <
190
+ C extends (instance: () => Instance<M>) => any = (
191
+ instance: () => Instance<M>
192
+ ) => Instance<M>
193
+ >(
194
+ s: C
195
+ ) => ModelUsage<M, C>;
196
+ wrapper: R;
197
+ extends: <E extends Record<string, any>>(e: E) => ModelUsage<M, R> & E;
198
+ };
199
+
200
+ // eslint-disable-next-line @typescript-eslint/naming-convention
201
+ export declare interface model {
202
+ <M extends Model>(modelFn: M): ModelUsage<M, undefined>;
203
+ <M extends Model, R extends (instance: () => Instance<M>) => any>(
204
+ modelFn: M,
205
+ s: R
206
+ ): ModelUsage<M, R>;
207
+ <
208
+ M extends Model,
209
+ R extends undefined | ((instance: () => Instance<M>) => any) = undefined
210
+ >(
211
+ modelFn: M,
212
+ s?: R
213
+ ): ModelUsage<M, R>;
214
+ createField: <P extends () => any>(
215
+ callback: P,
216
+ deps?: any[]
217
+ ) => FieldStructure<ReturnType<P>>;
218
+ createMethod: <P extends (...args: any[]) => any = (...args: any[]) => any>(
219
+ method: P
220
+ ) => MethodStructure<P>;
221
+ }
222
+
223
+ /** createSignal * */
224
+
225
+ export interface SignalOptions {
226
+ cutOff?: boolean;
227
+ }
228
+
229
+ // eslint-disable-next-line @typescript-eslint/no-redeclare
230
+ declare interface SignalStore<
231
+ S = any,
232
+ T extends ModelInstance = any,
233
+ R extends undefined | ((instance: () => T) => any) = undefined
234
+ > extends StoreIndex<S, T, R> {
235
+ getToken: () => Token;
236
+ subscribe: (dispatcher: Dispatch) => () => void;
237
+ getSignal: () => {
238
+ (options?: SignalOptions): R extends undefined ? T : ReturnType<R>;
239
+ startStatistics: () => void;
240
+ stopStatistics: () => void;
241
+ subscribe: (dispatcher: Dispatch) => () => void;
242
+ store: Store<S, T, R>;
243
+ };
244
+ }
245
+
246
+ export declare function createSignal<
247
+ S,
248
+ T extends ModelInstance,
249
+ R extends undefined | ((instance: () => T) => any) = undefined
250
+ >(store: Store<S, T, R>): SignalStore<S, T, R>;
251
+
252
+ /** createSelector * */
253
+
254
+ declare type SelectMethod<
255
+ T extends ModelInstance = any,
256
+ R extends undefined | ((instance: () => T) => any) = undefined
257
+ > = {
258
+ (): R extends undefined ? T : ReturnType<R>;
259
+ <C extends (instance: () => R extends undefined ? T : ReturnType<R>) => any>(
260
+ selector: C
261
+ ): ReturnType<C>;
262
+ <C extends (instance: () => R extends undefined ? T : ReturnType<R>) => any>(
263
+ selector?: C
264
+ ): ReturnType<R> | ReturnType<C>;
265
+ };
266
+
267
+ // eslint-disable-next-line @typescript-eslint/no-redeclare
268
+ declare interface SelectorStore<
269
+ S = any,
270
+ T extends ModelInstance = any,
271
+ R extends undefined | ((instance: () => T) => any) = undefined
272
+ > extends StoreIndex<S, T, R> {
273
+ getToken: () => Token;
274
+ subscribe: (dispatcher: Dispatch) => () => void;
275
+ select: SelectMethod<T, R>;
276
+ }
277
+
278
+ declare interface SelectorOption<T = any> {
279
+ equality?: (current: T, next: T) => boolean;
280
+ }
281
+
282
+ export declare function createSelector<
283
+ S,
284
+ T extends ModelInstance,
285
+ R extends undefined | ((instance: () => T) => any) = undefined
286
+ >(store: Store<S, T, R>, opts?: SelectorOption): SelectorStore<S, T, R>;
287
+
288
+ /** config * */
289
+ export declare function config(configuration: Config): {
290
+ createStore: <
291
+ S,
292
+ T extends ModelInstance,
293
+ D extends S,
294
+ R extends undefined | ((instance: () => T) => any) = undefined
295
+ >(
296
+ model: Model<S, T> | Key<S, T, R> | ModelUsage<Model<S, T>, R>,
297
+ state?: D
298
+ ) => Store<S, T, R>;
299
+ createKey: <
300
+ S,
301
+ T extends ModelInstance,
302
+ D extends S,
303
+ R extends undefined | ((instance: () => T) => any) = undefined
304
+ >(
305
+ model: Model<S, T> | ModelUsage<Model<S, T>, R>,
306
+ state?: D
307
+ ) => ModelKey<S, T, R>;
308
+ createStores: (...modelKeys: (ModelKey | StoreIndex)[]) => StoreCollection;
309
+ model: model;
310
+ };
311
+
312
+ /** validations * */
313
+ export declare const validations: {
314
+ isInstanceFromNoStateModel: (instance: any) => boolean;
315
+ isModelKey: <
316
+ S,
317
+ T extends ModelInstance,
318
+ R extends undefined | ((instance: () => T) => any) = undefined
319
+ >(
320
+ data: any
321
+ ) => data is ModelKey<S, T, R>;
322
+ isModelStore: <
323
+ S,
324
+ T extends ModelInstance,
325
+ R extends undefined | ((instance: () => T) => any) = undefined
326
+ >(
327
+ data: any
328
+ ) => data is Store<S, T, R>;
329
+ isModelUsage: <
330
+ M extends Model,
331
+ R extends undefined | ((instance: () => T) => any) = undefined
332
+ >(
333
+ data: M
334
+ ) => data is ModelUsage<typeof data, R>;
335
+ isStoreIndex: <
336
+ S,
337
+ T extends ModelInstance,
338
+ R extends undefined | ((instance: () => T) => any) = undefined
339
+ >(
340
+ data: any
341
+ ) => data is StoreIndex<S, T, R>;
342
+ };
343
+
344
+ /** tools * */
345
+ export declare function shallowEqual(prev: any, current: any): boolean;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "as-model",
4
- "version": "0.2.8",
4
+ "version": "0.3.0",
5
5
  "description": "This is a model state management tool",
6
6
  "license": "MIT",
7
7
  "author": "Jimmy.Harding",