🎵...killing me softly, with your fingers... 🎶
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 c182c1e · Browse files · 7 files·+35 added−147 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+35 added−147 removed
- other:+35 added−147 removed
Provenance
- Commit
19a6b66- Tree
681cccc- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
c182c1e
7 files changed+35 added−147 removed
modified:src/common.ts+4 added-26 removed
63 unmodified lines64 }65 // otherwise return undefined66 }67 68-// We use Maps as `drafts` for Sets, not Objects69-// See proxy.js70-export function assignSet(target: Map<any, any>, override) {71- override.forEach(value => {72- // When we add new drafts we have to remove their originals if present73- const prev = original(value)74- if (prev) target.delete(prev)75- // @ts-ignore TODO investigate76- target.add(value)77- })78- return target79-}80-81-// We use Maps as `drafts` for Maps, not Objects82-// See proxy.js83-export function assignMap(target: Map<any, any>, override: Map<any, any>) {84- override.forEach((value, key) => target.set(key, value))85- return target86-}87-68 export const assign =69 Object.assign ||70 ((target, ...overrides) => {71 overrides.forEach(override => {7 unmodified lines79 typeof Reflect !== "undefined" && Reflect.ownKeys80 ? Reflect.ownKeys81 : typeof Object.getOwnPropertySymbols !== "undefined"82 ? obj =>103- Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(104- obj105- ) as any)83+ Object.getOwnPropertyNames(obj).concat(84+ Object.getOwnPropertySymbols(obj) as any85+ )86 : Object.getOwnPropertyNames87 88 export function each<T extends Objectish>(89 obj: T,4 unmodified lines94 obj.forEach((entry, index) => iter(index, entry, obj))95 } else if (obj && typeof obj === "object") {96 ownKeys(obj).forEach(key => iter(key, obj[key], obj))97 } else {118- throw new Error("Cannot iterate primitive " + obj)98+ throw new Error("Nope")99 }100 }101 102 export function isEnumerable(base: {}, prop: PropertyKey): boolean {45 unmodified lines148 invokeGetters?: boolean149 ): T150 export function shallowCopy(base, invokeGetters = false) {151 if (Array.isArray(base)) return base.slice()172- if (isMap(base)) return new Map(base)173- if (isSet(base)) return new Set(base)152 const clone = Object.create(Object.getPrototypeOf(base))153 ownKeys(base).forEach(key => {154 if (key === DRAFT_STATE) {155 return // Never copy over draft state.39 unmodified linesmodified:src/es5.ts+7 added-36 removed
11 unmodified lines12 shallowCopy,13 DRAFT_STATE,14 latest15 } from "./common"16-import {proxyMap, hasMapChanges} from "./map"17-import {proxySet, hasSetChanges} from "./set"16 17 import {ImmerScope} from "./scope"18 import {ImmerState, Drafted, AnyObject, AnyMap} from "./types"19+import {markChanged} from "./proxy"20 21 // TODO: merge with ImmerState?22 interface ES5BaseState {23 scope: ImmerScope180 unmodified lines204 if (!state.modified) {205 if (Array.isArray(state.base)) {206 if (hasArrayChanges(state)) markChangedES5(state)207 } else if (isMap(state.base)) {209- if (hasMapChanges(state)) markChangedES5(state)208+ // TODO: use switch209+ continue210 } else if (isSet(state.base)) {211- if (hasSetChanges(state)) markChangedES5(state)211+ continue212 } else if (hasObjectChanges(state)) {213 markChangedES5(state)214 }215 }6 unmodified lines222 if (!object || typeof object !== "object") return223 const state = object[DRAFT_STATE]224 if (!state) return225 const {base, draft, assigned} = state226- if (isSet(object)) {227- if (hasSetChanges(state)) {228- markChangedES5(state)229- object.forEach(v => {230- markChangesRecursively(v)231- })232- }233- } else if (isMap(object)) {234- // if (hasMapChanges(object)) {235- object.forEach((value, key) => {236- if (assigned && base.get(key) === undefined && !has(base, key)) {237- // TODO: this code seems invalid for Maps!238- assigned.set(key, true)239- markChangedES5(state)240- } else if (!assigned || !assigned.get(key)) {241- // TODO: === false?242- // Only untouched properties trigger recursion.243- markChangesRecursively(draft.get(key))244- }245- })246- // Look for removed keys.247- // TODO: is this code needed?248- // TODO: use each?249- if (assigned)250- base.forEach((value, key) => {251- // The `undefined` check is a fast path for pre-existing keys.252- if (draft.get(key) === undefined && !has(draft, key)) {253- assigned.set(key, false)254- markChangedES5(state)255- }256- })257- // }226+ if (isSet(object) || isMap(object)) {227+ // TODO: create switch here228+ return229 } else if (!Array.isArray(object)) {230 // Look for added keys.231 // TODO: use each?232 Object.keys(draft).forEach(key => {89 unmodified linesmodified:src/immer.ts+1 added-1 removed
420 unmodified lines421 scope.canAutoFreeze = false422 }423 424 // Unchanged drafts are never passed to the `onAssign` hook.425- if (isDraftProp && !isSet && value === get(state.base, prop)) return425+ // if (isDraftProp && !isSet && value === get(state.base, prop)) return426 }427 // Unchanged draft properties are ignored.428 else if (isDraftProp && is(value, get(state.base, prop))) {429 return40 unmodified linesmodified:src/map.ts-19 removed
165 unmodified linesExpand all166167168169170171172170171175176177178179180181182183184185186187188189190165 unmodified linesExpand all }}
export function proxyMap(target, parent) { if (target instanceof DraftMap) { return target // TODO: or copy? } return new DraftMap(target, parent)}
export function hasMapChanges(state) { const {base, draft} = state
if (base.size !== draft.size) return true
// IE11 supports only forEach iteration let hasChanges = false // TODO: optimize: break on first difference draft.forEach(function(value, key) { if (!hasChanges) { hasChanges = isDraftable(value) ? value.modified : value !== base.get(key) } }) return hasChanges}modified:src/patches.ts+23 added-22 removed
86 unmodified lines87 patches: Patch[],88 inversePatches: Patch[]89 ) {90 const {base, copy} = state91- if (state.assigned)92- each(state.assigned, (key, assignedValue) => {93- const origValue = get(base, key)94- const value = get(copy, key)95- const op = !assignedValue ? "remove" : has(base, key) ? "replace" : "add"96- if (origValue === value && op === "replace") return97- const path = basePath.concat(key as any)98- patches.push(op === "remove" ? {op, path} : {op, path, value})99- inversePatches.push(100- op === "add"101- ? {op: "remove", path}102- : op === "remove"103- ? {op: "add", path, value: origValue}104- : {op: "replace", path, value: origValue}105- )106- })91+ each(state.assigned, (key, assignedValue) => {92+ const origValue = get(base, key)93+ const value = get(copy, key)94+ const op = !assignedValue ? "remove" : has(base, key) ? "replace" : "add"95+ if (origValue === value && op === "replace") return96+ const path = basePath.concat(key as any)97+ patches.push(op === "remove" ? {op, path} : {op, path, value})98+ inversePatches.push(99+ op === "add"100+ ? {op: "remove", path}101+ : op === "remove"102+ ? {op: "add", path, value: origValue}103+ : {op: "replace", path, value: origValue}104+ )105+ })106 }107 108 function generateSetPatches(109 state: SetState,51 unmodified lines161 if (!base || typeof base !== "object")162 throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignore163 }164 166- const value = deepClone(patch.value) // used to clone patch to ensure original patch is not modified, see #411165+ const value = deepClonePatchValue(patch.value) // used to clone patch to ensure original patch is not modified, see #411166 167 const key = path[path.length - 1]168 switch (op) {169 case "replace":40 unmodified lines210 211 // TODO: optimize: this is quite a performance hit, can we detect intelligently when it is needed?212 // E.g. auto-draft when new objects from outside are assigned and modified?213 // (See failing test when deepClone just returns obj)215-function deepClone(obj) {214+function deepClonePatchValue(obj) {215 if (!obj || typeof obj !== "object") return obj217- if (Array.isArray(obj)) return obj.map(deepClone)216+ if (Array.isArray(obj)) return obj.map(deepClonePatchValue)217 if (isMap(obj))219- return new Map(Array.from(obj.entries()).map(([k, v]) => [k, deepClone(v)]))220- if (isSet(obj)) return new Set(Array.from(obj.values()).map(deepClone))218+ return new Map(219+ Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])220+ )221+ // Not needed: if (isSet(obj)) return new Set(Array.from(obj.values()).map(deepClone))222 const cloned = Object.create(Object.getPrototypeOf(obj))222- for (const key in obj) cloned[key] = deepClone(obj[key])223+ for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])224 return cloned225 }modified:src/proxy.ts-24 removed
211 unmodified lines212 }213 return objectTraps.set!.call(this, state[0], prop, value, state[0])214 }215 216-// Used by Map and Set drafts217-const reflectTraps = makeReflectTraps([218- "ownKeys",219- "has",220- "set",221- "deleteProperty",222- "defineProperty",223- "getOwnPropertyDescriptor",224- "preventExtensions",225- "isExtensible",226- "getPrototypeOf"227-])228-216 /**217 * Map drafts218 */219 30 unmodified lines250 if (!state.copy) {251 state.copy = shallowCopy(state.base)252 }253 }267-268-/** Create traps that all use the `Reflect` API on the `latest(state)` */269-function makeReflectTraps<T extends object>(270- names: (keyof typeof Reflect)[]271-): ProxyHandler<T> {272- return names.reduce((traps, name) => {273- // @ts-ignore274- traps[name] = (state, ...args) => Reflect[name](latest(state), ...args)275- return traps276- }, {} as any)277-}modified:src/set.ts-19 removed
142 unmodified lines143 }144 }145 146 export function proxySet(target, parent) {147- if (target instanceof DraftSet) {148- return target // TODO: or copy?149- }147 return new DraftSet(target, parent)148 }152-153-export function hasSetChanges(state) {154- const {base, draft} = state155-156- if (base.size !== draft.size) return true157-158- // IE11 supports only forEach iteration159- let hasChanges = false160- // TODO: optimize: break on first diff161- draft.forEach(function(value, key) {162- if (!hasChanges) {163- hasChanges = isDraftable(value) ? value.modified : !base.has(key)164- }165- })166- return hasChanges167-}