Made ES5 version work
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 5036e7f · Browse files · 6 files·+23 added−36 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+23 added−36 removed
- tests:+0 added−25 removed
- other:+23 added−11 removed
Provenance
- Commit
870f44e- Tree
617ec0d- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
5036e7f
6 files changed+23 added−36 removed
deleted:__tests__/__snapshots__/current.js.snap-25 removed
1-// Jest Snapshot v1, https://goo.gl/fbAQLP2-3-exports[`auto freeze - es5 will freeze maps 1`] = `"[Immer] This object has been frozen and should not be mutated"`;4-5-exports[`auto freeze - es5 will freeze maps 2`] = `"[Immer] This object has been frozen and should not be mutated"`;6-7-exports[`auto freeze - es5 will freeze maps 3`] = `"[Immer] This object has been frozen and should not be mutated"`;8-9-exports[`auto freeze - es5 will freeze sets 1`] = `"[Immer] This object has been frozen and should not be mutated"`;10-11-exports[`auto freeze - es5 will freeze sets 2`] = `"[Immer] This object has been frozen and should not be mutated"`;12-13-exports[`auto freeze - es5 will freeze sets 3`] = `"[Immer] This object has been frozen and should not be mutated"`;14-15-exports[`auto freeze - proxy will freeze maps 1`] = `"[Immer] This object has been frozen and should not be mutated"`;16-17-exports[`auto freeze - proxy will freeze maps 2`] = `"[Immer] This object has been frozen and should not be mutated"`;18-19-exports[`auto freeze - proxy will freeze maps 3`] = `"[Immer] This object has been frozen and should not be mutated"`;20-21-exports[`auto freeze - proxy will freeze sets 1`] = `"[Immer] This object has been frozen and should not be mutated"`;22-23-exports[`auto freeze - proxy will freeze sets 2`] = `"[Immer] This object has been frozen and should not be mutated"`;24-25-exports[`auto freeze - proxy will freeze sets 3`] = `"[Immer] This object has been frozen and should not be mutated"`;modified:src/core/current.ts+6 added-1 removed
10 unmodified lines1112131415161718195 unmodified lines252627282829303132333435363722 unmodified lines10 unmodified lines ArchtypeMap, ArchtypeSet, getArchtype} from "../internal"import {getPlugin} from "../utils/plugins"
export function current<T>(value: T): Texport function current(value: any): any { if (!isDraft(value)) die(22, value)5 unmodified lines const state: ImmerState | undefined = value[DRAFT_STATE] let copy: any const archType = getArchtype(value) if (state) { if (!state.modified_) return state.base_ if ( !state.modified_ && (state.type_ < 4 || !getPlugin("ES5").hasChanges_(state as any)) ) return state.base_ // Optimization: avoid generating new drafts during copying state.finalized_ = true copy = copyHelper(value, archType) state.finalized_ = false22 unmodified linesmodified:src/core/finalize.ts+1 added-1 removed
131 unmodified lines132 rootScope.canAutoFreeze_ = false133 } else return134 }135 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.136- if (isDraftable(childValue)) {136+ if (isDraftable(childValue) && !Object.isFrozen(childValue)) {137 if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {138 // optimization: if an object is not a draft, and we don't have to139 // deepfreeze everything, and we are sure that no drafts are left in the remaining object140 // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.16 unmodified linesmodified:src/plugins/es5.ts+8 added-1 removed
248 unmodified lines249 // For all other cases, we don't have to compare, as they would have been picked up by the index setters250 return false251 }252 253+ function hasChanges_(state: ES5State) {254+ return state.type_ === ProxyTypeES5Object255+ ? hasObjectChanges(state)256+ : hasArrayChanges(state)257+ }258+259 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {260 if (state.revoked_) die(3, JSON.stringify(latest(state)))261 }262 263 loadPlugin("ES5", {264 createES5Proxy_,259- willFinalizeES5_265+ willFinalizeES5_,266+ hasChanges_267 })268 }modified:src/utils/common.ts+7 added-8 removed
25 unmodified lines26 /*#__PURE__*/27 export function isDraftable(value: any): boolean {28 if (!value) return false29 return (30- (isPlainObject(value) ||31- Array.isArray(value) ||32- !!value[DRAFTABLE] ||33- !!value.constructor[DRAFTABLE] ||34- isMap(value) ||35- isSet(value)) &&36- !isFrozen(value)30+ isPlainObject(value) ||31+ Array.isArray(value) ||32+ !!value[DRAFTABLE] ||33+ !!value.constructor[DRAFTABLE] ||34+ isMap(value) ||35+ isSet(value)36 )37 }38 39 /*#__PURE__*/130 unmodified lines170 return Object.create(Object.getPrototypeOf(base), descriptors)171 }172 173 export function freeze(obj: any, deep: boolean): void {175- if (isDraft(obj) || !isDraftable(obj)) return174+ if (Object.isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return175 if (getArchtype(obj) > 1 /* Map or Set */) {176 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any177 }178 Object.freeze(obj)12 unmodified linesmodified:src/utils/plugins.ts+1 added
35 unmodified lines36373839404142434468 unmodified lines35 unmodified lines createES5Proxy_<T>( base: T, parent?: ImmerState ): Drafted<T, ES5ObjectState | ES5ArrayState> hasChanges_(state: ES5ArrayState | ES5ObjectState): boolean } MapSet?: { proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T68 unmodified lines