unclassed scope
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent cb3b9d6 · Browse files · 7 files·+72 added−54 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+72 added−54 removed
- other:+72 added−54 removed
Provenance
- Commit
7a38af9- Tree
9e6b457- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
cb3b9d6
7 files changed+72 added−54 removed
modified:src/core/finalize.ts+4 added-3 removed
16 unmodified lines17181920212122232425262 unmodified lines2930313232333435363711 unmodified lines49505152525354555657101 unmodified linesExpand all16 unmodified lines ProxyTypeES5Object, ProxyTypeES5Array, ProxyTypeSet, getPlugin, die die, revokeScope} from "../internal"
export function processResult(result: any, scope: ImmerScope) { scope.unfinalizedDrafts_ = scope.drafts_.length2 unmodified lines if (!scope.immer_.useProxies_) getPlugin("ES5").willFinalizeES5_(scope, result, isReplaced) if (isReplaced) { if (baseDraft[DRAFT_STATE].modified_) { scope.revoke_() revokeScope(scope) die(4) } if (isDraftable(result)) { // Finalize the result in case it contains (or is) a subset of the draft.11 unmodified lines } else { // Finalize the base draft. result = finalize(scope, baseDraft, []) } scope.revoke_() revokeScope(scope) if (scope.patches_) { scope.patchListener_!(scope.patches_, scope.inversePatches_!) } return result !== NOTHING ? result : undefined101 unmodified linesExpand allmodified:src/core/immerClass.ts+16 added-12 removed
2 unmodified lines3 IProduce,4 ImmerState,5 Drafted,6 isDraftable,7- ImmerScope,7 processResult,8 NOTHING,9 Patch,10 Objectish,8 unmodified lines19 freeze,20 getPlugin,21 die,22 hasProxies,24- isMinified23+ isMinified,24+ enterScope,25+ revokeScope,26+ leaveScope,27+ usePatchesInScope,28+ getCurrentScope29 } from "../internal"30 31 interface ProducersFns {32 produce: IProduce56 unmodified lines89 let result90 91 // Only plain objects, arrays, and "immerable classes" are drafted.92 if (isDraftable(base)) {89- const scope = ImmerScope.enter_(this)93+ const scope = enterScope(this)94 const proxy = createProxy(this, base, undefined)95 let hasError = true96 try {97 result = recipe(proxy)98 hasError = false99 } finally {100 // finally instead of catch + rethrow better preserves original stack97- if (hasError) scope.revoke_()98- else scope.leave_()101+ if (hasError) revokeScope(scope)102+ else leaveScope(scope)103 }104 if (typeof Promise !== "undefined" && result instanceof Promise) {105 return result.then(106 result => {103- scope.usePatches_(patchListener)107+ usePatchesInScope(scope, patchListener)108 return processResult(result, scope)109 },110 error => {107- scope.revoke_()111+ revokeScope(scope)112 throw error113 }114 )115 }112- scope.usePatches_(patchListener)116+ usePatchesInScope(scope, patchListener)117 return processResult(result, scope)118 } else {119 result = recipe(base)120 if (result === NOTHING) return undefined18 unmodified lines139 }140 141 createDraft<T extends Objectish>(base: T): Draft<T> {142 if (!isDraftable(base)) die(8)139- const scope = ImmerScope.enter_(this)143+ const scope = enterScope(this)144 const proxy = createProxy(this, base, undefined)145 proxy[DRAFT_STATE].isManual_ = true142- scope.leave_()146+ leaveScope(scope)147 return proxy as any148 }149 150 finishDraft<D extends Draft<any>>(5 unmodified lines156 if (!state || !state.isManual_) die(9)157 if (state.finalized_) die(10)158 }159 const {scope_: scope} = state156- scope.usePatches_(patchListener)160+ usePatchesInScope(scope, patchListener)161 return processResult(undefined, scope)162 }163 164 /**55 unmodified lines220 : immer.useProxies_221 ? createProxyProxy(value, parent)222 : getPlugin("ES5").createES5Proxy_(value, parent)223 220- const scope = parent ? parent.scope_ : ImmerScope.current_!224+ const scope = parent ? parent.scope_ : getCurrentScope()225 scope.drafts_.push(draft)226 return draft227 }228 7 unmodified linesmodified:src/core/proxy.ts+2 added-2 removed
10 unmodified lines11 Drafted,12 AnyObject,13 AnyArray,14 Objectish,15- ImmerScope,15+ getCurrentScope,16 DRAFT_STATE,17 die,18 createProxy,19 ProxyTypeProxyObject,39 unmodified lines59 const isArray = Array.isArray(base)60 const state: ProxyState = {61 type_: isArray ? ProxyTypeProxyArray : (ProxyTypeProxyObject as any),62 // Track which produce call this is associated with.63- scope_: parent ? parent.scope_ : ImmerScope.current_!,63+ scope_: parent ? parent.scope_ : getCurrentScope()!,64 // True for both shallow and deep changes.65 modified_: false,66 // Used during finalization.67 finalized_: false,191 unmodified linesmodified:src/core/scope.ts+44 added-32 removed
7 unmodified lines8 ProxyTypeProxyObject,9 ProxyTypeProxyArray,10 getPlugin11 } from "../internal"12+import {die} from "../utils/errors"13 14 /** Each scope represents a `produce` call. */14-// TODO: non-class?15-export class ImmerScope {16- static current_?: ImmerScope15 16+export interface ImmerScope {17 patches_?: Patch[]18 inversePatches_?: Patch[]19 canAutoFreeze_: boolean20 drafts_: any[]21 parent_?: ImmerScope22 patchListener_?: PatchListener23 immer_: Immer25- unfinalizedDrafts_ = 024+ unfinalizedDrafts_: number25+}26 27- constructor(parent: ImmerScope | undefined, immer: Immer) {28- this.drafts_ = []29- this.parent_ = parent30- this.immer_ = immer27+let currentScope: ImmerScope | undefined28 29+export function getCurrentScope() {30+ if (__DEV__ && !currentScope) die(0)31+ return currentScope!32+}33+34+function createScope(35+ parent_: ImmerScope | undefined,36+ immer_: Immer37+): ImmerScope {38+ return {39+ drafts_: [],40+ parent_,41+ immer_,42 // Whenever the modified draft contains a draft from another scope, we43 // need to prevent auto-freezing so the unowned draft can be finalized.34- this.canAutoFreeze_ = true44+ canAutoFreeze_: true,45+ unfinalizedDrafts_: 046 }47+}48 37- usePatches_(patchListener?: PatchListener) {38- if (patchListener) {39- getPlugin("Patches") // assert we have the plugin40- this.patches_ = []41- this.inversePatches_ = []42- this.patchListener_ = patchListener43- }49+export function usePatchesInScope(50+ scope: ImmerScope,51+ patchListener?: PatchListener52+) {53+ if (patchListener) {54+ getPlugin("Patches") // assert we have the plugin55+ scope.patches_ = []56+ scope.inversePatches_ = []57+ scope.patchListener_ = patchListener58 }59+}60 46- revoke_() {47- this.leave_()48- this.drafts_.forEach(revoke)49- // @ts-ignore50- this.drafts_ = null51- }61+export function revokeScope(scope: ImmerScope) {62+ leaveScope(scope)63+ scope.drafts_.forEach(revokeDraft)64+ // @ts-ignore65+ scope.drafts_ = null66+}67 53- leave_() {54- if (this === ImmerScope.current_) {55- ImmerScope.current_ = this.parent_56- }68+export function leaveScope(scope: ImmerScope) {69+ if (scope === currentScope) {70+ currentScope = scope.parent_71 }72+}73 59- static enter_(immer: Immer) {60- const scope = new ImmerScope(ImmerScope.current_, immer)61- ImmerScope.current_ = scope62- return scope63- }74+export function enterScope(immer: Immer) {75+ return (currentScope = createScope(currentScope, immer))76 }77 66-function revoke(draft: Drafted) {78+function revokeDraft(draft: Drafted) {79 const state: ImmerState = draft[DRAFT_STATE]80 if (81 state.type_ === ProxyTypeProxyObject ||82 state.type_ === ProxyTypeProxyArray4 unmodified linesmodified:src/plugins/es5.ts+2 added-1 removed
16 unmodified lines17 createProxy,18 ProxyTypeES5Array,19 ProxyTypeES5Object,20 AnyObject,21+ getCurrentScope,22 die23 } from "../internal"24 25 type ES5State = ES5ArrayState | ES5ObjectState35 unmodified lines61 })62 63 const state: ES5ObjectState | ES5ArrayState = {64 type_: isArray ? ProxyTypeES5Array : (ProxyTypeES5Object as any),64- scope_: parent ? parent.scope_ : ImmerScope.current_!,65+ scope_: parent ? parent.scope_ : getCurrentScope(),66 modified_: false,67 finalizing_: false,68 finalized_: false,69 assigned_: {},244 unmodified linesmodified:src/plugins/mapset.ts+3 added-3 removed
4 unmodified lines5 AnySet,6 MapState,7 SetState,8 DRAFT_STATE,9- ImmerScope,9+ getCurrentScope,10 latest,11 iteratorSymbol,12 isDraftable,13 createProxy,36 unmodified lines50 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {51 this[DRAFT_STATE] = {52 type_: ProxyTypeMap,53 parent_: parent,54- scope_: parent ? parent.scope_ : ImmerScope.current_!,54+ scope_: parent ? parent.scope_ : getCurrentScope()!,55 modified_: false,56 finalized_: false,57 copy_: undefined,58 assigned_: undefined,144 unmodified lines203 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {204 this[DRAFT_STATE] = {205 type_: ProxyTypeSet,206 parent_: parent,207- scope_: parent ? parent.scope_ : ImmerScope.current_!,207+ scope_: parent ? parent.scope_ : getCurrentScope()!,208 modified_: false,209 finalized_: false,210 copy_: undefined,211 base_: target,128 unmodified linesmodified:notes.txt+1 added-1 removed
12 unmodified lines13 [ ] check if we can create more local vars14 [x] verify performance15 [ ] kill ownKeys?16 [x] kill enumerations17-[ ] check todos17+[x] check todos18 [ ] ~~kill marchChangesSweep recursive if possible and no perf impact~~ does have perf impact19 [ ] optimize each closures away? pass in additional args?20 [x] document performance tip: if externally added data is frozen, it doesn't recurse (also update related issue) #46521 [x] put some invariants behind __DEV__96 unmodified lines