es5.js
This file
Path
src/es5.js
198 lines · 5.6 KiB · JavaScript
Last changed by
Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199"use strict"// @ts-check
import { is, isProxyable, isProxy, freeze, PROXY_STATE, shallowCopy, RETURNED_AND_MODIFIED_ERROR, each, finalize} from "./common"
const descriptors = {}let states = null
function createState(parent, proxy, base) { return { modified: false, hasCopy: false, parent, base, proxy, copy: undefined, finished: false, finalizing: false, finalized: false }}
function source(state) { return state.hasCopy ? state.copy : state.base}
function get(state, prop) { assertUnfinished(state) const value = source(state)[prop] if (!state.finalizing && value === state.base[prop] && isProxyable(value)) { // only create a proxy if the value is proxyable, and the value was in the base state // if it wasn't in the base state, the object is already modified and we will process it in finalize prepareCopy(state) return (state.copy[prop] = createProxy(state, value)) } return value}
function set(state, prop, value) { assertUnfinished(state) if (!state.modified) { if (is(source(state)[prop], value)) return markChanged(state) prepareCopy(state) } state.copy[prop] = value}
function markChanged(state) { if (!state.modified) { state.modified = true if (state.parent) markChanged(state.parent) }}
function prepareCopy(state) { if (state.hasCopy) return state.hasCopy = true state.copy = shallowCopy(state.base)}
// creates a proxy for plain objects / arraysfunction createProxy(parent, base) { const proxy = shallowCopy(base) each(base, i => { Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i)) }) const state = createState(parent, proxy, base) createHiddenProperty(proxy, PROXY_STATE, state) states.push(state) return proxy}
function createPropertyProxy(prop) { return ( descriptors[prop] || (descriptors[prop] = { configurable: true, enumerable: true, get() { return get(this[PROXY_STATE], prop) }, set(value) { set(this[PROXY_STATE], prop, value) } }) )}
function assertUnfinished(state) { if (state.finished === true) throw new Error( "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process?" )}
// this sounds very expensive, but actually it is not that extensive in practice// as it will only visit proxies, and only do key-based change detection for objects for// which it is not already know that they are changed (that is, only object for which no known key was changed)function markChanges() { // intentionally we process the proxies in reverse order; // ideally we start by processing leafs in the tree, because if a child has changed, we don't have to check the parent anymore // reverse order of proxy creation approximates this for (let i = states.length - 1; i >= 0; i--) { const state = states[i] if (state.modified === false) { if (Array.isArray(state.base)) { if (hasArrayChanges(state)) markChanged(state) } else if (hasObjectChanges(state)) markChanged(state) } }}
function hasObjectChanges(state) { const baseKeys = Object.keys(state.base) const keys = Object.keys(state.proxy) return !shallowEqual(baseKeys, keys)}
function hasArrayChanges(state) { return state.proxy.length !== state.base.length}
export function produceEs5(baseState, producer) { const prevStates = states states = [] try { // create proxy for root const rootProxy = createProxy(undefined, baseState) // execute the thunk const returnValue = producer.call(rootProxy, rootProxy) // and finalize the modified proxy each(states, (_, state) => { state.finalizing = true }) // find and mark all changes (for parts not done yet) // TODO: store states by depth, to be able guarantee processing leaves first markChanges() let result = finalize(rootProxy) // check whether the draft was modified and/or a value was returned if (returnValue !== undefined && returnValue !== rootProxy) { // something was returned, and it wasn't the proxy itself if (rootProxy[PROXY_STATE].modified) throw new Error(RETURNED_AND_MODIFIED_ERROR) result = returnValue } // make sure all proxies become unusable each(states, (_, state) => { state.finished = true }) return result } finally { states = prevStates }}
function shallowEqual(objA, objB) { //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js if (is(objA, objB)) return true if ( typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null ) { return false } const keysA = Object.keys(objA) const keysB = Object.keys(objB) if (keysA.length !== keysB.length) return false for (let i = 0; i < keysA.length; i++) { if ( !hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]]) ) { return false } } return true}
function createHiddenProperty(target, prop, value) { Object.defineProperty(target, prop, { value: value, enumerable: false, writable: true })}