| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, '__esModule', { value: true });
|
|---|
| 4 |
|
|---|
| 5 | var _ref;
|
|---|
| 6 |
|
|---|
| 7 | // Should be no imports here!
|
|---|
| 8 | // Some things that should be evaluated before all else...
|
|---|
| 9 | // We only want to know if non-polyfilled symbols are available
|
|---|
| 10 | var hasSymbol = typeof Symbol !== "undefined" && typeof
|
|---|
| 11 | /*#__PURE__*/
|
|---|
| 12 | Symbol("x") === "symbol";
|
|---|
| 13 | var hasMap = typeof Map !== "undefined";
|
|---|
| 14 | var hasSet = typeof Set !== "undefined";
|
|---|
| 15 | var hasProxies = typeof Proxy !== "undefined" && typeof Proxy.revocable !== "undefined" && typeof Reflect !== "undefined";
|
|---|
| 16 | /**
|
|---|
| 17 | * The sentinel value returned by producers to replace the draft with undefined.
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | var NOTHING = hasSymbol ?
|
|---|
| 21 | /*#__PURE__*/
|
|---|
| 22 | Symbol.for("immer-nothing") : (_ref = {}, _ref["immer-nothing"] = true, _ref);
|
|---|
| 23 | /**
|
|---|
| 24 | * To let Immer treat your class instances as plain immutable objects
|
|---|
| 25 | * (albeit with a custom prototype), you must define either an instance property
|
|---|
| 26 | * or a static property on each of your custom classes.
|
|---|
| 27 | *
|
|---|
| 28 | * Otherwise, your class instance will never be drafted, which means it won't be
|
|---|
| 29 | * safe to mutate in a produce callback.
|
|---|
| 30 | */
|
|---|
| 31 |
|
|---|
| 32 | var DRAFTABLE = hasSymbol ?
|
|---|
| 33 | /*#__PURE__*/
|
|---|
| 34 | Symbol.for("immer-draftable") : "__$immer_draftable";
|
|---|
| 35 | var DRAFT_STATE = hasSymbol ?
|
|---|
| 36 | /*#__PURE__*/
|
|---|
| 37 | Symbol.for("immer-state") : "__$immer_state"; // Even a polyfilled Symbol might provide Symbol.iterator
|
|---|
| 38 |
|
|---|
| 39 | var iteratorSymbol = typeof Symbol != "undefined" && Symbol.iterator || "@@iterator";
|
|---|
| 40 |
|
|---|
| 41 | var errors = {
|
|---|
| 42 | 0: "Illegal state",
|
|---|
| 43 | 1: "Immer drafts cannot have computed properties",
|
|---|
| 44 | 2: "This object has been frozen and should not be mutated",
|
|---|
| 45 | 3: function _(data) {
|
|---|
| 46 | return "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " + data;
|
|---|
| 47 | },
|
|---|
| 48 | 4: "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",
|
|---|
| 49 | 5: "Immer forbids circular references",
|
|---|
| 50 | 6: "The first or second argument to `produce` must be a function",
|
|---|
| 51 | 7: "The third argument to `produce` must be a function or undefined",
|
|---|
| 52 | 8: "First argument to `createDraft` must be a plain object, an array, or an immerable object",
|
|---|
| 53 | 9: "First argument to `finishDraft` must be a draft returned by `createDraft`",
|
|---|
| 54 | 10: "The given draft is already finalized",
|
|---|
| 55 | 11: "Object.defineProperty() cannot be used on an Immer draft",
|
|---|
| 56 | 12: "Object.setPrototypeOf() cannot be used on an Immer draft",
|
|---|
| 57 | 13: "Immer only supports deleting array indices",
|
|---|
| 58 | 14: "Immer only supports setting array indices and the 'length' property",
|
|---|
| 59 | 15: function _(path) {
|
|---|
| 60 | return "Cannot apply patch, path doesn't resolve: " + path;
|
|---|
| 61 | },
|
|---|
| 62 | 16: 'Sets cannot have "replace" patches.',
|
|---|
| 63 | 17: function _(op) {
|
|---|
| 64 | return "Unsupported patch operation: " + op;
|
|---|
| 65 | },
|
|---|
| 66 | 18: function _(plugin) {
|
|---|
| 67 | return "The plugin for '" + plugin + "' has not been loaded into Immer. To enable the plugin, import and call `enable" + plugin + "()` when initializing your application.";
|
|---|
| 68 | },
|
|---|
| 69 | 20: "Cannot use proxies if Proxy, Proxy.revocable or Reflect are not available",
|
|---|
| 70 | 21: function _(thing) {
|
|---|
| 71 | return "produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '" + thing + "'";
|
|---|
| 72 | },
|
|---|
| 73 | 22: function _(thing) {
|
|---|
| 74 | return "'current' expects a draft, got: " + thing;
|
|---|
| 75 | },
|
|---|
| 76 | 23: function _(thing) {
|
|---|
| 77 | return "'original' expects a draft, got: " + thing;
|
|---|
| 78 | },
|
|---|
| 79 | 24: "Patching reserved attributes like __proto__, prototype and constructor is not allowed"
|
|---|
| 80 | };
|
|---|
| 81 | function die(error) {
|
|---|
| 82 | for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|---|
| 83 | args[_key - 1] = arguments[_key];
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | {
|
|---|
| 87 | var e = errors[error];
|
|---|
| 88 | var msg = !e ? "unknown error nr: " + error : typeof e === "function" ? e.apply(null, args) : e;
|
|---|
| 89 | throw new Error("[Immer] " + msg);
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /** Returns true if the given value is an Immer draft */
|
|---|
| 94 |
|
|---|
| 95 | /*#__PURE__*/
|
|---|
| 96 |
|
|---|
| 97 | function isDraft(value) {
|
|---|
| 98 | return !!value && !!value[DRAFT_STATE];
|
|---|
| 99 | }
|
|---|
| 100 | /** Returns true if the given value can be drafted by Immer */
|
|---|
| 101 |
|
|---|
| 102 | /*#__PURE__*/
|
|---|
| 103 |
|
|---|
| 104 | function isDraftable(value) {
|
|---|
| 105 | var _value$constructor;
|
|---|
| 106 |
|
|---|
| 107 | if (!value) return false;
|
|---|
| 108 | return isPlainObject(value) || Array.isArray(value) || !!value[DRAFTABLE] || !!((_value$constructor = value.constructor) === null || _value$constructor === void 0 ? void 0 : _value$constructor[DRAFTABLE]) || isMap(value) || isSet(value);
|
|---|
| 109 | }
|
|---|
| 110 | var objectCtorString =
|
|---|
| 111 | /*#__PURE__*/
|
|---|
| 112 | Object.prototype.constructor.toString();
|
|---|
| 113 | /*#__PURE__*/
|
|---|
| 114 |
|
|---|
| 115 | function isPlainObject(value) {
|
|---|
| 116 | if (!value || typeof value !== "object") return false;
|
|---|
| 117 | var proto = Object.getPrototypeOf(value);
|
|---|
| 118 |
|
|---|
| 119 | if (proto === null) {
|
|---|
| 120 | return true;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | var Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
|
|---|
| 124 | if (Ctor === Object) return true;
|
|---|
| 125 | return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
|
|---|
| 126 | }
|
|---|
| 127 | function original(value) {
|
|---|
| 128 | if (!isDraft(value)) die(23, value);
|
|---|
| 129 | return value[DRAFT_STATE].base_;
|
|---|
| 130 | }
|
|---|
| 131 | /*#__PURE__*/
|
|---|
| 132 |
|
|---|
| 133 | var ownKeys = typeof Reflect !== "undefined" && Reflect.ownKeys ? Reflect.ownKeys : typeof Object.getOwnPropertySymbols !== "undefined" ? function (obj) {
|
|---|
| 134 | return Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj));
|
|---|
| 135 | } :
|
|---|
| 136 | /* istanbul ignore next */
|
|---|
| 137 | Object.getOwnPropertyNames;
|
|---|
| 138 | var getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors || function getOwnPropertyDescriptors(target) {
|
|---|
| 139 | // Polyfill needed for Hermes and IE, see https://github.com/facebook/hermes/issues/274
|
|---|
| 140 | var res = {};
|
|---|
| 141 | ownKeys(target).forEach(function (key) {
|
|---|
| 142 | res[key] = Object.getOwnPropertyDescriptor(target, key);
|
|---|
| 143 | });
|
|---|
| 144 | return res;
|
|---|
| 145 | };
|
|---|
| 146 | function each(obj, iter, enumerableOnly) {
|
|---|
| 147 | if (enumerableOnly === void 0) {
|
|---|
| 148 | enumerableOnly = false;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | if (getArchtype(obj) === 0
|
|---|
| 152 | /* Object */
|
|---|
| 153 | ) {
|
|---|
| 154 | (enumerableOnly ? Object.keys : ownKeys)(obj).forEach(function (key) {
|
|---|
| 155 | if (!enumerableOnly || typeof key !== "symbol") iter(key, obj[key], obj);
|
|---|
| 156 | });
|
|---|
| 157 | } else {
|
|---|
| 158 | obj.forEach(function (entry, index) {
|
|---|
| 159 | return iter(index, entry, obj);
|
|---|
| 160 | });
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 | /*#__PURE__*/
|
|---|
| 164 |
|
|---|
| 165 | function getArchtype(thing) {
|
|---|
| 166 | /* istanbul ignore next */
|
|---|
| 167 | var state = thing[DRAFT_STATE];
|
|---|
| 168 | return state ? state.type_ > 3 ? state.type_ - 4 // cause Object and Array map back from 4 and 5
|
|---|
| 169 | : state.type_ // others are the same
|
|---|
| 170 | : Array.isArray(thing) ? 1
|
|---|
| 171 | /* Array */
|
|---|
| 172 | : isMap(thing) ? 2
|
|---|
| 173 | /* Map */
|
|---|
| 174 | : isSet(thing) ? 3
|
|---|
| 175 | /* Set */
|
|---|
| 176 | : 0
|
|---|
| 177 | /* Object */
|
|---|
| 178 | ;
|
|---|
| 179 | }
|
|---|
| 180 | /*#__PURE__*/
|
|---|
| 181 |
|
|---|
| 182 | function has(thing, prop) {
|
|---|
| 183 | return getArchtype(thing) === 2
|
|---|
| 184 | /* Map */
|
|---|
| 185 | ? thing.has(prop) : Object.prototype.hasOwnProperty.call(thing, prop);
|
|---|
| 186 | }
|
|---|
| 187 | /*#__PURE__*/
|
|---|
| 188 |
|
|---|
| 189 | function get(thing, prop) {
|
|---|
| 190 | // @ts-ignore
|
|---|
| 191 | return getArchtype(thing) === 2
|
|---|
| 192 | /* Map */
|
|---|
| 193 | ? thing.get(prop) : thing[prop];
|
|---|
| 194 | }
|
|---|
| 195 | /*#__PURE__*/
|
|---|
| 196 |
|
|---|
| 197 | function set(thing, propOrOldValue, value) {
|
|---|
| 198 | var t = getArchtype(thing);
|
|---|
| 199 | if (t === 2
|
|---|
| 200 | /* Map */
|
|---|
| 201 | ) thing.set(propOrOldValue, value);else if (t === 3
|
|---|
| 202 | /* Set */
|
|---|
| 203 | ) {
|
|---|
| 204 | thing.add(value);
|
|---|
| 205 | } else thing[propOrOldValue] = value;
|
|---|
| 206 | }
|
|---|
| 207 | /*#__PURE__*/
|
|---|
| 208 |
|
|---|
| 209 | function is(x, y) {
|
|---|
| 210 | // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
|
|---|
| 211 | if (x === y) {
|
|---|
| 212 | return x !== 0 || 1 / x === 1 / y;
|
|---|
| 213 | } else {
|
|---|
| 214 | return x !== x && y !== y;
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 | /*#__PURE__*/
|
|---|
| 218 |
|
|---|
| 219 | function isMap(target) {
|
|---|
| 220 | return hasMap && target instanceof Map;
|
|---|
| 221 | }
|
|---|
| 222 | /*#__PURE__*/
|
|---|
| 223 |
|
|---|
| 224 | function isSet(target) {
|
|---|
| 225 | return hasSet && target instanceof Set;
|
|---|
| 226 | }
|
|---|
| 227 | /*#__PURE__*/
|
|---|
| 228 |
|
|---|
| 229 | function latest(state) {
|
|---|
| 230 | return state.copy_ || state.base_;
|
|---|
| 231 | }
|
|---|
| 232 | /*#__PURE__*/
|
|---|
| 233 |
|
|---|
| 234 | function shallowCopy(base) {
|
|---|
| 235 | if (Array.isArray(base)) return Array.prototype.slice.call(base);
|
|---|
| 236 | var descriptors = getOwnPropertyDescriptors(base);
|
|---|
| 237 | delete descriptors[DRAFT_STATE];
|
|---|
| 238 | var keys = ownKeys(descriptors);
|
|---|
| 239 |
|
|---|
| 240 | for (var i = 0; i < keys.length; i++) {
|
|---|
| 241 | var key = keys[i];
|
|---|
| 242 | var desc = descriptors[key];
|
|---|
| 243 |
|
|---|
| 244 | if (desc.writable === false) {
|
|---|
| 245 | desc.writable = true;
|
|---|
| 246 | desc.configurable = true;
|
|---|
| 247 | } // like object.assign, we will read any _own_, get/set accessors. This helps in dealing
|
|---|
| 248 | // with libraries that trap values, like mobx or vue
|
|---|
| 249 | // unlike object.assign, non-enumerables will be copied as well
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 | if (desc.get || desc.set) descriptors[key] = {
|
|---|
| 253 | configurable: true,
|
|---|
| 254 | writable: true,
|
|---|
| 255 | enumerable: desc.enumerable,
|
|---|
| 256 | value: base[key]
|
|---|
| 257 | };
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | return Object.create(Object.getPrototypeOf(base), descriptors);
|
|---|
| 261 | }
|
|---|
| 262 | function freeze(obj, deep) {
|
|---|
| 263 | if (deep === void 0) {
|
|---|
| 264 | deep = false;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj;
|
|---|
| 268 |
|
|---|
| 269 | if (getArchtype(obj) > 1
|
|---|
| 270 | /* Map or Set */
|
|---|
| 271 | ) {
|
|---|
| 272 | obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | Object.freeze(obj);
|
|---|
| 276 | if (deep) each(obj, function (key, value) {
|
|---|
| 277 | return freeze(value, true);
|
|---|
| 278 | }, true);
|
|---|
| 279 | return obj;
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | function dontMutateFrozenCollections() {
|
|---|
| 283 | die(2);
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | function isFrozen(obj) {
|
|---|
| 287 | if (obj == null || typeof obj !== "object") return true; // See #600, IE dies on non-objects in Object.isFrozen
|
|---|
| 288 |
|
|---|
| 289 | return Object.isFrozen(obj);
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | /** Plugin utilities */
|
|---|
| 293 |
|
|---|
| 294 | var plugins = {};
|
|---|
| 295 | function getPlugin(pluginKey) {
|
|---|
| 296 | var plugin = plugins[pluginKey];
|
|---|
| 297 |
|
|---|
| 298 | if (!plugin) {
|
|---|
| 299 | die(18, pluginKey);
|
|---|
| 300 | } // @ts-ignore
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 | return plugin;
|
|---|
| 304 | }
|
|---|
| 305 | function loadPlugin(pluginKey, implementation) {
|
|---|
| 306 | if (!plugins[pluginKey]) plugins[pluginKey] = implementation;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | var currentScope;
|
|---|
| 310 | function getCurrentScope() {
|
|---|
| 311 | if ( !currentScope) die(0);
|
|---|
| 312 | return currentScope;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | function createScope(parent_, immer_) {
|
|---|
| 316 | return {
|
|---|
| 317 | drafts_: [],
|
|---|
| 318 | parent_: parent_,
|
|---|
| 319 | immer_: immer_,
|
|---|
| 320 | // Whenever the modified draft contains a draft from another scope, we
|
|---|
| 321 | // need to prevent auto-freezing so the unowned draft can be finalized.
|
|---|
| 322 | canAutoFreeze_: true,
|
|---|
| 323 | unfinalizedDrafts_: 0
|
|---|
| 324 | };
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | function usePatchesInScope(scope, patchListener) {
|
|---|
| 328 | if (patchListener) {
|
|---|
| 329 | getPlugin("Patches"); // assert we have the plugin
|
|---|
| 330 |
|
|---|
| 331 | scope.patches_ = [];
|
|---|
| 332 | scope.inversePatches_ = [];
|
|---|
| 333 | scope.patchListener_ = patchListener;
|
|---|
| 334 | }
|
|---|
| 335 | }
|
|---|
| 336 | function revokeScope(scope) {
|
|---|
| 337 | leaveScope(scope);
|
|---|
| 338 | scope.drafts_.forEach(revokeDraft); // @ts-ignore
|
|---|
| 339 |
|
|---|
| 340 | scope.drafts_ = null;
|
|---|
| 341 | }
|
|---|
| 342 | function leaveScope(scope) {
|
|---|
| 343 | if (scope === currentScope) {
|
|---|
| 344 | currentScope = scope.parent_;
|
|---|
| 345 | }
|
|---|
| 346 | }
|
|---|
| 347 | function enterScope(immer) {
|
|---|
| 348 | return currentScope = createScope(currentScope, immer);
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | function revokeDraft(draft) {
|
|---|
| 352 | var state = draft[DRAFT_STATE];
|
|---|
| 353 | if (state.type_ === 0
|
|---|
| 354 | /* ProxyObject */
|
|---|
| 355 | || state.type_ === 1
|
|---|
| 356 | /* ProxyArray */
|
|---|
| 357 | ) state.revoke_();else state.revoked_ = true;
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | function processResult(result, scope) {
|
|---|
| 361 | scope.unfinalizedDrafts_ = scope.drafts_.length;
|
|---|
| 362 | var baseDraft = scope.drafts_[0];
|
|---|
| 363 | var isReplaced = result !== undefined && result !== baseDraft;
|
|---|
| 364 | if (!scope.immer_.useProxies_) getPlugin("ES5").willFinalizeES5_(scope, result, isReplaced);
|
|---|
| 365 |
|
|---|
| 366 | if (isReplaced) {
|
|---|
| 367 | if (baseDraft[DRAFT_STATE].modified_) {
|
|---|
| 368 | revokeScope(scope);
|
|---|
| 369 | die(4);
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | if (isDraftable(result)) {
|
|---|
| 373 | // Finalize the result in case it contains (or is) a subset of the draft.
|
|---|
| 374 | result = finalize(scope, result);
|
|---|
| 375 | if (!scope.parent_) maybeFreeze(scope, result);
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | if (scope.patches_) {
|
|---|
| 379 | getPlugin("Patches").generateReplacementPatches_(baseDraft[DRAFT_STATE].base_, result, scope.patches_, scope.inversePatches_);
|
|---|
| 380 | }
|
|---|
| 381 | } else {
|
|---|
| 382 | // Finalize the base draft.
|
|---|
| 383 | result = finalize(scope, baseDraft, []);
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | revokeScope(scope);
|
|---|
| 387 |
|
|---|
| 388 | if (scope.patches_) {
|
|---|
| 389 | scope.patchListener_(scope.patches_, scope.inversePatches_);
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | return result !== NOTHING ? result : undefined;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | function finalize(rootScope, value, path) {
|
|---|
| 396 | // Don't recurse in tho recursive data structures
|
|---|
| 397 | if (isFrozen(value)) return value;
|
|---|
| 398 | var state = value[DRAFT_STATE]; // A plain object, might need freezing, might contain drafts
|
|---|
| 399 |
|
|---|
| 400 | if (!state) {
|
|---|
| 401 | each(value, function (key, childValue) {
|
|---|
| 402 | return finalizeProperty(rootScope, state, value, key, childValue, path);
|
|---|
| 403 | }, true // See #590, don't recurse into non-enumerable of non drafted objects
|
|---|
| 404 | );
|
|---|
| 405 | return value;
|
|---|
| 406 | } // Never finalize drafts owned by another scope.
|
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 | if (state.scope_ !== rootScope) return value; // Unmodified draft, return the (frozen) original
|
|---|
| 410 |
|
|---|
| 411 | if (!state.modified_) {
|
|---|
| 412 | maybeFreeze(rootScope, state.base_, true);
|
|---|
| 413 | return state.base_;
|
|---|
| 414 | } // Not finalized yet, let's do that now
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 | if (!state.finalized_) {
|
|---|
| 418 | state.finalized_ = true;
|
|---|
| 419 | state.scope_.unfinalizedDrafts_--;
|
|---|
| 420 | var result = // For ES5, create a good copy from the draft first, with added keys and without deleted keys.
|
|---|
| 421 | state.type_ === 4
|
|---|
| 422 | /* ES5Object */
|
|---|
| 423 | || state.type_ === 5
|
|---|
| 424 | /* ES5Array */
|
|---|
| 425 | ? state.copy_ = shallowCopy(state.draft_) : state.copy_; // Finalize all children of the copy
|
|---|
| 426 | // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628
|
|---|
| 427 | // To preserve insertion order in all cases we then clear the set
|
|---|
| 428 | // And we let finalizeProperty know it needs to re-add non-draft children back to the target
|
|---|
| 429 |
|
|---|
| 430 | var resultEach = result;
|
|---|
| 431 | var isSet = false;
|
|---|
| 432 |
|
|---|
| 433 | if (state.type_ === 3
|
|---|
| 434 | /* Set */
|
|---|
| 435 | ) {
|
|---|
| 436 | resultEach = new Set(result);
|
|---|
| 437 | result.clear();
|
|---|
| 438 | isSet = true;
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | each(resultEach, function (key, childValue) {
|
|---|
| 442 | return finalizeProperty(rootScope, state, result, key, childValue, path, isSet);
|
|---|
| 443 | }); // everything inside is frozen, we can freeze here
|
|---|
| 444 |
|
|---|
| 445 | maybeFreeze(rootScope, result, false); // first time finalizing, let's create those patches
|
|---|
| 446 |
|
|---|
| 447 | if (path && rootScope.patches_) {
|
|---|
| 448 | getPlugin("Patches").generatePatches_(state, path, rootScope.patches_, rootScope.inversePatches_);
|
|---|
| 449 | }
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | return state.copy_;
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | function finalizeProperty(rootScope, parentState, targetObject, prop, childValue, rootPath, targetIsSet) {
|
|---|
| 456 | if ( childValue === targetObject) die(5);
|
|---|
| 457 |
|
|---|
| 458 | if (isDraft(childValue)) {
|
|---|
| 459 | var path = rootPath && parentState && parentState.type_ !== 3
|
|---|
| 460 | /* Set */
|
|---|
| 461 | && // Set objects are atomic since they have no keys.
|
|---|
| 462 | !has(parentState.assigned_, prop) // Skip deep patches for assigned keys.
|
|---|
| 463 | ? rootPath.concat(prop) : undefined; // Drafts owned by `scope` are finalized here.
|
|---|
| 464 |
|
|---|
| 465 | var res = finalize(rootScope, childValue, path);
|
|---|
| 466 | set(targetObject, prop, res); // Drafts from another scope must prevented to be frozen
|
|---|
| 467 | // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze
|
|---|
| 468 |
|
|---|
| 469 | if (isDraft(res)) {
|
|---|
| 470 | rootScope.canAutoFreeze_ = false;
|
|---|
| 471 | } else return;
|
|---|
| 472 | } else if (targetIsSet) {
|
|---|
| 473 | targetObject.add(childValue);
|
|---|
| 474 | } // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.
|
|---|
| 475 |
|
|---|
| 476 |
|
|---|
| 477 | if (isDraftable(childValue) && !isFrozen(childValue)) {
|
|---|
| 478 | if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {
|
|---|
| 479 | // optimization: if an object is not a draft, and we don't have to
|
|---|
| 480 | // deepfreeze everything, and we are sure that no drafts are left in the remaining object
|
|---|
| 481 | // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.
|
|---|
| 482 | // This benefits especially adding large data tree's without further processing.
|
|---|
| 483 | // See add-data.js perf test
|
|---|
| 484 | return;
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | finalize(rootScope, childValue); // immer deep freezes plain objects, so if there is no parent state, we freeze as well
|
|---|
| 488 |
|
|---|
| 489 | if (!parentState || !parentState.scope_.parent_) maybeFreeze(rootScope, childValue);
|
|---|
| 490 | }
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | function maybeFreeze(scope, value, deep) {
|
|---|
| 494 | if (deep === void 0) {
|
|---|
| 495 | deep = false;
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | // we never freeze for a non-root scope; as it would prevent pruning for drafts inside wrapping objects
|
|---|
| 499 | if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {
|
|---|
| 500 | freeze(value, deep);
|
|---|
| 501 | }
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | /**
|
|---|
| 505 | * Returns a new draft of the `base` object.
|
|---|
| 506 | *
|
|---|
| 507 | * The second argument is the parent draft-state (used internally).
|
|---|
| 508 | */
|
|---|
| 509 |
|
|---|
| 510 | function createProxyProxy(base, parent) {
|
|---|
| 511 | var isArray = Array.isArray(base);
|
|---|
| 512 | var state = {
|
|---|
| 513 | type_: isArray ? 1
|
|---|
| 514 | /* ProxyArray */
|
|---|
| 515 | : 0
|
|---|
| 516 | /* ProxyObject */
|
|---|
| 517 | ,
|
|---|
| 518 | // Track which produce call this is associated with.
|
|---|
| 519 | scope_: parent ? parent.scope_ : getCurrentScope(),
|
|---|
| 520 | // True for both shallow and deep changes.
|
|---|
| 521 | modified_: false,
|
|---|
| 522 | // Used during finalization.
|
|---|
| 523 | finalized_: false,
|
|---|
| 524 | // Track which properties have been assigned (true) or deleted (false).
|
|---|
| 525 | assigned_: {},
|
|---|
| 526 | // The parent draft state.
|
|---|
| 527 | parent_: parent,
|
|---|
| 528 | // The base state.
|
|---|
| 529 | base_: base,
|
|---|
| 530 | // The base proxy.
|
|---|
| 531 | draft_: null,
|
|---|
| 532 | // The base copy with any updated values.
|
|---|
| 533 | copy_: null,
|
|---|
| 534 | // Called by the `produce` function.
|
|---|
| 535 | revoke_: null,
|
|---|
| 536 | isManual_: false
|
|---|
| 537 | }; // the traps must target something, a bit like the 'real' base.
|
|---|
| 538 | // but also, we need to be able to determine from the target what the relevant state is
|
|---|
| 539 | // (to avoid creating traps per instance to capture the state in closure,
|
|---|
| 540 | // and to avoid creating weird hidden properties as well)
|
|---|
| 541 | // So the trick is to use 'state' as the actual 'target'! (and make sure we intercept everything)
|
|---|
| 542 | // Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb
|
|---|
| 543 |
|
|---|
| 544 | var target = state;
|
|---|
| 545 | var traps = objectTraps;
|
|---|
| 546 |
|
|---|
| 547 | if (isArray) {
|
|---|
| 548 | target = [state];
|
|---|
| 549 | traps = arrayTraps;
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | var _Proxy$revocable = Proxy.revocable(target, traps),
|
|---|
| 553 | revoke = _Proxy$revocable.revoke,
|
|---|
| 554 | proxy = _Proxy$revocable.proxy;
|
|---|
| 555 |
|
|---|
| 556 | state.draft_ = proxy;
|
|---|
| 557 | state.revoke_ = revoke;
|
|---|
| 558 | return proxy;
|
|---|
| 559 | }
|
|---|
| 560 | /**
|
|---|
| 561 | * Object drafts
|
|---|
| 562 | */
|
|---|
| 563 |
|
|---|
| 564 | var objectTraps = {
|
|---|
| 565 | get: function get(state, prop) {
|
|---|
| 566 | if (prop === DRAFT_STATE) return state;
|
|---|
| 567 | var source = latest(state);
|
|---|
| 568 |
|
|---|
| 569 | if (!has(source, prop)) {
|
|---|
| 570 | // non-existing or non-own property...
|
|---|
| 571 | return readPropFromProto(state, source, prop);
|
|---|
| 572 | }
|
|---|
| 573 |
|
|---|
| 574 | var value = source[prop];
|
|---|
| 575 |
|
|---|
| 576 | if (state.finalized_ || !isDraftable(value)) {
|
|---|
| 577 | return value;
|
|---|
| 578 | } // Check for existing draft in modified state.
|
|---|
| 579 | // Assigned values are never drafted. This catches any drafts we created, too.
|
|---|
| 580 |
|
|---|
| 581 |
|
|---|
| 582 | if (value === peek(state.base_, prop)) {
|
|---|
| 583 | prepareCopy(state);
|
|---|
| 584 | return state.copy_[prop] = createProxy(state.scope_.immer_, value, state);
|
|---|
| 585 | }
|
|---|
| 586 |
|
|---|
| 587 | return value;
|
|---|
| 588 | },
|
|---|
| 589 | has: function has(state, prop) {
|
|---|
| 590 | return prop in latest(state);
|
|---|
| 591 | },
|
|---|
| 592 | ownKeys: function ownKeys(state) {
|
|---|
| 593 | return Reflect.ownKeys(latest(state));
|
|---|
| 594 | },
|
|---|
| 595 | set: function set(state, prop
|
|---|
| 596 | /* strictly not, but helps TS */
|
|---|
| 597 | , value) {
|
|---|
| 598 | var desc = getDescriptorFromProto(latest(state), prop);
|
|---|
| 599 |
|
|---|
| 600 | if (desc === null || desc === void 0 ? void 0 : desc.set) {
|
|---|
| 601 | // special case: if this write is captured by a setter, we have
|
|---|
| 602 | // to trigger it with the correct context
|
|---|
| 603 | desc.set.call(state.draft_, value);
|
|---|
| 604 | return true;
|
|---|
| 605 | }
|
|---|
| 606 |
|
|---|
| 607 | if (!state.modified_) {
|
|---|
| 608 | // the last check is because we need to be able to distinguish setting a non-existing to undefined (which is a change)
|
|---|
| 609 | // from setting an existing property with value undefined to undefined (which is not a change)
|
|---|
| 610 | var current = peek(latest(state), prop); // special case, if we assigning the original value to a draft, we can ignore the assignment
|
|---|
| 611 |
|
|---|
| 612 | var currentState = current === null || current === void 0 ? void 0 : current[DRAFT_STATE];
|
|---|
| 613 |
|
|---|
| 614 | if (currentState && currentState.base_ === value) {
|
|---|
| 615 | state.copy_[prop] = value;
|
|---|
| 616 | state.assigned_[prop] = false;
|
|---|
| 617 | return true;
|
|---|
| 618 | }
|
|---|
| 619 |
|
|---|
| 620 | if (is(value, current) && (value !== undefined || has(state.base_, prop))) return true;
|
|---|
| 621 | prepareCopy(state);
|
|---|
| 622 | markChanged(state);
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | if (state.copy_[prop] === value && ( // special case: handle new props with value 'undefined'
|
|---|
| 626 | value !== undefined || prop in state.copy_) || // special case: NaN
|
|---|
| 627 | Number.isNaN(value) && Number.isNaN(state.copy_[prop])) return true; // @ts-ignore
|
|---|
| 628 |
|
|---|
| 629 | state.copy_[prop] = value;
|
|---|
| 630 | state.assigned_[prop] = true;
|
|---|
| 631 | return true;
|
|---|
| 632 | },
|
|---|
| 633 | deleteProperty: function deleteProperty(state, prop) {
|
|---|
| 634 | // The `undefined` check is a fast path for pre-existing keys.
|
|---|
| 635 | if (peek(state.base_, prop) !== undefined || prop in state.base_) {
|
|---|
| 636 | state.assigned_[prop] = false;
|
|---|
| 637 | prepareCopy(state);
|
|---|
| 638 | markChanged(state);
|
|---|
| 639 | } else {
|
|---|
| 640 | // if an originally not assigned property was deleted
|
|---|
| 641 | delete state.assigned_[prop];
|
|---|
| 642 | } // @ts-ignore
|
|---|
| 643 |
|
|---|
| 644 |
|
|---|
| 645 | if (state.copy_) delete state.copy_[prop];
|
|---|
| 646 | return true;
|
|---|
| 647 | },
|
|---|
| 648 | // Note: We never coerce `desc.value` into an Immer draft, because we can't make
|
|---|
| 649 | // the same guarantee in ES5 mode.
|
|---|
| 650 | getOwnPropertyDescriptor: function getOwnPropertyDescriptor(state, prop) {
|
|---|
| 651 | var owner = latest(state);
|
|---|
| 652 | var desc = Reflect.getOwnPropertyDescriptor(owner, prop);
|
|---|
| 653 | if (!desc) return desc;
|
|---|
| 654 | return {
|
|---|
| 655 | writable: true,
|
|---|
| 656 | configurable: state.type_ !== 1
|
|---|
| 657 | /* ProxyArray */
|
|---|
| 658 | || prop !== "length",
|
|---|
| 659 | enumerable: desc.enumerable,
|
|---|
| 660 | value: owner[prop]
|
|---|
| 661 | };
|
|---|
| 662 | },
|
|---|
| 663 | defineProperty: function defineProperty() {
|
|---|
| 664 | die(11);
|
|---|
| 665 | },
|
|---|
| 666 | getPrototypeOf: function getPrototypeOf(state) {
|
|---|
| 667 | return Object.getPrototypeOf(state.base_);
|
|---|
| 668 | },
|
|---|
| 669 | setPrototypeOf: function setPrototypeOf() {
|
|---|
| 670 | die(12);
|
|---|
| 671 | }
|
|---|
| 672 | };
|
|---|
| 673 | /**
|
|---|
| 674 | * Array drafts
|
|---|
| 675 | */
|
|---|
| 676 |
|
|---|
| 677 | var arrayTraps = {};
|
|---|
| 678 | each(objectTraps, function (key, fn) {
|
|---|
| 679 | // @ts-ignore
|
|---|
| 680 | arrayTraps[key] = function () {
|
|---|
| 681 | arguments[0] = arguments[0][0];
|
|---|
| 682 | return fn.apply(this, arguments);
|
|---|
| 683 | };
|
|---|
| 684 | });
|
|---|
| 685 |
|
|---|
| 686 | arrayTraps.deleteProperty = function (state, prop) {
|
|---|
| 687 | if ( isNaN(parseInt(prop))) die(13); // @ts-ignore
|
|---|
| 688 |
|
|---|
| 689 | return arrayTraps.set.call(this, state, prop, undefined);
|
|---|
| 690 | };
|
|---|
| 691 |
|
|---|
| 692 | arrayTraps.set = function (state, prop, value) {
|
|---|
| 693 | if ( prop !== "length" && isNaN(parseInt(prop))) die(14);
|
|---|
| 694 | return objectTraps.set.call(this, state[0], prop, value, state[0]);
|
|---|
| 695 | }; // Access a property without creating an Immer draft.
|
|---|
| 696 |
|
|---|
| 697 |
|
|---|
| 698 | function peek(draft, prop) {
|
|---|
| 699 | var state = draft[DRAFT_STATE];
|
|---|
| 700 | var source = state ? latest(state) : draft;
|
|---|
| 701 | return source[prop];
|
|---|
| 702 | }
|
|---|
| 703 |
|
|---|
| 704 | function readPropFromProto(state, source, prop) {
|
|---|
| 705 | var _desc$get;
|
|---|
| 706 |
|
|---|
| 707 | var desc = getDescriptorFromProto(source, prop);
|
|---|
| 708 | return desc ? "value" in desc ? desc.value : // This is a very special case, if the prop is a getter defined by the
|
|---|
| 709 | // prototype, we should invoke it with the draft as context!
|
|---|
| 710 | (_desc$get = desc.get) === null || _desc$get === void 0 ? void 0 : _desc$get.call(state.draft_) : undefined;
|
|---|
| 711 | }
|
|---|
| 712 |
|
|---|
| 713 | function getDescriptorFromProto(source, prop) {
|
|---|
| 714 | // 'in' checks proto!
|
|---|
| 715 | if (!(prop in source)) return undefined;
|
|---|
| 716 | var proto = Object.getPrototypeOf(source);
|
|---|
| 717 |
|
|---|
| 718 | while (proto) {
|
|---|
| 719 | var desc = Object.getOwnPropertyDescriptor(proto, prop);
|
|---|
| 720 | if (desc) return desc;
|
|---|
| 721 | proto = Object.getPrototypeOf(proto);
|
|---|
| 722 | }
|
|---|
| 723 |
|
|---|
| 724 | return undefined;
|
|---|
| 725 | }
|
|---|
| 726 |
|
|---|
| 727 | function markChanged(state) {
|
|---|
| 728 | if (!state.modified_) {
|
|---|
| 729 | state.modified_ = true;
|
|---|
| 730 |
|
|---|
| 731 | if (state.parent_) {
|
|---|
| 732 | markChanged(state.parent_);
|
|---|
| 733 | }
|
|---|
| 734 | }
|
|---|
| 735 | }
|
|---|
| 736 | function prepareCopy(state) {
|
|---|
| 737 | if (!state.copy_) {
|
|---|
| 738 | state.copy_ = shallowCopy(state.base_);
|
|---|
| 739 | }
|
|---|
| 740 | }
|
|---|
| 741 |
|
|---|
| 742 | var Immer =
|
|---|
| 743 | /*#__PURE__*/
|
|---|
| 744 | function () {
|
|---|
| 745 | function Immer(config) {
|
|---|
| 746 | var _this = this;
|
|---|
| 747 |
|
|---|
| 748 | this.useProxies_ = hasProxies;
|
|---|
| 749 | this.autoFreeze_ = true;
|
|---|
| 750 | /**
|
|---|
| 751 | * The `produce` function takes a value and a "recipe function" (whose
|
|---|
| 752 | * return value often depends on the base state). The recipe function is
|
|---|
| 753 | * free to mutate its first argument however it wants. All mutations are
|
|---|
| 754 | * only ever applied to a __copy__ of the base state.
|
|---|
| 755 | *
|
|---|
| 756 | * Pass only a function to create a "curried producer" which relieves you
|
|---|
| 757 | * from passing the recipe function every time.
|
|---|
| 758 | *
|
|---|
| 759 | * Only plain objects and arrays are made mutable. All other objects are
|
|---|
| 760 | * considered uncopyable.
|
|---|
| 761 | *
|
|---|
| 762 | * Note: This function is __bound__ to its `Immer` instance.
|
|---|
| 763 | *
|
|---|
| 764 | * @param {any} base - the initial state
|
|---|
| 765 | * @param {Function} recipe - function that receives a proxy of the base state as first argument and which can be freely modified
|
|---|
| 766 | * @param {Function} patchListener - optional function that will be called with all the patches produced here
|
|---|
| 767 | * @returns {any} a new state, or the initial state if nothing was modified
|
|---|
| 768 | */
|
|---|
| 769 |
|
|---|
| 770 | this.produce = function (base, recipe, patchListener) {
|
|---|
| 771 | // curried invocation
|
|---|
| 772 | if (typeof base === "function" && typeof recipe !== "function") {
|
|---|
| 773 | var defaultBase = recipe;
|
|---|
| 774 | recipe = base;
|
|---|
| 775 | var self = _this;
|
|---|
| 776 | return function curriedProduce(base) {
|
|---|
| 777 | var _this2 = this;
|
|---|
| 778 |
|
|---|
| 779 | if (base === void 0) {
|
|---|
| 780 | base = defaultBase;
|
|---|
| 781 | }
|
|---|
| 782 |
|
|---|
| 783 | for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|---|
| 784 | args[_key - 1] = arguments[_key];
|
|---|
| 785 | }
|
|---|
| 786 |
|
|---|
| 787 | return self.produce(base, function (draft) {
|
|---|
| 788 | var _recipe;
|
|---|
| 789 |
|
|---|
| 790 | return (_recipe = recipe).call.apply(_recipe, [_this2, draft].concat(args));
|
|---|
| 791 | }); // prettier-ignore
|
|---|
| 792 | };
|
|---|
| 793 | }
|
|---|
| 794 |
|
|---|
| 795 | if (typeof recipe !== "function") die(6);
|
|---|
| 796 | if (patchListener !== undefined && typeof patchListener !== "function") die(7);
|
|---|
| 797 | var result; // Only plain objects, arrays, and "immerable classes" are drafted.
|
|---|
| 798 |
|
|---|
| 799 | if (isDraftable(base)) {
|
|---|
| 800 | var scope = enterScope(_this);
|
|---|
| 801 | var proxy = createProxy(_this, base, undefined);
|
|---|
| 802 | var hasError = true;
|
|---|
| 803 |
|
|---|
| 804 | try {
|
|---|
| 805 | result = recipe(proxy);
|
|---|
| 806 | hasError = false;
|
|---|
| 807 | } finally {
|
|---|
| 808 | // finally instead of catch + rethrow better preserves original stack
|
|---|
| 809 | if (hasError) revokeScope(scope);else leaveScope(scope);
|
|---|
| 810 | }
|
|---|
| 811 |
|
|---|
| 812 | if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|---|
| 813 | return result.then(function (result) {
|
|---|
| 814 | usePatchesInScope(scope, patchListener);
|
|---|
| 815 | return processResult(result, scope);
|
|---|
| 816 | }, function (error) {
|
|---|
| 817 | revokeScope(scope);
|
|---|
| 818 | throw error;
|
|---|
| 819 | });
|
|---|
| 820 | }
|
|---|
| 821 |
|
|---|
| 822 | usePatchesInScope(scope, patchListener);
|
|---|
| 823 | return processResult(result, scope);
|
|---|
| 824 | } else if (!base || typeof base !== "object") {
|
|---|
| 825 | result = recipe(base);
|
|---|
| 826 | if (result === undefined) result = base;
|
|---|
| 827 | if (result === NOTHING) result = undefined;
|
|---|
| 828 | if (_this.autoFreeze_) freeze(result, true);
|
|---|
| 829 |
|
|---|
| 830 | if (patchListener) {
|
|---|
| 831 | var p = [];
|
|---|
| 832 | var ip = [];
|
|---|
| 833 | getPlugin("Patches").generateReplacementPatches_(base, result, p, ip);
|
|---|
| 834 | patchListener(p, ip);
|
|---|
| 835 | }
|
|---|
| 836 |
|
|---|
| 837 | return result;
|
|---|
| 838 | } else die(21, base);
|
|---|
| 839 | };
|
|---|
| 840 |
|
|---|
| 841 | this.produceWithPatches = function (base, recipe) {
|
|---|
| 842 | // curried invocation
|
|---|
| 843 | if (typeof base === "function") {
|
|---|
| 844 | return function (state) {
|
|---|
| 845 | for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|---|
| 846 | args[_key2 - 1] = arguments[_key2];
|
|---|
| 847 | }
|
|---|
| 848 |
|
|---|
| 849 | return _this.produceWithPatches(state, function (draft) {
|
|---|
| 850 | return base.apply(void 0, [draft].concat(args));
|
|---|
| 851 | });
|
|---|
| 852 | };
|
|---|
| 853 | }
|
|---|
| 854 |
|
|---|
| 855 | var patches, inversePatches;
|
|---|
| 856 |
|
|---|
| 857 | var result = _this.produce(base, recipe, function (p, ip) {
|
|---|
| 858 | patches = p;
|
|---|
| 859 | inversePatches = ip;
|
|---|
| 860 | });
|
|---|
| 861 |
|
|---|
| 862 | if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|---|
| 863 | return result.then(function (nextState) {
|
|---|
| 864 | return [nextState, patches, inversePatches];
|
|---|
| 865 | });
|
|---|
| 866 | }
|
|---|
| 867 |
|
|---|
| 868 | return [result, patches, inversePatches];
|
|---|
| 869 | };
|
|---|
| 870 |
|
|---|
| 871 | if (typeof (config === null || config === void 0 ? void 0 : config.useProxies) === "boolean") this.setUseProxies(config.useProxies);
|
|---|
| 872 | if (typeof (config === null || config === void 0 ? void 0 : config.autoFreeze) === "boolean") this.setAutoFreeze(config.autoFreeze);
|
|---|
| 873 | }
|
|---|
| 874 |
|
|---|
| 875 | var _proto = Immer.prototype;
|
|---|
| 876 |
|
|---|
| 877 | _proto.createDraft = function createDraft(base) {
|
|---|
| 878 | if (!isDraftable(base)) die(8);
|
|---|
| 879 | if (isDraft(base)) base = current(base);
|
|---|
| 880 | var scope = enterScope(this);
|
|---|
| 881 | var proxy = createProxy(this, base, undefined);
|
|---|
| 882 | proxy[DRAFT_STATE].isManual_ = true;
|
|---|
| 883 | leaveScope(scope);
|
|---|
| 884 | return proxy;
|
|---|
| 885 | };
|
|---|
| 886 |
|
|---|
| 887 | _proto.finishDraft = function finishDraft(draft, patchListener) {
|
|---|
| 888 | var state = draft && draft[DRAFT_STATE];
|
|---|
| 889 |
|
|---|
| 890 | {
|
|---|
| 891 | if (!state || !state.isManual_) die(9);
|
|---|
| 892 | if (state.finalized_) die(10);
|
|---|
| 893 | }
|
|---|
| 894 |
|
|---|
| 895 | var scope = state.scope_;
|
|---|
| 896 | usePatchesInScope(scope, patchListener);
|
|---|
| 897 | return processResult(undefined, scope);
|
|---|
| 898 | }
|
|---|
| 899 | /**
|
|---|
| 900 | * Pass true to automatically freeze all copies created by Immer.
|
|---|
| 901 | *
|
|---|
| 902 | * By default, auto-freezing is enabled.
|
|---|
| 903 | */
|
|---|
| 904 | ;
|
|---|
| 905 |
|
|---|
| 906 | _proto.setAutoFreeze = function setAutoFreeze(value) {
|
|---|
| 907 | this.autoFreeze_ = value;
|
|---|
| 908 | }
|
|---|
| 909 | /**
|
|---|
| 910 | * Pass true to use the ES2015 `Proxy` class when creating drafts, which is
|
|---|
| 911 | * always faster than using ES5 proxies.
|
|---|
| 912 | *
|
|---|
| 913 | * By default, feature detection is used, so calling this is rarely necessary.
|
|---|
| 914 | */
|
|---|
| 915 | ;
|
|---|
| 916 |
|
|---|
| 917 | _proto.setUseProxies = function setUseProxies(value) {
|
|---|
| 918 | if (value && !hasProxies) {
|
|---|
| 919 | die(20);
|
|---|
| 920 | }
|
|---|
| 921 |
|
|---|
| 922 | this.useProxies_ = value;
|
|---|
| 923 | };
|
|---|
| 924 |
|
|---|
| 925 | _proto.applyPatches = function applyPatches(base, patches) {
|
|---|
| 926 | // If a patch replaces the entire state, take that replacement as base
|
|---|
| 927 | // before applying patches
|
|---|
| 928 | var i;
|
|---|
| 929 |
|
|---|
| 930 | for (i = patches.length - 1; i >= 0; i--) {
|
|---|
| 931 | var patch = patches[i];
|
|---|
| 932 |
|
|---|
| 933 | if (patch.path.length === 0 && patch.op === "replace") {
|
|---|
| 934 | base = patch.value;
|
|---|
| 935 | break;
|
|---|
| 936 | }
|
|---|
| 937 | } // If there was a patch that replaced the entire state, start from the
|
|---|
| 938 | // patch after that.
|
|---|
| 939 |
|
|---|
| 940 |
|
|---|
| 941 | if (i > -1) {
|
|---|
| 942 | patches = patches.slice(i + 1);
|
|---|
| 943 | }
|
|---|
| 944 |
|
|---|
| 945 | var applyPatchesImpl = getPlugin("Patches").applyPatches_;
|
|---|
| 946 |
|
|---|
| 947 | if (isDraft(base)) {
|
|---|
| 948 | // N.B: never hits if some patch a replacement, patches are never drafts
|
|---|
| 949 | return applyPatchesImpl(base, patches);
|
|---|
| 950 | } // Otherwise, produce a copy of the base state.
|
|---|
| 951 |
|
|---|
| 952 |
|
|---|
| 953 | return this.produce(base, function (draft) {
|
|---|
| 954 | return applyPatchesImpl(draft, patches);
|
|---|
| 955 | });
|
|---|
| 956 | };
|
|---|
| 957 |
|
|---|
| 958 | return Immer;
|
|---|
| 959 | }();
|
|---|
| 960 | function createProxy(immer, value, parent) {
|
|---|
| 961 | // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft
|
|---|
| 962 | var draft = isMap(value) ? getPlugin("MapSet").proxyMap_(value, parent) : isSet(value) ? getPlugin("MapSet").proxySet_(value, parent) : immer.useProxies_ ? createProxyProxy(value, parent) : getPlugin("ES5").createES5Proxy_(value, parent);
|
|---|
| 963 | var scope = parent ? parent.scope_ : getCurrentScope();
|
|---|
| 964 | scope.drafts_.push(draft);
|
|---|
| 965 | return draft;
|
|---|
| 966 | }
|
|---|
| 967 |
|
|---|
| 968 | function current(value) {
|
|---|
| 969 | if (!isDraft(value)) die(22, value);
|
|---|
| 970 | return currentImpl(value);
|
|---|
| 971 | }
|
|---|
| 972 |
|
|---|
| 973 | function currentImpl(value) {
|
|---|
| 974 | if (!isDraftable(value)) return value;
|
|---|
| 975 | var state = value[DRAFT_STATE];
|
|---|
| 976 | var copy;
|
|---|
| 977 | var archType = getArchtype(value);
|
|---|
| 978 |
|
|---|
| 979 | if (state) {
|
|---|
| 980 | if (!state.modified_ && (state.type_ < 4 || !getPlugin("ES5").hasChanges_(state))) return state.base_; // Optimization: avoid generating new drafts during copying
|
|---|
| 981 |
|
|---|
| 982 | state.finalized_ = true;
|
|---|
| 983 | copy = copyHelper(value, archType);
|
|---|
| 984 | state.finalized_ = false;
|
|---|
| 985 | } else {
|
|---|
| 986 | copy = copyHelper(value, archType);
|
|---|
| 987 | }
|
|---|
| 988 |
|
|---|
| 989 | each(copy, function (key, childValue) {
|
|---|
| 990 | if (state && get(state.base_, key) === childValue) return; // no need to copy or search in something that didn't change
|
|---|
| 991 |
|
|---|
| 992 | set(copy, key, currentImpl(childValue));
|
|---|
| 993 | }); // In the future, we might consider freezing here, based on the current settings
|
|---|
| 994 |
|
|---|
| 995 | return archType === 3
|
|---|
| 996 | /* Set */
|
|---|
| 997 | ? new Set(copy) : copy;
|
|---|
| 998 | }
|
|---|
| 999 |
|
|---|
| 1000 | function copyHelper(value, archType) {
|
|---|
| 1001 | // creates a shallow copy, even if it is a map or set
|
|---|
| 1002 | switch (archType) {
|
|---|
| 1003 | case 2
|
|---|
| 1004 | /* Map */
|
|---|
| 1005 | :
|
|---|
| 1006 | return new Map(value);
|
|---|
| 1007 |
|
|---|
| 1008 | case 3
|
|---|
| 1009 | /* Set */
|
|---|
| 1010 | :
|
|---|
| 1011 | // Set will be cloned as array temporarily, so that we can replace individual items
|
|---|
| 1012 | return Array.from(value);
|
|---|
| 1013 | }
|
|---|
| 1014 |
|
|---|
| 1015 | return shallowCopy(value);
|
|---|
| 1016 | }
|
|---|
| 1017 |
|
|---|
| 1018 | function enableES5() {
|
|---|
| 1019 | function willFinalizeES5_(scope, result, isReplaced) {
|
|---|
| 1020 | if (!isReplaced) {
|
|---|
| 1021 | if (scope.patches_) {
|
|---|
| 1022 | markChangesRecursively(scope.drafts_[0]);
|
|---|
| 1023 | } // This is faster when we don't care about which attributes changed.
|
|---|
| 1024 |
|
|---|
| 1025 |
|
|---|
| 1026 | markChangesSweep(scope.drafts_);
|
|---|
| 1027 | } // When a child draft is returned, look for changes.
|
|---|
| 1028 | else if (isDraft(result) && result[DRAFT_STATE].scope_ === scope) {
|
|---|
| 1029 | markChangesSweep(scope.drafts_);
|
|---|
| 1030 | }
|
|---|
| 1031 | }
|
|---|
| 1032 |
|
|---|
| 1033 | function createES5Draft(isArray, base) {
|
|---|
| 1034 | if (isArray) {
|
|---|
| 1035 | var draft = new Array(base.length);
|
|---|
| 1036 |
|
|---|
| 1037 | for (var i = 0; i < base.length; i++) {
|
|---|
| 1038 | Object.defineProperty(draft, "" + i, proxyProperty(i, true));
|
|---|
| 1039 | }
|
|---|
| 1040 |
|
|---|
| 1041 | return draft;
|
|---|
| 1042 | } else {
|
|---|
| 1043 | var _descriptors = getOwnPropertyDescriptors(base);
|
|---|
| 1044 |
|
|---|
| 1045 | delete _descriptors[DRAFT_STATE];
|
|---|
| 1046 | var keys = ownKeys(_descriptors);
|
|---|
| 1047 |
|
|---|
| 1048 | for (var _i = 0; _i < keys.length; _i++) {
|
|---|
| 1049 | var key = keys[_i];
|
|---|
| 1050 | _descriptors[key] = proxyProperty(key, isArray || !!_descriptors[key].enumerable);
|
|---|
| 1051 | }
|
|---|
| 1052 |
|
|---|
| 1053 | return Object.create(Object.getPrototypeOf(base), _descriptors);
|
|---|
| 1054 | }
|
|---|
| 1055 | }
|
|---|
| 1056 |
|
|---|
| 1057 | function createES5Proxy_(base, parent) {
|
|---|
| 1058 | var isArray = Array.isArray(base);
|
|---|
| 1059 | var draft = createES5Draft(isArray, base);
|
|---|
| 1060 | var state = {
|
|---|
| 1061 | type_: isArray ? 5
|
|---|
| 1062 | /* ES5Array */
|
|---|
| 1063 | : 4
|
|---|
| 1064 | /* ES5Object */
|
|---|
| 1065 | ,
|
|---|
| 1066 | scope_: parent ? parent.scope_ : getCurrentScope(),
|
|---|
| 1067 | modified_: false,
|
|---|
| 1068 | finalized_: false,
|
|---|
| 1069 | assigned_: {},
|
|---|
| 1070 | parent_: parent,
|
|---|
| 1071 | // base is the object we are drafting
|
|---|
| 1072 | base_: base,
|
|---|
| 1073 | // draft is the draft object itself, that traps all reads and reads from either the base (if unmodified) or copy (if modified)
|
|---|
| 1074 | draft_: draft,
|
|---|
| 1075 | copy_: null,
|
|---|
| 1076 | revoked_: false,
|
|---|
| 1077 | isManual_: false
|
|---|
| 1078 | };
|
|---|
| 1079 | Object.defineProperty(draft, DRAFT_STATE, {
|
|---|
| 1080 | value: state,
|
|---|
| 1081 | // enumerable: false <- the default
|
|---|
| 1082 | writable: true
|
|---|
| 1083 | });
|
|---|
| 1084 | return draft;
|
|---|
| 1085 | } // property descriptors are recycled to make sure we don't create a get and set closure per property,
|
|---|
| 1086 | // but share them all instead
|
|---|
| 1087 |
|
|---|
| 1088 |
|
|---|
| 1089 | var descriptors = {};
|
|---|
| 1090 |
|
|---|
| 1091 | function proxyProperty(prop, enumerable) {
|
|---|
| 1092 | var desc = descriptors[prop];
|
|---|
| 1093 |
|
|---|
| 1094 | if (desc) {
|
|---|
| 1095 | desc.enumerable = enumerable;
|
|---|
| 1096 | } else {
|
|---|
| 1097 | descriptors[prop] = desc = {
|
|---|
| 1098 | configurable: true,
|
|---|
| 1099 | enumerable: enumerable,
|
|---|
| 1100 | get: function get() {
|
|---|
| 1101 | var state = this[DRAFT_STATE];
|
|---|
| 1102 | assertUnrevoked(state); // @ts-ignore
|
|---|
| 1103 |
|
|---|
| 1104 | return objectTraps.get(state, prop);
|
|---|
| 1105 | },
|
|---|
| 1106 | set: function set(value) {
|
|---|
| 1107 | var state = this[DRAFT_STATE];
|
|---|
| 1108 | assertUnrevoked(state); // @ts-ignore
|
|---|
| 1109 |
|
|---|
| 1110 | objectTraps.set(state, prop, value);
|
|---|
| 1111 | }
|
|---|
| 1112 | };
|
|---|
| 1113 | }
|
|---|
| 1114 |
|
|---|
| 1115 | return desc;
|
|---|
| 1116 | } // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.
|
|---|
| 1117 |
|
|---|
| 1118 |
|
|---|
| 1119 | function markChangesSweep(drafts) {
|
|---|
| 1120 | // The natural order of drafts in the `scope` array is based on when they
|
|---|
| 1121 | // were accessed. By processing drafts in reverse natural order, we have a
|
|---|
| 1122 | // better chance of processing leaf nodes first. When a leaf node is known to
|
|---|
| 1123 | // have changed, we can avoid any traversal of its ancestor nodes.
|
|---|
| 1124 | for (var i = drafts.length - 1; i >= 0; i--) {
|
|---|
| 1125 | var state = drafts[i][DRAFT_STATE];
|
|---|
| 1126 |
|
|---|
| 1127 | if (!state.modified_) {
|
|---|
| 1128 | switch (state.type_) {
|
|---|
| 1129 | case 5
|
|---|
| 1130 | /* ES5Array */
|
|---|
| 1131 | :
|
|---|
| 1132 | if (hasArrayChanges(state)) markChanged(state);
|
|---|
| 1133 | break;
|
|---|
| 1134 |
|
|---|
| 1135 | case 4
|
|---|
| 1136 | /* ES5Object */
|
|---|
| 1137 | :
|
|---|
| 1138 | if (hasObjectChanges(state)) markChanged(state);
|
|---|
| 1139 | break;
|
|---|
| 1140 | }
|
|---|
| 1141 | }
|
|---|
| 1142 | }
|
|---|
| 1143 | }
|
|---|
| 1144 |
|
|---|
| 1145 | function markChangesRecursively(object) {
|
|---|
| 1146 | if (!object || typeof object !== "object") return;
|
|---|
| 1147 | var state = object[DRAFT_STATE];
|
|---|
| 1148 | if (!state) return;
|
|---|
| 1149 | var base_ = state.base_,
|
|---|
| 1150 | draft_ = state.draft_,
|
|---|
| 1151 | assigned_ = state.assigned_,
|
|---|
| 1152 | type_ = state.type_;
|
|---|
| 1153 |
|
|---|
| 1154 | if (type_ === 4
|
|---|
| 1155 | /* ES5Object */
|
|---|
| 1156 | ) {
|
|---|
| 1157 | // Look for added keys.
|
|---|
| 1158 | // probably there is a faster way to detect changes, as sweep + recurse seems to do some
|
|---|
| 1159 | // unnecessary work.
|
|---|
| 1160 | // also: probably we can store the information we detect here, to speed up tree finalization!
|
|---|
| 1161 | each(draft_, function (key) {
|
|---|
| 1162 | if (key === DRAFT_STATE) return; // The `undefined` check is a fast path for pre-existing keys.
|
|---|
| 1163 |
|
|---|
| 1164 | if (base_[key] === undefined && !has(base_, key)) {
|
|---|
| 1165 | assigned_[key] = true;
|
|---|
| 1166 | markChanged(state);
|
|---|
| 1167 | } else if (!assigned_[key]) {
|
|---|
| 1168 | // Only untouched properties trigger recursion.
|
|---|
| 1169 | markChangesRecursively(draft_[key]);
|
|---|
| 1170 | }
|
|---|
| 1171 | }); // Look for removed keys.
|
|---|
| 1172 |
|
|---|
| 1173 | each(base_, function (key) {
|
|---|
| 1174 | // The `undefined` check is a fast path for pre-existing keys.
|
|---|
| 1175 | if (draft_[key] === undefined && !has(draft_, key)) {
|
|---|
| 1176 | assigned_[key] = false;
|
|---|
| 1177 | markChanged(state);
|
|---|
| 1178 | }
|
|---|
| 1179 | });
|
|---|
| 1180 | } else if (type_ === 5
|
|---|
| 1181 | /* ES5Array */
|
|---|
| 1182 | ) {
|
|---|
| 1183 | if (hasArrayChanges(state)) {
|
|---|
| 1184 | markChanged(state);
|
|---|
| 1185 | assigned_.length = true;
|
|---|
| 1186 | }
|
|---|
| 1187 |
|
|---|
| 1188 | if (draft_.length < base_.length) {
|
|---|
| 1189 | for (var i = draft_.length; i < base_.length; i++) {
|
|---|
| 1190 | assigned_[i] = false;
|
|---|
| 1191 | }
|
|---|
| 1192 | } else {
|
|---|
| 1193 | for (var _i2 = base_.length; _i2 < draft_.length; _i2++) {
|
|---|
| 1194 | assigned_[_i2] = true;
|
|---|
| 1195 | }
|
|---|
| 1196 | } // Minimum count is enough, the other parts has been processed.
|
|---|
| 1197 |
|
|---|
| 1198 |
|
|---|
| 1199 | var min = Math.min(draft_.length, base_.length);
|
|---|
| 1200 |
|
|---|
| 1201 | for (var _i3 = 0; _i3 < min; _i3++) {
|
|---|
| 1202 | // Only untouched indices trigger recursion.
|
|---|
| 1203 | if (!draft_.hasOwnProperty(_i3)) {
|
|---|
| 1204 | assigned_[_i3] = true;
|
|---|
| 1205 | }
|
|---|
| 1206 |
|
|---|
| 1207 | if (assigned_[_i3] === undefined) markChangesRecursively(draft_[_i3]);
|
|---|
| 1208 | }
|
|---|
| 1209 | }
|
|---|
| 1210 | }
|
|---|
| 1211 |
|
|---|
| 1212 | function hasObjectChanges(state) {
|
|---|
| 1213 | var base_ = state.base_,
|
|---|
| 1214 | draft_ = state.draft_; // Search for added keys and changed keys. Start at the back, because
|
|---|
| 1215 | // non-numeric keys are ordered by time of definition on the object.
|
|---|
| 1216 |
|
|---|
| 1217 | var keys = ownKeys(draft_);
|
|---|
| 1218 |
|
|---|
| 1219 | for (var i = keys.length - 1; i >= 0; i--) {
|
|---|
| 1220 | var key = keys[i];
|
|---|
| 1221 | if (key === DRAFT_STATE) continue;
|
|---|
| 1222 | var baseValue = base_[key]; // The `undefined` check is a fast path for pre-existing keys.
|
|---|
| 1223 |
|
|---|
| 1224 | if (baseValue === undefined && !has(base_, key)) {
|
|---|
| 1225 | return true;
|
|---|
| 1226 | } // Once a base key is deleted, future changes go undetected, because its
|
|---|
| 1227 | // descriptor is erased. This branch detects any missed changes.
|
|---|
| 1228 | else {
|
|---|
| 1229 | var value = draft_[key];
|
|---|
| 1230 |
|
|---|
| 1231 | var _state = value && value[DRAFT_STATE];
|
|---|
| 1232 |
|
|---|
| 1233 | if (_state ? _state.base_ !== baseValue : !is(value, baseValue)) {
|
|---|
| 1234 | return true;
|
|---|
| 1235 | }
|
|---|
| 1236 | }
|
|---|
| 1237 | } // At this point, no keys were added or changed.
|
|---|
| 1238 | // Compare key count to determine if keys were deleted.
|
|---|
| 1239 |
|
|---|
| 1240 |
|
|---|
| 1241 | var baseIsDraft = !!base_[DRAFT_STATE];
|
|---|
| 1242 | return keys.length !== ownKeys(base_).length + (baseIsDraft ? 0 : 1); // + 1 to correct for DRAFT_STATE
|
|---|
| 1243 | }
|
|---|
| 1244 |
|
|---|
| 1245 | function hasArrayChanges(state) {
|
|---|
| 1246 | var draft_ = state.draft_;
|
|---|
| 1247 | if (draft_.length !== state.base_.length) return true; // See #116
|
|---|
| 1248 | // If we first shorten the length, our array interceptors will be removed.
|
|---|
| 1249 | // If after that new items are added, result in the same original length,
|
|---|
| 1250 | // those last items will have no intercepting property.
|
|---|
| 1251 | // So if there is no own descriptor on the last position, we know that items were removed and added
|
|---|
| 1252 | // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check
|
|---|
| 1253 | // the last one
|
|---|
| 1254 | // last descriptor can be not a trap, if the array was extended
|
|---|
| 1255 |
|
|---|
| 1256 | var descriptor = Object.getOwnPropertyDescriptor(draft_, draft_.length - 1); // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)
|
|---|
| 1257 |
|
|---|
| 1258 | if (descriptor && !descriptor.get) return true; // if we miss a property, it has been deleted, so array probobaly changed
|
|---|
| 1259 |
|
|---|
| 1260 | for (var i = 0; i < draft_.length; i++) {
|
|---|
| 1261 | if (!draft_.hasOwnProperty(i)) return true;
|
|---|
| 1262 | } // For all other cases, we don't have to compare, as they would have been picked up by the index setters
|
|---|
| 1263 |
|
|---|
| 1264 |
|
|---|
| 1265 | return false;
|
|---|
| 1266 | }
|
|---|
| 1267 |
|
|---|
| 1268 | function hasChanges_(state) {
|
|---|
| 1269 | return state.type_ === 4
|
|---|
| 1270 | /* ES5Object */
|
|---|
| 1271 | ? hasObjectChanges(state) : hasArrayChanges(state);
|
|---|
| 1272 | }
|
|---|
| 1273 |
|
|---|
| 1274 | function assertUnrevoked(state
|
|---|
| 1275 | /*ES5State | MapState | SetState*/
|
|---|
| 1276 | ) {
|
|---|
| 1277 | if (state.revoked_) die(3, JSON.stringify(latest(state)));
|
|---|
| 1278 | }
|
|---|
| 1279 |
|
|---|
| 1280 | loadPlugin("ES5", {
|
|---|
| 1281 | createES5Proxy_: createES5Proxy_,
|
|---|
| 1282 | willFinalizeES5_: willFinalizeES5_,
|
|---|
| 1283 | hasChanges_: hasChanges_
|
|---|
| 1284 | });
|
|---|
| 1285 | }
|
|---|
| 1286 |
|
|---|
| 1287 | function enablePatches() {
|
|---|
| 1288 | var REPLACE = "replace";
|
|---|
| 1289 | var ADD = "add";
|
|---|
| 1290 | var REMOVE = "remove";
|
|---|
| 1291 |
|
|---|
| 1292 | function generatePatches_(state, basePath, patches, inversePatches) {
|
|---|
| 1293 | switch (state.type_) {
|
|---|
| 1294 | case 0
|
|---|
| 1295 | /* ProxyObject */
|
|---|
| 1296 | :
|
|---|
| 1297 | case 4
|
|---|
| 1298 | /* ES5Object */
|
|---|
| 1299 | :
|
|---|
| 1300 | case 2
|
|---|
| 1301 | /* Map */
|
|---|
| 1302 | :
|
|---|
| 1303 | return generatePatchesFromAssigned(state, basePath, patches, inversePatches);
|
|---|
| 1304 |
|
|---|
| 1305 | case 5
|
|---|
| 1306 | /* ES5Array */
|
|---|
| 1307 | :
|
|---|
| 1308 | case 1
|
|---|
| 1309 | /* ProxyArray */
|
|---|
| 1310 | :
|
|---|
| 1311 | return generateArrayPatches(state, basePath, patches, inversePatches);
|
|---|
| 1312 |
|
|---|
| 1313 | case 3
|
|---|
| 1314 | /* Set */
|
|---|
| 1315 | :
|
|---|
| 1316 | return generateSetPatches(state, basePath, patches, inversePatches);
|
|---|
| 1317 | }
|
|---|
| 1318 | }
|
|---|
| 1319 |
|
|---|
| 1320 | function generateArrayPatches(state, basePath, patches, inversePatches) {
|
|---|
| 1321 | var base_ = state.base_,
|
|---|
| 1322 | assigned_ = state.assigned_;
|
|---|
| 1323 | var copy_ = state.copy_; // Reduce complexity by ensuring `base` is never longer.
|
|---|
| 1324 |
|
|---|
| 1325 | if (copy_.length < base_.length) {
|
|---|
| 1326 | var _ref = [copy_, base_];
|
|---|
| 1327 | base_ = _ref[0];
|
|---|
| 1328 | copy_ = _ref[1];
|
|---|
| 1329 | var _ref2 = [inversePatches, patches];
|
|---|
| 1330 | patches = _ref2[0];
|
|---|
| 1331 | inversePatches = _ref2[1];
|
|---|
| 1332 | } // Process replaced indices.
|
|---|
| 1333 |
|
|---|
| 1334 |
|
|---|
| 1335 | for (var i = 0; i < base_.length; i++) {
|
|---|
| 1336 | if (assigned_[i] && copy_[i] !== base_[i]) {
|
|---|
| 1337 | var path = basePath.concat([i]);
|
|---|
| 1338 | patches.push({
|
|---|
| 1339 | op: REPLACE,
|
|---|
| 1340 | path: path,
|
|---|
| 1341 | // Need to maybe clone it, as it can in fact be the original value
|
|---|
| 1342 | // due to the base/copy inversion at the start of this function
|
|---|
| 1343 | value: clonePatchValueIfNeeded(copy_[i])
|
|---|
| 1344 | });
|
|---|
| 1345 | inversePatches.push({
|
|---|
| 1346 | op: REPLACE,
|
|---|
| 1347 | path: path,
|
|---|
| 1348 | value: clonePatchValueIfNeeded(base_[i])
|
|---|
| 1349 | });
|
|---|
| 1350 | }
|
|---|
| 1351 | } // Process added indices.
|
|---|
| 1352 |
|
|---|
| 1353 |
|
|---|
| 1354 | for (var _i = base_.length; _i < copy_.length; _i++) {
|
|---|
| 1355 | var _path = basePath.concat([_i]);
|
|---|
| 1356 |
|
|---|
| 1357 | patches.push({
|
|---|
| 1358 | op: ADD,
|
|---|
| 1359 | path: _path,
|
|---|
| 1360 | // Need to maybe clone it, as it can in fact be the original value
|
|---|
| 1361 | // due to the base/copy inversion at the start of this function
|
|---|
| 1362 | value: clonePatchValueIfNeeded(copy_[_i])
|
|---|
| 1363 | });
|
|---|
| 1364 | }
|
|---|
| 1365 |
|
|---|
| 1366 | if (base_.length < copy_.length) {
|
|---|
| 1367 | inversePatches.push({
|
|---|
| 1368 | op: REPLACE,
|
|---|
| 1369 | path: basePath.concat(["length"]),
|
|---|
| 1370 | value: base_.length
|
|---|
| 1371 | });
|
|---|
| 1372 | }
|
|---|
| 1373 | } // This is used for both Map objects and normal objects.
|
|---|
| 1374 |
|
|---|
| 1375 |
|
|---|
| 1376 | function generatePatchesFromAssigned(state, basePath, patches, inversePatches) {
|
|---|
| 1377 | var base_ = state.base_,
|
|---|
| 1378 | copy_ = state.copy_;
|
|---|
| 1379 | each(state.assigned_, function (key, assignedValue) {
|
|---|
| 1380 | var origValue = get(base_, key);
|
|---|
| 1381 | var value = get(copy_, key);
|
|---|
| 1382 | var op = !assignedValue ? REMOVE : has(base_, key) ? REPLACE : ADD;
|
|---|
| 1383 | if (origValue === value && op === REPLACE) return;
|
|---|
| 1384 | var path = basePath.concat(key);
|
|---|
| 1385 | patches.push(op === REMOVE ? {
|
|---|
| 1386 | op: op,
|
|---|
| 1387 | path: path
|
|---|
| 1388 | } : {
|
|---|
| 1389 | op: op,
|
|---|
| 1390 | path: path,
|
|---|
| 1391 | value: value
|
|---|
| 1392 | });
|
|---|
| 1393 | inversePatches.push(op === ADD ? {
|
|---|
| 1394 | op: REMOVE,
|
|---|
| 1395 | path: path
|
|---|
| 1396 | } : op === REMOVE ? {
|
|---|
| 1397 | op: ADD,
|
|---|
| 1398 | path: path,
|
|---|
| 1399 | value: clonePatchValueIfNeeded(origValue)
|
|---|
| 1400 | } : {
|
|---|
| 1401 | op: REPLACE,
|
|---|
| 1402 | path: path,
|
|---|
| 1403 | value: clonePatchValueIfNeeded(origValue)
|
|---|
| 1404 | });
|
|---|
| 1405 | });
|
|---|
| 1406 | }
|
|---|
| 1407 |
|
|---|
| 1408 | function generateSetPatches(state, basePath, patches, inversePatches) {
|
|---|
| 1409 | var base_ = state.base_,
|
|---|
| 1410 | copy_ = state.copy_;
|
|---|
| 1411 | var i = 0;
|
|---|
| 1412 | base_.forEach(function (value) {
|
|---|
| 1413 | if (!copy_.has(value)) {
|
|---|
| 1414 | var path = basePath.concat([i]);
|
|---|
| 1415 | patches.push({
|
|---|
| 1416 | op: REMOVE,
|
|---|
| 1417 | path: path,
|
|---|
| 1418 | value: value
|
|---|
| 1419 | });
|
|---|
| 1420 | inversePatches.unshift({
|
|---|
| 1421 | op: ADD,
|
|---|
| 1422 | path: path,
|
|---|
| 1423 | value: value
|
|---|
| 1424 | });
|
|---|
| 1425 | }
|
|---|
| 1426 |
|
|---|
| 1427 | i++;
|
|---|
| 1428 | });
|
|---|
| 1429 | i = 0;
|
|---|
| 1430 | copy_.forEach(function (value) {
|
|---|
| 1431 | if (!base_.has(value)) {
|
|---|
| 1432 | var path = basePath.concat([i]);
|
|---|
| 1433 | patches.push({
|
|---|
| 1434 | op: ADD,
|
|---|
| 1435 | path: path,
|
|---|
| 1436 | value: value
|
|---|
| 1437 | });
|
|---|
| 1438 | inversePatches.unshift({
|
|---|
| 1439 | op: REMOVE,
|
|---|
| 1440 | path: path,
|
|---|
| 1441 | value: value
|
|---|
| 1442 | });
|
|---|
| 1443 | }
|
|---|
| 1444 |
|
|---|
| 1445 | i++;
|
|---|
| 1446 | });
|
|---|
| 1447 | }
|
|---|
| 1448 |
|
|---|
| 1449 | function generateReplacementPatches_(baseValue, replacement, patches, inversePatches) {
|
|---|
| 1450 | patches.push({
|
|---|
| 1451 | op: REPLACE,
|
|---|
| 1452 | path: [],
|
|---|
| 1453 | value: replacement === NOTHING ? undefined : replacement
|
|---|
| 1454 | });
|
|---|
| 1455 | inversePatches.push({
|
|---|
| 1456 | op: REPLACE,
|
|---|
| 1457 | path: [],
|
|---|
| 1458 | value: baseValue
|
|---|
| 1459 | });
|
|---|
| 1460 | }
|
|---|
| 1461 |
|
|---|
| 1462 | function applyPatches_(draft, patches) {
|
|---|
| 1463 | patches.forEach(function (patch) {
|
|---|
| 1464 | var path = patch.path,
|
|---|
| 1465 | op = patch.op;
|
|---|
| 1466 | var base = draft;
|
|---|
| 1467 |
|
|---|
| 1468 | for (var i = 0; i < path.length - 1; i++) {
|
|---|
| 1469 | var parentType = getArchtype(base);
|
|---|
| 1470 | var p = path[i];
|
|---|
| 1471 |
|
|---|
| 1472 | if (typeof p !== "string" && typeof p !== "number") {
|
|---|
| 1473 | p = "" + p;
|
|---|
| 1474 | } // See #738, avoid prototype pollution
|
|---|
| 1475 |
|
|---|
| 1476 |
|
|---|
| 1477 | if ((parentType === 0
|
|---|
| 1478 | /* Object */
|
|---|
| 1479 | || parentType === 1
|
|---|
| 1480 | /* Array */
|
|---|
| 1481 | ) && (p === "__proto__" || p === "constructor")) die(24);
|
|---|
| 1482 | if (typeof base === "function" && p === "prototype") die(24);
|
|---|
| 1483 | base = get(base, p);
|
|---|
| 1484 | if (typeof base !== "object") die(15, path.join("/"));
|
|---|
| 1485 | }
|
|---|
| 1486 |
|
|---|
| 1487 | var type = getArchtype(base);
|
|---|
| 1488 | var value = deepClonePatchValue(patch.value); // used to clone patch to ensure original patch is not modified, see #411
|
|---|
| 1489 |
|
|---|
| 1490 | var key = path[path.length - 1];
|
|---|
| 1491 |
|
|---|
| 1492 | switch (op) {
|
|---|
| 1493 | case REPLACE:
|
|---|
| 1494 | switch (type) {
|
|---|
| 1495 | case 2
|
|---|
| 1496 | /* Map */
|
|---|
| 1497 | :
|
|---|
| 1498 | return base.set(key, value);
|
|---|
| 1499 |
|
|---|
| 1500 | /* istanbul ignore next */
|
|---|
| 1501 |
|
|---|
| 1502 | case 3
|
|---|
| 1503 | /* Set */
|
|---|
| 1504 | :
|
|---|
| 1505 | die(16);
|
|---|
| 1506 |
|
|---|
| 1507 | default:
|
|---|
| 1508 | // if value is an object, then it's assigned by reference
|
|---|
| 1509 | // in the following add or remove ops, the value field inside the patch will also be modifyed
|
|---|
| 1510 | // so we use value from the cloned patch
|
|---|
| 1511 | // @ts-ignore
|
|---|
| 1512 | return base[key] = value;
|
|---|
| 1513 | }
|
|---|
| 1514 |
|
|---|
| 1515 | case ADD:
|
|---|
| 1516 | switch (type) {
|
|---|
| 1517 | case 1
|
|---|
| 1518 | /* Array */
|
|---|
| 1519 | :
|
|---|
| 1520 | return key === "-" ? base.push(value) : base.splice(key, 0, value);
|
|---|
| 1521 |
|
|---|
| 1522 | case 2
|
|---|
| 1523 | /* Map */
|
|---|
| 1524 | :
|
|---|
| 1525 | return base.set(key, value);
|
|---|
| 1526 |
|
|---|
| 1527 | case 3
|
|---|
| 1528 | /* Set */
|
|---|
| 1529 | :
|
|---|
| 1530 | return base.add(value);
|
|---|
| 1531 |
|
|---|
| 1532 | default:
|
|---|
| 1533 | return base[key] = value;
|
|---|
| 1534 | }
|
|---|
| 1535 |
|
|---|
| 1536 | case REMOVE:
|
|---|
| 1537 | switch (type) {
|
|---|
| 1538 | case 1
|
|---|
| 1539 | /* Array */
|
|---|
| 1540 | :
|
|---|
| 1541 | return base.splice(key, 1);
|
|---|
| 1542 |
|
|---|
| 1543 | case 2
|
|---|
| 1544 | /* Map */
|
|---|
| 1545 | :
|
|---|
| 1546 | return base.delete(key);
|
|---|
| 1547 |
|
|---|
| 1548 | case 3
|
|---|
| 1549 | /* Set */
|
|---|
| 1550 | :
|
|---|
| 1551 | return base.delete(patch.value);
|
|---|
| 1552 |
|
|---|
| 1553 | default:
|
|---|
| 1554 | return delete base[key];
|
|---|
| 1555 | }
|
|---|
| 1556 |
|
|---|
| 1557 | default:
|
|---|
| 1558 | die(17, op);
|
|---|
| 1559 | }
|
|---|
| 1560 | });
|
|---|
| 1561 | return draft;
|
|---|
| 1562 | }
|
|---|
| 1563 |
|
|---|
| 1564 | function deepClonePatchValue(obj) {
|
|---|
| 1565 | if (!isDraftable(obj)) return obj;
|
|---|
| 1566 | if (Array.isArray(obj)) return obj.map(deepClonePatchValue);
|
|---|
| 1567 | if (isMap(obj)) return new Map(Array.from(obj.entries()).map(function (_ref3) {
|
|---|
| 1568 | var k = _ref3[0],
|
|---|
| 1569 | v = _ref3[1];
|
|---|
| 1570 | return [k, deepClonePatchValue(v)];
|
|---|
| 1571 | }));
|
|---|
| 1572 | if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue));
|
|---|
| 1573 | var cloned = Object.create(Object.getPrototypeOf(obj));
|
|---|
| 1574 |
|
|---|
| 1575 | for (var key in obj) {
|
|---|
| 1576 | cloned[key] = deepClonePatchValue(obj[key]);
|
|---|
| 1577 | }
|
|---|
| 1578 |
|
|---|
| 1579 | if (has(obj, DRAFTABLE)) cloned[DRAFTABLE] = obj[DRAFTABLE];
|
|---|
| 1580 | return cloned;
|
|---|
| 1581 | }
|
|---|
| 1582 |
|
|---|
| 1583 | function clonePatchValueIfNeeded(obj) {
|
|---|
| 1584 | if (isDraft(obj)) {
|
|---|
| 1585 | return deepClonePatchValue(obj);
|
|---|
| 1586 | } else return obj;
|
|---|
| 1587 | }
|
|---|
| 1588 |
|
|---|
| 1589 | loadPlugin("Patches", {
|
|---|
| 1590 | applyPatches_: applyPatches_,
|
|---|
| 1591 | generatePatches_: generatePatches_,
|
|---|
| 1592 | generateReplacementPatches_: generateReplacementPatches_
|
|---|
| 1593 | });
|
|---|
| 1594 | }
|
|---|
| 1595 |
|
|---|
| 1596 | // types only!
|
|---|
| 1597 | function enableMapSet() {
|
|---|
| 1598 | /* istanbul ignore next */
|
|---|
| 1599 | var _extendStatics = function extendStatics(d, b) {
|
|---|
| 1600 | _extendStatics = Object.setPrototypeOf || {
|
|---|
| 1601 | __proto__: []
|
|---|
| 1602 | } instanceof Array && function (d, b) {
|
|---|
| 1603 | d.__proto__ = b;
|
|---|
| 1604 | } || function (d, b) {
|
|---|
| 1605 | for (var p in b) {
|
|---|
| 1606 | if (b.hasOwnProperty(p)) d[p] = b[p];
|
|---|
| 1607 | }
|
|---|
| 1608 | };
|
|---|
| 1609 |
|
|---|
| 1610 | return _extendStatics(d, b);
|
|---|
| 1611 | }; // Ugly hack to resolve #502 and inherit built in Map / Set
|
|---|
| 1612 |
|
|---|
| 1613 |
|
|---|
| 1614 | function __extends(d, b) {
|
|---|
| 1615 | _extendStatics(d, b);
|
|---|
| 1616 |
|
|---|
| 1617 | function __() {
|
|---|
| 1618 | this.constructor = d;
|
|---|
| 1619 | }
|
|---|
| 1620 |
|
|---|
| 1621 | d.prototype = ( // @ts-ignore
|
|---|
| 1622 | __.prototype = b.prototype, new __());
|
|---|
| 1623 | }
|
|---|
| 1624 |
|
|---|
| 1625 | var DraftMap = function (_super) {
|
|---|
| 1626 | __extends(DraftMap, _super); // Create class manually, cause #502
|
|---|
| 1627 |
|
|---|
| 1628 |
|
|---|
| 1629 | function DraftMap(target, parent) {
|
|---|
| 1630 | this[DRAFT_STATE] = {
|
|---|
| 1631 | type_: 2
|
|---|
| 1632 | /* Map */
|
|---|
| 1633 | ,
|
|---|
| 1634 | parent_: parent,
|
|---|
| 1635 | scope_: parent ? parent.scope_ : getCurrentScope(),
|
|---|
| 1636 | modified_: false,
|
|---|
| 1637 | finalized_: false,
|
|---|
| 1638 | copy_: undefined,
|
|---|
| 1639 | assigned_: undefined,
|
|---|
| 1640 | base_: target,
|
|---|
| 1641 | draft_: this,
|
|---|
| 1642 | isManual_: false,
|
|---|
| 1643 | revoked_: false
|
|---|
| 1644 | };
|
|---|
| 1645 | return this;
|
|---|
| 1646 | }
|
|---|
| 1647 |
|
|---|
| 1648 | var p = DraftMap.prototype;
|
|---|
| 1649 | Object.defineProperty(p, "size", {
|
|---|
| 1650 | get: function get() {
|
|---|
| 1651 | return latest(this[DRAFT_STATE]).size;
|
|---|
| 1652 | } // enumerable: false,
|
|---|
| 1653 | // configurable: true
|
|---|
| 1654 |
|
|---|
| 1655 | });
|
|---|
| 1656 |
|
|---|
| 1657 | p.has = function (key) {
|
|---|
| 1658 | return latest(this[DRAFT_STATE]).has(key);
|
|---|
| 1659 | };
|
|---|
| 1660 |
|
|---|
| 1661 | p.set = function (key, value) {
|
|---|
| 1662 | var state = this[DRAFT_STATE];
|
|---|
| 1663 | assertUnrevoked(state);
|
|---|
| 1664 |
|
|---|
| 1665 | if (!latest(state).has(key) || latest(state).get(key) !== value) {
|
|---|
| 1666 | prepareMapCopy(state);
|
|---|
| 1667 | markChanged(state);
|
|---|
| 1668 | state.assigned_.set(key, true);
|
|---|
| 1669 | state.copy_.set(key, value);
|
|---|
| 1670 | state.assigned_.set(key, true);
|
|---|
| 1671 | }
|
|---|
| 1672 |
|
|---|
| 1673 | return this;
|
|---|
| 1674 | };
|
|---|
| 1675 |
|
|---|
| 1676 | p.delete = function (key) {
|
|---|
| 1677 | if (!this.has(key)) {
|
|---|
| 1678 | return false;
|
|---|
| 1679 | }
|
|---|
| 1680 |
|
|---|
| 1681 | var state = this[DRAFT_STATE];
|
|---|
| 1682 | assertUnrevoked(state);
|
|---|
| 1683 | prepareMapCopy(state);
|
|---|
| 1684 | markChanged(state);
|
|---|
| 1685 |
|
|---|
| 1686 | if (state.base_.has(key)) {
|
|---|
| 1687 | state.assigned_.set(key, false);
|
|---|
| 1688 | } else {
|
|---|
| 1689 | state.assigned_.delete(key);
|
|---|
| 1690 | }
|
|---|
| 1691 |
|
|---|
| 1692 | state.copy_.delete(key);
|
|---|
| 1693 | return true;
|
|---|
| 1694 | };
|
|---|
| 1695 |
|
|---|
| 1696 | p.clear = function () {
|
|---|
| 1697 | var state = this[DRAFT_STATE];
|
|---|
| 1698 | assertUnrevoked(state);
|
|---|
| 1699 |
|
|---|
| 1700 | if (latest(state).size) {
|
|---|
| 1701 | prepareMapCopy(state);
|
|---|
| 1702 | markChanged(state);
|
|---|
| 1703 | state.assigned_ = new Map();
|
|---|
| 1704 | each(state.base_, function (key) {
|
|---|
| 1705 | state.assigned_.set(key, false);
|
|---|
| 1706 | });
|
|---|
| 1707 | state.copy_.clear();
|
|---|
| 1708 | }
|
|---|
| 1709 | };
|
|---|
| 1710 |
|
|---|
| 1711 | p.forEach = function (cb, thisArg) {
|
|---|
| 1712 | var _this = this;
|
|---|
| 1713 |
|
|---|
| 1714 | var state = this[DRAFT_STATE];
|
|---|
| 1715 | latest(state).forEach(function (_value, key, _map) {
|
|---|
| 1716 | cb.call(thisArg, _this.get(key), key, _this);
|
|---|
| 1717 | });
|
|---|
| 1718 | };
|
|---|
| 1719 |
|
|---|
| 1720 | p.get = function (key) {
|
|---|
| 1721 | var state = this[DRAFT_STATE];
|
|---|
| 1722 | assertUnrevoked(state);
|
|---|
| 1723 | var value = latest(state).get(key);
|
|---|
| 1724 |
|
|---|
| 1725 | if (state.finalized_ || !isDraftable(value)) {
|
|---|
| 1726 | return value;
|
|---|
| 1727 | }
|
|---|
| 1728 |
|
|---|
| 1729 | if (value !== state.base_.get(key)) {
|
|---|
| 1730 | return value; // either already drafted or reassigned
|
|---|
| 1731 | } // despite what it looks, this creates a draft only once, see above condition
|
|---|
| 1732 |
|
|---|
| 1733 |
|
|---|
| 1734 | var draft = createProxy(state.scope_.immer_, value, state);
|
|---|
| 1735 | prepareMapCopy(state);
|
|---|
| 1736 | state.copy_.set(key, draft);
|
|---|
| 1737 | return draft;
|
|---|
| 1738 | };
|
|---|
| 1739 |
|
|---|
| 1740 | p.keys = function () {
|
|---|
| 1741 | return latest(this[DRAFT_STATE]).keys();
|
|---|
| 1742 | };
|
|---|
| 1743 |
|
|---|
| 1744 | p.values = function () {
|
|---|
| 1745 | var _this2 = this,
|
|---|
| 1746 | _ref;
|
|---|
| 1747 |
|
|---|
| 1748 | var iterator = this.keys();
|
|---|
| 1749 | return _ref = {}, _ref[iteratorSymbol] = function () {
|
|---|
| 1750 | return _this2.values();
|
|---|
| 1751 | }, _ref.next = function next() {
|
|---|
| 1752 | var r = iterator.next();
|
|---|
| 1753 | /* istanbul ignore next */
|
|---|
| 1754 |
|
|---|
| 1755 | if (r.done) return r;
|
|---|
| 1756 |
|
|---|
| 1757 | var value = _this2.get(r.value);
|
|---|
| 1758 |
|
|---|
| 1759 | return {
|
|---|
| 1760 | done: false,
|
|---|
| 1761 | value: value
|
|---|
| 1762 | };
|
|---|
| 1763 | }, _ref;
|
|---|
| 1764 | };
|
|---|
| 1765 |
|
|---|
| 1766 | p.entries = function () {
|
|---|
| 1767 | var _this3 = this,
|
|---|
| 1768 | _ref2;
|
|---|
| 1769 |
|
|---|
| 1770 | var iterator = this.keys();
|
|---|
| 1771 | return _ref2 = {}, _ref2[iteratorSymbol] = function () {
|
|---|
| 1772 | return _this3.entries();
|
|---|
| 1773 | }, _ref2.next = function next() {
|
|---|
| 1774 | var r = iterator.next();
|
|---|
| 1775 | /* istanbul ignore next */
|
|---|
| 1776 |
|
|---|
| 1777 | if (r.done) return r;
|
|---|
| 1778 |
|
|---|
| 1779 | var value = _this3.get(r.value);
|
|---|
| 1780 |
|
|---|
| 1781 | return {
|
|---|
| 1782 | done: false,
|
|---|
| 1783 | value: [r.value, value]
|
|---|
| 1784 | };
|
|---|
| 1785 | }, _ref2;
|
|---|
| 1786 | };
|
|---|
| 1787 |
|
|---|
| 1788 | p[iteratorSymbol] = function () {
|
|---|
| 1789 | return this.entries();
|
|---|
| 1790 | };
|
|---|
| 1791 |
|
|---|
| 1792 | return DraftMap;
|
|---|
| 1793 | }(Map);
|
|---|
| 1794 |
|
|---|
| 1795 | function proxyMap_(target, parent) {
|
|---|
| 1796 | // @ts-ignore
|
|---|
| 1797 | return new DraftMap(target, parent);
|
|---|
| 1798 | }
|
|---|
| 1799 |
|
|---|
| 1800 | function prepareMapCopy(state) {
|
|---|
| 1801 | if (!state.copy_) {
|
|---|
| 1802 | state.assigned_ = new Map();
|
|---|
| 1803 | state.copy_ = new Map(state.base_);
|
|---|
| 1804 | }
|
|---|
| 1805 | }
|
|---|
| 1806 |
|
|---|
| 1807 | var DraftSet = function (_super) {
|
|---|
| 1808 | __extends(DraftSet, _super); // Create class manually, cause #502
|
|---|
| 1809 |
|
|---|
| 1810 |
|
|---|
| 1811 | function DraftSet(target, parent) {
|
|---|
| 1812 | this[DRAFT_STATE] = {
|
|---|
| 1813 | type_: 3
|
|---|
| 1814 | /* Set */
|
|---|
| 1815 | ,
|
|---|
| 1816 | parent_: parent,
|
|---|
| 1817 | scope_: parent ? parent.scope_ : getCurrentScope(),
|
|---|
| 1818 | modified_: false,
|
|---|
| 1819 | finalized_: false,
|
|---|
| 1820 | copy_: undefined,
|
|---|
| 1821 | base_: target,
|
|---|
| 1822 | draft_: this,
|
|---|
| 1823 | drafts_: new Map(),
|
|---|
| 1824 | revoked_: false,
|
|---|
| 1825 | isManual_: false
|
|---|
| 1826 | };
|
|---|
| 1827 | return this;
|
|---|
| 1828 | }
|
|---|
| 1829 |
|
|---|
| 1830 | var p = DraftSet.prototype;
|
|---|
| 1831 | Object.defineProperty(p, "size", {
|
|---|
| 1832 | get: function get() {
|
|---|
| 1833 | return latest(this[DRAFT_STATE]).size;
|
|---|
| 1834 | } // enumerable: true,
|
|---|
| 1835 |
|
|---|
| 1836 | });
|
|---|
| 1837 |
|
|---|
| 1838 | p.has = function (value) {
|
|---|
| 1839 | var state = this[DRAFT_STATE];
|
|---|
| 1840 | assertUnrevoked(state); // bit of trickery here, to be able to recognize both the value, and the draft of its value
|
|---|
| 1841 |
|
|---|
| 1842 | if (!state.copy_) {
|
|---|
| 1843 | return state.base_.has(value);
|
|---|
| 1844 | }
|
|---|
| 1845 |
|
|---|
| 1846 | if (state.copy_.has(value)) return true;
|
|---|
| 1847 | if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value))) return true;
|
|---|
| 1848 | return false;
|
|---|
| 1849 | };
|
|---|
| 1850 |
|
|---|
| 1851 | p.add = function (value) {
|
|---|
| 1852 | var state = this[DRAFT_STATE];
|
|---|
| 1853 | assertUnrevoked(state);
|
|---|
| 1854 |
|
|---|
| 1855 | if (!this.has(value)) {
|
|---|
| 1856 | prepareSetCopy(state);
|
|---|
| 1857 | markChanged(state);
|
|---|
| 1858 | state.copy_.add(value);
|
|---|
| 1859 | }
|
|---|
| 1860 |
|
|---|
| 1861 | return this;
|
|---|
| 1862 | };
|
|---|
| 1863 |
|
|---|
| 1864 | p.delete = function (value) {
|
|---|
| 1865 | if (!this.has(value)) {
|
|---|
| 1866 | return false;
|
|---|
| 1867 | }
|
|---|
| 1868 |
|
|---|
| 1869 | var state = this[DRAFT_STATE];
|
|---|
| 1870 | assertUnrevoked(state);
|
|---|
| 1871 | prepareSetCopy(state);
|
|---|
| 1872 | markChanged(state);
|
|---|
| 1873 | return state.copy_.delete(value) || (state.drafts_.has(value) ? state.copy_.delete(state.drafts_.get(value)) :
|
|---|
| 1874 | /* istanbul ignore next */
|
|---|
| 1875 | false);
|
|---|
| 1876 | };
|
|---|
| 1877 |
|
|---|
| 1878 | p.clear = function () {
|
|---|
| 1879 | var state = this[DRAFT_STATE];
|
|---|
| 1880 | assertUnrevoked(state);
|
|---|
| 1881 |
|
|---|
| 1882 | if (latest(state).size) {
|
|---|
| 1883 | prepareSetCopy(state);
|
|---|
| 1884 | markChanged(state);
|
|---|
| 1885 | state.copy_.clear();
|
|---|
| 1886 | }
|
|---|
| 1887 | };
|
|---|
| 1888 |
|
|---|
| 1889 | p.values = function () {
|
|---|
| 1890 | var state = this[DRAFT_STATE];
|
|---|
| 1891 | assertUnrevoked(state);
|
|---|
| 1892 | prepareSetCopy(state);
|
|---|
| 1893 | return state.copy_.values();
|
|---|
| 1894 | };
|
|---|
| 1895 |
|
|---|
| 1896 | p.entries = function entries() {
|
|---|
| 1897 | var state = this[DRAFT_STATE];
|
|---|
| 1898 | assertUnrevoked(state);
|
|---|
| 1899 | prepareSetCopy(state);
|
|---|
| 1900 | return state.copy_.entries();
|
|---|
| 1901 | };
|
|---|
| 1902 |
|
|---|
| 1903 | p.keys = function () {
|
|---|
| 1904 | return this.values();
|
|---|
| 1905 | };
|
|---|
| 1906 |
|
|---|
| 1907 | p[iteratorSymbol] = function () {
|
|---|
| 1908 | return this.values();
|
|---|
| 1909 | };
|
|---|
| 1910 |
|
|---|
| 1911 | p.forEach = function forEach(cb, thisArg) {
|
|---|
| 1912 | var iterator = this.values();
|
|---|
| 1913 | var result = iterator.next();
|
|---|
| 1914 |
|
|---|
| 1915 | while (!result.done) {
|
|---|
| 1916 | cb.call(thisArg, result.value, result.value, this);
|
|---|
| 1917 | result = iterator.next();
|
|---|
| 1918 | }
|
|---|
| 1919 | };
|
|---|
| 1920 |
|
|---|
| 1921 | return DraftSet;
|
|---|
| 1922 | }(Set);
|
|---|
| 1923 |
|
|---|
| 1924 | function proxySet_(target, parent) {
|
|---|
| 1925 | // @ts-ignore
|
|---|
| 1926 | return new DraftSet(target, parent);
|
|---|
| 1927 | }
|
|---|
| 1928 |
|
|---|
| 1929 | function prepareSetCopy(state) {
|
|---|
| 1930 | if (!state.copy_) {
|
|---|
| 1931 | // create drafts for all entries to preserve insertion order
|
|---|
| 1932 | state.copy_ = new Set();
|
|---|
| 1933 | state.base_.forEach(function (value) {
|
|---|
| 1934 | if (isDraftable(value)) {
|
|---|
| 1935 | var draft = createProxy(state.scope_.immer_, value, state);
|
|---|
| 1936 | state.drafts_.set(value, draft);
|
|---|
| 1937 | state.copy_.add(draft);
|
|---|
| 1938 | } else {
|
|---|
| 1939 | state.copy_.add(value);
|
|---|
| 1940 | }
|
|---|
| 1941 | });
|
|---|
| 1942 | }
|
|---|
| 1943 | }
|
|---|
| 1944 |
|
|---|
| 1945 | function assertUnrevoked(state
|
|---|
| 1946 | /*ES5State | MapState | SetState*/
|
|---|
| 1947 | ) {
|
|---|
| 1948 | if (state.revoked_) die(3, JSON.stringify(latest(state)));
|
|---|
| 1949 | }
|
|---|
| 1950 |
|
|---|
| 1951 | loadPlugin("MapSet", {
|
|---|
| 1952 | proxyMap_: proxyMap_,
|
|---|
| 1953 | proxySet_: proxySet_
|
|---|
| 1954 | });
|
|---|
| 1955 | }
|
|---|
| 1956 |
|
|---|
| 1957 | function enableAllPlugins() {
|
|---|
| 1958 | enableES5();
|
|---|
| 1959 | enableMapSet();
|
|---|
| 1960 | enablePatches();
|
|---|
| 1961 | }
|
|---|
| 1962 |
|
|---|
| 1963 | var immer =
|
|---|
| 1964 | /*#__PURE__*/
|
|---|
| 1965 | new Immer();
|
|---|
| 1966 | /**
|
|---|
| 1967 | * The `produce` function takes a value and a "recipe function" (whose
|
|---|
| 1968 | * return value often depends on the base state). The recipe function is
|
|---|
| 1969 | * free to mutate its first argument however it wants. All mutations are
|
|---|
| 1970 | * only ever applied to a __copy__ of the base state.
|
|---|
| 1971 | *
|
|---|
| 1972 | * Pass only a function to create a "curried producer" which relieves you
|
|---|
| 1973 | * from passing the recipe function every time.
|
|---|
| 1974 | *
|
|---|
| 1975 | * Only plain objects and arrays are made mutable. All other objects are
|
|---|
| 1976 | * considered uncopyable.
|
|---|
| 1977 | *
|
|---|
| 1978 | * Note: This function is __bound__ to its `Immer` instance.
|
|---|
| 1979 | *
|
|---|
| 1980 | * @param {any} base - the initial state
|
|---|
| 1981 | * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
|
|---|
| 1982 | * @param {Function} patchListener - optional function that will be called with all the patches produced here
|
|---|
| 1983 | * @returns {any} a new state, or the initial state if nothing was modified
|
|---|
| 1984 | */
|
|---|
| 1985 |
|
|---|
| 1986 | var produce = immer.produce;
|
|---|
| 1987 | /**
|
|---|
| 1988 | * Like `produce`, but `produceWithPatches` always returns a tuple
|
|---|
| 1989 | * [nextState, patches, inversePatches] (instead of just the next state)
|
|---|
| 1990 | */
|
|---|
| 1991 |
|
|---|
| 1992 | var produceWithPatches =
|
|---|
| 1993 | /*#__PURE__*/
|
|---|
| 1994 | immer.produceWithPatches.bind(immer);
|
|---|
| 1995 | /**
|
|---|
| 1996 | * Pass true to automatically freeze all copies created by Immer.
|
|---|
| 1997 | *
|
|---|
| 1998 | * Always freeze by default, even in production mode
|
|---|
| 1999 | */
|
|---|
| 2000 |
|
|---|
| 2001 | var setAutoFreeze =
|
|---|
| 2002 | /*#__PURE__*/
|
|---|
| 2003 | immer.setAutoFreeze.bind(immer);
|
|---|
| 2004 | /**
|
|---|
| 2005 | * Pass true to use the ES2015 `Proxy` class when creating drafts, which is
|
|---|
| 2006 | * always faster than using ES5 proxies.
|
|---|
| 2007 | *
|
|---|
| 2008 | * By default, feature detection is used, so calling this is rarely necessary.
|
|---|
| 2009 | */
|
|---|
| 2010 |
|
|---|
| 2011 | var setUseProxies =
|
|---|
| 2012 | /*#__PURE__*/
|
|---|
| 2013 | immer.setUseProxies.bind(immer);
|
|---|
| 2014 | /**
|
|---|
| 2015 | * Apply an array of Immer patches to the first argument.
|
|---|
| 2016 | *
|
|---|
| 2017 | * This function is a producer, which means copy-on-write is in effect.
|
|---|
| 2018 | */
|
|---|
| 2019 |
|
|---|
| 2020 | var applyPatches =
|
|---|
| 2021 | /*#__PURE__*/
|
|---|
| 2022 | immer.applyPatches.bind(immer);
|
|---|
| 2023 | /**
|
|---|
| 2024 | * Create an Immer draft from the given base state, which may be a draft itself.
|
|---|
| 2025 | * The draft can be modified until you finalize it with the `finishDraft` function.
|
|---|
| 2026 | */
|
|---|
| 2027 |
|
|---|
| 2028 | var createDraft =
|
|---|
| 2029 | /*#__PURE__*/
|
|---|
| 2030 | immer.createDraft.bind(immer);
|
|---|
| 2031 | /**
|
|---|
| 2032 | * Finalize an Immer draft from a `createDraft` call, returning the base state
|
|---|
| 2033 | * (if no changes were made) or a modified copy. The draft must *not* be
|
|---|
| 2034 | * mutated afterwards.
|
|---|
| 2035 | *
|
|---|
| 2036 | * Pass a function as the 2nd argument to generate Immer patches based on the
|
|---|
| 2037 | * changes that were made.
|
|---|
| 2038 | */
|
|---|
| 2039 |
|
|---|
| 2040 | var finishDraft =
|
|---|
| 2041 | /*#__PURE__*/
|
|---|
| 2042 | immer.finishDraft.bind(immer);
|
|---|
| 2043 | /**
|
|---|
| 2044 | * This function is actually a no-op, but can be used to cast an immutable type
|
|---|
| 2045 | * to an draft type and make TypeScript happy
|
|---|
| 2046 | *
|
|---|
| 2047 | * @param value
|
|---|
| 2048 | */
|
|---|
| 2049 |
|
|---|
| 2050 | function castDraft(value) {
|
|---|
| 2051 | return value;
|
|---|
| 2052 | }
|
|---|
| 2053 | /**
|
|---|
| 2054 | * This function is actually a no-op, but can be used to cast a mutable type
|
|---|
| 2055 | * to an immutable type and make TypeScript happy
|
|---|
| 2056 | * @param value
|
|---|
| 2057 | */
|
|---|
| 2058 |
|
|---|
| 2059 | function castImmutable(value) {
|
|---|
| 2060 | return value;
|
|---|
| 2061 | }
|
|---|
| 2062 |
|
|---|
| 2063 | exports.Immer = Immer;
|
|---|
| 2064 | exports.applyPatches = applyPatches;
|
|---|
| 2065 | exports.castDraft = castDraft;
|
|---|
| 2066 | exports.castImmutable = castImmutable;
|
|---|
| 2067 | exports.createDraft = createDraft;
|
|---|
| 2068 | exports.current = current;
|
|---|
| 2069 | exports.default = produce;
|
|---|
| 2070 | exports.enableAllPlugins = enableAllPlugins;
|
|---|
| 2071 | exports.enableES5 = enableES5;
|
|---|
| 2072 | exports.enableMapSet = enableMapSet;
|
|---|
| 2073 | exports.enablePatches = enablePatches;
|
|---|
| 2074 | exports.finishDraft = finishDraft;
|
|---|
| 2075 | exports.freeze = freeze;
|
|---|
| 2076 | exports.immerable = DRAFTABLE;
|
|---|
| 2077 | exports.isDraft = isDraft;
|
|---|
| 2078 | exports.isDraftable = isDraftable;
|
|---|
| 2079 | exports.nothing = NOTHING;
|
|---|
| 2080 | exports.original = original;
|
|---|
| 2081 | exports.produce = produce;
|
|---|
| 2082 | exports.produceWithPatches = produceWithPatches;
|
|---|
| 2083 | exports.setAutoFreeze = setAutoFreeze;
|
|---|
| 2084 | exports.setUseProxies = setUseProxies;
|
|---|
| 2085 | //# sourceMappingURL=immer.cjs.development.js.map
|
|---|