coaction 0.1.4 → 0.1.5

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
@@ -19,7 +19,7 @@ npm install coaction
19
19
  ```jsx
20
20
  import { create } from 'coaction';
21
21
 
22
- const useStore = create((set, get) => ({
22
+ const useStore = create((set) => ({
23
23
  count: 0,
24
24
  increment: () => set((state) => state.count++)
25
25
  }));
@@ -100,19 +100,25 @@ const handleDraft = (store, internal) => {
100
100
  }
101
101
  };
102
102
 
103
- const getInitialState = (store, createState) => {
103
+ const getInitialState = (store, createState, internal) => {
104
104
  const makeState = fn => {
105
105
  // make sure createState is a function
106
106
  if (process.env.NODE_ENV !== 'production' && typeof fn !== 'function') {
107
107
  throw new Error('createState should be a function');
108
108
  }
109
109
  let state = fn(store.setState, store.getState, store);
110
- if (typeof state === 'function') {
110
+ // support 3rd party library store like zustand, redux
111
+ if (state.getState) {
112
+ state = state.getState();
113
+ // support 3rd party library store like pinia
114
+ } else if (typeof state === 'function') {
111
115
  state = state();
112
116
  }
117
+ // support bind store like mobx
113
118
  if (state[bindSymbol]) {
114
119
  const rawState = state[bindSymbol].bind(state);
115
- state[bindSymbol].handleStore(store, rawState, state);
120
+ state[bindSymbol].handleStore(store, rawState, state, internal);
121
+ delete state[bindSymbol];
116
122
  return rawState;
117
123
  }
118
124
  return state;
@@ -198,7 +204,7 @@ const createSelectorWithArray = createSelectorCreatorWithArray();
198
204
  const getRawState = (store, internal, initialState, options) => {
199
205
  const rawState = {};
200
206
  const handle = (_rawState, _initialState, sliceKey) => {
201
- internal.mutableInstance = store.toRaw?.(_initialState);
207
+ internal.mutableInstance = internal.toMutableRaw?.(_initialState);
202
208
  const descriptors = Object.getOwnPropertyDescriptors(_initialState);
203
209
  Object.entries(descriptors).forEach(([key, descriptor]) => {
204
210
  if (Object.prototype.hasOwnProperty.call(descriptor, 'value')) {
@@ -345,8 +351,8 @@ const getRawState = (store, internal, initialState, options) => {
345
351
  done?.(result);
346
352
  return result;
347
353
  }
348
- if (internal.mutableInstance && store.act) {
349
- const result = store.act(() => {
354
+ if (internal.mutableInstance && internal.actMutable) {
355
+ const result = internal.actMutable(() => {
350
356
  return fn.apply(sliceKey ? store.getState()[sliceKey] : store.getState(), args);
351
357
  });
352
358
  done?.(result);
@@ -392,8 +398,8 @@ const handleState = (store, internal, options) => {
392
398
  const enablePatches = store.transport ?? options.enablePatches;
393
399
  if (!enablePatches) {
394
400
  if (internal.mutableInstance) {
395
- if (store.act) {
396
- store.act(() => {
401
+ if (internal.actMutable) {
402
+ internal.actMutable(() => {
397
403
  fn.apply(null);
398
404
  });
399
405
  return [];
@@ -413,7 +419,11 @@ const handleState = (store, internal, options) => {
413
419
  internal.rootState = internal.backupState;
414
420
  throw error;
415
421
  }
416
- internal.listeners.forEach(listener => listener());
422
+ if (internal.updateImmutable) {
423
+ internal.updateImmutable(internal.rootState);
424
+ } else {
425
+ internal.listeners.forEach(listener => listener());
426
+ }
417
427
  return [];
418
428
  }
419
429
  internal.backupState = internal.rootState;
@@ -441,7 +451,11 @@ const handleState = (store, internal, options) => {
441
451
  if (finalPatches.patches.length) {
442
452
  store.apply(internal.rootState, finalPatches.patches);
443
453
  if (!internal.mutableInstance) {
444
- internal.listeners.forEach(listener => listener());
454
+ if (internal.updateImmutable) {
455
+ internal.updateImmutable(internal.rootState);
456
+ } else {
457
+ internal.listeners.forEach(listener => listener());
458
+ }
445
459
  }
446
460
  }
447
461
  return [internal.rootState, patches, inversePatches];
@@ -593,7 +607,11 @@ const create = (createState, options = {}) => {
593
607
  };
594
608
  const apply = (state = internal.rootState, patches) => {
595
609
  internal.rootState = patches ? mutative.apply(state, patches) : state;
596
- internal.listeners.forEach(listener => listener());
610
+ if (internal.updateImmutable) {
611
+ internal.updateImmutable(internal.rootState);
612
+ } else {
613
+ internal.listeners.forEach(listener => listener());
614
+ }
597
615
  };
598
616
  const getPureState = () => internal.rootState;
599
617
  const isSliceStore = typeof createState === 'object';
@@ -609,7 +627,7 @@ const create = (createState, options = {}) => {
609
627
  getPureState
610
628
  });
611
629
  applyMiddlewares(store, options.middlewares ?? []);
612
- const initialState = getInitialState(store, createState);
630
+ const initialState = getInitialState(store, createState, internal);
613
631
  store.getInitialState = () => initialState;
614
632
  internal.rootState = getRawState(store, internal, initialState, options);
615
633
  return {
@@ -96,19 +96,25 @@ const handleDraft = (store, internal) => {
96
96
  }
97
97
  };
98
98
 
99
- const getInitialState = (store, createState) => {
99
+ const getInitialState = (store, createState, internal) => {
100
100
  const makeState = fn => {
101
101
  // make sure createState is a function
102
102
  if (process.env.NODE_ENV !== 'production' && typeof fn !== 'function') {
103
103
  throw new Error('createState should be a function');
104
104
  }
105
105
  let state = fn(store.setState, store.getState, store);
106
- if (typeof state === 'function') {
106
+ // support 3rd party library store like zustand, redux
107
+ if (state.getState) {
108
+ state = state.getState();
109
+ // support 3rd party library store like pinia
110
+ } else if (typeof state === 'function') {
107
111
  state = state();
108
112
  }
113
+ // support bind store like mobx
109
114
  if (state[bindSymbol]) {
110
115
  const rawState = state[bindSymbol].bind(state);
111
- state[bindSymbol].handleStore(store, rawState, state);
116
+ state[bindSymbol].handleStore(store, rawState, state, internal);
117
+ delete state[bindSymbol];
112
118
  return rawState;
113
119
  }
114
120
  return state;
@@ -194,7 +200,7 @@ const createSelectorWithArray = createSelectorCreatorWithArray();
194
200
  const getRawState = (store, internal, initialState, options) => {
195
201
  const rawState = {};
196
202
  const handle = (_rawState, _initialState, sliceKey) => {
197
- internal.mutableInstance = store.toRaw?.(_initialState);
203
+ internal.mutableInstance = internal.toMutableRaw?.(_initialState);
198
204
  const descriptors = Object.getOwnPropertyDescriptors(_initialState);
199
205
  Object.entries(descriptors).forEach(([key, descriptor]) => {
200
206
  if (Object.prototype.hasOwnProperty.call(descriptor, 'value')) {
@@ -341,8 +347,8 @@ const getRawState = (store, internal, initialState, options) => {
341
347
  done?.(result);
342
348
  return result;
343
349
  }
344
- if (internal.mutableInstance && store.act) {
345
- const result = store.act(() => {
350
+ if (internal.mutableInstance && internal.actMutable) {
351
+ const result = internal.actMutable(() => {
346
352
  return fn.apply(sliceKey ? store.getState()[sliceKey] : store.getState(), args);
347
353
  });
348
354
  done?.(result);
@@ -388,8 +394,8 @@ const handleState = (store, internal, options) => {
388
394
  const enablePatches = store.transport ?? options.enablePatches;
389
395
  if (!enablePatches) {
390
396
  if (internal.mutableInstance) {
391
- if (store.act) {
392
- store.act(() => {
397
+ if (internal.actMutable) {
398
+ internal.actMutable(() => {
393
399
  fn.apply(null);
394
400
  });
395
401
  return [];
@@ -409,7 +415,11 @@ const handleState = (store, internal, options) => {
409
415
  internal.rootState = internal.backupState;
410
416
  throw error;
411
417
  }
412
- internal.listeners.forEach(listener => listener());
418
+ if (internal.updateImmutable) {
419
+ internal.updateImmutable(internal.rootState);
420
+ } else {
421
+ internal.listeners.forEach(listener => listener());
422
+ }
413
423
  return [];
414
424
  }
415
425
  internal.backupState = internal.rootState;
@@ -437,7 +447,11 @@ const handleState = (store, internal, options) => {
437
447
  if (finalPatches.patches.length) {
438
448
  store.apply(internal.rootState, finalPatches.patches);
439
449
  if (!internal.mutableInstance) {
440
- internal.listeners.forEach(listener => listener());
450
+ if (internal.updateImmutable) {
451
+ internal.updateImmutable(internal.rootState);
452
+ } else {
453
+ internal.listeners.forEach(listener => listener());
454
+ }
441
455
  }
442
456
  }
443
457
  return [internal.rootState, patches, inversePatches];
@@ -589,7 +603,11 @@ const create = (createState, options = {}) => {
589
603
  };
590
604
  const apply$1 = (state = internal.rootState, patches) => {
591
605
  internal.rootState = patches ? apply(state, patches) : state;
592
- internal.listeners.forEach(listener => listener());
606
+ if (internal.updateImmutable) {
607
+ internal.updateImmutable(internal.rootState);
608
+ } else {
609
+ internal.listeners.forEach(listener => listener());
610
+ }
593
611
  };
594
612
  const getPureState = () => internal.rootState;
595
613
  const isSliceStore = typeof createState === 'object';
@@ -605,7 +623,7 @@ const create = (createState, options = {}) => {
605
623
  getPureState
606
624
  });
607
625
  applyMiddlewares(store, options.middlewares ?? []);
608
- const initialState = getInitialState(store, createState);
626
+ const initialState = getInitialState(store, createState, internal);
609
627
  store.getInitialState = () => initialState;
610
628
  internal.rootState = getRawState(store, internal, initialState, options);
611
629
  return {