scope.ts
This file
Path
src/scope.ts
70 lines · 1.4 KiB · TypeScript
Last changed by
Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import { Patch, PatchListener, Drafted, ProxyType, Immer, DRAFT_STATE} from "./internal"import {ImmerState} from "./types-internal"
/** Each scope represents a `produce` call. */// TODO: non-class?export class ImmerScope { static current_?: ImmerScope
patches_?: Patch[] inversePatches_?: Patch[] canAutoFreeze_: boolean drafts_: any[] parent_?: ImmerScope patchListener_?: PatchListener immer_: Immer
constructor(parent: ImmerScope | undefined, immer: Immer) { this.drafts_ = [] this.parent_ = parent this.immer_ = immer
// Whenever the modified draft contains a draft from another scope, we // need to prevent auto-freezing so the unowned draft can be finalized. this.canAutoFreeze_ = true }
usePatches_(patchListener?: PatchListener) { if (patchListener) { this.patches_ = [] this.inversePatches_ = [] this.patchListener_ = patchListener } }
revoke_() { this.leave_() this.drafts_.forEach(revoke) // @ts-ignore this.drafts_ = null }
leave_() { if (this === ImmerScope.current_) { ImmerScope.current_ = this.parent_ } }
static enter_(immer: Immer) { const scope = new ImmerScope(ImmerScope.current_, immer) ImmerScope.current_ = scope return scope }}
function revoke(draft: Drafted) { const state: ImmerState = draft[DRAFT_STATE] if ( state.type_ === ProxyType.ProxyObject || state.type_ === ProxyType.ProxyArray ) state.revoke_() else state.revoked_ = true}