@vve/immer 9.0.0-alpha.3 → 9.0.0-alpha.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/lib/proxy.js ADDED
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createProxy = createProxy;
7
+ exports.willFinalize = willFinalize;
8
+ var _common = require("./common");
9
+ var _scope = require("./scope");
10
+ // Do nothing before being finalized.
11
+ function willFinalize() {}
12
+ function createProxy(base, parent) {
13
+ var scope = parent ? parent.scope : _scope.ImmerScope.current;
14
+ var state = {
15
+ // Track which produce call this is associated with.
16
+ scope,
17
+ // True for both shallow and deep changes.
18
+ modified: false,
19
+ // Used during finalization.
20
+ finalized: false,
21
+ // Track which properties have been assigned (true) or deleted (false).
22
+ assigned: {},
23
+ // The parent draft state.
24
+ parent,
25
+ // The base state.
26
+ base,
27
+ // The base proxy.
28
+ draft: null,
29
+ // Any property proxies.
30
+ drafts: {},
31
+ // The base copy with any updated values.
32
+ copy: null,
33
+ // Called by the `produce` function.
34
+ revoke: null
35
+ };
36
+ var _ref = Array.isArray(base) ?
37
+ // [state] is used for arrays, to make sure the proxy is array-ish and not violate invariants,
38
+ // although state itself is an object
39
+ Proxy.revocable([state], arrayTraps) : Proxy.revocable(state, objectTraps),
40
+ revoke = _ref.revoke,
41
+ proxy = _ref.proxy;
42
+ state.draft = proxy;
43
+ state.revoke = revoke;
44
+ scope.drafts.push(proxy);
45
+ return proxy;
46
+ }
47
+ var objectTraps = {
48
+ get,
49
+ has(target, prop) {
50
+ return prop in source(target);
51
+ },
52
+ ownKeys(target) {
53
+ return Reflect.ownKeys(source(target));
54
+ },
55
+ set,
56
+ deleteProperty,
57
+ getOwnPropertyDescriptor,
58
+ defineProperty() {
59
+ throw new Error("Object.defineProperty() cannot be used on an Immer draft"); // prettier-ignore
60
+ },
61
+
62
+ getPrototypeOf(target) {
63
+ return Object.getPrototypeOf(target.base);
64
+ },
65
+ setPrototypeOf() {
66
+ throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft"); // prettier-ignore
67
+ }
68
+ };
69
+
70
+ var arrayTraps = {};
71
+ (0, _common.each)(objectTraps, (key, fn) => {
72
+ arrayTraps[key] = function () {
73
+ arguments[0] = arguments[0][0];
74
+ return fn.apply(this, arguments);
75
+ };
76
+ });
77
+ arrayTraps.deleteProperty = function (state, prop) {
78
+ if (isNaN(parseInt(prop))) {
79
+ throw new Error("Immer only supports deleting array indices"); // prettier-ignore
80
+ }
81
+
82
+ return objectTraps.deleteProperty.call(this, state[0], prop);
83
+ };
84
+ arrayTraps.set = function (state, prop, value) {
85
+ if (prop !== "length" && isNaN(parseInt(prop))) {
86
+ throw new Error("Immer only supports setting array indices and the 'length' property"); // prettier-ignore
87
+ }
88
+
89
+ return objectTraps.set.call(this, state[0], prop, value);
90
+ };
91
+
92
+ // returns the object we should be reading the current value from, which is base, until some change has been made
93
+ function source(state) {
94
+ return state.copy || state.base;
95
+ }
96
+
97
+ // Access a property without creating an Immer draft.
98
+ function peek(draft, prop) {
99
+ var state = draft[_common.DRAFT_STATE];
100
+ var desc = Reflect.getOwnPropertyDescriptor(state ? source(state) : draft, prop);
101
+ return desc && desc.value;
102
+ }
103
+ function get(state, prop) {
104
+ if (prop === _common.DRAFT_STATE) return state;
105
+ var drafts = state.drafts;
106
+
107
+ // Check for existing draft in unmodified state.
108
+ if (!state.modified && (0, _common.has)(drafts, prop)) {
109
+ return drafts[prop];
110
+ }
111
+ var value = source(state)[prop];
112
+ if (state.finalized || !(0, _common.isDraftable)(value)) {
113
+ return value;
114
+ }
115
+
116
+ // Check for existing draft in modified state.
117
+ if (state.modified) {
118
+ // Assigned values are never drafted. This catches any drafts we created, too.
119
+ if (value !== peek(state.base, prop)) return value;
120
+ // Store drafts on the copy (when one exists).
121
+ drafts = state.copy;
122
+ }
123
+ return drafts[prop] = createProxy(value, state);
124
+ }
125
+ function set(state, prop, value) {
126
+ if (!state.modified) {
127
+ var baseValue = peek(state.base, prop);
128
+ // Optimize based on value's truthiness. Truthy values are guaranteed to
129
+ // never be undefined, so we can avoid the `in` operator. Lastly, truthy
130
+ // values may be drafts, but falsy values are never drafts.
131
+ var isUnchanged = value ? (0, _common.is)(baseValue, value) || value === state.drafts[prop] : (0, _common.is)(baseValue, value) && prop in state.base;
132
+ if (isUnchanged) return true;
133
+ markChanged(state);
134
+ }
135
+ state.assigned[prop] = true;
136
+ state.copy[prop] = value;
137
+ return true;
138
+ }
139
+ function deleteProperty(state, prop) {
140
+ // The `undefined` check is a fast path for pre-existing keys.
141
+ if (peek(state.base, prop) !== undefined || prop in state.base) {
142
+ state.assigned[prop] = false;
143
+ markChanged(state);
144
+ }
145
+ if (state.copy) delete state.copy[prop];
146
+ return true;
147
+ }
148
+
149
+ // Note: We never coerce `desc.value` into an Immer draft, because we can't make
150
+ // the same guarantee in ES5 mode.
151
+ function getOwnPropertyDescriptor(state, prop) {
152
+ var owner = source(state);
153
+ var desc = Reflect.getOwnPropertyDescriptor(owner, prop);
154
+ if (desc) {
155
+ desc.writable = true;
156
+ desc.configurable = !Array.isArray(owner) || prop !== "length";
157
+ }
158
+ return desc;
159
+ }
160
+ function markChanged(state) {
161
+ if (!state.modified) {
162
+ state.modified = true;
163
+ state.copy = (0, _common.assign)((0, _common.shallowCopy)(state.base), state.drafts);
164
+ state.drafts = null;
165
+ if (state.parent) markChanged(state.parent);
166
+ }
167
+ }
168
+ //# sourceMappingURL=proxy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.js","names":["Object","defineProperty","exports","value","createProxy","willFinalize","_common","require","_scope","base","parent","scope","ImmerScope","current","state","modified","finalized","assigned","draft","drafts","copy","revoke","_ref","Array","isArray","Proxy","revocable","arrayTraps","objectTraps","proxy","push","get","has","target","prop","source","ownKeys","Reflect","set","deleteProperty","getOwnPropertyDescriptor","Error","getPrototypeOf","setPrototypeOf","each","key","fn","arguments","apply","isNaN","parseInt","call","peek","DRAFT_STATE","desc","isDraftable","baseValue","isUnchanged","is","markChanged","undefined","owner","writable","configurable","assign","shallowCopy"],"sources":["../proxy.js"],"sourcesContent":["\"use strict\"\nimport {\n assign,\n each,\n has,\n is,\n isDraftable,\n isDraft,\n shallowCopy,\n DRAFT_STATE\n} from \"./common\"\nimport {ImmerScope} from \"./scope\"\n\n// Do nothing before being finalized.\nexport function willFinalize() {}\n\nexport function createProxy(base, parent) {\n const scope = parent ? parent.scope : ImmerScope.current\n const state = {\n // Track which produce call this is associated with.\n scope,\n // True for both shallow and deep changes.\n modified: false,\n // Used during finalization.\n finalized: false,\n // Track which properties have been assigned (true) or deleted (false).\n assigned: {},\n // The parent draft state.\n parent,\n // The base state.\n base,\n // The base proxy.\n draft: null,\n // Any property proxies.\n drafts: {},\n // The base copy with any updated values.\n copy: null,\n // Called by the `produce` function.\n revoke: null\n }\n\n const {revoke, proxy} = Array.isArray(base)\n ? // [state] is used for arrays, to make sure the proxy is array-ish and not violate invariants,\n // although state itself is an object\n Proxy.revocable([state], arrayTraps)\n : Proxy.revocable(state, objectTraps)\n\n state.draft = proxy\n state.revoke = revoke\n\n scope.drafts.push(proxy)\n return proxy\n}\n\nconst objectTraps = {\n get,\n has(target, prop) {\n return prop in source(target)\n },\n ownKeys(target) {\n return Reflect.ownKeys(source(target))\n },\n set,\n deleteProperty,\n getOwnPropertyDescriptor,\n defineProperty() {\n throw new Error(\"Object.defineProperty() cannot be used on an Immer draft\") // prettier-ignore\n },\n getPrototypeOf(target) {\n return Object.getPrototypeOf(target.base)\n },\n setPrototypeOf() {\n throw new Error(\"Object.setPrototypeOf() cannot be used on an Immer draft\") // prettier-ignore\n }\n}\n\nconst arrayTraps = {}\neach(objectTraps, (key, fn) => {\n arrayTraps[key] = function() {\n arguments[0] = arguments[0][0]\n return fn.apply(this, arguments)\n }\n})\narrayTraps.deleteProperty = function(state, prop) {\n if (isNaN(parseInt(prop))) {\n throw new Error(\"Immer only supports deleting array indices\") // prettier-ignore\n }\n return objectTraps.deleteProperty.call(this, state[0], prop)\n}\narrayTraps.set = function(state, prop, value) {\n if (prop !== \"length\" && isNaN(parseInt(prop))) {\n throw new Error(\"Immer only supports setting array indices and the 'length' property\") // prettier-ignore\n }\n return objectTraps.set.call(this, state[0], prop, value)\n}\n\n// returns the object we should be reading the current value from, which is base, until some change has been made\nfunction source(state) {\n return state.copy || state.base\n}\n\n// Access a property without creating an Immer draft.\nfunction peek(draft, prop) {\n const state = draft[DRAFT_STATE]\n const desc = Reflect.getOwnPropertyDescriptor(\n state ? source(state) : draft,\n prop\n )\n return desc && desc.value\n}\n\nfunction get(state, prop) {\n if (prop === DRAFT_STATE) return state\n let {drafts} = state\n\n // Check for existing draft in unmodified state.\n if (!state.modified && has(drafts, prop)) {\n return drafts[prop]\n }\n\n const value = source(state)[prop]\n if (state.finalized || !isDraftable(value)) {\n return value\n }\n\n // Check for existing draft in modified state.\n if (state.modified) {\n // Assigned values are never drafted. This catches any drafts we created, too.\n if (value !== peek(state.base, prop)) return value\n // Store drafts on the copy (when one exists).\n drafts = state.copy\n }\n\n return (drafts[prop] = createProxy(value, state))\n}\n\nfunction set(state, prop, value) {\n if (!state.modified) {\n const baseValue = peek(state.base, prop)\n // Optimize based on value's truthiness. Truthy values are guaranteed to\n // never be undefined, so we can avoid the `in` operator. Lastly, truthy\n // values may be drafts, but falsy values are never drafts.\n const isUnchanged = value\n ? is(baseValue, value) || value === state.drafts[prop]\n : is(baseValue, value) && prop in state.base\n if (isUnchanged) return true\n markChanged(state)\n }\n state.assigned[prop] = true\n state.copy[prop] = value\n return true\n}\n\nfunction deleteProperty(state, prop) {\n // The `undefined` check is a fast path for pre-existing keys.\n if (peek(state.base, prop) !== undefined || prop in state.base) {\n state.assigned[prop] = false\n markChanged(state)\n }\n if (state.copy) delete state.copy[prop]\n return true\n}\n\n// Note: We never coerce `desc.value` into an Immer draft, because we can't make\n// the same guarantee in ES5 mode.\nfunction getOwnPropertyDescriptor(state, prop) {\n const owner = source(state)\n const desc = Reflect.getOwnPropertyDescriptor(owner, prop)\n if (desc) {\n desc.writable = true\n desc.configurable = !Array.isArray(owner) || prop !== \"length\"\n }\n return desc\n}\n\nfunction markChanged(state) {\n if (!state.modified) {\n state.modified = true\n state.copy = assign(shallowCopy(state.base), state.drafts)\n state.drafts = null\n if (state.parent) markChanged(state.parent)\n }\n}\n"],"mappings":"AAAA,YAAY;;AAAAA,MAAA,CAAAC,cAAA,CAAAC,OAAA;EAAAC,KAAA;AAAA;AAAAD,OAAA,CAAAE,WAAA,GAAAA,WAAA;AAAAF,OAAA,CAAAG,YAAA,GAAAA,YAAA;AACZ,IAAAC,OAAA,GAAAC,OAAA;AAUA,IAAAC,MAAA,GAAAD,OAAA;AAEA;AACO,SAASF,YAAYA,CAAA,EAAG,CAAC;AAEzB,SAASD,WAAWA,CAACK,IAAI,EAAEC,MAAM,EAAE;EACtC,IAAMC,KAAK,GAAGD,MAAM,GAAGA,MAAM,CAACC,KAAK,GAAGC,iBAAU,CAACC,OAAO;EACxD,IAAMC,KAAK,GAAG;IACV;IACAH,KAAK;IACL;IACAI,QAAQ,EAAE,KAAK;IACf;IACAC,SAAS,EAAE,KAAK;IAChB;IACAC,QAAQ,EAAE,CAAC,CAAC;IACZ;IACAP,MAAM;IACN;IACAD,IAAI;IACJ;IACAS,KAAK,EAAE,IAAI;IACX;IACAC,MAAM,EAAE,CAAC,CAAC;IACV;IACAC,IAAI,EAAE,IAAI;IACV;IACAC,MAAM,EAAE;EACZ,CAAC;EAED,IAAAC,IAAA,GAAwBC,KAAK,CAACC,OAAO,CAACf,IAAI,CAAC;IACrC;IACA;IACAgB,KAAK,CAACC,SAAS,CAAC,CAACZ,KAAK,CAAC,EAAEa,UAAU,CAAC,GACpCF,KAAK,CAACC,SAAS,CAACZ,KAAK,EAAEc,WAAW,CAAC;IAJlCP,MAAM,GAAAC,IAAA,CAAND,MAAM;IAAEQ,KAAK,GAAAP,IAAA,CAALO,KAAK;EAMp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,EAAEyB,IAAI,CAAC,KAAK0B,SAAS,IAAI1B,IAAI,IAAIpB,KAAK,CAACL,IAAI,EAAE;IAC5DK,KAAK,CAACG,QAAQ,CAACiB,IAAI,CAAC,GAAG,KAAK;IAC5ByB,WAAW,CAAC7C,KAAK,CAAC;EACtB;EACA,IAAIA,KAAK,CAACM,IAAI,EAAE,OAAON,KAAK,CAACM,IAAI,CAACc,IAAI,CAAC;EACvC,OAAO,IAAI;AACf;;AAEA;AACA;AACA,SAASM,wBAAwBA,CAAC1B,KAAK,EAAEoB,IAAI,EAAE;EAC3C,IAAM2B,KAAK,GAAG1B,MAAM,CAACrB,KAAK,CAAC;EAC3B,IAAMwC,IAAI,GAAGjB,OAAO,CAACG,wBAAwB,CAACqB,KAAK,EAAE3B,IAAI,CAAC;EAC1D,IAAIoB,IAAI,EAAE;IACNA,IAAI,CAACQ,QAAQ,GAAG,IAAI;IACpBR,IAAI,CAACS,YAAY,GAAG,CAACxC,KAAK,CAACC,OAAO,CAACqC,KAAK,CAAC,IAAI3B,IAAI,KAAK,QAAQ;EAClE;EACA,OAAOoB,IAAI;AACf;AAEA,SAASK,WAAWA,CAAC7C,KAAK,EAAE;EACxB,IAAI,CAACA,KAAK,CAACC,QAAQ,EAAE;IACjBD,KAAK,CAACC,QAAQ,GAAG,IAAI;IACrBD,KAAK,CAACM,IAAI,GAAG,IAAA4C,cAAM,EAAC,IAAAC,mBAAW,EAACnD,KAAK,CAACL,IAAI,CAAC,EAAEK,KAAK,CAACK,MAAM,CAAC;IAC1DL,KAAK,CAACK,MAAM,GAAG,IAAI;IACnB,IAAIL,KAAK,CAACJ,MAAM,EAAEiD,WAAW,CAAC7C,KAAK,CAACJ,MAAM,CAAC;EAC/C;AACJ"}
package/lib/scope.js ADDED
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ImmerScope = void 0;
7
+ var _common = require("./common");
8
+ /** Each scope represents a `produce` call. */
9
+ class ImmerScope {
10
+ constructor(parent) {
11
+ this.drafts = [];
12
+ this.parent = parent;
13
+
14
+ // Whenever the modified draft contains a draft from another scope, we
15
+ // need to prevent auto-freezing so the unowned draft can be finalized.
16
+ this.canAutoFreeze = true;
17
+
18
+ // To avoid prototype lookups:
19
+ this.patches = null;
20
+ }
21
+ usePatches(patchListener) {
22
+ if (patchListener) {
23
+ this.patches = [];
24
+ this.inversePatches = [];
25
+ this.patchListener = patchListener;
26
+ }
27
+ }
28
+ revoke(keepAlive) {
29
+ this.leave();
30
+ keepAlive || this.drafts.forEach(revoke);
31
+ this.drafts = null; // Make draft-related methods throw.
32
+ }
33
+
34
+ leave() {
35
+ if (this === ImmerScope.current) {
36
+ ImmerScope.current = this.parent;
37
+ }
38
+ }
39
+ }
40
+ exports.ImmerScope = ImmerScope;
41
+ ImmerScope.current = null;
42
+ ImmerScope.enter = function () {
43
+ return this.current = new ImmerScope(this.current);
44
+ };
45
+ function revoke(draft) {
46
+ draft[_common.DRAFT_STATE].revoke();
47
+ }
48
+ //# sourceMappingURL=scope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scope.js","names":["_common","require","ImmerScope","constructor","parent","drafts","canAutoFreeze","patches","usePatches","patchListener","inversePatches","revoke","keepAlive","leave","forEach","current","exports","enter","draft","DRAFT_STATE"],"sources":["../scope.js"],"sourcesContent":["import {DRAFT_STATE} from \"./common\"\n\n/** Each scope represents a `produce` call. */\nexport class ImmerScope {\n constructor(parent) {\n this.drafts = []\n this.parent = parent\n\n // Whenever the modified draft contains a draft from another scope, we\n // need to prevent auto-freezing so the unowned draft can be finalized.\n this.canAutoFreeze = true\n\n // To avoid prototype lookups:\n this.patches = null\n }\n usePatches(patchListener) {\n if (patchListener) {\n this.patches = []\n this.inversePatches = []\n this.patchListener = patchListener\n }\n }\n revoke(keepAlive) {\n this.leave()\n keepAlive || this.drafts.forEach(revoke)\n this.drafts = null // Make draft-related methods throw.\n }\n leave() {\n if (this === ImmerScope.current) {\n ImmerScope.current = this.parent\n }\n }\n}\n\nImmerScope.current = null\nImmerScope.enter = function() {\n return (this.current = new ImmerScope(this.current))\n}\n\nfunction revoke(draft) {\n draft[DRAFT_STATE].revoke()\n}\n"],"mappings":";;;;;;AAAA,IAAAA,OAAA,GAAAC,OAAA;AAEA;AACO,MAAMC,UAAU,CAAC;EACpBC,WAAWA,CAACC,MAAM,EAAE;IAChB,IAAI,CAACC,MAAM,GAAG,EAAE;IAChB,IAAI,CAACD,MAAM,GAAGA,MAAM;;IAEpB;IACA;IACA,IAAI,CAACE,aAAa,GAAG,IAAI;;IAEzB;IACA,IAAI,CAACC,OAAO,GAAG,IAAI;EACvB;EACAC,UAAUA,CAACC,aAAa,EAAE;IACtB,IAAIA,aAAa,EAAE;MACf,IAAI,CAACF,OAAO,GAAG,EAAE;MACjB,IAAI,CAACG,cAAc,GAAG,EAAE;MACxB,IAAI,CAACD,aAAa,GAAGA,aAAa;IACtC;EACJ;EACAE,MAAMA,CAACC,SAAS,EAAE;IACd,IAAI,CAACC,KAAK,EAAE;IACZD,SAAS,IAAI,IAAI,CAACP,MAAM,CAACS,OAAO,CAACH,MAAM,CAAC;IACxC,IAAI,CAACN,MAAM,GAAG,IAAI,EAAC;EACvB;;EACAQ,KAAKA,CAAA,EAAG;IACJ,IAAI,IAAI,KAAKX,UAAU,CAACa,OAAO,EAAE;MAC7Bb,UAAU,CAACa,OAAO,GAAG,IAAI,CAACX,MAAM;IACpC;EACJ;AACJ;AAACY,OAAA,CAAAd,UAAA,GAAAA,UAAA;AAEDA,UAAU,CAACa,OAAO,GAAG,IAAI;AACzBb,UAAU,CAACe,KAAK,GAAG,YAAW;EAC1B,OAAQ,IAAI,CAACF,OAAO,GAAG,IAAIb,UAAU,CAAC,IAAI,CAACa,OAAO,CAAC;AACvD,CAAC;AAED,SAASJ,MAAMA,CAACO,KAAK,EAAE;EACnBA,KAAK,CAACC,mBAAW,CAAC,CAACR,MAAM,EAAE;AAC/B"}
package/package.json CHANGED
@@ -1,15 +1,14 @@
1
1
  {
2
2
  "name": "@vve/immer",
3
- "version": "9.0.0-alpha.3",
3
+ "version": "9.0.0-alpha.5",
4
4
  "description": "Create your next immutable state by mutating the current one",
5
- "main": "dist/immer.js",
6
- "umd:main": "dist/immer.umd.js",
7
- "unpkg": "dist/immer.umd.js",
8
- "jsdelivr": "dist/immer.umd.js",
9
- "module": "dist/immer.module.js",
10
- "jsnext:main": "dist/immer.module.js",
11
- "react-native": "dist/immer.module.js",
12
- "types": "./dist/immer.d.ts",
5
+ "main": "lib/index.js",
6
+ "umd:main": "umd/index.js",
7
+ "unpkg": "umd/index.min.js",
8
+ "jsdelivr": "umd/index.min.js",
9
+ "module": "es/index.js",
10
+ "jsnext:main": "es/index.js",
11
+ "react-native": "es/index.js",
13
12
  "repository": {
14
13
  "type": "git",
15
14
  "url": "https://github.com/immerjs/immer.git"
@@ -26,7 +25,9 @@
26
25
  },
27
26
  "homepage": "https://github.com/immerjs/immer#readme",
28
27
  "files": [
29
- "dist",
28
+ "es",
29
+ "lib",
30
+ "umd",
30
31
  "src"
31
32
  ],
32
33
  "devDependencies": {
@@ -72,7 +73,7 @@
72
73
  "test:perf": "NODE_ENV=production yarn-or-npm build && cd __performance_tests__ && babel-node add-data.js && babel-node todo.js && babel-node incremental.js",
73
74
  "test:dts": "tsc -p __tests__/tsconfig.json --noEmit",
74
75
  "coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage",
75
- "build": "rimraf dist/ && pnpx bili && cpx 'src/immer.d.ts' dist -v",
76
+ "build": "lecp",
76
77
  "typed": "cpx 'src/immer.d.ts' dist -v"
77
78
  }
78
79
  }