@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/es/common.js +83 -0
- package/es/common.js.map +1 -0
- package/es/es5.js +242 -0
- package/es/es5.js.map +1 -0
- package/es/immer.js +263 -0
- package/es/immer.js.map +1 -0
- package/es/index.js +65 -0
- package/es/index.js.map +1 -0
- package/es/patches.js +149 -0
- package/es/patches.js.map +1 -0
- package/es/proxy.js +164 -0
- package/es/proxy.js.map +1 -0
- package/es/scope.js +42 -0
- package/es/scope.js.map +1 -0
- package/lib/common.js +103 -0
- package/lib/common.js.map +1 -0
- package/lib/es5.js +246 -0
- package/lib/es5.js.map +1 -0
- package/lib/immer.d.ts +248 -0
- package/lib/immer.js +272 -0
- package/lib/immer.js.map +1 -0
- package/lib/index.js +119 -0
- package/lib/index.js.map +1 -0
- package/lib/patches.js +156 -0
- package/lib/patches.js.map +1 -0
- package/lib/proxy.js +168 -0
- package/lib/proxy.js.map +1 -0
- package/lib/scope.js +48 -0
- package/lib/scope.js.map +1 -0
- package/package.json +12 -11
- package/umd/index.js +1134 -0
- package/umd/index.js.map +1 -0
- package/umd/index.min.js +2 -0
- package/umd/index.min.js.map +1 -0
- package/dist/immer.js +0 -1016
- package/dist/immer.js.map +0 -1
- package/dist/immer.module.js +0 -999
- package/dist/immer.module.js.map +0 -1
- package/dist/immer.umd.js +0 -2
- package/dist/immer.umd.js.map +0 -1
- /package/{dist → es}/immer.d.ts +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","names":["NOTHING","Symbol","exports","DRAFTABLE","for","DRAFT_STATE","isDraft","value","isDraftable","Array","isArray","proto","Object","getPrototypeOf","prototype","constructor","original","base","assign","target","key","has","ownKeys","Reflect","getOwnPropertySymbols","obj","getOwnPropertyNames","concat","shallowCopy","invokeGetters","arguments","length","undefined","slice","clone","create","forEach","desc","getOwnPropertyDescriptor","get","Error","call","enumerable","defineProperty","writable","configurable","each","cb","i","isEnumerable","prop","thing","hasOwnProperty","is","x","y"],"sources":["../common.js"],"sourcesContent":["export const NOTHING =\n typeof Symbol !== \"undefined\"\n ? Symbol(\"immer-nothing\")\n : {[\"immer-nothing\"]: true}\n\nexport const DRAFTABLE =\n typeof Symbol !== \"undefined\" && Symbol.for\n ? Symbol.for(\"immer-draftable\")\n : \"__$immer_draftable\"\n\nexport const DRAFT_STATE =\n typeof Symbol !== \"undefined\" && Symbol.for\n ? Symbol.for(\"immer-state\")\n : \"__$immer_state\"\n\nexport function isDraft(value) {\n return !!value && !!value[DRAFT_STATE]\n}\n\nexport function isDraftable(value) {\n if (!value || typeof value !== \"object\") return false\n if (Array.isArray(value)) return true\n const proto = Object.getPrototypeOf(value)\n if (!proto || proto === Object.prototype) return true\n return !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE]\n}\n\nexport function original(value) {\n if (value && value[DRAFT_STATE]) {\n return value[DRAFT_STATE].base\n }\n // otherwise return undefined\n}\n\nexport const assign =\n Object.assign ||\n function assign(target, value) {\n for (let key in value) {\n if (has(value, key)) {\n target[key] = value[key]\n }\n }\n return target\n }\n\nexport const ownKeys =\n typeof Reflect !== \"undefined\" && Reflect.ownKeys\n ? Reflect.ownKeys\n : typeof Object.getOwnPropertySymbols !== \"undefined\"\n ? obj =>\n Object.getOwnPropertyNames(obj).concat(\n Object.getOwnPropertySymbols(obj)\n )\n : Object.getOwnPropertyNames\n\nexport function shallowCopy(base, invokeGetters = false) {\n if (Array.isArray(base)) return base.slice()\n const clone = Object.create(Object.getPrototypeOf(base))\n ownKeys(base).forEach(key => {\n if (key === DRAFT_STATE) {\n return // Never copy over draft state.\n }\n const desc = Object.getOwnPropertyDescriptor(base, key)\n let {value} = desc\n if (desc.get) {\n if (!invokeGetters) {\n throw new Error(\"Immer drafts cannot have computed properties\")\n }\n value = desc.get.call(base)\n }\n if (desc.enumerable) {\n clone[key] = value\n } else {\n Object.defineProperty(clone, key, {\n value,\n writable: true,\n configurable: true\n })\n }\n })\n return clone\n}\n\nexport function each(value, cb) {\n if (Array.isArray(value)) {\n for (let i = 0; i < value.length; i++) cb(i, value[i], value)\n } else {\n ownKeys(value).forEach(key => cb(key, value[key], value))\n }\n}\n\nexport function isEnumerable(base, prop) {\n const desc = Object.getOwnPropertyDescriptor(base, prop)\n return !!desc && desc.enumerable\n}\n\nexport function has(thing, prop) {\n return Object.prototype.hasOwnProperty.call(thing, prop)\n}\n\nexport function is(x, y) {\n // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js\n if (x === y) {\n return x !== 0 || 1 / x === 1 / y\n } else {\n return x !== x && y !== y\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAAO,IAAMA,OAAO,GAChB,OAAOC,MAAM,KAAK,WAAW,GACvBA,MAAM,CAAC,eAAe,CAAC,GACvB;EAAC,CAAC,eAAe,GAAG;AAAI,CAAC;AAAAC,OAAA,CAAAF,OAAA,GAAAA,OAAA;AAE5B,IAAMG,SAAS,GAClB,OAAOF,MAAM,KAAK,WAAW,IAAIA,MAAM,CAACG,GAAG,GACrCH,MAAM,CAACG,GAAG,CAAC,iBAAiB,CAAC,GAC7B,oBAAoB;AAAAF,OAAA,CAAAC,SAAA,GAAAA,SAAA;AAEvB,IAAME,WAAW,GACpB,OAAOJ,MAAM,KAAK,WAAW,IAAIA,MAAM,CAACG,GAAG,GACrCH,MAAM,CAACG,GAAG,CAAC,aAAa,CAAC,GACzB,gBAAgB;AAAAF,OAAA,CAAAG,WAAA,GAAAA,WAAA;AAEnB,SAASC,OAAOA,CAACC,KAAK,EAAE;EAC3B,OAAO,CAAC,CAACA,KAAK,IAAI,CAAC,CAACA,KAAK,CAACF,WAAW,CAAC;AAC1C;AAEO,SAASG,WAAWA,CAACD,KAAK,EAAE;EAC/B,IAAI,CAACA,KAAK,IAAI,OAAOA,KAAK,KAAK,QAAQ,EAAE,OAAO,K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AAC,EAAEzC,KAAK,CAACyC,CAAC,CAAC,EAAEzC,KAAK,CAAC;EACjE,CAAC,MAAM;IACHe,OAAO,CAACf,KAAK,CAAC,CAAC6B,OAAO,CAAChB,GAAG,IAAI2B,EAAE,CAAC3B,GAAG,EAAEb,KAAK,CAACa,GAAG,CAAC,EAAEb,KAAK,CAAC,CAAC;EAC7D;AACJ;AAEO,SAAS0C,YAAYA,CAAChC,IAAI,EAAEiC,IAAI,EAAE;EACrC,IAAMb,IAAI,GAAGzB,MAAM,CAAC0B,wBAAwB,CAACrB,IAAI,EAAEiC,IAAI,CAAC;EACxD,OAAO,CAAC,CAACb,IAAI,IAAIA,IAAI,CAACK,UAAU;AACpC;AAEO,SAASrB,GAAGA,CAAC8B,KAAK,EAAED,IAAI,EAAE;EAC7B,OAAOtC,MAAM,CAACE,SAAS,CAACsC,cAAc,CAACX,IAAI,CAACU,KAAK,EAAED,IAAI,CAAC;AAC5D;AAEO,SAASG,EAAEA,CAACC,CAAC,EAAEC,CAAC,EAAE;EACrB;EACA,IAAID,CAAC,KAAKC,CAAC,EAAE;IACT,OAAOD,CAAC,KAAK,CAAC,IAAI,CAAC,GAAGA,CAAC,KAAK,CAAC,GAAGC,CAAC;EACrC,CAAC,MAAM;IACH,OAAOD,CAAC,KAAKA,CAAC,IAAIC,CAAC,KAAKA,CAAC;EAC7B;AACJ"}
|
package/lib/es5.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
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
|
+
// property descriptors are recycled to make sure we don't create a get and set closure per property,
|
|
11
|
+
// but share them all instead
|
|
12
|
+
var descriptors = {};
|
|
13
|
+
var noop = () => {};
|
|
14
|
+
function willFinalize(scope, result, isReplaced) {
|
|
15
|
+
scope.drafts.forEach(draft => {
|
|
16
|
+
draft[_common.DRAFT_STATE].finalizing = true;
|
|
17
|
+
});
|
|
18
|
+
if (!isReplaced) {
|
|
19
|
+
if (scope.patches) {
|
|
20
|
+
markChangesRecursively(scope.drafts[0]);
|
|
21
|
+
}
|
|
22
|
+
// This is faster when we don't care about which attributes changed.
|
|
23
|
+
markChangesSweep(scope.drafts);
|
|
24
|
+
}
|
|
25
|
+
// When a child draft is returned, look for changes.
|
|
26
|
+
else if ((0, _common.isDraft)(result) && result[_common.DRAFT_STATE].scope === scope) {
|
|
27
|
+
markChangesSweep(scope.drafts);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function createProxy(base, parent) {
|
|
31
|
+
var isArray = Array.isArray(base);
|
|
32
|
+
var draft = clonePotentialDraft(base);
|
|
33
|
+
(0, _common.each)(draft, prop => {
|
|
34
|
+
proxyProperty(draft, prop, isArray || (0, _common.isEnumerable)(base, prop));
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// See "proxy.js" for property documentation.
|
|
38
|
+
var scope = parent ? parent.scope : _scope.ImmerScope.current;
|
|
39
|
+
var state = {
|
|
40
|
+
scope,
|
|
41
|
+
modified: false,
|
|
42
|
+
finalizing: false,
|
|
43
|
+
// es5 only
|
|
44
|
+
finalized: false,
|
|
45
|
+
assigned: {},
|
|
46
|
+
parent,
|
|
47
|
+
base,
|
|
48
|
+
draft,
|
|
49
|
+
copy: null,
|
|
50
|
+
revoke,
|
|
51
|
+
revoked: false // es5 only
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
createHiddenProperty(draft, _common.DRAFT_STATE, state);
|
|
55
|
+
scope.drafts.push(draft);
|
|
56
|
+
return draft;
|
|
57
|
+
}
|
|
58
|
+
function revoke() {
|
|
59
|
+
this.revoked = true;
|
|
60
|
+
}
|
|
61
|
+
function source(state) {
|
|
62
|
+
return state.copy || state.base;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Access a property without creating an Immer draft.
|
|
66
|
+
function peek(draft, prop) {
|
|
67
|
+
var state = draft[_common.DRAFT_STATE];
|
|
68
|
+
if (state && !state.finalizing) {
|
|
69
|
+
state.finalizing = true;
|
|
70
|
+
var value = draft[prop];
|
|
71
|
+
state.finalizing = false;
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
return draft[prop];
|
|
75
|
+
}
|
|
76
|
+
function get(state, prop) {
|
|
77
|
+
assertUnrevoked(state);
|
|
78
|
+
var value = peek(source(state), prop);
|
|
79
|
+
if (state.finalizing) return value;
|
|
80
|
+
// Create a draft if the value is unmodified.
|
|
81
|
+
if (value === peek(state.base, prop) && (0, _common.isDraftable)(value)) {
|
|
82
|
+
prepareCopy(state);
|
|
83
|
+
return state.copy[prop] = createProxy(value, state);
|
|
84
|
+
}
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
87
|
+
function set(state, prop, value) {
|
|
88
|
+
assertUnrevoked(state);
|
|
89
|
+
state.assigned[prop] = true;
|
|
90
|
+
if (!state.modified) {
|
|
91
|
+
if ((0, _common.is)(value, peek(source(state), prop))) return;
|
|
92
|
+
markChanged(state);
|
|
93
|
+
prepareCopy(state);
|
|
94
|
+
}
|
|
95
|
+
state.copy[prop] = value;
|
|
96
|
+
}
|
|
97
|
+
function markChanged(state) {
|
|
98
|
+
if (!state.modified) {
|
|
99
|
+
state.modified = true;
|
|
100
|
+
if (state.parent) markChanged(state.parent);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function prepareCopy(state) {
|
|
104
|
+
if (!state.copy) state.copy = clonePotentialDraft(state.base);
|
|
105
|
+
}
|
|
106
|
+
function clonePotentialDraft(base) {
|
|
107
|
+
var state = base && base[_common.DRAFT_STATE];
|
|
108
|
+
if (state) {
|
|
109
|
+
state.finalizing = true;
|
|
110
|
+
var draft = (0, _common.shallowCopy)(state.draft, true);
|
|
111
|
+
state.finalizing = false;
|
|
112
|
+
return draft;
|
|
113
|
+
}
|
|
114
|
+
return (0, _common.shallowCopy)(base);
|
|
115
|
+
}
|
|
116
|
+
function proxyProperty(draft, prop, enumerable) {
|
|
117
|
+
var desc = descriptors[prop];
|
|
118
|
+
if (desc) {
|
|
119
|
+
desc.enumerable = enumerable;
|
|
120
|
+
} else {
|
|
121
|
+
descriptors[prop] = desc = {
|
|
122
|
+
configurable: true,
|
|
123
|
+
enumerable,
|
|
124
|
+
get() {
|
|
125
|
+
return get(this[_common.DRAFT_STATE], prop);
|
|
126
|
+
},
|
|
127
|
+
set(value) {
|
|
128
|
+
set(this[_common.DRAFT_STATE], prop, value);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
Object.defineProperty(draft, prop, desc);
|
|
133
|
+
}
|
|
134
|
+
function assertUnrevoked(state) {
|
|
135
|
+
if (state.revoked === true) throw new Error("Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " + JSON.stringify(source(state)));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.
|
|
139
|
+
function markChangesSweep(drafts) {
|
|
140
|
+
// The natural order of drafts in the `scope` array is based on when they
|
|
141
|
+
// were accessed. By processing drafts in reverse natural order, we have a
|
|
142
|
+
// better chance of processing leaf nodes first. When a leaf node is known to
|
|
143
|
+
// have changed, we can avoid any traversal of its ancestor nodes.
|
|
144
|
+
for (var i = drafts.length - 1; i >= 0; i--) {
|
|
145
|
+
var state = drafts[i][_common.DRAFT_STATE];
|
|
146
|
+
if (!state.modified) {
|
|
147
|
+
if (Array.isArray(state.base)) {
|
|
148
|
+
if (hasArrayChanges(state)) markChanged(state);
|
|
149
|
+
} else if (hasObjectChanges(state)) markChanged(state);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function markChangesRecursively(object) {
|
|
154
|
+
if (!object || typeof object !== "object") return;
|
|
155
|
+
var state = object[_common.DRAFT_STATE];
|
|
156
|
+
if (!state) return;
|
|
157
|
+
var base = state.base,
|
|
158
|
+
draft = state.draft,
|
|
159
|
+
assigned = state.assigned;
|
|
160
|
+
if (!Array.isArray(object)) {
|
|
161
|
+
// Look for added keys.
|
|
162
|
+
Object.keys(draft).forEach(key => {
|
|
163
|
+
// The `undefined` check is a fast path for pre-existing keys.
|
|
164
|
+
if (base[key] === undefined && !(0, _common.has)(base, key)) {
|
|
165
|
+
assigned[key] = true;
|
|
166
|
+
markChanged(state);
|
|
167
|
+
} else if (!assigned[key]) {
|
|
168
|
+
// Only untouched properties trigger recursion.
|
|
169
|
+
markChangesRecursively(draft[key]);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
// Look for removed keys.
|
|
173
|
+
Object.keys(base).forEach(key => {
|
|
174
|
+
// The `undefined` check is a fast path for pre-existing keys.
|
|
175
|
+
if (draft[key] === undefined && !(0, _common.has)(draft, key)) {
|
|
176
|
+
assigned[key] = false;
|
|
177
|
+
markChanged(state);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
} else if (hasArrayChanges(state)) {
|
|
181
|
+
markChanged(state);
|
|
182
|
+
assigned.length = true;
|
|
183
|
+
if (draft.length < base.length) {
|
|
184
|
+
for (var i = draft.length; i < base.length; i++) assigned[i] = false;
|
|
185
|
+
} else {
|
|
186
|
+
for (var _i = base.length; _i < draft.length; _i++) assigned[_i] = true;
|
|
187
|
+
}
|
|
188
|
+
for (var _i2 = 0; _i2 < draft.length; _i2++) {
|
|
189
|
+
// Only untouched indices trigger recursion.
|
|
190
|
+
if (assigned[_i2] === undefined) markChangesRecursively(draft[_i2]);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function hasObjectChanges(state) {
|
|
195
|
+
var base = state.base,
|
|
196
|
+
draft = state.draft;
|
|
197
|
+
|
|
198
|
+
// Search for added keys and changed keys. Start at the back, because
|
|
199
|
+
// non-numeric keys are ordered by time of definition on the object.
|
|
200
|
+
var keys = Object.keys(draft);
|
|
201
|
+
for (var i = keys.length - 1; i >= 0; i--) {
|
|
202
|
+
var key = keys[i];
|
|
203
|
+
var baseValue = base[key];
|
|
204
|
+
// The `undefined` check is a fast path for pre-existing keys.
|
|
205
|
+
if (baseValue === undefined && !(0, _common.has)(base, key)) {
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
// Once a base key is deleted, future changes go undetected, because its
|
|
209
|
+
// descriptor is erased. This branch detects any missed changes.
|
|
210
|
+
else {
|
|
211
|
+
var value = draft[key];
|
|
212
|
+
var _state = value && value[_common.DRAFT_STATE];
|
|
213
|
+
if (_state ? _state.base !== baseValue : !(0, _common.is)(value, baseValue)) {
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// At this point, no keys were added or changed.
|
|
220
|
+
// Compare key count to determine if keys were deleted.
|
|
221
|
+
return keys.length !== Object.keys(base).length;
|
|
222
|
+
}
|
|
223
|
+
function hasArrayChanges(state) {
|
|
224
|
+
var draft = state.draft;
|
|
225
|
+
if (draft.length !== state.base.length) return true;
|
|
226
|
+
// See #116
|
|
227
|
+
// If we first shorten the length, our array interceptors will be removed.
|
|
228
|
+
// If after that new items are added, result in the same original length,
|
|
229
|
+
// those last items will have no intercepting property.
|
|
230
|
+
// So if there is no own descriptor on the last position, we know that items were removed and added
|
|
231
|
+
// N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check
|
|
232
|
+
// the last one
|
|
233
|
+
var descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1);
|
|
234
|
+
// descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)
|
|
235
|
+
if (descriptor && !descriptor.get) return true;
|
|
236
|
+
// For all other cases, we don't have to compare, as they would have been picked up by the index setters
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
function createHiddenProperty(target, prop, value) {
|
|
240
|
+
Object.defineProperty(target, prop, {
|
|
241
|
+
value: value,
|
|
242
|
+
enumerable: false,
|
|
243
|
+
writable: true
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
//# sourceMappingURL=es5.js.map
|
package/lib/es5.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"es5.js","names":["Object","defineProperty","exports","value","createProxy","willFinalize","_common","require","_scope","descriptors","noop","scope","result","isReplaced","drafts","forEach","draft","DRAFT_STATE","finalizing","patches","markChangesRecursively","markChangesSweep","isDraft","base","parent","isArray","Array","clonePotentialDraft","each","prop","proxyProperty","isEnumerable","ImmerScope","current","state","modified","finalized","assigned","copy","revoke","revoked","createHiddenProperty","push","source","peek","get","assertUnrevoked","isDraftable","prepareCopy","set","is","markChanged","shallowCopy","enumerable","desc","configurable","Error","JSON","stringify","i","length","hasArrayChanges","hasObjectChanges","object","keys","key","undefined","has","baseValue","descriptor","getOwnPropertyDescriptor","target","writable"],"sources":["../es5.js"],"sourcesContent":["\"use strict\"\nimport {\n each,\n has,\n is,\n isDraft,\n isDraftable,\n isEnumerable,\n shallowCopy,\n DRAFT_STATE\n} from \"./common\"\nimport {ImmerScope} from \"./scope\"\n\n// property descriptors are recycled to make sure we don't create a get and set closure per property,\n// but share them all instead\nconst descriptors = {}\nconst noop = () => {}\n\nexport function willFinalize(scope, result, isReplaced) {\n scope.drafts.forEach(draft => {\n draft[DRAFT_STATE].finalizing = true\n })\n if (!isReplaced) {\n if (scope.patches) {\n markChangesRecursively(scope.drafts[0])\n }\n // This is faster when we don't care about which attributes changed.\n markChangesSweep(scope.drafts)\n }\n // When a child draft is returned, look for changes.\n else if (isDraft(result) && result[DRAFT_STATE].scope === scope) {\n markChangesSweep(scope.drafts)\n }\n}\n\nexport function createProxy(base, parent) {\n const isArray = Array.isArray(base)\n const draft = clonePotentialDraft(base)\n each(draft, prop => {\n proxyProperty(draft, prop, isArray || isEnumerable(base, prop))\n })\n\n // See \"proxy.js\" for property documentation.\n const scope = parent ? parent.scope : ImmerScope.current\n const state = {\n scope,\n modified: false,\n finalizing: false, // es5 only\n finalized: false,\n assigned: {},\n parent,\n base,\n draft,\n copy: null,\n revoke,\n revoked: false // es5 only\n }\n\n createHiddenProperty(draft, DRAFT_STATE, state)\n scope.drafts.push(draft)\n return draft\n}\n\nfunction revoke() {\n this.revoked = true\n}\n\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 if (state && !state.finalizing) {\n state.finalizing = true\n const value = draft[prop]\n state.finalizing = false\n return value\n }\n return draft[prop]\n}\n\nfunction get(state, prop) {\n assertUnrevoked(state)\n const value = peek(source(state), prop)\n if (state.finalizing) return value\n // Create a draft if the value is unmodified.\n if (value === peek(state.base, prop) && isDraftable(value)) {\n prepareCopy(state)\n return (state.copy[prop] = createProxy(value, state))\n }\n return value\n}\n\nfunction set(state, prop, value) {\n assertUnrevoked(state)\n state.assigned[prop] = true\n if (!state.modified) {\n if (is(value, peek(source(state), prop))) return\n markChanged(state)\n prepareCopy(state)\n }\n state.copy[prop] = value\n}\n\nfunction markChanged(state) {\n if (!state.modified) {\n state.modified = true\n if (state.parent) markChanged(state.parent)\n }\n}\n\nfunction prepareCopy(state) {\n if (!state.copy) state.copy = clonePotentialDraft(state.base)\n}\n\nfunction clonePotentialDraft(base) {\n const state = base && base[DRAFT_STATE]\n if (state) {\n state.finalizing = true\n const draft = shallowCopy(state.draft, true)\n state.finalizing = false\n return draft\n }\n return shallowCopy(base)\n}\n\nfunction proxyProperty(draft, prop, enumerable) {\n let desc = descriptors[prop]\n if (desc) {\n desc.enumerable = enumerable\n } else {\n descriptors[prop] = desc = {\n configurable: true,\n enumerable,\n get() {\n return get(this[DRAFT_STATE], prop)\n },\n set(value) {\n set(this[DRAFT_STATE], prop, value)\n }\n }\n }\n Object.defineProperty(draft, prop, desc)\n}\n\nfunction assertUnrevoked(state) {\n if (state.revoked === true)\n throw new Error(\n \"Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? \" +\n JSON.stringify(source(state))\n )\n}\n\n// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.\nfunction markChangesSweep(drafts) {\n // The natural order of drafts in the `scope` array is based on when they\n // were accessed. By processing drafts in reverse natural order, we have a\n // better chance of processing leaf nodes first. When a leaf node is known to\n // have changed, we can avoid any traversal of its ancestor nodes.\n for (let i = drafts.length - 1; i >= 0; i--) {\n const state = drafts[i][DRAFT_STATE]\n if (!state.modified) {\n if (Array.isArray(state.base)) {\n if (hasArrayChanges(state)) markChanged(state)\n } else if (hasObjectChanges(state)) markChanged(state)\n }\n }\n}\n\nfunction markChangesRecursively(object) {\n if (!object || typeof object !== \"object\") return\n const state = object[DRAFT_STATE]\n if (!state) return\n const {base, draft, assigned} = state\n if (!Array.isArray(object)) {\n // Look for added keys.\n Object.keys(draft).forEach(key => {\n // The `undefined` check is a fast path for pre-existing keys.\n if (base[key] === undefined && !has(base, key)) {\n assigned[key] = true\n markChanged(state)\n } else if (!assigned[key]) {\n // Only untouched properties trigger recursion.\n markChangesRecursively(draft[key])\n }\n })\n // Look for removed keys.\n Object.keys(base).forEach(key => {\n // The `undefined` check is a fast path for pre-existing keys.\n if (draft[key] === undefined && !has(draft, key)) {\n assigned[key] = false\n markChanged(state)\n }\n })\n } else if (hasArrayChanges(state)) {\n markChanged(state)\n assigned.length = true\n if (draft.length < base.length) {\n for (let i = draft.length; i < base.length; i++) assigned[i] = false\n } else {\n for (let i = base.length; i < draft.length; i++) assigned[i] = true\n }\n for (let i = 0; i < draft.length; i++) {\n // Only untouched indices trigger recursion.\n if (assigned[i] === undefined) markChangesRecursively(draft[i])\n }\n }\n}\n\nfunction hasObjectChanges(state) {\n const {base, draft} = state\n\n // Search for added keys and changed keys. Start at the back, because\n // non-numeric keys are ordered by time of definition on the object.\n const keys = Object.keys(draft)\n for (let i = keys.length - 1; i >= 0; i--) {\n const key = keys[i]\n const baseValue = base[key]\n // The `undefined` check is a fast path for pre-existing keys.\n if (baseValue === undefined && !has(base, key)) {\n return true\n }\n // Once a base key is deleted, future changes go undetected, because its\n // descriptor is erased. This branch detects any missed changes.\n else {\n const value = draft[key]\n const state = value && value[DRAFT_STATE]\n if (state ? state.base !== baseValue : !is(value, baseValue)) {\n return true\n }\n }\n }\n\n // At this point, no keys were added or changed.\n // Compare key count to determine if keys were deleted.\n return keys.length !== Object.keys(base).length\n}\n\nfunction hasArrayChanges(state) {\n const {draft} = state\n if (draft.length !== state.base.length) return true\n // See #116\n // If we first shorten the length, our array interceptors will be removed.\n // If after that new items are added, result in the same original length,\n // those last items will have no intercepting property.\n // So if there is no own descriptor on the last position, we know that items were removed and added\n // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check\n // the last one\n const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)\n // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)\n if (descriptor && !descriptor.get) return true\n // For all other cases, we don't have to compare, as they would have been picked up by the index setters\n return false\n}\n\nfunction createHiddenProperty(target, prop, value) {\n Object.defineProperty(target, prop, {\n value: value,\n enumerable: false,\n writable: true\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;AACA;AACA,IAAME,WAAW,GAAG,CAAC,CAAC;AACtB,IAAMC,IAAI,GAAGA,CAAA,KAAM,CAAC,CAAC;AAEd,SAASL,YAAYA,CAACM,KAAK,EAAEC,MAAM,EAAEC,UAAU,EAAE;EACpDF,KAAK,CAACG,MAAM,CAACC,OAAO,CAACC,KAAK,IAAI;IAC1BA,KAAK,CAACC,mBAAW,CAAC,CAACC,UAAU,GAAG,IAAI;EACxC,CAAC,CAAC;EACF,IAAI,CAACL,UAAU,EAAE;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/B,KAAK,IAAIA,KAAK,CAACc,mBAAW,CAAC;MACzC,IAAIiB,MAAK,GAAGA,MAAK,CAACX,IAAI,KAAK6C,SAAS,GAAG,CAAC,IAAAlB,UAAE,EAAC/C,KAAK,EAAEiE,SAAS,CAAC,EAAE;QAC1D,OAAO,IAAI;MACf;IACJ;EACJ;;EAEA;EACA;EACA,OAAOJ,IAAI,CAACJ,MAAM,KAAK5D,MAAM,CAACgE,IAAI,CAACzC,IAAI,CAAC,CAACqC,MAAM;AACnD;AAEA,SAASC,eAAeA,CAAC3B,KAAK,EAAE;EAC5B,IAAOlB,KAAK,GAAIkB,KAAK,CAAdlB,KAAK;EACZ,IAAIA,KAAK,CAAC4C,MAAM,KAAK1B,KAAK,CAACX,IAAI,CAACqC,MAAM,EAAE,OAAO,IAAI;EACnD;EACA;EACA;EACA;EACA;EACA;EACA;EACA,IAAMS,UAAU,GAAGrE,MAAM,CAACsE,wBAAwB,CAACtD,KAAK,EAAEA,KAAK,CAAC4C,MAAM,GAAG,CAAC,CAAC;EAC3E;EACA,IAAIS,UAAU,IAAI,CAACA,UAAU,CAACxB,GAAG,EAAE,OAAO,IAAI;EAC9C;EACA,OAAO,KAAK;AAChB;AAEA,SAASJ,oBAAoBA,CAAC8B,MAAM,EAAE1C,IAAI,EAAE1B,KAAK,EAAE;EAC/CH,MAAM,CAACC,cAAc,CAACsE,MAAM,EAAE1C,IAAI,EAAE;IAChC1B,KAAK,EAAEA,KAAK;IACZkD,UAAU,EAAE,KAAK;IACjBmB,QAAQ,EAAE;EACd,CAAC,CAAC;AACN"}
|
package/lib/immer.d.ts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
type Tail<T extends any[]> = ((...t: T) => any) extends ((
|
|
2
|
+
_: any,
|
|
3
|
+
...tail: infer TT
|
|
4
|
+
) => any)
|
|
5
|
+
? TT
|
|
6
|
+
: []
|
|
7
|
+
|
|
8
|
+
/** Object types that should never be mapped */
|
|
9
|
+
type AtomicObject =
|
|
10
|
+
| Function
|
|
11
|
+
| Map<any, any>
|
|
12
|
+
| WeakMap<any, any>
|
|
13
|
+
| Set<any>
|
|
14
|
+
| WeakSet<any>
|
|
15
|
+
| Promise<any>
|
|
16
|
+
| Date
|
|
17
|
+
| RegExp
|
|
18
|
+
| Boolean
|
|
19
|
+
| Number
|
|
20
|
+
| String
|
|
21
|
+
|
|
22
|
+
export type Draft<T> = T extends AtomicObject
|
|
23
|
+
? T
|
|
24
|
+
: T extends object
|
|
25
|
+
? {-readonly [K in keyof T]: Draft<T[K]>}
|
|
26
|
+
: T
|
|
27
|
+
|
|
28
|
+
/** Convert a mutable type into a readonly type */
|
|
29
|
+
export type Immutable<T> = T extends AtomicObject
|
|
30
|
+
? T
|
|
31
|
+
: T extends object
|
|
32
|
+
? {readonly [K in keyof T]: Immutable<T[K]>}
|
|
33
|
+
: T
|
|
34
|
+
|
|
35
|
+
export interface Patch {
|
|
36
|
+
op: "replace" | "remove" | "add"
|
|
37
|
+
path: (string | number)[]
|
|
38
|
+
value?: any
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void
|
|
42
|
+
|
|
43
|
+
/** Converts `nothing` into `undefined` */
|
|
44
|
+
type FromNothing<T> = T extends Nothing ? undefined : T
|
|
45
|
+
|
|
46
|
+
/** The inferred return type of `produce` */
|
|
47
|
+
export type Produced<Base, Return> = Return extends void
|
|
48
|
+
? Base
|
|
49
|
+
: Return extends Promise<infer Result>
|
|
50
|
+
? Promise<Result extends void ? Base : FromNothing<Result>>
|
|
51
|
+
: FromNothing<Return>
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The `produce` function takes a value and a "recipe function" (whose
|
|
55
|
+
* return value often depends on the base state). The recipe function is
|
|
56
|
+
* free to mutate its first argument however it wants. All mutations are
|
|
57
|
+
* only ever applied to a __copy__ of the base state.
|
|
58
|
+
*
|
|
59
|
+
* Pass only a function to create a "curried producer" which relieves you
|
|
60
|
+
* from passing the recipe function every time.
|
|
61
|
+
*
|
|
62
|
+
* Only plain objects and arrays are made mutable. All other objects are
|
|
63
|
+
* considered uncopyable.
|
|
64
|
+
*
|
|
65
|
+
* Note: This function is __bound__ to its `Immer` instance.
|
|
66
|
+
*
|
|
67
|
+
* @param {any} base - the initial state
|
|
68
|
+
* @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
|
|
69
|
+
* @param {Function} patchListener - optional function that will be called with all the patches produced here
|
|
70
|
+
* @returns {any} a new state, or the initial state if nothing was modified
|
|
71
|
+
*/
|
|
72
|
+
export interface IProduce {
|
|
73
|
+
/** Curried producer */
|
|
74
|
+
<
|
|
75
|
+
Recipe extends (...args: any[]) => any,
|
|
76
|
+
Params extends any[] = Parameters<Recipe>,
|
|
77
|
+
T = Params[0]
|
|
78
|
+
>(
|
|
79
|
+
recipe: Recipe
|
|
80
|
+
): <Base extends Immutable<T>>(
|
|
81
|
+
base: Base,
|
|
82
|
+
...rest: Tail<Params>
|
|
83
|
+
) => Produced<Base, ReturnType<Recipe>>
|
|
84
|
+
// ^ by making the returned type generic, the actual type of the passed in object is preferred
|
|
85
|
+
// over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe
|
|
86
|
+
// Note: the type of S is the widened version of T, so it can have more props than T, but that is technically actually correct!
|
|
87
|
+
|
|
88
|
+
/** Curried producer with initial state */
|
|
89
|
+
<
|
|
90
|
+
Recipe extends (...args: any[]) => any,
|
|
91
|
+
Params extends any[] = Parameters<Recipe>,
|
|
92
|
+
T = Params[0]
|
|
93
|
+
>(
|
|
94
|
+
recipe: Recipe,
|
|
95
|
+
initialState: Immutable<T>
|
|
96
|
+
): <Base extends Immutable<T>>(
|
|
97
|
+
base?: Base,
|
|
98
|
+
...rest: Tail<Params>
|
|
99
|
+
) => Produced<Base, ReturnType<Recipe>>
|
|
100
|
+
|
|
101
|
+
/** Normal producer */
|
|
102
|
+
<Base, D = Draft<Base>, Return = void>(
|
|
103
|
+
base: Base,
|
|
104
|
+
recipe: (draft: D) => Return,
|
|
105
|
+
listener?: PatchListener
|
|
106
|
+
): Produced<Base, Return>
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export const produce: IProduce
|
|
110
|
+
export default produce
|
|
111
|
+
|
|
112
|
+
/** Use a class type for `nothing` so its type is unique */
|
|
113
|
+
declare class Nothing {
|
|
114
|
+
// This lets us do `Exclude<T, Nothing>`
|
|
115
|
+
private _: any
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The sentinel value returned by producers to replace the draft with undefined.
|
|
120
|
+
*/
|
|
121
|
+
export const nothing: Nothing
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* To let Immer treat your class instances as plain immutable objects
|
|
125
|
+
* (albeit with a custom prototype), you must define either an instance property
|
|
126
|
+
* or a static property on each of your custom classes.
|
|
127
|
+
*
|
|
128
|
+
* Otherwise, your class instance will never be drafted, which means it won't be
|
|
129
|
+
* safe to mutate in a produce callback.
|
|
130
|
+
*/
|
|
131
|
+
export const immerable: unique symbol
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Pass true to automatically freeze all copies created by Immer.
|
|
135
|
+
*
|
|
136
|
+
* By default, auto-freezing is disabled in production.
|
|
137
|
+
*/
|
|
138
|
+
export function setAutoFreeze(autoFreeze: boolean): void
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Pass true to use the ES2015 `Proxy` class when creating drafts, which is
|
|
142
|
+
* always faster than using ES5 proxies.
|
|
143
|
+
*
|
|
144
|
+
* By default, feature detection is used, so calling this is rarely necessary.
|
|
145
|
+
*/
|
|
146
|
+
export function setUseProxies(useProxies: boolean): void
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Apply an array of Immer patches to the first argument.
|
|
150
|
+
*
|
|
151
|
+
* This function is a producer, which means copy-on-write is in effect.
|
|
152
|
+
*/
|
|
153
|
+
export function applyPatches<S>(base: S, patches: Patch[]): S
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Create an Immer draft from the given base state, which may be a draft itself.
|
|
157
|
+
* The draft can be modified until you finalize it with the `finishDraft` function.
|
|
158
|
+
*/
|
|
159
|
+
export function createDraft<T>(base: T): Draft<T>
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Finalize an Immer draft from a `createDraft` call, returning the base state
|
|
163
|
+
* (if no changes were made) or a modified copy. The draft must *not* be
|
|
164
|
+
* mutated afterwards.
|
|
165
|
+
*
|
|
166
|
+
* Pass a function as the 2nd argument to generate Immer patches based on the
|
|
167
|
+
* changes that were made.
|
|
168
|
+
*/
|
|
169
|
+
export function finishDraft<T>(draft: T, listener?: PatchListener): Immutable<T>
|
|
170
|
+
|
|
171
|
+
/** Get the underlying object that is represented by the given draft */
|
|
172
|
+
export function original<T>(value: T): T | void
|
|
173
|
+
|
|
174
|
+
/** Returns true if the given value is an Immer draft */
|
|
175
|
+
export function isDraft(value: any): boolean
|
|
176
|
+
|
|
177
|
+
/** Returns true if the given value can be drafted by Immer */
|
|
178
|
+
export function isDraftable(value: any): boolean
|
|
179
|
+
|
|
180
|
+
export class Immer {
|
|
181
|
+
constructor(config: {
|
|
182
|
+
useProxies?: boolean
|
|
183
|
+
autoFreeze?: boolean
|
|
184
|
+
onAssign?: (
|
|
185
|
+
state: ImmerState,
|
|
186
|
+
prop: string | number,
|
|
187
|
+
value: unknown
|
|
188
|
+
) => void
|
|
189
|
+
onDelete?: (state: ImmerState, prop: string | number) => void
|
|
190
|
+
onCopy?: (state: ImmerState) => void
|
|
191
|
+
})
|
|
192
|
+
/**
|
|
193
|
+
* The `produce` function takes a value and a "recipe function" (whose
|
|
194
|
+
* return value often depends on the base state). The recipe function is
|
|
195
|
+
* free to mutate its first argument however it wants. All mutations are
|
|
196
|
+
* only ever applied to a __copy__ of the base state.
|
|
197
|
+
*
|
|
198
|
+
* Pass only a function to create a "curried producer" which relieves you
|
|
199
|
+
* from passing the recipe function every time.
|
|
200
|
+
*
|
|
201
|
+
* Only plain objects and arrays are made mutable. All other objects are
|
|
202
|
+
* considered uncopyable.
|
|
203
|
+
*
|
|
204
|
+
* Note: This function is __bound__ to its `Immer` instance.
|
|
205
|
+
*
|
|
206
|
+
* @param {any} base - the initial state
|
|
207
|
+
* @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
|
|
208
|
+
* @param {Function} patchListener - optional function that will be called with all the patches produced here
|
|
209
|
+
* @returns {any} a new state, or the initial state if nothing was modified
|
|
210
|
+
*/
|
|
211
|
+
produce: IProduce
|
|
212
|
+
/**
|
|
213
|
+
* When true, `produce` will freeze the copies it creates.
|
|
214
|
+
*/
|
|
215
|
+
readonly autoFreeze: boolean
|
|
216
|
+
/**
|
|
217
|
+
* When true, drafts are ES2015 proxies.
|
|
218
|
+
*/
|
|
219
|
+
readonly useProxies: boolean
|
|
220
|
+
/**
|
|
221
|
+
* Pass true to automatically freeze all copies created by Immer.
|
|
222
|
+
*
|
|
223
|
+
* By default, auto-freezing is disabled in production.
|
|
224
|
+
*/
|
|
225
|
+
setAutoFreeze(autoFreeze: boolean): void
|
|
226
|
+
/**
|
|
227
|
+
* Pass true to use the ES2015 `Proxy` class when creating drafts, which is
|
|
228
|
+
* always faster than using ES5 proxies.
|
|
229
|
+
*
|
|
230
|
+
* By default, feature detection is used, so calling this is rarely necessary.
|
|
231
|
+
*/
|
|
232
|
+
setUseProxies(useProxies: boolean): void
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export interface ImmerState<T = any> {
|
|
236
|
+
parent?: ImmerState
|
|
237
|
+
base: T
|
|
238
|
+
copy: T
|
|
239
|
+
assigned: {[prop: string]: boolean; [index: number]: boolean}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Backward compatibility with --target es5
|
|
243
|
+
declare global {
|
|
244
|
+
interface Set<T> {}
|
|
245
|
+
interface Map<K, V> {}
|
|
246
|
+
interface WeakSet<T> {}
|
|
247
|
+
interface WeakMap<K extends object, V> {}
|
|
248
|
+
}
|