es5.js
This file
Path
src/es5.js
222 lines · 5.9 KiB · JavaScript
Last changed by
Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
History of this fileTake it
transpile es5 codeMichel Weststratebefore adoptionf012154
lines 1–3why, one step back1"use strict"2// @ts-check3Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 4why, one step back4import {Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 5why, one step back5 is,Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 6–9why, one step back6 isProxyable,7 isProxy,8 freeze,9 PROXY_STATE,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 10–13why, one step back10 shallowCopy,11 verifyReturnValue,12 each,13 finalizeFixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 14why, one step back14} from "./common"transpile es5 codeMichel Weststratebefore adoptionf012154
line 15why, one step back15Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 16–17why, one step back16const descriptors = {}17let states = nulltranspile es5 codeMichel Weststratebefore adoptionf012154
line 18why, one step back18Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 19–27why, one step back19function createState(parent, proxy, base) {20 return {21 modified: false,22 hasCopy: false,23 parent,24 base,25 proxy,26 copy: undefined,27 finished: false,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 28–29why, one step back28 finalizing: false,29 finalized: falsetranspile es5 codeMichel Weststratebefore adoptionf012154
line 30why, one step back30 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 31why, one step back31}transpile es5 codeMichel Weststratebefore adoptionf012154
line 32why, one step back32Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 33–42why, one step back33function source(state) {34 return state.hasCopy ? state.copy : state.base35}3637function get(state, prop) {38 assertUnfinished(state)39 const value = source(state)[prop]40 if (!state.finalizing && !isProxy(value) && isProxyable(value)) {41 prepareCopy(state)42 return (state.copy[prop] = createProxy(state, value))transpile es5 codeMichel Weststratebefore adoptionf012154
line 43why, one step back43 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 44–45why, one step back44 return value45}transpile es5 codeMichel Weststratebefore adoptionf012154
line 46why, one step back46Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 47–49why, one step back47function set(state, prop, value) {48 assertUnfinished(state)49 if (!state.modified) {Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 50why, one step back50 if (is(source(state)[prop], value)) returnMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 51why, one step back51 markChanged(state)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 52why, one step back52 prepareCopy(state)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 53–55why, one step back53 }54 state.copy[prop] = value55}transpile es5 codeMichel Weststratebefore adoptionf012154
line 56why, one step back56Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 57–62why, one step back57function markChanged(state) {58 if (!state.modified) {59 state.modified = true60 if (state.parent) markChanged(state.parent)61 }62}transpile es5 codeMichel Weststratebefore adoptionf012154
line 63why, one step back63Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 64–66why, one step back64function prepareCopy(state) {65 if (state.hasCopy) return66 state.hasCopy = trueMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 67why, one step back67 state.copy = shallowCopy(state.base)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 68why, one step back68}transpile es5 codeMichel Weststratebefore adoptionf012154
line 69why, one step back69Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 70–71why, one step back70// creates a proxy for plain objects / arrays71function createProxy(parent, base) {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 72–74why, one step back72 const proxy = Array.isArray(base)73 ? createArrayProxy(base)74 : createObjectProxy(base)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 75–79why, one step back75 const state = createState(parent, proxy, base)76 createHiddenProperty(proxy, PROXY_STATE, state)77 states.push(state)78 return proxy79}transpile es5 codeMichel Weststratebefore adoptionf012154
line 80why, one step back80Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 81–91why, one step back81function createPropertyProxy(prop) {82 return (83 descriptors[prop] ||84 (descriptors[prop] = {85 configurable: true,86 enumerable: true,87 get() {88 return get(this[PROXY_STATE], prop)89 },90 set(value) {91 set(this[PROXY_STATE], prop, value)transpile es5 codeMichel Weststratebefore adoptionf012154
line 92why, one step back92 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 93–95why, one step back93 })94 )95}transpile es5 codeMichel Weststratebefore adoptionf012154
line 96why, one step back96Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 97–98why, one step back97function createObjectProxy(base) {98 const proxy = Object.assign({}, base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 99why, one step back99 each(base, prop => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 100why, one step back100 Object.defineProperty(proxy, prop, createPropertyProxy(prop))Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 101why, one step back101 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 102–103why, one step back102 return proxy103}transpile es5 codeMichel Weststratebefore adoptionf012154
line 104why, one step back104Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 105–106why, one step back105function createArrayProxy(base) {106 const proxy = new Array(base.length)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 107why, one step back107 each(base, i => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 108why, one step back108 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 109why, one step back109 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 110–111why, one step back110 return proxy111}transpile es5 codeMichel Weststratebefore adoptionf012154
line 112why, one step back112Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 113–116why, one step back113function assertUnfinished(state) {114 if (state.finished === true)115 throw new Error(116 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process?"transpile es5 codeMichel Weststratebefore adoptionf012154
line 117why, one step back117 )Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 118why, one step back118}transpile es5 codeMichel Weststratebefore adoptionf012154
line 119why, one step back119Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 120–132why, one step back120// this sounds very expensive, but actually it is not that extensive in practice121// as it will only visit proxies, and only do key-based change detection for objects for122// which it is not already know that they are changed (that is, only object for which no known key was changed)123function markChanges() {124 // intentionally we process the proxies in reverse order;125 // ideally we start by processing leafs in the tree, because if a child has changed, we don't have to check the parent anymore126 // reverse order of proxy creation approximates this127 for (let i = states.length - 1; i >= 0; i--) {128 const state = states[i]129 if (state.modified === false) {130 if (Array.isArray(state.base)) {131 if (hasArrayChanges(state)) markChanged(state)132 } else if (hasObjectChanges(state)) markChanged(state)transpile es5 codeMichel Weststratebefore adoptionf012154
lines 133–134why, one step back133 }134 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 135why, one step back135}transpile es5 codeMichel Weststratebefore adoptionf012154
line 136why, one step back136Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 137–141why, one step back137function hasObjectChanges(state) {138 const baseKeys = Object.keys(state.base)139 const keys = Object.keys(state.proxy)140 return !shallowEqual(baseKeys, keys)141}transpile es5 codeMichel Weststratebefore adoptionf012154
line 142why, one step back142Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 143–145why, one step back143function hasArrayChanges(state) {144 return state.proxy.length !== state.base.length145}transpile es5 codeMichel Weststratebefore adoptionf012154
line 146why, one step back146Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 147–148why, one step back147export function finalizeObject(proxy, state) {148 const res = (state.copy = shallowCopy(proxy))Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 149why, one step back149 const base = state.baseMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 150–152why, one step back150 each(res, (prop, value) => {151 if (value !== base[prop]) res[prop] = finalize(value)152 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 153why, one step back153 return freeze(res)transpile es5 codeMichel Weststratebefore adoptionf012154
lines 154–155why, one step back154}155Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 156–157why, one step back156export function finalizeArray(proxy, state) {157 const res = (state.copy = shallowCopy(proxy))Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 158why, one step back158 const base = state.baseMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 159–161why, one step back159 each(res, (i, value) => {160 if (value !== base[i]) res[i] = finalize(value)161 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 162why, one step back162 return freeze(res)transpile es5 codeMichel Weststratebefore adoptionf012154
lines 163–164why, one step back163}164Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 165–171why, one step back165export function produceEs5(baseState, producer) {166 const prevStates = states167 states = []168 try {169 // create proxy for root170 const rootClone = createProxy(undefined, baseState)171 // execute the thunkMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 172why, one step back172 verifyReturnValue(producer(rootClone))Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 173why, one step back173 // and finalize the modified proxyMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 174–176why, one step back174 each(states, (_, state) => {175 state.finalizing = true176 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 177why, one step back177 // find and mark all changes (for parts not done yet)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 178why, one step back178 // TODO: store states by depth, to be able guarantee processing leaves firstMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 179–181why, one step back179 markChanges()180 const res = finalize(rootClone)181 // make sure all proxies become unusableMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 182–184why, one step back182 each(states, (_, state) => {183 state.finished = true184 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 185–187why, one step back185 return res186 } finally {187 states = prevStatestranspile es5 codeMichel Weststratebefore adoptionf012154
lines 188–192why, one step back188 }189}190191function shallowEqual(objA, objB) {192 //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.jsAdded polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 193why, one step back193 if (is(objA, objB)) return truetranspile es5 codeMichel Weststratebefore adoptionf012154
lines 194–207why, one step back194 if (195 typeof objA !== "object" ||196 objA === null ||197 typeof objB !== "object" ||198 objB === null199 ) {200 return false201 }202 const keysA = Object.keys(objA)203 const keysB = Object.keys(objB)204 if (keysA.length !== keysB.length) return false205 for (let i = 0; i < keysA.length; i++) {206 if (207 !hasOwnProperty.call(objB, keysA[i]) ||Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 208why, one step back208 !is(objA[keysA[i]], objB[keysA[i]])transpile es5 codeMichel Weststratebefore adoptionf012154
lines 209–215why, one step back209 ) {210 return false211 }212 }213 return true214}215Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 216–221why, one step back216function createHiddenProperty(target, prop, value) {217 Object.defineProperty(target, prop, {218 value: value,219 enumerable: false,220 writable: true221 })transpile es5 codeMichel Weststratebefore adoptionf012154
line 222why, one step back222}