@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/immer.js ADDED
@@ -0,0 +1,272 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Immer = void 0;
7
+ var legacyProxy = _interopRequireWildcard(require("./es5"));
8
+ var modernProxy = _interopRequireWildcard(require("./proxy"));
9
+ var _patches = require("./patches");
10
+ var _common = require("./common");
11
+ var _scope = require("./scope");
12
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
14
+ function verifyMinified() {}
15
+ var configDefaults = {
16
+ useProxies: typeof Proxy !== "undefined" && typeof Reflect !== "undefined",
17
+ autoFreeze: typeof process !== "undefined" ? process.env.NODE_ENV !== "production" : verifyMinified.name === "verifyMinified",
18
+ onAssign: null,
19
+ onDelete: null,
20
+ onCopy: null
21
+ };
22
+ class Immer {
23
+ constructor(config) {
24
+ (0, _common.assign)(this, configDefaults, config);
25
+ this.setUseProxies(this.useProxies);
26
+ this.produce = this.produce.bind(this);
27
+ }
28
+ produce(base, recipe, patchListener) {
29
+ // curried invocation
30
+ if (typeof base === "function" && typeof recipe !== "function") {
31
+ var defaultBase = recipe;
32
+ recipe = base;
33
+ var self = this;
34
+ return function curriedProduce() {
35
+ var base = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultBase;
36
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
37
+ args[_key - 1] = arguments[_key];
38
+ }
39
+ return self.produce(base, draft => recipe.call(this, draft, ...args)); // prettier-ignore
40
+ };
41
+ }
42
+
43
+ // prettier-ignore
44
+ {
45
+ if (typeof recipe !== "function") {
46
+ throw new Error("The first or second argument to `produce` must be a function");
47
+ }
48
+ if (patchListener !== undefined && typeof patchListener !== "function") {
49
+ throw new Error("The third argument to `produce` must be a function or undefined");
50
+ }
51
+ }
52
+ var result;
53
+
54
+ // Only plain objects, arrays, and "immerable classes" are drafted.
55
+ if ((0, _common.isDraftable)(base)) {
56
+ var scope = _scope.ImmerScope.enter();
57
+ var proxy = this.createProxy(base);
58
+ var hasError = true;
59
+ try {
60
+ result = recipe(proxy);
61
+ hasError = false;
62
+ } finally {
63
+ // finally instead of catch + rethrow better preserves original stack
64
+ if (hasError) scope.revoke();else scope.leave();
65
+ }
66
+ if (result instanceof Promise) {
67
+ return result.then(result => {
68
+ scope.usePatches(patchListener);
69
+ return this.processResult(result, scope);
70
+ }, error => {
71
+ scope.revoke();
72
+ throw error;
73
+ });
74
+ }
75
+ scope.usePatches(patchListener);
76
+ return this.processResult(result, scope);
77
+ } else {
78
+ result = recipe(base);
79
+ if (result === undefined) return base;
80
+ return result !== _common.NOTHING ? result : undefined;
81
+ }
82
+ }
83
+ createDraft(base) {
84
+ if (!(0, _common.isDraftable)(base)) {
85
+ throw new Error("First argument to `createDraft` must be a plain object, an array, or an immerable object"); // prettier-ignore
86
+ }
87
+
88
+ var scope = _scope.ImmerScope.enter();
89
+ var proxy = this.createProxy(base, null);
90
+ proxy[_common.DRAFT_STATE].isManual = true;
91
+ scope.leave();
92
+ return proxy;
93
+ }
94
+ finishDraft(draft, patchListener, keepAlive) {
95
+ var state = draft && draft[_common.DRAFT_STATE];
96
+ if (!state || !state.isManual) {
97
+ throw new Error("First argument to `finishDraft` must be a draft returned by `createDraft`"); // prettier-ignore
98
+ }
99
+
100
+ if (state.finalized) {
101
+ throw new Error("The given draft is already finalized"); // prettier-ignore
102
+ }
103
+
104
+ var scope = state.scope;
105
+ scope.usePatches(patchListener);
106
+ return this.processResult(undefined, scope, keepAlive);
107
+ }
108
+ setAutoFreeze(value) {
109
+ this.autoFreeze = value;
110
+ }
111
+ setUseProxies(value) {
112
+ this.useProxies = value;
113
+ (0, _common.assign)(this, value ? modernProxy : legacyProxy);
114
+ }
115
+ applyPatches(base, patches) {
116
+ // Mutate the base state when a draft is passed.
117
+ if ((0, _common.isDraft)(base)) {
118
+ return (0, _patches.applyPatches)(base, patches);
119
+ }
120
+ // Otherwise, produce a copy of the base state.
121
+ return this.produce(base, draft => (0, _patches.applyPatches)(draft, patches));
122
+ }
123
+ /** @internal */
124
+ processResult(result, scope, keepAlive) {
125
+ var baseDraft = scope.drafts[0];
126
+ var isReplaced = result !== undefined && result !== baseDraft;
127
+ this.willFinalize(scope, result, isReplaced);
128
+ if (isReplaced) {
129
+ if (baseDraft[_common.DRAFT_STATE].modified) {
130
+ scope.revoke(keepAlive);
131
+ throw new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."); // prettier-ignore
132
+ }
133
+
134
+ if ((0, _common.isDraftable)(result)) {
135
+ // Finalize the result in case it contains (or is) a subset of the draft.
136
+ result = this.finalize(result, null, scope);
137
+ }
138
+ if (scope.patches) {
139
+ scope.patches.push({
140
+ op: "replace",
141
+ path: [],
142
+ value: result
143
+ });
144
+ scope.inversePatches.push({
145
+ op: "replace",
146
+ path: [],
147
+ value: baseDraft[_common.DRAFT_STATE].base
148
+ });
149
+ }
150
+ } else {
151
+ // Finalize the base draft.
152
+ result = this.finalize(baseDraft, [], scope, keepAlive);
153
+ }
154
+ scope.revoke(keepAlive);
155
+ if (scope.patches) {
156
+ scope.patchListener(scope.patches, scope.inversePatches);
157
+ }
158
+ return result !== _common.NOTHING ? result : undefined;
159
+ }
160
+ /**
161
+ * @internal
162
+ * Finalize a draft, returning either the unmodified base state or a modified
163
+ * copy of the base state.
164
+ */
165
+ finalize(draft, path, scope, keepAlive) {
166
+ var state = draft[_common.DRAFT_STATE];
167
+ if (!state) {
168
+ if (Object.isFrozen(draft)) return draft;
169
+ return this.finalizeTree(draft, null, scope);
170
+ }
171
+ // Never finalize drafts owned by another scope.
172
+ if (state.scope !== scope) {
173
+ return draft;
174
+ }
175
+ if (!state.modified) {
176
+ return state.base;
177
+ }
178
+ if (!state.finalized) {
179
+ if (!keepAlive) state.finalized = true;
180
+ this.finalizeTree(state.draft, path, scope);
181
+ if (this.onDelete) {
182
+ // The `assigned` object is unreliable with ES5 drafts.
183
+ if (this.useProxies) {
184
+ var assigned = state.assigned;
185
+ for (var prop in assigned) {
186
+ if (!assigned[prop]) this.onDelete(state, prop);
187
+ }
188
+ } else {
189
+ var base = state.base,
190
+ copy = state.copy;
191
+ (0, _common.each)(base, prop => {
192
+ if (!(0, _common.has)(copy, prop)) this.onDelete(state, prop);
193
+ });
194
+ }
195
+ }
196
+ if (this.onCopy) {
197
+ this.onCopy(state);
198
+ }
199
+
200
+ // At this point, all descendants of `state.copy` have been finalized,
201
+ // so we can be sure that `scope.canAutoFreeze` is accurate.
202
+ if (this.autoFreeze && scope.canAutoFreeze) {
203
+ Object.freeze(state.copy);
204
+ }
205
+ if (path && scope.patches) {
206
+ (0, _patches.generatePatches)(state, path, scope.patches, scope.inversePatches);
207
+ }
208
+ }
209
+ return state.copy;
210
+ }
211
+ /**
212
+ * @internal
213
+ * Finalize all drafts in the given state tree.
214
+ */
215
+ finalizeTree(root, rootPath, scope) {
216
+ var state = root[_common.DRAFT_STATE];
217
+ if (state) {
218
+ if (!this.useProxies) {
219
+ // Create the final copy, with added keys and without deleted keys.
220
+ state.copy = (0, _common.shallowCopy)(state.draft, true);
221
+ }
222
+ root = state.copy;
223
+ }
224
+ var needPatches = !!rootPath && !!scope.patches;
225
+ var finalizeProperty = (prop, value, parent) => {
226
+ if (value === parent) {
227
+ throw Error("Immer forbids circular references");
228
+ }
229
+
230
+ // In the `finalizeTree` method, only the `root` object may be a draft.
231
+ var isDraftProp = !!state && parent === root;
232
+ if ((0, _common.isDraft)(value)) {
233
+ var path = isDraftProp && needPatches && !state.assigned[prop] ? rootPath.concat(prop) : null;
234
+
235
+ // Drafts owned by `scope` are finalized here.
236
+ value = this.finalize(value, path, scope);
237
+
238
+ // Drafts from another scope must prevent auto-freezing.
239
+ if ((0, _common.isDraft)(value)) {
240
+ scope.canAutoFreeze = false;
241
+ }
242
+
243
+ // Preserve non-enumerable properties.
244
+ if (Array.isArray(parent) || (0, _common.isEnumerable)(parent, prop)) {
245
+ parent[prop] = value;
246
+ } else {
247
+ Object.defineProperty(parent, prop, {
248
+ value
249
+ });
250
+ }
251
+
252
+ // Unchanged drafts are never passed to the `onAssign` hook.
253
+ if (isDraftProp && value === state.base[prop]) return;
254
+ }
255
+ // Unchanged draft properties are ignored.
256
+ else if (isDraftProp && (0, _common.is)(value, state.base[prop])) {
257
+ return;
258
+ }
259
+ // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.
260
+ else if ((0, _common.isDraftable)(value) && !Object.isFrozen(value)) {
261
+ (0, _common.each)(value, finalizeProperty);
262
+ }
263
+ if (isDraftProp && this.onAssign) {
264
+ this.onAssign(state, prop, value);
265
+ }
266
+ };
267
+ (0, _common.each)(root, finalizeProperty);
268
+ return root;
269
+ }
270
+ }
271
+ exports.Immer = Immer;
272
+ //# sourceMappingURL=immer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"immer.js","names":["legacyProxy","_interopRequireWildcard","require","modernProxy","_patches","_common","_scope","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","verifyMinified","configDefaults","useProxies","Proxy","Reflect","autoFreeze","process","env","NODE_ENV","name","onAssign","onDelete","onCopy","Immer","constructor","config","assign","setUseProxies","produce","bind","base","recipe","patchListener","defaultBase","self","curriedProduce","arguments","length","undefined","_len","args","Array","_key","draft","Error","result","isDraftable","scope","ImmerScope","enter","proxy","createProxy","hasError","revoke","leave","Promise","then","usePatches","processResult","error","NOTHING","createDraft","DRAFT_STATE","isManual","finishDraft","keepAlive","state","finalized","setAutoFreeze","value","applyPatches","patches","isDraft","baseDraft","drafts","isReplaced","willFinalize","modified","finalize","push","op","path","inversePatches","isFrozen","finalizeTree","assigned","prop","copy","each","canAutoFreeze","freeze","generatePatches","root","rootPath","shallowCopy","needPatches","finalizeProperty","parent","isDraftProp","concat","isArray","isEnumerable","is","exports"],"sources":["../immer.js"],"sourcesContent":["import * as legacyProxy from \"./es5\"\nimport * as modernProxy from \"./proxy\"\nimport {applyPatches, generatePatches} from \"./patches\"\nimport {\n assign,\n each,\n has,\n is,\n isDraft,\n isDraftable,\n isEnumerable,\n shallowCopy,\n DRAFT_STATE,\n NOTHING\n} from \"./common\"\nimport {ImmerScope} from \"./scope\"\n\nfunction verifyMinified() {}\n\nconst configDefaults = {\n useProxies: typeof Proxy !== \"undefined\" && typeof Reflect !== \"undefined\",\n autoFreeze:\n typeof process !== \"undefined\"\n ? process.env.NODE_ENV !== \"production\"\n : verifyMinified.name === \"verifyMinified\",\n onAssign: null,\n onDelete: null,\n onCopy: null\n}\n\nexport class Immer {\n constructor(config) {\n assign(this, configDefaults, config)\n this.setUseProxies(this.useProxies)\n this.produce = this.produce.bind(this)\n }\n produce(base, recipe, patchListener) {\n // curried invocation\n if (typeof base === \"function\" && typeof recipe !== \"function\") {\n const defaultBase = recipe\n recipe = base\n\n const self = this\n return function curriedProduce(base = defaultBase, ...args) {\n return self.produce(base, draft => recipe.call(this, draft, ...args)) // prettier-ignore\n }\n }\n\n // prettier-ignore\n {\n if (typeof recipe !== \"function\") {\n throw new Error(\"The first or second argument to `produce` must be a function\")\n }\n if (patchListener !== undefined && typeof patchListener !== \"function\") {\n throw new Error(\"The third argument to `produce` must be a function or undefined\")\n }\n }\n\n let result\n\n // Only plain objects, arrays, and \"immerable classes\" are drafted.\n if (isDraftable(base)) {\n const scope = ImmerScope.enter()\n const proxy = this.createProxy(base)\n let hasError = true\n try {\n result = recipe(proxy)\n hasError = false\n } finally {\n // finally instead of catch + rethrow better preserves original stack\n if (hasError) scope.revoke()\n else scope.leave()\n }\n if (result instanceof Promise) {\n return result.then(\n result => {\n scope.usePatches(patchListener)\n return this.processResult(result, scope)\n },\n error => {\n scope.revoke()\n throw error\n }\n )\n }\n scope.usePatches(patchListener)\n return this.processResult(result, scope)\n } else {\n result = recipe(base)\n if (result === undefined) return base\n return result !== NOTHING ? result : undefined\n }\n }\n createDraft(base) {\n if (!isDraftable(base)) {\n throw new Error(\"First argument to `createDraft` must be a plain object, an array, or an immerable object\") // prettier-ignore\n }\n const scope = ImmerScope.enter()\n const proxy = this.createProxy(base, null)\n proxy[DRAFT_STATE].isManual = true\n scope.leave()\n return proxy\n }\n finishDraft(draft, patchListener, keepAlive) {\n const state = draft && draft[DRAFT_STATE]\n if (!state || !state.isManual) {\n throw new Error(\"First argument to `finishDraft` must be a draft returned by `createDraft`\") // prettier-ignore\n }\n if (state.finalized) {\n throw new Error(\"The given draft is already finalized\") // prettier-ignore\n }\n const {scope} = state\n scope.usePatches(patchListener)\n return this.processResult(undefined, scope, keepAlive)\n }\n setAutoFreeze(value) {\n this.autoFreeze = value\n }\n setUseProxies(value) {\n this.useProxies = value\n assign(this, value ? modernProxy : legacyProxy)\n }\n applyPatches(base, patches) {\n // Mutate the base state when a draft is passed.\n if (isDraft(base)) {\n return applyPatches(base, patches)\n }\n // Otherwise, produce a copy of the base state.\n return this.produce(base, draft => applyPatches(draft, patches))\n }\n /** @internal */\n processResult(result, scope, keepAlive) {\n const baseDraft = scope.drafts[0]\n const isReplaced = result !== undefined && result !== baseDraft\n this.willFinalize(scope, result, isReplaced)\n if (isReplaced) {\n if (baseDraft[DRAFT_STATE].modified) {\n scope.revoke(keepAlive)\n throw new Error(\"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.\") // prettier-ignore\n }\n if (isDraftable(result)) {\n // Finalize the result in case it contains (or is) a subset of the draft.\n result = this.finalize(result, null, scope)\n }\n if (scope.patches) {\n scope.patches.push({\n op: \"replace\",\n path: [],\n value: result\n })\n scope.inversePatches.push({\n op: \"replace\",\n path: [],\n value: baseDraft[DRAFT_STATE].base\n })\n }\n } else {\n // Finalize the base draft.\n result = this.finalize(baseDraft, [], scope, keepAlive)\n }\n scope.revoke(keepAlive)\n if (scope.patches) {\n scope.patchListener(scope.patches, scope.inversePatches)\n }\n return result !== NOTHING ? result : undefined\n }\n /**\n * @internal\n * Finalize a draft, returning either the unmodified base state or a modified\n * copy of the base state.\n */\n finalize(draft, path, scope, keepAlive) {\n const state = draft[DRAFT_STATE]\n if (!state) {\n if (Object.isFrozen(draft)) return draft\n return this.finalizeTree(draft, null, scope)\n }\n // Never finalize drafts owned by another scope.\n if (state.scope !== scope) {\n return draft\n }\n if (!state.modified) {\n return state.base\n }\n if (!state.finalized) {\n if (!keepAlive) state.finalized = true\n this.finalizeTree(state.draft, path, scope)\n\n if (this.onDelete) {\n // The `assigned` object is unreliable with ES5 drafts.\n if (this.useProxies) {\n const {assigned} = state\n for (const prop in assigned) {\n if (!assigned[prop]) this.onDelete(state, prop)\n }\n } else {\n const {base, copy} = state\n each(base, prop => {\n if (!has(copy, prop)) this.onDelete(state, prop)\n })\n }\n }\n if (this.onCopy) {\n this.onCopy(state)\n }\n\n // At this point, all descendants of `state.copy` have been finalized,\n // so we can be sure that `scope.canAutoFreeze` is accurate.\n if (this.autoFreeze && scope.canAutoFreeze) {\n Object.freeze(state.copy)\n }\n\n if (path && scope.patches) {\n generatePatches(\n state,\n path,\n scope.patches,\n scope.inversePatches\n )\n }\n }\n return state.copy\n }\n /**\n * @internal\n * Finalize all drafts in the given state tree.\n */\n finalizeTree(root, rootPath, scope) {\n const state = root[DRAFT_STATE]\n if (state) {\n if (!this.useProxies) {\n // Create the final copy, with added keys and without deleted keys.\n state.copy = shallowCopy(state.draft, true)\n }\n root = state.copy\n }\n\n const needPatches = !!rootPath && !!scope.patches\n const finalizeProperty = (prop, value, parent) => {\n if (value === parent) {\n throw Error(\"Immer forbids circular references\")\n }\n\n // In the `finalizeTree` method, only the `root` object may be a draft.\n const isDraftProp = !!state && parent === root\n\n if (isDraft(value)) {\n const path =\n isDraftProp && needPatches && !state.assigned[prop]\n ? rootPath.concat(prop)\n : null\n\n // Drafts owned by `scope` are finalized here.\n value = this.finalize(value, path, scope)\n\n // Drafts from another scope must prevent auto-freezing.\n if (isDraft(value)) {\n scope.canAutoFreeze = false\n }\n\n // Preserve non-enumerable properties.\n if (Array.isArray(parent) || isEnumerable(parent, prop)) {\n parent[prop] = value\n } else {\n Object.defineProperty(parent, prop, {value})\n }\n\n // Unchanged drafts are never passed to the `onAssign` hook.\n if (isDraftProp && value === state.base[prop]) return\n }\n // Unchanged draft properties are ignored.\n else if (isDraftProp && is(value, state.base[prop])) {\n return\n }\n // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.\n else if (isDraftable(value) && !Object.isFrozen(value)) {\n each(value, finalizeProperty)\n }\n\n if (isDraftProp && this.onAssign) {\n this.onAssign(state, prop, value)\n }\n }\n\n each(root, finalizeProperty)\n return root\n }\n}\n"],"mappings":";;;;;;AAAA,IAAAA,WAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,WAAA,GAAAF,uBAAA,CAAAC,OAAA;AACA,IAAAE,QAAA,GAAAF,OAAA;AACA,IAAAG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;MACA;MAAA,KACK,IAAI,IAAAxC,mBAAW,EAACuB,KAAK,CAAC,IAAI,CAACpE,MAAM,CAACkF,QAAQ,CAACd,KAAK,CAAC,EAAE;QACpD,IAAAmB,YAAI,EAACnB,KAAK,EAAE2B,gBAAgB,CAAC;MACjC;MAEA,IAAIE,WAAW,IAAI,IAAI,CAAC9E,QAAQ,EAAE;QAC9B,IAAI,CAACA,QAAQ,CAAC8C,KAAK,EAAEoB,IAAI,EAAEjB,KAAK,CAAC;MACrC;IACJ,CAAC;IAED,IAAAmB,YAAI,EAACI,IAAI,EAAEI,gBAAgB,CAAC;IAC5B,OAAOJ,IAAI;EACf;AACJ;AAACW,OAAA,CAAAhF,KAAA,GAAAA,KAAA"}
package/lib/index.js ADDED
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "DRAFT_STATE", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _common.DRAFT_STATE;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "Immer", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _immer.Immer;
16
+ }
17
+ });
18
+ exports.finishDraft = exports.default = exports.createDraft = exports.applyPatches = void 0;
19
+ Object.defineProperty(exports, "immerable", {
20
+ enumerable: true,
21
+ get: function get() {
22
+ return _common.DRAFTABLE;
23
+ }
24
+ });
25
+ Object.defineProperty(exports, "isDraft", {
26
+ enumerable: true,
27
+ get: function get() {
28
+ return _common.isDraft;
29
+ }
30
+ });
31
+ Object.defineProperty(exports, "isDraftable", {
32
+ enumerable: true,
33
+ get: function get() {
34
+ return _common.isDraftable;
35
+ }
36
+ });
37
+ Object.defineProperty(exports, "nothing", {
38
+ enumerable: true,
39
+ get: function get() {
40
+ return _common.NOTHING;
41
+ }
42
+ });
43
+ Object.defineProperty(exports, "original", {
44
+ enumerable: true,
45
+ get: function get() {
46
+ return _common.original;
47
+ }
48
+ });
49
+ exports.setUseProxies = exports.setAutoFreeze = exports.produce = void 0;
50
+ var _immer = require("./immer");
51
+ var _common = require("./common");
52
+ var immer = new _immer.Immer();
53
+
54
+ /**
55
+ * The `produce` function takes a value and a "recipe function" (whose
56
+ * return value often depends on the base state). The recipe function is
57
+ * free to mutate its first argument however it wants. All mutations are
58
+ * only ever applied to a __copy__ of the base state.
59
+ *
60
+ * Pass only a function to create a "curried producer" which relieves you
61
+ * from passing the recipe function every time.
62
+ *
63
+ * Only plain objects and arrays are made mutable. All other objects are
64
+ * considered uncopyable.
65
+ *
66
+ * Note: This function is __bound__ to its `Immer` instance.
67
+ *
68
+ * @param {any} base - the initial state
69
+ * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
70
+ * @param {Function} patchListener - optional function that will be called with all the patches produced here
71
+ * @returns {any} a new state, or the initial state if nothing was modified
72
+ */
73
+ var produce = immer.produce;
74
+ exports.produce = produce;
75
+ var _default = produce;
76
+ /**
77
+ * Pass true to automatically freeze all copies created by Immer.
78
+ *
79
+ * By default, auto-freezing is disabled in production.
80
+ */
81
+ exports.default = _default;
82
+ var setAutoFreeze = immer.setAutoFreeze.bind(immer);
83
+
84
+ /**
85
+ * Pass true to use the ES2015 `Proxy` class when creating drafts, which is
86
+ * always faster than using ES5 proxies.
87
+ *
88
+ * By default, feature detection is used, so calling this is rarely necessary.
89
+ */
90
+ exports.setAutoFreeze = setAutoFreeze;
91
+ var setUseProxies = immer.setUseProxies.bind(immer);
92
+
93
+ /**
94
+ * Apply an array of Immer patches to the first argument.
95
+ *
96
+ * This function is a producer, which means copy-on-write is in effect.
97
+ */
98
+ exports.setUseProxies = setUseProxies;
99
+ var applyPatches = immer.applyPatches.bind(immer);
100
+
101
+ /**
102
+ * Create an Immer draft from the given base state, which may be a draft itself.
103
+ * The draft can be modified until you finalize it with the `finishDraft` function.
104
+ */
105
+ exports.applyPatches = applyPatches;
106
+ var createDraft = immer.createDraft.bind(immer);
107
+
108
+ /**
109
+ * Finalize an Immer draft from a `createDraft` call, returning the base state
110
+ * (if no changes were made) or a modified copy. The draft must *not* be
111
+ * mutated afterwards.
112
+ *
113
+ * Pass a function as the 2nd argument to generate Immer patches based on the
114
+ * changes that were made.
115
+ */
116
+ exports.createDraft = createDraft;
117
+ var finishDraft = immer.finishDraft.bind(immer);
118
+ exports.finishDraft = finishDraft;
119
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["_immer","require","_common","immer","Immer","produce","exports","_default","default","setAutoFreeze","bind","setUseProxies","applyPatches","createDraft","finishDraft"],"sources":["../index.js"],"sourcesContent":["import {Immer} from \"./immer\"\n\nconst immer = new Immer()\n\n/**\n * The `produce` function takes a value and a \"recipe function\" (whose\n * return value often depends on the base state). The recipe function is\n * free to mutate its first argument however it wants. All mutations are\n * only ever applied to a __copy__ of the base state.\n *\n * Pass only a function to create a \"curried producer\" which relieves you\n * from passing the recipe function every time.\n *\n * Only plain objects and arrays are made mutable. All other objects are\n * considered uncopyable.\n *\n * Note: This function is __bound__ to its `Immer` instance.\n *\n * @param {any} base - the initial state\n * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified\n * @param {Function} patchListener - optional function that will be called with all the patches produced here\n * @returns {any} a new state, or the initial state if nothing was modified\n */\nexport const produce = immer.produce\nexport default produce\n\n/**\n * Pass true to automatically freeze all copies created by Immer.\n *\n * By default, auto-freezing is disabled in production.\n */\nexport const setAutoFreeze = immer.setAutoFreeze.bind(immer)\n\n/**\n * Pass true to use the ES2015 `Proxy` class when creating drafts, which is\n * always faster than using ES5 proxies.\n *\n * By default, feature detection is used, so calling this is rarely necessary.\n */\nexport const setUseProxies = immer.setUseProxies.bind(immer)\n\n/**\n * Apply an array of Immer patches to the first argument.\n *\n * This function is a producer, which means copy-on-write is in effect.\n */\nexport const applyPatches = immer.applyPatches.bind(immer)\n\n/**\n * Create an Immer draft from the given base state, which may be a draft itself.\n * The draft can be modified until you finalize it with the `finishDraft` function.\n */\nexport const createDraft = immer.createDraft.bind(immer)\n\n/**\n * Finalize an Immer draft from a `createDraft` call, returning the base state\n * (if no changes were made) or a modified copy. The draft must *not* be\n * mutated afterwards.\n *\n * Pass a function as the 2nd argument to generate Immer patches based on the\n * changes that were made.\n */\nexport const finishDraft = immer.finishDraft.bind(immer)\n\nexport {\n original,\n isDraft,\n isDraftable,\n NOTHING as nothing,\n DRAFTABLE as immerable,\n DRAFT_STATE\n} from \"./common\"\n\nexport {Immer}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAgEA,IAAAC,OAAA,GAAAD,OAAA;AA9DA,IAAME,KAAK,GAAG,IAAIC,YAAK,EAAE;;AAEzB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,IAAMC,OAAO,GAAGF,KAAK,CAACE,OAAO;AAAAC,OAAA,CAAAD,OAAA,GAAAA,OAAA;AAAA,IAAAE,QAAA,GACrBF,OAAO;AAEtB;AACA;AACA;AACA;AACA;AAJAC,OAAA,CAAAE,OAAA,GAAAD,QAAA;AAKO,IAAME,aAAa,GAAGN,KAAK,CAACM,aAAa,CAACC,IAAI,CAACP,KAAK,CAAC;;AAE5D;AACA;AACA;AACA;AACA;AACA;AALAG,OAAA,CAAAG,aAAA,GAAAA,aAAA;AAMO,IAAME,aAAa,GAAGR,KAAK,CAACQ,aAAa,CAACD,IAAI,CAACP,KAAK,CAAC;;AAE5D;AACA;AACA;AACA;AACA;AAJAG,OAAA,CAAAK,aAAA,GAAAA,aAAA;AAKO,IAAMC,YAAY,GAAGT,KAAK,CAACS,YAAY,CAACF,IAAI,CAACP,KAAK,CAAC;;AAE1D;AACA;AACA;AACA;AAHAG,OAAA,CAAAM,YAAA,GAAAA,YAAA;AAIO,IAAMC,WAAW,GAAGV,KAAK,CAACU,WAAW,CAACH,IAAI,CAACP,KAAK,CAAC;;AAExD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAPAG,OAAA,CAAAO,WAAA,GAAAA,WAAA;AAQO,IAAMC,WAAW,GAAGX,KAAK,CAACW,WAAW,CAACJ,IAAI,CAACP,KAAK,CAAC;AAAAG,OAAA,CAAAQ,WAAA,GAAAA,WAAA"}
package/lib/patches.js ADDED
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.applyPatches = applyPatches;
7
+ exports.generatePatches = generatePatches;
8
+ var _common = require("./common");
9
+ function generatePatches(state, basePath, patches, inversePatches) {
10
+ Array.isArray(state.base) ? generateArrayPatches(state, basePath, patches, inversePatches) : generateObjectPatches(state, basePath, patches, inversePatches);
11
+ }
12
+ function generateArrayPatches(state, basePath, patches, inversePatches) {
13
+ var base = state.base,
14
+ copy = state.copy,
15
+ assigned = state.assigned;
16
+
17
+ // Reduce complexity by ensuring `base` is never longer.
18
+ if (copy.length < base.length) {
19
+ ;
20
+ var _ref = [copy, base];
21
+ base = _ref[0];
22
+ copy = _ref[1];
23
+ var _ref2 = [inversePatches, patches];
24
+ patches = _ref2[0];
25
+ inversePatches = _ref2[1];
26
+ }
27
+ var delta = copy.length - base.length;
28
+
29
+ // Find the first replaced index.
30
+ var start = 0;
31
+ while (base[start] === copy[start] && start < base.length) {
32
+ ++start;
33
+ }
34
+
35
+ // Find the last replaced index. Search from the end to optimize splice patches.
36
+ var end = base.length;
37
+ while (end > start && base[end - 1] === copy[end + delta - 1]) {
38
+ --end;
39
+ }
40
+
41
+ // Process replaced indices.
42
+ for (var i = start; i < end; ++i) {
43
+ if (assigned[i] && copy[i] !== base[i]) {
44
+ var path = basePath.concat([i]);
45
+ patches.push({
46
+ op: "replace",
47
+ path,
48
+ value: copy[i]
49
+ });
50
+ inversePatches.push({
51
+ op: "replace",
52
+ path,
53
+ value: base[i]
54
+ });
55
+ }
56
+ }
57
+ var useRemove = end != base.length;
58
+ var replaceCount = patches.length;
59
+
60
+ // Process added indices.
61
+ for (var _i = end + delta - 1; _i >= end; --_i) {
62
+ var _path = basePath.concat([_i]);
63
+ patches[replaceCount + _i - end] = {
64
+ op: "add",
65
+ path: _path,
66
+ value: copy[_i]
67
+ };
68
+ if (useRemove) {
69
+ inversePatches.push({
70
+ op: "remove",
71
+ path: _path
72
+ });
73
+ }
74
+ }
75
+
76
+ // One "replace" patch reverses all non-splicing "add" patches.
77
+ if (!useRemove) {
78
+ inversePatches.push({
79
+ op: "replace",
80
+ path: basePath.concat(["length"]),
81
+ value: base.length
82
+ });
83
+ }
84
+ }
85
+ function generateObjectPatches(state, basePath, patches, inversePatches) {
86
+ var base = state.base,
87
+ copy = state.copy;
88
+ (0, _common.each)(state.assigned, (key, assignedValue) => {
89
+ var origValue = base[key];
90
+ var value = copy[key];
91
+ var op = !assignedValue ? "remove" : key in base ? "replace" : "add";
92
+ if (origValue === value && op === "replace") return;
93
+ var path = basePath.concat(key);
94
+ patches.push(op === "remove" ? {
95
+ op,
96
+ path
97
+ } : {
98
+ op,
99
+ path,
100
+ value
101
+ });
102
+ inversePatches.push(op === "add" ? {
103
+ op: "remove",
104
+ path
105
+ } : op === "remove" ? {
106
+ op: "add",
107
+ path,
108
+ value: origValue
109
+ } : {
110
+ op: "replace",
111
+ path,
112
+ value: origValue
113
+ });
114
+ });
115
+ }
116
+ function applyPatches(draft, patches) {
117
+ for (var i = 0; i < patches.length; i++) {
118
+ var patch = patches[i];
119
+ var path = patch.path;
120
+ if (path.length === 0 && patch.op === "replace") {
121
+ draft = patch.value;
122
+ } else {
123
+ var base = draft;
124
+ for (var _i2 = 0; _i2 < path.length - 1; _i2++) {
125
+ base = base[path[_i2]];
126
+ if (!base || typeof base !== "object") throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")); // prettier-ignore
127
+ }
128
+
129
+ var key = path[path.length - 1];
130
+ switch (patch.op) {
131
+ case "replace":
132
+ base[key] = patch.value;
133
+ break;
134
+ case "add":
135
+ if (Array.isArray(base)) {
136
+ // TODO: support "foo/-" paths for appending to an array
137
+ base.splice(key, 0, patch.value);
138
+ } else {
139
+ base[key] = patch.value;
140
+ }
141
+ break;
142
+ case "remove":
143
+ if (Array.isArray(base)) {
144
+ base.splice(key, 1);
145
+ } else {
146
+ delete base[key];
147
+ }
148
+ break;
149
+ default:
150
+ throw new Error("Unsupported patch operation: " + patch.op);
151
+ }
152
+ }
153
+ }
154
+ return draft;
155
+ }
156
+ //# sourceMappingURL=patches.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"patches.js","names":["_common","require","generatePatches","state","basePath","patches","inversePatches","Array","isArray","base","generateArrayPatches","generateObjectPatches","copy","assigned","length","_ref","_ref2","delta","start","end","i","path","concat","push","op","value","useRemove","replaceCount","each","key","assignedValue","origValue","applyPatches","draft","patch","Error","join","splice"],"sources":["../patches.js"],"sourcesContent":["import {each} from \"./common\"\n\nexport function generatePatches(state, basePath, patches, inversePatches) {\n Array.isArray(state.base)\n ? generateArrayPatches(state, basePath, patches, inversePatches)\n : generateObjectPatches(state, basePath, patches, inversePatches)\n}\n\nfunction generateArrayPatches(state, basePath, patches, inversePatches) {\n let {base, copy, assigned} = state\n\n // Reduce complexity by ensuring `base` is never longer.\n if (copy.length < base.length) {\n ;[base, copy] = [copy, base]\n ;[patches, inversePatches] = [inversePatches, patches]\n }\n\n const delta = copy.length - base.length\n\n // Find the first replaced index.\n let start = 0\n while (base[start] === copy[start] && start < base.length) {\n ++start\n }\n\n // Find the last replaced index. Search from the end to optimize splice patches.\n let end = base.length\n while (end > start && base[end - 1] === copy[end + delta - 1]) {\n --end\n }\n\n // Process replaced indices.\n for (let i = start; i < end; ++i) {\n if (assigned[i] && copy[i] !== base[i]) {\n const path = basePath.concat([i])\n patches.push({\n op: \"replace\",\n path,\n value: copy[i]\n })\n inversePatches.push({\n op: \"replace\",\n path,\n value: base[i]\n })\n }\n }\n\n const useRemove = end != base.length\n const replaceCount = patches.length\n\n // Process added indices.\n for (let i = end + delta - 1; i >= end; --i) {\n const path = basePath.concat([i])\n patches[replaceCount + i - end] = {\n op: \"add\",\n path,\n value: copy[i]\n }\n if (useRemove) {\n inversePatches.push({\n op: \"remove\",\n path\n })\n }\n }\n\n // One \"replace\" patch reverses all non-splicing \"add\" patches.\n if (!useRemove) {\n inversePatches.push({\n op: \"replace\",\n path: basePath.concat([\"length\"]),\n value: base.length\n })\n }\n}\n\nfunction generateObjectPatches(state, basePath, patches, inversePatches) {\n const {base, copy} = state\n each(state.assigned, (key, assignedValue) => {\n const origValue = base[key]\n const value = copy[key]\n const op = !assignedValue ? \"remove\" : key in base ? \"replace\" : \"add\"\n if (origValue === value && op === \"replace\") return\n const path = basePath.concat(key)\n patches.push(op === \"remove\" ? {op, path} : {op, path, value})\n inversePatches.push(\n op === \"add\"\n ? {op: \"remove\", path}\n : op === \"remove\"\n ? {op: \"add\", path, value: origValue}\n : {op: \"replace\", path, value: origValue}\n )\n })\n}\n\nexport function applyPatches(draft, patches) {\n for (let i = 0; i < patches.length; i++) {\n const patch = patches[i]\n const {path} = patch\n if (path.length === 0 && patch.op === \"replace\") {\n draft = patch.value\n } else {\n let base = draft\n for (let i = 0; i < path.length - 1; i++) {\n base = base[path[i]]\n if (!base || typeof base !== \"object\")\n throw new Error(\"Cannot apply patch, path doesn't resolve: \" + path.join(\"/\")) // prettier-ignore\n }\n const key = path[path.length - 1]\n switch (patch.op) {\n case \"replace\":\n base[key] = patch.value\n break\n case \"add\":\n if (Array.isArray(base)) {\n // TODO: support \"foo/-\" paths for appending to an array\n base.splice(key, 0, patch.value)\n } else {\n base[key] = patch.value\n }\n break\n case \"remove\":\n if (Array.isArray(base)) {\n base.splice(key, 1)\n } else {\n delete base[key]\n }\n break\n default:\n throw new Error(\"Unsupported patch operation: \" + patch.op)\n }\n }\n }\n return draft\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,OAAA,GAAAC,OAAA;AAEO,SAASC,eAAeA,CAACC,KAAK,EAA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