@coaction/mobx 0.1.5 → 1.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Michael Lin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -24,7 +24,6 @@ import { makeAutoObservable } from 'mobx';
24
24
  const useStore = create(() =>
25
25
  makeAutoObservable(
26
26
  bindMobx({
27
- name: 'test',
28
27
  count: 0,
29
28
  get double() {
30
29
  return this.count * 2;
@@ -37,6 +36,59 @@ const useStore = create(() =>
37
36
  );
38
37
  ```
39
38
 
39
+ ## Limitations
40
+
41
+ - `@coaction/mobx` only supports binding a whole MobX store.
42
+ - Coaction `Slices` mode is not supported in this adapter.
43
+
44
+ ### setState()
45
+
46
+ - It is recommended to update state through store methods, instead of directly updating the state.
47
+ - Any direct mutations to the state outside of methods should be wrapped in setState()
48
+
49
+ ```js
50
+ // ❌ it will not be triggered state update
51
+ store.getState().counter.count += 1;
52
+
53
+ // ✅ it will be triggered state update
54
+ store.setState(() => {
55
+ store.getState().counter.count += 1;
56
+ });
57
+ ```
58
+
59
+ ## Performance
60
+
61
+ ![Benchmark](./benchmark.jpg)
62
+
63
+ Measure(ops/sec) to update 10K arrays, bigger is better([view source](https://github.com/unadlib/mutative/blob/main/test/performance/benchmark.ts)).
64
+
65
+ | Library | Test Case | Ops/sec |
66
+ | -------------- | ------------------------------- | ------- |
67
+ | @coaction/mobx | bigInitWithoutRefsWithoutAssign | 37.44 |
68
+ | mobx | bigInitWithoutRefsWithoutAssign | 37.67 |
69
+ | coaction | bigInitWithoutRefsWithoutAssign | 18,809 |
70
+ | mobx-keystone | bigInitWithoutRefsWithoutAssign | 8.53 |
71
+ | @coaction/mobx | bigInitWithoutRefsWithAssign | 1.54 |
72
+ | mobx | bigInitWithoutRefsWithAssign | 10.78 |
73
+ | coaction | bigInitWithoutRefsWithAssign | 45.20 |
74
+ | mobx-keystone | bigInitWithoutRefsWithAssign | 0.13 |
75
+ | @coaction/mobx | bigInitWithRefsWithoutAssign | 14.99 |
76
+ | mobx | bigInitWithRefsWithoutAssign | 16.68 |
77
+ | coaction | bigInitWithRefsWithoutAssign | 255 |
78
+ | mobx-keystone | bigInitWithRefsWithoutAssign | 2.35 |
79
+ | @coaction/mobx | bigInitWithRefsWithAssign | 1.01 |
80
+ | mobx | bigInitWithRefsWithAssign | 7.71 |
81
+ | coaction | bigInitWithRefsWithAssign | 57.22 |
82
+ | mobx-keystone | bigInitWithRefsWithAssign | 0.11 |
83
+ | @coaction/mobx | init | 38.57 |
84
+ | mobx | init | 43.88 |
85
+ | coaction | init | 8,523 |
86
+ | mobx-keystone | init | 41.19 |
87
+
88
+ This table benchmarks various state management libraries on large initialization tasks. Coaction stands out dramatically, performing at least hundreds of times faster in certain scenarios. For example, in the “bigInitWithoutRefsWithoutAssign” test, Coaction achieves 18,809 ops/sec compared to MobX’s 37.67 ops/sec—over 500 times faster. Similarly, in the “init” test, Coaction reaches 8,523 ops/sec versus MobX’s 43.88 ops/sec—an increase of roughly 200 times. Additionally, Coaction consistently outperforms other libraries across various initialization scenarios, showcasing its exceptional efficiency in handling large-scale data initialization. These results highlight Coaction’s superior performance and make it a highly effective solution for managing complex state in modern front-end applications.
89
+
90
+ > We will also provide more complete benchmarking.
91
+
40
92
  ## Documentation
41
93
 
42
94
  You can find the documentation [here](https://github.com/unadlib/coaction).
@@ -0,0 +1,9 @@
1
+ interface BindMobx {
2
+ <T>(target: T): T;
3
+ }
4
+ /**
5
+ * Bind a store to Mobx
6
+ */
7
+ declare const bindMobx: BindMobx;
8
+
9
+ export { bindMobx };
@@ -4,5 +4,6 @@ interface BindMobx {
4
4
  /**
5
5
  * Bind a store to Mobx
6
6
  */
7
- export declare const bindMobx: BindMobx;
8
- export {};
7
+ declare const bindMobx: BindMobx;
8
+
9
+ export { bindMobx };
package/dist/index.js ADDED
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ bindMobx: () => bindMobx
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/index.ts
28
+ var import_mutability = require("mutability");
29
+ var import_coaction = require("coaction");
30
+ var import_mobx = require("mobx");
31
+ var instancesMap = /* @__PURE__ */ new WeakMap();
32
+ var handleStore = (store, rawState, state, internal) => {
33
+ if (internal.toMutableRaw) return;
34
+ internal.toMutableRaw = (key) => instancesMap.get(key);
35
+ Object.assign(store, {
36
+ subscribe: import_mobx.autorun
37
+ });
38
+ internal.actMutable = import_mobx.runInAction;
39
+ store.apply = (state2 = store.getState(), patches) => {
40
+ if (!patches) {
41
+ (0, import_mobx.runInAction)(() => {
42
+ Object.assign(store.getState(), state2);
43
+ });
44
+ return;
45
+ }
46
+ (0, import_mobx.runInAction)(() => {
47
+ (0, import_mutability.apply)(state2, patches);
48
+ });
49
+ };
50
+ };
51
+ var bindMobx = (0, import_coaction.createBinder)({
52
+ handleStore,
53
+ handleState: (options) => {
54
+ const descriptors = Object.getOwnPropertyDescriptors(options);
55
+ const copyState = Object.defineProperties(
56
+ {},
57
+ descriptors
58
+ );
59
+ const rawState = Object.defineProperties({}, descriptors);
60
+ return {
61
+ copyState,
62
+ bind: (state) => {
63
+ instancesMap.set(rawState, state);
64
+ return rawState;
65
+ }
66
+ };
67
+ }
68
+ });
69
+ // Annotate the CommonJS export names for ESM import in node:
70
+ 0 && (module.exports = {
71
+ bindMobx
72
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,45 @@
1
+ // src/index.ts
2
+ import { apply } from "mutability";
3
+ import { createBinder } from "coaction";
4
+ import { autorun, runInAction } from "mobx";
5
+ var instancesMap = /* @__PURE__ */ new WeakMap();
6
+ var handleStore = (store, rawState, state, internal) => {
7
+ if (internal.toMutableRaw) return;
8
+ internal.toMutableRaw = (key) => instancesMap.get(key);
9
+ Object.assign(store, {
10
+ subscribe: autorun
11
+ });
12
+ internal.actMutable = runInAction;
13
+ store.apply = (state2 = store.getState(), patches) => {
14
+ if (!patches) {
15
+ runInAction(() => {
16
+ Object.assign(store.getState(), state2);
17
+ });
18
+ return;
19
+ }
20
+ runInAction(() => {
21
+ apply(state2, patches);
22
+ });
23
+ };
24
+ };
25
+ var bindMobx = createBinder({
26
+ handleStore,
27
+ handleState: (options) => {
28
+ const descriptors = Object.getOwnPropertyDescriptors(options);
29
+ const copyState = Object.defineProperties(
30
+ {},
31
+ descriptors
32
+ );
33
+ const rawState = Object.defineProperties({}, descriptors);
34
+ return {
35
+ copyState,
36
+ bind: (state) => {
37
+ instancesMap.set(rawState, state);
38
+ return rawState;
39
+ }
40
+ };
41
+ }
42
+ });
43
+ export {
44
+ bindMobx
45
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coaction/mobx",
3
- "version": "0.1.5",
3
+ "version": "1.0.0",
4
4
  "description": "A Coaction integration tool for MobX",
5
5
  "keywords": [
6
6
  "state",
@@ -12,42 +12,32 @@
12
12
  ],
13
13
  "homepage": "https://github.com/unadlib/coaction/tree/main/packages/coaction-mobx#readme",
14
14
  "license": "MIT",
15
- "main": "dist/coaction-mobx.cjs.js",
16
- "module": "dist/coaction-mobx.esm.js",
17
- "umd:main": "dist/coaction-mobx.umd.min.js",
15
+ "main": "dist/index.js",
16
+ "module": "dist/index.mjs",
18
17
  "exports": {
19
18
  ".": {
20
- "types": {
21
- "import": "./dist/coaction-mobx.cjs.mjs",
22
- "default": "./dist/coaction-mobx.cjs.js"
23
- },
24
- "module": "./dist/coaction-mobx.esm.js",
25
- "import": "./dist/coaction-mobx.cjs.mjs",
26
- "default": "./dist/coaction-mobx.cjs.js"
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.mjs",
21
+ "require": "./dist/index.js",
22
+ "default": "./dist/index.mjs"
27
23
  },
28
24
  "./package.json": "./package.json"
29
25
  },
30
- "types": "dist/coaction-mobx.cjs.d.ts",
26
+ "types": "dist/index.d.ts",
31
27
  "sideEffects": false,
32
28
  "files": [
33
29
  "dist"
34
30
  ],
35
31
  "repository": {
36
32
  "type": "git",
37
- "url": "git+ssh://git@github.com/unadlib/coaction.git"
33
+ "url": "https://github.com/unadlib/coaction.git",
34
+ "directory": "packages/coaction-mobx"
38
35
  },
39
- "scripts": {},
40
36
  "bugs": {
41
37
  "url": "https://github.com/unadlib/coaction/issues"
42
38
  },
43
- "preconstruct": {
44
- "umdName": "CoactionMobx",
45
- "entrypoints": [
46
- "./index.ts"
47
- ]
48
- },
49
39
  "peerDependencies": {
50
- "coaction": "^0.1.5",
40
+ "coaction": "^0.2.0",
51
41
  "mobx": "^6.13.2",
52
42
  "mutative": "^1.1.0",
53
43
  "react": "^17.0.0 || ^18.0.0 || ^19.0.0"
@@ -67,20 +57,32 @@
67
57
  }
68
58
  },
69
59
  "devDependencies": {
70
- "@testing-library/react": "^14.2.1",
71
- "@types/jsdom": "^12.2.3",
72
- "@types/react": "^18.3.12",
73
- "@types/react-dom": "^18.3.0",
74
- "@types/use-sync-external-store": "^0.0.6",
75
- "coaction": "^0.1.5",
76
- "jsdom": "^25.0.1",
60
+ "@testing-library/react": "^16.3.2",
61
+ "@types/jsdom": "^27.0.0",
62
+ "@types/react": "^19.2.14",
63
+ "@types/react-dom": "^19.2.3",
64
+ "@types/use-sync-external-store": "^1.5.0",
65
+ "coaction": "^0.2.0",
66
+ "data-transport": "^5.0.3",
67
+ "jsdom": "^28.1.0",
77
68
  "jsdom-global": "^3.0.2",
78
- "mobx": "^6.13.2",
79
- "mutative": "^1.1.0",
80
- "react": "^18.0.0",
81
- "react-dom": "^18.0.0"
69
+ "mobx": "^6.15.0",
70
+ "mutative": "^1.3.0",
71
+ "react": "^19.2.4",
72
+ "react-dom": "^19.2.4"
82
73
  },
83
74
  "dependencies": {
84
- "mutability": "^1.1.1"
75
+ "mutability": "^1.2.1"
76
+ },
77
+ "publishConfig": {
78
+ "access": "public",
79
+ "provenance": true
80
+ },
81
+ "author": "unadlib",
82
+ "scripts": {
83
+ "clean": "rm -rf dist",
84
+ "test": "vitest run test",
85
+ "build": "tsup index.ts --format cjs,esm --dts --clean --out-dir dist",
86
+ "dev": "tsup index.ts --format cjs,esm --dts --watch --out-dir dist"
85
87
  }
86
- }
88
+ }
@@ -1,2 +0,0 @@
1
- export * from "./declarations/src/index.js";
2
- //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29hY3Rpb24tbW9ieC5janMuZC5tdHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuL2RlY2xhcmF0aW9ucy9zcmMvaW5kZXguZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9
@@ -1,2 +0,0 @@
1
- export * from "./declarations/src/index";
2
- //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29hY3Rpb24tbW9ieC5janMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4vZGVjbGFyYXRpb25zL3NyYy9pbmRleC5kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
@@ -1,62 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var mutability = require('mutability');
6
- var coaction = require('coaction');
7
- var mobx = require('mobx');
8
-
9
- const instancesMap = new WeakMap();
10
- const handleStore = (store, rawState, state, internal) => {
11
- if (internal.toMutableRaw) return;
12
- internal.toMutableRaw = key => instancesMap.get(key);
13
- Object.assign(store, {
14
- subscribe: mobx.autorun
15
- });
16
- internal.actMutable = mobx.runInAction;
17
- store.apply = (state = store.getState(), patches) => {
18
- if (!patches) {
19
- if (store.isSliceStore) {
20
- if (typeof state === 'object' && state !== null) {
21
- mobx.runInAction(() => {
22
- for (const key in state) {
23
- const _key = key;
24
- const _state = state[_key];
25
- if (typeof _state === 'object' && _state !== null) {
26
- Object.assign(store.getState()[_key], _state);
27
- }
28
- }
29
- });
30
- }
31
- } else {
32
- mobx.runInAction(() => {
33
- Object.assign(store.getState(), state);
34
- });
35
- }
36
- return;
37
- }
38
- mobx.runInAction(() => {
39
- mutability.apply(state, patches);
40
- });
41
- };
42
- };
43
- /**
44
- * Bind a store to Mobx
45
- */
46
- const bindMobx = coaction.createBinder({
47
- handleStore,
48
- handleState: options => {
49
- const descriptors = Object.getOwnPropertyDescriptors(options);
50
- const copyState = Object.defineProperties({}, descriptors);
51
- const rawState = Object.defineProperties({}, descriptors);
52
- return {
53
- copyState,
54
- bind: state => {
55
- instancesMap.set(rawState, state);
56
- return rawState;
57
- }
58
- };
59
- }
60
- });
61
-
62
- exports.bindMobx = bindMobx;
@@ -1,3 +0,0 @@
1
- export {
2
- bindMobx
3
- } from "./coaction-mobx.cjs.js";
@@ -1,58 +0,0 @@
1
- import { apply } from 'mutability';
2
- import { createBinder } from 'coaction';
3
- import { autorun, runInAction } from 'mobx';
4
-
5
- const instancesMap = new WeakMap();
6
- const handleStore = (store, rawState, state, internal) => {
7
- if (internal.toMutableRaw) return;
8
- internal.toMutableRaw = key => instancesMap.get(key);
9
- Object.assign(store, {
10
- subscribe: autorun
11
- });
12
- internal.actMutable = runInAction;
13
- store.apply = (state = store.getState(), patches) => {
14
- if (!patches) {
15
- if (store.isSliceStore) {
16
- if (typeof state === 'object' && state !== null) {
17
- runInAction(() => {
18
- for (const key in state) {
19
- const _key = key;
20
- const _state = state[_key];
21
- if (typeof _state === 'object' && _state !== null) {
22
- Object.assign(store.getState()[_key], _state);
23
- }
24
- }
25
- });
26
- }
27
- } else {
28
- runInAction(() => {
29
- Object.assign(store.getState(), state);
30
- });
31
- }
32
- return;
33
- }
34
- runInAction(() => {
35
- apply(state, patches);
36
- });
37
- };
38
- };
39
- /**
40
- * Bind a store to Mobx
41
- */
42
- const bindMobx = createBinder({
43
- handleStore,
44
- handleState: options => {
45
- const descriptors = Object.getOwnPropertyDescriptors(options);
46
- const copyState = Object.defineProperties({}, descriptors);
47
- const rawState = Object.defineProperties({}, descriptors);
48
- return {
49
- copyState,
50
- bind: state => {
51
- instancesMap.set(rawState, state);
52
- return rawState;
53
- }
54
- };
55
- }
56
- });
57
-
58
- export { bindMobx };
@@ -1,2 +0,0 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("mutative"),require("coaction"),require("mobx")):"function"==typeof define&&define.amd?define(["exports","mutative","coaction","mobx"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).CoactionMobx={},e.Mutative,e.Coaction,e.Mobx)}(this,(function(e,t,r,n){"use strict";const o="replace";function a(e){return Array.isArray(e)?1:e instanceof Map?2:e instanceof Set?3:0}function c(e,t){return 2===a(e)?e.get(t):e[t]}function i(e){if("object"!=typeof e||null===e)return e;if(Array.isArray(e))return e.map(i);if(e instanceof Map)return new Map(Array.from(e.entries()).map((([e,t])=>[e,i(t)])));if(e instanceof Set)return new Set(Array.from(e).map(i));const t=Object.create(Object.getPrototypeOf(e));for(const r in e)t[r]=i(e[r]);return t}const s=new WeakMap,u=r.createBinder({handleStore:(e,t,r,u)=>{u.toMutableRaw||(u.toMutableRaw=e=>s.get(e),Object.assign(e,{subscribe:n.autorun}),u.actMutable=n.runInAction,e.apply=(t=e.getState(),r)=>{r?n.runInAction((()=>{!function(e,t){let r;for(r=t.length-1;r>=0;r-=1){const{value:n,op:a,path:c}=t[r];if(!c.length&&a===o||""===c&&"add"===a){e=n;break}}r>-1&&(t=t.slice(r+1)),t.forEach((t=>{const{path:r,op:n}=t,s=function(e){return Array.isArray(e)?e:e.split("/").map((e=>e.replace(/~1/g,"/").replace(/~0/g,"~"))).slice(1)}(r);let u=e;for(let e=0;e<s.length-1;e+=1){const t=a(u);let r=s[e];if("string"!=typeof r&&"number"!=typeof r&&(r=String(r)),(0===t||1===t)&&("__proto__"===r||"constructor"===r)||"function"==typeof u&&"prototype"===r)throw new Error("Patching reserved attributes like __proto__ and constructor is not allowed.");if(u=c(3===a(u)?Array.from(u):u,r),"object"!=typeof u)throw new Error(`Cannot apply patch at '${s.join("/")}'.`)}const p=a(u),f=i(t.value),l=s[s.length-1];switch(n){case o:switch(p){case 2:return u.set(l,f);case 3:throw new Error("Cannot apply replace patch to set.");default:return u[l]=f}case"add":switch(p){case 1:return"-"===l?u.push(f):u.splice(l,0,f);case 2:return u.set(l,f);case 3:return u.add(f);default:return u[l]=f}case"remove":switch(p){case 1:return u.splice(l,1);case 2:return u.delete(l);case 3:return u.delete(t.value);default:return delete u[l]}default:throw new Error(`Unsupported patch operation: ${n}.`)}}))}(t,r)})):e.isSliceStore?"object"==typeof t&&null!==t&&n.runInAction((()=>{for(const r in t){const n=r,o=t[n];"object"==typeof o&&null!==o&&Object.assign(e.getState()[n],o)}})):n.runInAction((()=>{Object.assign(e.getState(),t)}))})},handleState:e=>{const t=Object.getOwnPropertyDescriptors(e),r=Object.defineProperties({},t),n=Object.defineProperties({},t);return{copyState:r,bind:e=>(s.set(n,e),n)}}});e.bindMobx=u,Object.defineProperty(e,"__esModule",{value:!0})}));
2
- //# sourceMappingURL=coaction-mobx.umd.min.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"coaction-mobx.umd.min.js","sources":["../../../node_modules/mutability/dist/index.esm.js","../src/index.ts"],"sourcesContent":["import{create as e}from\"mutative\";const t=\"remove\",r=\"replace\",n=\"add\";function o(e){return Array.isArray(e)?1:e instanceof Map?2:e instanceof Set?3:0}function a(e,t){return 2===o(e)?e.get(t):e[t]}function c(e){if(\"object\"!=typeof e||null===e)return e;if(Array.isArray(e))return e.map(c);if(e instanceof Map)return new Map(Array.from(e.entries()).map((([e,t])=>[e,c(t)])));if(e instanceof Set)return new Set(Array.from(e).map(c));const t=Object.create(Object.getPrototypeOf(e));for(const r in e)t[r]=c(e[r]);return t}function s(e,s){let i;for(i=s.length-1;i>=0;i-=1){const{value:t,op:o,path:a}=s[i];if(!a.length&&o===r||\"\"===a&&o===n){e=t;break}}i>-1&&(s=s.slice(i+1)),s.forEach((s=>{const{path:i,op:p}=s,u=function(e){return Array.isArray(e)?e:e.split(\"/\").map((e=>e.replace(/~1/g,\"/\").replace(/~0/g,\"~\"))).slice(1)}(i);let f=e;for(let e=0;e<u.length-1;e+=1){const t=o(f);let r=u[e];if(\"string\"!=typeof r&&\"number\"!=typeof r&&(r=String(r)),(0===t||1===t)&&(\"__proto__\"===r||\"constructor\"===r)||\"function\"==typeof f&&\"prototype\"===r)throw new Error(\"Patching reserved attributes like __proto__ and constructor is not allowed.\");if(f=a(3===o(f)?Array.from(f):f,r),\"object\"!=typeof f)throw new Error(`Cannot apply patch at '${u.join(\"/\")}'.`)}const l=o(f),h=c(s.value),y=u[u.length-1];switch(p){case r:switch(l){case 2:return f.set(y,h);case 3:throw new Error(\"Cannot apply replace patch to set.\");default:return f[y]=h}case n:switch(l){case 1:return\"-\"===y?f.push(h):f.splice(y,0,h);case 2:return f.set(y,h);case 3:return f.add(h);default:return f[y]=h}case t:switch(l){case 1:return f.splice(y,1);case 2:return f.delete(y);case 3:return f.delete(s.value);default:return delete f[y]}default:throw new Error(`Unsupported patch operation: ${p}.`)}}))}const i=(t,r)=>{const[,n,o]=e(t,r,{enablePatches:!0});return s(t,n),{inversePatches:o,patches:n}};export{s as apply,i as mutate};\n//# sourceMappingURL=index.esm.js.map\n","import { apply } from 'mutability';\nimport { type Store, createBinder } from 'coaction';\nimport { autorun, runInAction } from 'mobx';\n\nconst instancesMap = new WeakMap<object, object>();\n\nconst handleStore = (\n store: Store<object>,\n rawState: object,\n state: object,\n internal: any\n) => {\n if (internal.toMutableRaw) return;\n internal.toMutableRaw = (key: object) => instancesMap.get(key);\n Object.assign(store, {\n subscribe: autorun\n });\n internal.actMutable = runInAction;\n store.apply = (state = store.getState(), patches) => {\n if (!patches) {\n if (store.isSliceStore) {\n if (typeof state === 'object' && state !== null) {\n runInAction(() => {\n for (const key in state) {\n const _key = key as keyof typeof state;\n const _state = state[_key];\n if (typeof _state === 'object' && _state !== null) {\n Object.assign(store.getState()[_key], _state);\n }\n }\n });\n }\n } else {\n runInAction(() => {\n Object.assign(store.getState(), state);\n });\n }\n return;\n }\n runInAction(() => {\n apply(state, patches!);\n });\n };\n};\n\ninterface BindMobx {\n <T>(target: T): T;\n}\n\n/**\n * Bind a store to Mobx\n */\nexport const bindMobx = createBinder<BindMobx>({\n handleStore,\n handleState: (options) => {\n const descriptors = Object.getOwnPropertyDescriptors(options);\n const copyState = Object.defineProperties(\n {},\n descriptors\n ) as typeof options;\n const rawState = Object.defineProperties({}, descriptors) as typeof options;\n return {\n copyState,\n bind: (state) => {\n instancesMap.set(rawState, state);\n return rawState;\n }\n };\n }\n});\n"],"names":["r","o","e","Array","isArray","Map","Set","a","t","get","c","map","from","entries","Object","create","getPrototypeOf","instancesMap","WeakMap","bindMobx","createBinder","handleStore","store","rawState","state","internal","toMutableRaw","key","assign","subscribe","autorun","actMutable","runInAction","apply","getState","patches","s","i","length","value","op","path","slice","forEach","p","u","split","replace","f","String","Error","join","l","h","y","set","push","splice","add","delete","isSliceStore","_key","_state","handleState","options","descriptors","getOwnPropertyDescriptors","copyState","defineProperties","bind"],"mappings":"4WAAkC,MAAiBA,EAAE,UAAkB,SAASC,EAAEC,GAAG,OAAOC,MAAMC,QAAQF,GAAG,EAAEA,aAAaG,IAAI,EAAEH,aAAaI,IAAI,EAAE,CAAC,CAAC,SAASC,EAAEL,EAAEM,GAAG,OAAO,IAAIP,EAAEC,GAAGA,EAAEO,IAAID,GAAGN,EAAEM,EAAE,CAAC,SAASE,EAAER,GAAG,GAAG,iBAAiBA,GAAG,OAAOA,EAAE,OAAOA,EAAE,GAAGC,MAAMC,QAAQF,GAAG,OAAOA,EAAES,IAAID,GAAG,GAAGR,aAAaG,IAAI,OAAO,IAAIA,IAAIF,MAAMS,KAAKV,EAAEW,WAAWF,OAAOT,EAAEM,KAAK,CAACN,EAAEQ,EAAEF,OAAO,GAAGN,aAAaI,IAAI,OAAO,IAAIA,IAAIH,MAAMS,KAAKV,GAAGS,IAAID,IAAI,MAAMF,EAAEM,OAAOC,OAAOD,OAAOE,eAAed,IAAI,IA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