as-model 0.1.12 → 0.1.15

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/README.md CHANGED
@@ -1,483 +1,483 @@
1
- [![npm][npm-image]][npm-url]
2
- [![NPM downloads][npm-downloads-image]][npm-url]
3
- [![standard][standard-image]][standard-url]
4
-
5
- [npm-image]: https://img.shields.io/npm/v/as-model.svg?style=flat-square
6
- [npm-url]: https://www.npmjs.com/package/as-model
7
- [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
8
- [standard-url]: http://npm.im/standard
9
- [npm-downloads-image]: https://img.shields.io/npm/dm/as-model.svg?style=flat-square
10
-
11
-
12
- # as-model
13
-
14
- This is a simple state management library with model coding style for javascript/typescript.
15
-
16
- ## Code first
17
-
18
- Create a model function:
19
-
20
- ```js
21
- // model.js
22
- // A parameter for model state.
23
- export function counting(state){
24
- // Define instance object for outside usage.
25
- return {
26
- // Define properties for instance.
27
- count: state,
28
- symbol: !state? '': (state > 0? '+' : '-'),
29
- // Create action methods for changing state.
30
- increase:()=> state + 1,
31
- decrease:()=> state - 1,
32
- add(...additions){
33
- return additions.reduce((result, current)=>{
34
- return result + current;
35
- }, state);
36
- }
37
- };
38
- }
39
- ```
40
-
41
- Create store:
42
-
43
- ```js
44
- // store.js
45
- import {counting} from './model';
46
- import {createStore} from 'as-model';
47
-
48
- // Create and initialize a model store.
49
- const store = createStore(counting, 0);
50
- // Get instance and call action methods to change state.
51
- store.getInstance().increase();
52
- // Get new properties from instance.
53
- console.log(store.getInstance().count); // 1
54
- store.getInstance().add(2,3);
55
- console.log(store.getInstance().count); // 6
56
- ```
57
-
58
- Create multiple stores:
59
-
60
- ```js
61
- import {counting} from './model';
62
- import {createKey, createStores} from 'as-model';
63
-
64
- // Create model key with initial state.
65
- const countingKey0 = createKey(counting, 0);
66
- const countingKey1 = createKey(counting, 1);
67
-
68
- // Use model keys as templates to create multiple stores.
69
- const stores = createStores(countingKey0, countingKey1);
70
- // Find store by model key
71
- const store0 = stores.find(countingKey0);
72
- store0?.getInstance().increase();
73
- console.log(store0?.getInstance().count); // 1
74
- ```
75
-
76
- Model key is a template for creating multiple stores, and it is also an identifier to find the rightstore from multiple stores.
77
-
78
- Use **model** API to create store or key.
79
-
80
- ```js
81
- import {counting} from './model';
82
- import {model} from 'as-model';
83
-
84
- const store = model(counting).createStore(0);
85
- const key = model(counting).createKey(0);
86
- ......
87
- ```
88
-
89
- In typescript develop environment, `model` API can do a type check for making sure the model action method returns a correct type.
90
-
91
- ```js
92
- // ts
93
- import {model} from 'as-model';
94
-
95
- // The model api ensures every action method returns a same type value with model state.
96
- const counting = model((state: number)=>{
97
- return {
98
- count: state,
99
- increase:()=>state + 1 + '', // type error, should be number, but returns string.
100
- decrease:()=>state - 1,
101
- add(...additions: number[]){
102
- return additions.reduce((result, current)=>{
103
- return result + current;
104
- }, state);
105
- }
106
- };
107
- });
108
-
109
- const store = counting.createStore(0);
110
- const key = counting.createKey(0);
111
- ......
112
- ```
113
-
114
- Sync store
115
-
116
- ```js
117
- import {counting} from './model';
118
- import {model} from 'as-model';
119
-
120
- const store = model(counting).createStore(0);
121
- const {getInstance} = store;
122
- // Subscribe the state changes.
123
- const unsubscribe = store.subscribe((action)=>{
124
- console.log(store.getInstance());
125
- });
126
- getInstance().increase(); // output: {count: 1}
127
- // Destroy subscription.
128
- unsubscribe();
129
- ```
130
-
131
- Want to use async operations?
132
-
133
- ```js
134
- import {counting} from './model';
135
- import {model, createSelector} from 'as-model';
136
-
137
- const store = model(counting).select((getInstance)=>{
138
- const instance = getInstance();
139
- return {
140
- ...instance,
141
- async delayIncrease(){
142
- const ok = await new Promise((resolve)=>{
143
- setTimeout(()=>{
144
- resolve(true);
145
- },200);
146
- });
147
- if(ok){
148
- getInstance().increase();
149
- }
150
- }
151
- }
152
- }).createStore(0);
153
- const {subscribe, select} = createSelector(store);
154
- const unsubscribe = subscribe();
155
- // When use select with no parameter,
156
- // select method finds default selector for reproducing result
157
- const {delayIncrease} = select();
158
- await delayIncrease();
159
- select().count // 1
160
- ```
161
-
162
- Sync store with state in react hooks:
163
-
164
- ```js
165
- import {model, createStores} from 'as-model';
166
- import {
167
- createContext,
168
- useRef,
169
- useState,
170
- useEffect,
171
- useContext
172
- } from 'react';
173
-
174
- // Local state management
175
- function useModel(modelFn, defaultState){
176
- // Use ref to persist the store object.
177
- const storeRef = useRef(model(modelFn).createStore(defaultState));
178
- const store = storeRef.current;
179
- // Set store instance as an initial state for useState.
180
- const [state, setState] = useState(store.getInstance());
181
-
182
- useEffect(()=>{
183
- // Subscribe the state changes.
184
- const unsubscribe = store.subscribe((action)=>{
185
- setState(store.getInstance());
186
- });
187
- // Destroy subscription when component unmounts.
188
- return unsubscribe;
189
- }, []);
190
-
191
- return state;
192
- }
193
-
194
- function App(){
195
- const {
196
- count,
197
- increase
198
- } = useModel(counting, 0);
199
- }
200
-
201
- // global static state management
202
- function useModelStore(store){
203
- const [state, setState] = useState(store.getInstance());
204
-
205
- useEffect(()=>{
206
- const unsubscribe = store.subscribe((action)=>{
207
- setState(store.getInstance());
208
- });
209
- return unsubscribe;
210
- }, []);
211
-
212
- return state;
213
- }
214
-
215
- const store = model(counting).createStore({state: 0});
216
-
217
- function App(){
218
- const {
219
- count,
220
- increase
221
- } = useModelStore(store);
222
- }
223
-
224
- // global dynamic state management
225
- const ModelContext = createContext(null);
226
-
227
- function create(...keys){
228
- return {
229
- provide(Component){
230
- return function Provider(props){
231
- // Create and persist multiple stores in Component, that makes different elements from this Component carry different stores.
232
- const storesRef = useRef(createStores(...keys));
233
- const stores = storesRef.current;
234
- // Use Context to provide multiple stores to all the children.
235
- return (
236
- <ModelContext.Provider value={stores}>
237
- <Component {...props} />
238
- </ModelContext.Provider>
239
- );
240
- }
241
- }
242
- }
243
- }
244
-
245
- function useModelKey(key){
246
- const stores = useContext(ModelContext);
247
- if(stores==null){
248
- throw new Error('ModelContext is not provided');
249
- }
250
- // Use model key to find the right store.
251
- const store = stores.find(key);
252
- if(store==null){
253
- throw new Error('Can not find store by model key');
254
- }
255
- const [state, setState] = useState(store.getInstance());
256
-
257
- useEffect(()=>{
258
- const unsubscribe = store.subscribe((action)=>{
259
- setState(store.getInstance());
260
- });
261
- return unsubscribe;
262
- }, []);
263
-
264
- return state;
265
- }
266
-
267
- const countingKey = model(counting).createKey(0);
268
-
269
- const App = create(countingKey).provide(function App(){
270
- const {
271
- count,
272
- increase
273
- } = useModelKey(countingKey);
274
- });
275
- ```
276
-
277
- ## Install
278
-
279
- ```
280
- npm install as-model
281
- ```
282
-
283
- ## Simplify API
284
-
285
- ### createStore
286
-
287
- ```js
288
- function createStore(modelFnOrKey, initialState?):store
289
- ```
290
-
291
- #### parameters
292
-
293
- * modelFnOrKey - a model function accepts a state parameter and returns an object with action methods, or a model key of model function.
294
- * initialState - the initial state of the model.
295
-
296
- #### return
297
-
298
- A store object with model key and methods. The store object has `getInstance` method to get the instance of the model; `subscribe` method to subscribe the state changes; `update` method to update the store with new model function and initial state; `destroy` method to destroy the store.
299
-
300
- **store** structure:
301
-
302
- ```js
303
- {
304
- getInstance: ()=>instance,
305
- subscribe: (listener)=>unsubscribe,
306
- key: modelKey,
307
- destroy: ()=>void
308
- update: (
309
- data:{
310
- model?:modelFn,
311
- initialState?: any
312
- }
313
- )=>void
314
- }
315
- ```
316
-
317
- ### createKey
318
-
319
- ```js
320
- function createKey(modelFn, initialState?):key
321
- ```
322
-
323
- #### parameters
324
-
325
- * modelFn - a model function accepts a state parameter and returns an object with action methods.
326
- * initialState - the initial state of the model.
327
-
328
- #### return
329
-
330
- A model key function with `createStore` method to create a store with the model key.
331
-
332
- **key** structure:
333
-
334
- ```js
335
- {
336
- createStore: (initialState?)=>store
337
- }
338
- ```
339
-
340
- ### createStores
341
-
342
- ```js
343
- function createStores(...keys):stores
344
- ```
345
-
346
- #### parameters
347
-
348
- * keys - multiple model keys of model functions.
349
-
350
- #### return
351
-
352
- Multiple stores created by the model keys.
353
-
354
- **stores** structure:
355
-
356
- ```js
357
- {
358
- find: (key)=>store,
359
- destroy: ()=>void
360
- }
361
- ```
362
-
363
- ### createSignal
364
-
365
- ```js
366
- function createSignal(store):SignalStore
367
- ```
368
-
369
- #### parameters
370
-
371
- * store - a store object created by `createStore` method.
372
-
373
- #### return
374
-
375
- Signal store object with `subscribe` method to subscribe the state changes, and `getSignal` method to get the signal callback function.
376
-
377
- **signalAPI** structure:
378
-
379
- ```js
380
- {
381
- subscribe: (listener)=>unsubscribe,
382
- getSignal: ()=>signal,
383
- key: modelKey,
384
- }
385
- ```
386
-
387
- The signal function returns a real time instance from store. Only when the properties picked from real time instance are changed, the subscribed listener can receive an action notification.
388
-
389
- ### createSelector
390
-
391
- ```js
392
- function createSelector(store, opts?:SelectorOptions):SelectorStore
393
- ```
394
-
395
- #### parameters
396
-
397
- * store - a store object created by `createStore` method.
398
- * opts - (Optional) an object config to optimize createSelector.
399
-
400
- ```js
401
- {
402
- // When the selector is drived to reproduce a new data,
403
- // it compares if the result is different with the previous one,
404
- // if the camparing result is true, it represents no differ happens,
405
- // the subscribed callback will not be called.
406
- equality?: (current: T, next: T) => boolean;
407
- }
408
- ```
409
-
410
- #### return
411
-
412
- Selector store object with `subscribe` method to subscribe the state changes, and `select` method for reselecting instance.
413
-
414
- ```js
415
- {
416
- subscribe: (listener)=>unsubscribe,
417
- select: (selector?:(getInstance:()=>Instance)=>)=>any,
418
- key: modelKey,
419
- }
420
- ```
421
-
422
-
423
- ### model
424
-
425
- ```js
426
- function model(modelFn):modelAPI
427
- ```
428
-
429
- #### parameters
430
-
431
- * modelFn - a model function accepts a state parameter and returns an object with action methods.
432
-
433
- #### return
434
-
435
- Model api object with `createStore`, `createKey` methods to create store, key for the model function, and `select` method to set a default selector function (Use `createSelector(store).select()` to select the default one).
436
-
437
- **modelAPI** structure:
438
-
439
- ```js
440
- {
441
- createStore: (initialState?)=> store,
442
- createKey: (initialState?)=> key,
443
- select: (
444
- selector:(getInstance:()=>Instance)=>Record<string, any>|Array<any>
445
- )=>modelApI
446
- }
447
- ```
448
-
449
- ### config
450
-
451
- ```js
452
- function config(options):configAPI
453
- ```
454
-
455
- #### parameters
456
-
457
- * options - (Optional) an object with the following properties:
458
- * batchNotify - (Optional) a callback function to batch notify the listeners, for example: `unstable_batchedUpdates` from react-dom.
459
- * controlled - (Optional) a boolean state to tell as-model use controlled mode to output instance changes.
460
- * middleWares - (Optional) a middleWare array for reproducing state or ignore actions.
461
-
462
- #### return
463
-
464
- All apis above except `createSignal` and `createSelector` API.
465
-
466
- ```js
467
- {
468
- createStore: (modelFnOrKey, initialState?)=>store,
469
- createKey: (modelFn, initialState?)=>key,
470
- createStores: (...keys)=>stores,
471
- model: (modelFn)=>modelAPI
472
- }
473
- ```
474
-
475
- ## Browser Support
476
-
477
- ```
478
- chrome: '>=91',
479
- edge: '>=91',
480
- firefox: '=>90',
481
- safari: '>=15'
482
- ```
483
-
1
+ [![npm][npm-image]][npm-url]
2
+ [![NPM downloads][npm-downloads-image]][npm-url]
3
+ [![standard][standard-image]][standard-url]
4
+
5
+ [npm-image]: https://img.shields.io/npm/v/as-model.svg?style=flat-square
6
+ [npm-url]: https://www.npmjs.com/package/as-model
7
+ [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
8
+ [standard-url]: http://npm.im/standard
9
+ [npm-downloads-image]: https://img.shields.io/npm/dm/as-model.svg?style=flat-square
10
+
11
+
12
+ # as-model
13
+
14
+ This is a simple state management library with model coding style for javascript/typescript.
15
+
16
+ ## Code first
17
+
18
+ Create a model function:
19
+
20
+ ```js
21
+ // model.js
22
+ // A parameter for model state.
23
+ export function counting(state){
24
+ // Define instance object for outside usage.
25
+ return {
26
+ // Define properties for instance.
27
+ count: state,
28
+ symbol: !state? '': (state > 0? '+' : '-'),
29
+ // Create action methods for changing state.
30
+ increase:()=> state + 1,
31
+ decrease:()=> state - 1,
32
+ add(...additions){
33
+ return additions.reduce((result, current)=>{
34
+ return result + current;
35
+ }, state);
36
+ }
37
+ };
38
+ }
39
+ ```
40
+
41
+ Create store:
42
+
43
+ ```js
44
+ // store.js
45
+ import {counting} from './model';
46
+ import {createStore} from 'as-model';
47
+
48
+ // Create and initialize a model store.
49
+ const store = createStore(counting, 0);
50
+ // Get instance and call action methods to change state.
51
+ store.getInstance().increase();
52
+ // Get new properties from instance.
53
+ console.log(store.getInstance().count); // 1
54
+ store.getInstance().add(2,3);
55
+ console.log(store.getInstance().count); // 6
56
+ ```
57
+
58
+ Create multiple stores:
59
+
60
+ ```js
61
+ import {counting} from './model';
62
+ import {createKey, createStores} from 'as-model';
63
+
64
+ // Create model key with initial state.
65
+ const countingKey0 = createKey(counting, 0);
66
+ const countingKey1 = createKey(counting, 1);
67
+
68
+ // Use model keys as templates to create multiple stores.
69
+ const stores = createStores(countingKey0, countingKey1);
70
+ // Find store by model key
71
+ const store0 = stores.find(countingKey0);
72
+ store0?.getInstance().increase();
73
+ console.log(store0?.getInstance().count); // 1
74
+ ```
75
+
76
+ Model key is a template for creating multiple stores, and it is also an identifier to find the rightstore from multiple stores.
77
+
78
+ Use **model** API to create store or key.
79
+
80
+ ```js
81
+ import {counting} from './model';
82
+ import {model} from 'as-model';
83
+
84
+ const store = model(counting).createStore(0);
85
+ const key = model(counting).createKey(0);
86
+ ......
87
+ ```
88
+
89
+ In typescript develop environment, `model` API can do a type check for making sure the model action method returns a correct type.
90
+
91
+ ```js
92
+ // ts
93
+ import {model} from 'as-model';
94
+
95
+ // The model api ensures every action method returns a same type value with model state.
96
+ const counting = model((state: number)=>{
97
+ return {
98
+ count: state,
99
+ increase:()=>state + 1 + '', // type error, should be number, but returns string.
100
+ decrease:()=>state - 1,
101
+ add(...additions: number[]){
102
+ return additions.reduce((result, current)=>{
103
+ return result + current;
104
+ }, state);
105
+ }
106
+ };
107
+ });
108
+
109
+ const store = counting.createStore(0);
110
+ const key = counting.createKey(0);
111
+ ......
112
+ ```
113
+
114
+ Sync store
115
+
116
+ ```js
117
+ import {counting} from './model';
118
+ import {model} from 'as-model';
119
+
120
+ const store = model(counting).createStore(0);
121
+ const {getInstance} = store;
122
+ // Subscribe the state changes.
123
+ const unsubscribe = store.subscribe((action)=>{
124
+ console.log(store.getInstance());
125
+ });
126
+ getInstance().increase(); // output: {count: 1}
127
+ // Destroy subscription.
128
+ unsubscribe();
129
+ ```
130
+
131
+ Want to use async operations?
132
+
133
+ ```js
134
+ import {counting} from './model';
135
+ import {model, createSelector} from 'as-model';
136
+
137
+ const store = model(counting).select((getInstance)=>{
138
+ const instance = getInstance();
139
+ return {
140
+ ...instance,
141
+ async delayIncrease(){
142
+ const ok = await new Promise((resolve)=>{
143
+ setTimeout(()=>{
144
+ resolve(true);
145
+ },200);
146
+ });
147
+ if(ok){
148
+ getInstance().increase();
149
+ }
150
+ }
151
+ }
152
+ }).createStore(0);
153
+ const {subscribe, select} = createSelector(store);
154
+ const unsubscribe = subscribe();
155
+ // When use select with no parameter,
156
+ // select method finds default selector for reproducing result
157
+ const {delayIncrease} = select();
158
+ await delayIncrease();
159
+ select().count // 1
160
+ ```
161
+
162
+ Sync store with state in react hooks:
163
+
164
+ ```js
165
+ import {model, createStores} from 'as-model';
166
+ import {
167
+ createContext,
168
+ useRef,
169
+ useState,
170
+ useEffect,
171
+ useContext
172
+ } from 'react';
173
+
174
+ // Local state management
175
+ function useModel(modelFn, defaultState){
176
+ // Use ref to persist the store object.
177
+ const storeRef = useRef(model(modelFn).createStore(defaultState));
178
+ const store = storeRef.current;
179
+ // Set store instance as an initial state for useState.
180
+ const [state, setState] = useState(store.getInstance());
181
+
182
+ useEffect(()=>{
183
+ // Subscribe the state changes.
184
+ const unsubscribe = store.subscribe((action)=>{
185
+ setState(store.getInstance());
186
+ });
187
+ // Destroy subscription when component unmounts.
188
+ return unsubscribe;
189
+ }, []);
190
+
191
+ return state;
192
+ }
193
+
194
+ function App(){
195
+ const {
196
+ count,
197
+ increase
198
+ } = useModel(counting, 0);
199
+ }
200
+
201
+ // global static state management
202
+ function useModelStore(store){
203
+ const [state, setState] = useState(store.getInstance());
204
+
205
+ useEffect(()=>{
206
+ const unsubscribe = store.subscribe((action)=>{
207
+ setState(store.getInstance());
208
+ });
209
+ return unsubscribe;
210
+ }, []);
211
+
212
+ return state;
213
+ }
214
+
215
+ const store = model(counting).createStore({state: 0});
216
+
217
+ function App(){
218
+ const {
219
+ count,
220
+ increase
221
+ } = useModelStore(store);
222
+ }
223
+
224
+ // global dynamic state management
225
+ const ModelContext = createContext(null);
226
+
227
+ function create(...keys){
228
+ return {
229
+ provide(Component){
230
+ return function Provider(props){
231
+ // Create and persist multiple stores in Component, that makes different elements from this Component carry different stores.
232
+ const storesRef = useRef(createStores(...keys));
233
+ const stores = storesRef.current;
234
+ // Use Context to provide multiple stores to all the children.
235
+ return (
236
+ <ModelContext.Provider value={stores}>
237
+ <Component {...props} />
238
+ </ModelContext.Provider>
239
+ );
240
+ }
241
+ }
242
+ }
243
+ }
244
+
245
+ function useModelKey(key){
246
+ const stores = useContext(ModelContext);
247
+ if(stores==null){
248
+ throw new Error('ModelContext is not provided');
249
+ }
250
+ // Use model key to find the right store.
251
+ const store = stores.find(key);
252
+ if(store==null){
253
+ throw new Error('Can not find store by model key');
254
+ }
255
+ const [state, setState] = useState(store.getInstance());
256
+
257
+ useEffect(()=>{
258
+ const unsubscribe = store.subscribe((action)=>{
259
+ setState(store.getInstance());
260
+ });
261
+ return unsubscribe;
262
+ }, []);
263
+
264
+ return state;
265
+ }
266
+
267
+ const countingKey = model(counting).createKey(0);
268
+
269
+ const App = create(countingKey).provide(function App(){
270
+ const {
271
+ count,
272
+ increase
273
+ } = useModelKey(countingKey);
274
+ });
275
+ ```
276
+
277
+ ## Install
278
+
279
+ ```
280
+ npm install as-model
281
+ ```
282
+
283
+ ## Simplify API
284
+
285
+ ### createStore
286
+
287
+ ```js
288
+ function createStore(modelFnOrKey, initialState?):store
289
+ ```
290
+
291
+ #### parameters
292
+
293
+ * modelFnOrKey - a model function accepts a state parameter and returns an object with action methods, or a model key of model function.
294
+ * initialState - the initial state of the model.
295
+
296
+ #### return
297
+
298
+ A store object with model key and methods. The store object has `getInstance` method to get the instance of the model; `subscribe` method to subscribe the state changes; `update` method to update the store with new model function and initial state; `destroy` method to destroy the store.
299
+
300
+ **store** structure:
301
+
302
+ ```js
303
+ {
304
+ getInstance: ()=>instance,
305
+ subscribe: (listener)=>unsubscribe,
306
+ key: modelKey,
307
+ destroy: ()=>void
308
+ update: (
309
+ data:{
310
+ model?:modelFn,
311
+ initialState?: any
312
+ }
313
+ )=>void
314
+ }
315
+ ```
316
+
317
+ ### createKey
318
+
319
+ ```js
320
+ function createKey(modelFn, initialState?):key
321
+ ```
322
+
323
+ #### parameters
324
+
325
+ * modelFn - a model function accepts a state parameter and returns an object with action methods.
326
+ * initialState - the initial state of the model.
327
+
328
+ #### return
329
+
330
+ A model key function with `createStore` method to create a store with the model key.
331
+
332
+ **key** structure:
333
+
334
+ ```js
335
+ {
336
+ createStore: (initialState?)=>store
337
+ }
338
+ ```
339
+
340
+ ### createStores
341
+
342
+ ```js
343
+ function createStores(...keys):stores
344
+ ```
345
+
346
+ #### parameters
347
+
348
+ * keys - multiple model keys of model functions.
349
+
350
+ #### return
351
+
352
+ Multiple stores created by the model keys.
353
+
354
+ **stores** structure:
355
+
356
+ ```js
357
+ {
358
+ find: (key)=>store,
359
+ destroy: ()=>void
360
+ }
361
+ ```
362
+
363
+ ### createSignal
364
+
365
+ ```js
366
+ function createSignal(store):SignalStore
367
+ ```
368
+
369
+ #### parameters
370
+
371
+ * store - a store object created by `createStore` method.
372
+
373
+ #### return
374
+
375
+ Signal store object with `subscribe` method to subscribe the state changes, and `getSignal` method to get the signal callback function.
376
+
377
+ **signalAPI** structure:
378
+
379
+ ```js
380
+ {
381
+ subscribe: (listener)=>unsubscribe,
382
+ getSignal: ()=>signal,
383
+ key: modelKey,
384
+ }
385
+ ```
386
+
387
+ The signal function returns a real time instance from store. Only when the properties picked from real time instance are changed, the subscribed listener can receive an action notification.
388
+
389
+ ### createSelector
390
+
391
+ ```js
392
+ function createSelector(store, opts?:SelectorOptions):SelectorStore
393
+ ```
394
+
395
+ #### parameters
396
+
397
+ * store - a store object created by `createStore` method.
398
+ * opts - (Optional) an object config to optimize createSelector.
399
+
400
+ ```js
401
+ {
402
+ // When the selector is drived to reproduce a new data,
403
+ // it compares if the result is different with the previous one,
404
+ // if the camparing result is true, it represents no differ happens,
405
+ // the subscribed callback will not be called.
406
+ equality?: (current: T, next: T) => boolean;
407
+ }
408
+ ```
409
+
410
+ #### return
411
+
412
+ Selector store object with `subscribe` method to subscribe the state changes, and `select` method for reselecting instance.
413
+
414
+ ```js
415
+ {
416
+ subscribe: (listener)=>unsubscribe,
417
+ select: (selector?:(getInstance:()=>Instance)=>)=>any,
418
+ key: modelKey,
419
+ }
420
+ ```
421
+
422
+
423
+ ### model
424
+
425
+ ```js
426
+ function model(modelFn):modelAPI
427
+ ```
428
+
429
+ #### parameters
430
+
431
+ * modelFn - a model function accepts a state parameter and returns an object with action methods.
432
+
433
+ #### return
434
+
435
+ Model api object with `createStore`, `createKey` methods to create store, key for the model function, and `select` method to set a default selector function (Use `createSelector(store).select()` to select the default one).
436
+
437
+ **modelAPI** structure:
438
+
439
+ ```js
440
+ {
441
+ createStore: (initialState?)=> store,
442
+ createKey: (initialState?)=> key,
443
+ select: (
444
+ selector:(getInstance:()=>Instance)=>Record<string, any>|Array<any>
445
+ )=>modelApI
446
+ }
447
+ ```
448
+
449
+ ### config
450
+
451
+ ```js
452
+ function config(options):configAPI
453
+ ```
454
+
455
+ #### parameters
456
+
457
+ * options - (Optional) an object with the following properties:
458
+ * batchNotify - (Optional) a callback function to batch notify the listeners, for example: `unstable_batchedUpdates` from react-dom.
459
+ * controlled - (Optional) a boolean state to tell as-model use controlled mode to output instance changes.
460
+ * middleWares - (Optional) a middleWare array for reproducing state or ignore actions.
461
+
462
+ #### return
463
+
464
+ All apis above except `createSignal` and `createSelector` API.
465
+
466
+ ```js
467
+ {
468
+ createStore: (modelFnOrKey, initialState?)=>store,
469
+ createKey: (modelFn, initialState?)=>key,
470
+ createStores: (...keys)=>stores,
471
+ model: (modelFn)=>modelAPI
472
+ }
473
+ ```
474
+
475
+ ## Browser Support
476
+
477
+ ```
478
+ chrome: '>=91',
479
+ edge: '>=91',
480
+ firefox: '=>90',
481
+ safari: '>=15'
482
+ ```
483
+