Skip to content
Myr

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

7 files+72 added−54 removed

modified:src/core/finalize.ts+4 added-3 removed
modified:src/core/immerClass.ts+16 added-12 removed
2 unmodified lines
3 IProduce,4 ImmerState,5 Drafted,6 isDraftable,7 ImmerScope,7 processResult,8 NOTHING,9 Patch,10 Objectish,
8 unmodified lines
19 freeze,20 getPlugin,21 die,22 hasProxies,24 isMinified23 isMinified,24 enterScope,25 revokeScope,26 leaveScope,27 usePatchesInScope,28 getCurrentScope29} from "../internal"3031interface ProducersFns {32 produce: IProduce
56 unmodified lines
89 let result9091 // 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 undefined
18 unmodified lines
139 }140141 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 }149150 finishDraft<D extends Draft<any>>(
5 unmodified lines
156 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 }163164 /**
55 unmodified lines
220 : immer.useProxies_221 ? createProxyProxy(value, parent)222 : getPlugin("ES5").createES5Proxy_(value, parent)223220 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 lines
modified:src/core/proxy.ts+2 added-2 removed
10 unmodified lines
11 Drafted,12 AnyObject,13 AnyArray,14 Objectish,15 ImmerScope,15 getCurrentScope,16 DRAFT_STATE,17 die,18 createProxy,19 ProxyTypeProxyObject,
39 unmodified lines
59 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 lines
modified:src/core/scope.ts+44 added-32 removed
7 unmodified lines
8 ProxyTypeProxyObject,9 ProxyTypeProxyArray,10 getPlugin11} from "../internal"12import {die} from "../utils/errors"1314/** Each scope represents a `produce` call. */14// TODO: non-class?15export class ImmerScope {16 static current_?: ImmerScope1516export interface ImmerScope {17 patches_?: Patch[]18 inversePatches_?: Patch[]19 canAutoFreeze_: boolean20 drafts_: any[]21 parent_?: ImmerScope22 patchListener_?: PatchListener23 immer_: Immer25 unfinalizedDrafts_ = 024 unfinalizedDrafts_: number25}2627 constructor(parent: ImmerScope | undefined, immer: Immer) {28 this.drafts_ = []29 this.parent_ = parent30 this.immer_ = immer27let currentScope: ImmerScope | undefined2829export function getCurrentScope() {30 if (__DEV__ && !currentScope) die(0)31 return currentScope!32}3334function 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}4837 usePatches_(patchListener?: PatchListener) {38 if (patchListener) {39 getPlugin("Patches") // assert we have the plugin40 this.patches_ = []41 this.inversePatches_ = []42 this.patchListener_ = patchListener43 }49export 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}6046 revoke_() {47 this.leave_()48 this.drafts_.forEach(revoke)49 // @ts-ignore50 this.drafts_ = null51 }61export function revokeScope(scope: ImmerScope) {62 leaveScope(scope)63 scope.drafts_.forEach(revokeDraft)64 // @ts-ignore65 scope.drafts_ = null66}6753 leave_() {54 if (this === ImmerScope.current_) {55 ImmerScope.current_ = this.parent_56 }68export function leaveScope(scope: ImmerScope) {69 if (scope === currentScope) {70 currentScope = scope.parent_71 }72}7359 static enter_(immer: Immer) {60 const scope = new ImmerScope(ImmerScope.current_, immer)61 ImmerScope.current_ = scope62 return scope63 }74export function enterScope(immer: Immer) {75 return (currentScope = createScope(currentScope, immer))76}7766function revoke(draft: Drafted) {78function revokeDraft(draft: Drafted) {79 const state: ImmerState = draft[DRAFT_STATE]80 if (81 state.type_ === ProxyTypeProxyObject ||82 state.type_ === ProxyTypeProxyArray
4 unmodified lines
modified:src/plugins/es5.ts+2 added-1 removed
16 unmodified lines
17 createProxy,18 ProxyTypeES5Array,19 ProxyTypeES5Object,20 AnyObject,21 getCurrentScope,22 die23} from "../internal"2425type ES5State = ES5ArrayState | ES5ObjectState
35 unmodified lines
61 })6263 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 lines
modified:src/plugins/mapset.ts+3 added-3 removed
4 unmodified lines
5 AnySet,6 MapState,7 SetState,8 DRAFT_STATE,9 ImmerScope,9 getCurrentScope,10 latest,11 iteratorSymbol,12 isDraftable,13 createProxy,
36 unmodified lines
50 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 lines
203 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 lines
modified:notes.txt+1 added-1 removed
12 unmodified lines
13[ ] 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