Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 2cb5496 point at it.

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 1–4why, one step back
1"use strict"
2// @ts-check34import {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 5–6why, one step back
5 each,
6 has,

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 7why, one step back
7 is,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 8–9why, one step back
8 isDraft,
9 isDraftable,

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 10why, one step back
10 shallowCopy,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 11why, one step back
11 DRAFT_STATE

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 12–15why, one step back
12} from "./common"
1314const descriptors = {}15

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 16–30why, one step back
16// 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 back
31 let draft
32 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 back
41 each(base, prop => {
42 Object.defineProperty(draft, "" + prop, createPropertyProxy("" + prop))43 })44

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 45why, one step back
45 // See "proxy.js" for property documentation.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 46why, one step back
46 const state = {

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 47why, one step back
47 scope: parent ? parent.scope : currentScope(),

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 48why, one step back
48 modified: false,

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 49why, one step back
49 finalizing: false, // es5 only

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 50why, one step back
50 finalized: false,

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 51why, one step back
51 assigned: {},

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 52–53why, one step back
52 parent,
53 base,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 54–56why, one step back
54 draft,
55 copy: null,56 revoke,

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 57why, one step back
57 revoked: false // es5 only

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 58why, one step back
58 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 59–60why, one step back
59
60 createHiddenProperty(draft, DRAFT_STATE, state)

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 61why, one step back
61 state.scope.push(state)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 62why, one step back
62 return draft

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 63–64why, one step back
63}
64

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 65–68why, one step back
65function revoke() {
66 this.revoked = true67}68

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 69why, one step back
69function source(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 70why, one step back
70 return state.copy || state.base

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 71–73why, one step back
71}
7273function get(state, prop) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 74why, one step back
74 assertUnrevoked(state)

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 75why, one step back
75 const value = source(state)[prop]

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 76–77why, one step back
76 // 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 #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 78why, one step back
78 prepareCopy(state)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 79why, one step back
79 return (state.copy[prop] = createDraft(value, state))

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 80–84why, one step back
80 }
81 return value82}8384function set(state, prop, value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 85why, one step back
85 assertUnrevoked(state)

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 86why, one step back
86 state.assigned[prop] = true

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 87–102why, one step back
87 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 back
103 if (!state.copy) state.copy = shallowCopy(state.base)

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 104–112why, one step back
104}
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 back
113 return get(this[DRAFT_STATE], prop)

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 114–115why, one step back
114 },
115 set(value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 116why, one step back
116 set(this[DRAFT_STATE], prop, value)

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 117–121why, one step back
117 }
118 })119 )120}121

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 122–123why, one step back
122function assertUnrevoked(state) {
123 if (state.revoked === true)

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 124why, one step back
124 throw new Error(

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 125–126why, one step back
125 "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 #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 127–129why, one step back
127 )
128}129

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 130–137why, one step back
130// 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 #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 138–145why, one step back
138 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}145

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 146–147why, one step back
146function markChangesRecursively(object) {
147 if (!object || typeof object !== "object") return

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 148why, one step back
148 const state = object[DRAFT_STATE]

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 149why, one step back
149 if (!state) return

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 150why, one step back
150 const {base, draft, assigned} = state

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 151–152why, one step back
151 if (!Array.isArray(object)) {
152 // Look for added keys.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 153why, one step back
153 Object.keys(draft).forEach(key => {

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 154–159why, one step back
154 // 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 back
160 markChangesRecursively(draft[key])

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 161why, one step back
161 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 162why, one step back
162 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 163–165why, one step back
163 // 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 back
166 if (draft[key] === undefined && !has(draft, key)) {

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 167–169why, one step back
167 assigned[key] = false
168 markChanged(state)169 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 170why, one step back
170 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 171–173why, one step back
171 } else if (hasArrayChanges(state)) {
172 markChanged(state)173 assigned.length = true

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 174–175why, one step back
174 if (draft.length < base.length) {
175 for (let i = draft.length; i < base.length; i++) assigned[i] = false

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 176why, one step back
176 } else {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 177why, one step back
177 for (let i = base.length; i < draft.length; i++) assigned[i] = true

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 178why, one step back
178 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 179why, one step back
179 for (let i = 0; i < draft.length; i++) {

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 180why, one step back
180 // Only untouched indices trigger recursion.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 181why, one step back
181 if (assigned[i] === undefined) markChangesRecursively(draft[i])

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 182why, one step back
182 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 183–185why, one step back
183 }
184}185

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 186why, one step back
186function hasObjectChanges(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 187why, one step back
187 const {base, draft} = state

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

lines 188–190why, one step back
188
189 // 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 back
191 const keys = Object.keys(draft)

nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374

line 192why, one step back
192 for (let i = keys.length - 1; i >= 0; i--) {

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

line 193why, one step back
193 // The `undefined` check is a fast path for pre-existing keys.

nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374

line 194why, one step back
194 if (base[keys[i]] === undefined && !has(base, keys[i])) {

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

lines 195–201why, one step back
195 return true
196 }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).length

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 202–204why, one step back
202}
203204function hasArrayChanges(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 205–206why, one step back
205 const {draft} = state
206 if (draft.length !== state.base.length) return true

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 207–213why, one step back
207 // See #116
208 // 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 added212 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check213 // the last one

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 214why, one step back
214 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 215–227why, one step back
215 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)
216 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 false219}220221function createHiddenProperty(target, prop, value) {222 Object.defineProperty(target, prop, {223 value: value,224 enumerable: false,225 writable: true226 })227}