es5.js
This file
Path
src/es5.js
218 lines · 7.0 KiB · JavaScript
Last changed by
feat: add `Immer` classaleclarsonbefore adoptionecd04dc
History of this fileTake it
The walk stopped after 200 commits: lines older than 5281004 point at it.
Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 1–4why, one step back1"use strict"2// @ts-check34import {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 5–6why, one step back5 each,6 has,Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
line 7why, one step back7 is,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 8–9why, one step back8 isDraft,9 isDraftable,Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
line 10why, one step back10 shallowCopy,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 11why, one step back11 DRAFT_STATEMerge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 12–15why, one step back12} from "./common"1314const descriptors = {}15feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 16–38why, one step back16// For nested produce calls:17export const scopes = []18export const currentScope = () => scopes[scopes.length - 1]1920export function willFinalize(result, baseDraft, needPatches) {21 const scope = currentScope()22 scope.forEach(state => (state.finalizing = true))23 if (result === undefined || result === baseDraft) {24 if (needPatches) markChangesRecursively(baseDraft)25 // This is faster when we don't care about which attributes changed.26 markChangesSweep(scope)27 }28}2930export function createDraft(base, parent) {31 if (isDraft(base)) throw new Error("This should never happen. Please report: https://github.com/mweststrate/immer/issues/new") // prettier-ignore3233 const draft = shallowCopy(base)34 each(base, prop => {35 Object.defineProperty(draft, "" + prop, createPropertyProxy("" + prop))36 })3738 const state = {Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
line 39why, one step back39 modified: false,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 40–41why, one step back40 finalizing: false,41 finalized: false,Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 42why, one step back42 assigned: {}, // true: value was assigned to these props, false: was removedMerge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 43–44why, one step back43 parent,44 base,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 45–48why, one step back45 draft,46 copy: null,47 revoke,48 revoked: falseMerge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
line 49why, one step back49 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 50–53why, one step back5051 createHiddenProperty(draft, DRAFT_STATE, state)52 currentScope().push(state)53 return draftMerge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 54–55why, one step back54}55feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 56–59why, one step back56function revoke() {57 this.revoked = true58}59Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
line 60why, one step back60function source(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 61why, one step back61 return state.copy || state.baseMerge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 62–64why, one step back62}6364function get(state, prop) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 65why, one step back65 assertUnrevoked(state)Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
line 66why, one step back66 const value = source(state)[prop]feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 67–68why, one step back67 // Drafts are only created for proxyable values that exist in the base state.68 if (!state.finalizing && value === state.base[prop] && isDraftable(value)) {Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
line 69why, one step back69 prepareCopy(state)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 70why, one step back70 return (state.copy[prop] = createDraft(value, state))Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 71–75why, one step back71 }72 return value73}7475function set(state, prop, value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 76why, one step back76 assertUnrevoked(state)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 77why, one step back77 state.assigned[prop] = true // optimization; skip this if there is no listenerMerge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 78–93why, one step back78 if (!state.modified) {79 if (is(source(state)[prop], value)) return80 markChanged(state)81 prepareCopy(state)82 }83 state.copy[prop] = value84}8586function markChanged(state) {87 if (!state.modified) {88 state.modified = true89 if (state.parent) markChanged(state.parent)90 }91}9293function prepareCopy(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 94why, one step back94 if (!state.copy) state.copy = shallowCopy(state.base)Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 95–103why, one step back95}9697function createPropertyProxy(prop) {98 return (99 descriptors[prop] ||100 (descriptors[prop] = {101 configurable: true,102 enumerable: true,103 get() {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 104why, one step back104 return get(this[DRAFT_STATE], prop)Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 105–106why, one step back105 },106 set(value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 107why, one step back107 set(this[DRAFT_STATE], prop, value)Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 108–112why, one step back108 }109 })110 )111}112feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 113–114why, one step back113function assertUnrevoked(state) {114 if (state.revoked === true)Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
line 115why, one step back115 throw new Error(Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
lines 116–117why, one step back116 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +117 JSON.stringify(state.copy || state.base)Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 118–120why, one step back118 )119}120feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 121–128why, one step back121// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.122function markChangesSweep(scope) {123 // The natural order of drafts in the `scope` array is based on when they124 // were accessed. By processing drafts in reverse natural order, we have a125 // better chance of processing leaf nodes first. When a leaf node is known to126 // have changed, we can avoid any traversal of its ancestor nodes.127 for (let i = scope.length - 1; i >= 0; i--) {128 const state = scope[i]Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 129–136why, one step back129 if (state.modified === false) {130 if (Array.isArray(state.base)) {131 if (hasArrayChanges(state)) markChanged(state)132 } else if (hasObjectChanges(state)) markChanged(state)133 }134 }135}136Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 137–138why, one step back137function markChangesRecursively(object) {138 if (!object || typeof object !== "object") returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 139why, one step back139 const state = object[DRAFT_STATE]Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 140why, one step back140 if (!state) returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 141why, one step back141 const {base, draft, assigned} = stateperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 142–143why, one step back142 if (!Array.isArray(object)) {143 // Look for added keys.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 144why, one step back144 Object.keys(draft).forEach(key => {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 145–150why, one step back145 // The `undefined` check is a fast path for pre-existing keys.146 if (base[key] === undefined && !has(base, key)) {147 assigned[key] = true148 markChanged(state)149 } else if (!assigned[key]) {150 // Only untouched properties trigger recursion.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 151why, one step back151 markChangesRecursively(draft[key])perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 152why, one step back152 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 153why, one step back153 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 154–156why, one step back154 // Look for removed keys.155 Object.keys(base).forEach(key => {156 // The `undefined` check is a fast path for pre-existing keys.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 157why, one step back157 if (draft[key] === undefined && !has(draft, key)) {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 158–160why, one step back158 assigned[key] = false159 markChanged(state)160 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 161why, one step back161 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 162–164why, one step back162 } else if (hasArrayChanges(state)) {163 markChanged(state)164 assigned.length = truefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 165–166why, one step back165 if (draft.length < base.length) {166 for (let i = draft.length; i < base.length; i++) assigned[i] = falseperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 167why, one step back167 } else {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 168why, one step back168 for (let i = base.length; i < draft.length; i++) assigned[i] = trueperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 169why, one step back169 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 170why, one step back170 for (let i = 0; i < draft.length; i++) {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 171why, one step back171 // Only untouched indices trigger recursion.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 172why, one step back172 if (assigned[i] === undefined) markChangesRecursively(draft[i])perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 173why, one step back173 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 174–176why, one step back174 }175}176Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
line 177why, one step back177function hasObjectChanges(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 178why, one step back178 const {base, draft} = stateMerge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 179–181why, one step back179180 // Search for added keys. Start at the back, because non-numeric keys181 // are ordered by time of definition on the object.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 182why, one step back182 const keys = Object.keys(draft)nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 183why, one step back183 for (let i = keys.length - 1; i >= 0; i--) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
line 184why, one step back184 // The `undefined` check is a fast path for pre-existing keys.nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 185why, one step back185 if (base[keys[i]] === undefined && !has(base, keys[i])) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 186–192why, one step back186 return true187 }188 }189190 // Since no keys have been added, we can compare lengths to know if an191 // object has been deleted.192 return keys.length !== Object.keys(base).lengthMerge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 193–195why, one step back193}194195function hasArrayChanges(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 196–197why, one step back196 const {draft} = state197 if (draft.length !== state.base.length) return trueFixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
lines 198–202why, one step back198 // See #116199 // If we first shorten the length, our array interceptors will be removed.200 // If after that new items are added, result in the same original length,201 // those last items will have no intercepting property.202 // So if there is no own descriptor on the last position, we know that items were removed and addedFixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
lines 203–204why, one step back203 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check204 // the last onefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 205why, one step back205 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 206why, one step back206 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
lines 207–209why, one step back207 if (descriptor && !descriptor.get) return true208 // For all other cases, we don't have to compare, as they would have been picked up by the index setters209 return falseMerge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004
lines 210–218why, one step back210}211212function createHiddenProperty(target, prop, value) {213 Object.defineProperty(target, prop, {214 value: value,215 enumerable: false,216 writable: true217 })218}