es5.js
This file
Path
src/es5.js
227 lines · 7.1 KiB · JavaScript
Last changed by
feat: passing drafts to producealeclarsonbefore adoption9c53415
History of this fileTake it
The walk stopped after 200 commits: lines older than 233e1d3 point at it.
Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
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 #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
line 7why, one step back7 is,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 8–9why, one step back8 isDraft,9 isDraftable,Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
line 10why, one step back10 shallowCopy,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 11why, one step back11 DRAFT_STATEMerge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
lines 12–15why, one step back12} from "./common"1314const descriptors = {}15feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 16–30why, 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) {feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 31–40why, one step back31 let draft32 if (isDraft(base)) {33 const state = base[DRAFT_STATE]34 // Avoid creating new drafts when copying.35 state.finalizing = true36 draft = shallowCopy(state.draft)37 state.finalizing = false38 } else {39 draft = shallowCopy(base)40 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 41–44why, one step back41 each(base, prop => {42 Object.defineProperty(draft, "" + prop, createPropertyProxy("" + prop))43 })44feat: passing drafts to producealeclarsonbefore adoption9c53415
line 45why, one step back45 // See "proxy.js" for property documentation.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 46why, one step back46 const state = {feat: passing drafts to producealeclarsonbefore adoption9c53415
line 47why, one step back47 scope: parent ? parent.scope : currentScope(),Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
line 48why, one step back48 modified: false,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 49why, one step back49 finalizing: false, // es5 onlyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 50why, one step back50 finalized: false,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 51why, one step back51 assigned: {},Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
lines 52–53why, one step back52 parent,53 base,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 54–56why, one step back54 draft,55 copy: null,56 revoke,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 57why, one step back57 revoked: false // es5 onlyMerge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
line 58why, one step back58 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 59–60why, one step back5960 createHiddenProperty(draft, DRAFT_STATE, state)feat: passing drafts to producealeclarsonbefore adoption9c53415
line 61why, one step back61 state.scope.push(state)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 62why, one step back62 return draftMerge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
lines 63–64why, one step back63}64feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 65–68why, one step back65function revoke() {66 this.revoked = true67}68Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
line 69why, one step back69function source(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 70why, one step back70 return state.copy || state.baseMerge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
lines 71–73why, one step back71}7273function get(state, prop) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 74why, one step back74 assertUnrevoked(state)Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
line 75why, one step back75 const value = source(state)[prop]feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 76–77why, one step back76 // Drafts are only created for proxyable values that exist in the base state.77 if (!state.finalizing && value === state.base[prop] && isDraftable(value)) {Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
line 78why, one step back78 prepareCopy(state)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 79why, one step back79 return (state.copy[prop] = createDraft(value, state))Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
lines 80–84why, one step back80 }81 return value82}8384function set(state, prop, value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 85why, one step back85 assertUnrevoked(state)feat: passing drafts to producealeclarsonbefore adoption9c53415
line 86why, one step back86 state.assigned[prop] = trueMerge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
lines 87–102why, one step back87 if (!state.modified) {88 if (is(source(state)[prop], value)) return89 markChanged(state)90 prepareCopy(state)91 }92 state.copy[prop] = value93}9495function markChanged(state) {96 if (!state.modified) {97 state.modified = true98 if (state.parent) markChanged(state.parent)99 }100}101102function prepareCopy(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 103why, one step back103 if (!state.copy) state.copy = shallowCopy(state.base)Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
lines 104–112why, one step back104}105106function createPropertyProxy(prop) {107 return (108 descriptors[prop] ||109 (descriptors[prop] = {110 configurable: true,111 enumerable: true,112 get() {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 113why, one step back113 return get(this[DRAFT_STATE], prop)Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
lines 114–115why, one step back114 },115 set(value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 116why, one step back116 set(this[DRAFT_STATE], prop, value)Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
lines 117–121why, one step back117 }118 })119 )120}121feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 122–123why, one step back122function assertUnrevoked(state) {123 if (state.revoked === true)Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
line 124why, one step back124 throw new Error(Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
lines 125–126why, one step back125 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +126 JSON.stringify(state.copy || state.base)Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
lines 127–129why, one step back127 )128}129feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 130–137why, one step back130// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.131function markChangesSweep(scope) {132 // The natural order of drafts in the `scope` array is based on when they133 // were accessed. By processing drafts in reverse natural order, we have a134 // better chance of processing leaf nodes first. When a leaf node is known to135 // have changed, we can avoid any traversal of its ancestor nodes.136 for (let i = scope.length - 1; i >= 0; i--) {137 const state = scope[i]Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
lines 138–145why, one step back138 if (state.modified === false) {139 if (Array.isArray(state.base)) {140 if (hasArrayChanges(state)) markChanged(state)141 } else if (hasObjectChanges(state)) markChanged(state)142 }143 }144}145Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 146–147why, one step back146function markChangesRecursively(object) {147 if (!object || typeof object !== "object") returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 148why, one step back148 const state = object[DRAFT_STATE]Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 149why, one step back149 if (!state) returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 150why, one step back150 const {base, draft, assigned} = stateperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 151–152why, one step back151 if (!Array.isArray(object)) {152 // Look for added keys.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 153why, one step back153 Object.keys(draft).forEach(key => {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 154–159why, one step back154 // The `undefined` check is a fast path for pre-existing keys.155 if (base[key] === undefined && !has(base, key)) {156 assigned[key] = true157 markChanged(state)158 } else if (!assigned[key]) {159 // Only untouched properties trigger recursion.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 160why, one step back160 markChangesRecursively(draft[key])perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 161why, one step back161 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 162why, one step back162 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 163–165why, one step back163 // Look for removed keys.164 Object.keys(base).forEach(key => {165 // The `undefined` check is a fast path for pre-existing keys.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 166why, one step back166 if (draft[key] === undefined && !has(draft, key)) {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 167–169why, one step back167 assigned[key] = false168 markChanged(state)169 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 170why, one step back170 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 171–173why, one step back171 } else if (hasArrayChanges(state)) {172 markChanged(state)173 assigned.length = truefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 174–175why, one step back174 if (draft.length < base.length) {175 for (let i = draft.length; i < base.length; i++) assigned[i] = falseperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 176why, one step back176 } else {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 177why, one step back177 for (let i = base.length; i < draft.length; i++) assigned[i] = trueperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 178why, one step back178 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 179why, one step back179 for (let i = 0; i < draft.length; i++) {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 180why, one step back180 // Only untouched indices trigger recursion.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 181why, one step back181 if (assigned[i] === undefined) markChangesRecursively(draft[i])perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 182why, one step back182 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 183–185why, one step back183 }184}185Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
line 186why, one step back186function hasObjectChanges(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 187why, one step back187 const {base, draft} = stateMerge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 188–190why, one step back188189 // Search for added keys. Start at the back, because non-numeric keys190 // are ordered by time of definition on the object.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 191why, one step back191 const keys = Object.keys(draft)nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 192why, one step back192 for (let i = keys.length - 1; i >= 0; i--) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
line 193why, one step back193 // The `undefined` check is a fast path for pre-existing keys.nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 194why, one step back194 if (base[keys[i]] === undefined && !has(base, keys[i])) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 195–201why, one step back195 return true196 }197 }198199 // Since no keys have been added, we can compare lengths to know if an200 // object has been deleted.201 return keys.length !== Object.keys(base).lengthMerge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
lines 202–204why, one step back202}203204function hasArrayChanges(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 205–206why, one step back205 const {draft} = state206 if (draft.length !== state.base.length) return trueFixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
lines 207–211why, one step back207 // See #116208 // If we first shorten the length, our array interceptors will be removed.209 // If after that new items are added, result in the same original length,210 // those last items will have no intercepting property.211 // 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 212–213why, one step back212 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check213 // the last onefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 214why, one step back214 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 215why, one step back215 // 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 216–218why, one step back216 if (descriptor && !descriptor.get) return true217 // For all other cases, we don't have to compare, as they would have been picked up by the index setters218 return falseMerge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
lines 219–227why, one step back219}220221function createHiddenProperty(target, prop, value) {222 Object.defineProperty(target, prop, {223 value: value,224 enumerable: false,225 writable: true226 })227}