Skip to content
Myr

scope.ts

This file

Path

src/core/scope.ts

72 lines · 1.4 KiB · TypeScript

Last changed by

Moving files into foldersMichel Weststratebefore adoptionab49edc

History of this file

Take it

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 1–72why, one step back
1import {
2 Patch,3 PatchListener,4 Drafted,5 Immer,6 DRAFT_STATE,7 ImmerState,8 ProxyTypeProxyObject,9 ProxyTypeProxyArray10} from "../internal"1112/** Each scope represents a `produce` call. */13// TODO: non-class?14export class ImmerScope {15 static current_?: ImmerScope1617 patches_?: Patch[]18 inversePatches_?: Patch[]19 canAutoFreeze_: boolean20 drafts_: any[]21 parent_?: ImmerScope22 patchListener_?: PatchListener23 immer_: Immer24 unfinalizedDrafts_ = 02526 constructor(parent: ImmerScope | undefined, immer: Immer) {27 this.drafts_ = []28 this.parent_ = parent29 this.immer_ = immer3031 // Whenever the modified draft contains a draft from another scope, we32 // need to prevent auto-freezing so the unowned draft can be finalized.33 this.canAutoFreeze_ = true34 }3536 usePatches_(patchListener?: PatchListener) {37 if (patchListener) {38 this.patches_ = []39 this.inversePatches_ = []40 this.patchListener_ = patchListener41 }42 }4344 revoke_() {45 this.leave_()46 this.drafts_.forEach(revoke)47 // @ts-ignore48 this.drafts_ = null49 }5051 leave_() {52 if (this === ImmerScope.current_) {53 ImmerScope.current_ = this.parent_54 }55 }5657 static enter_(immer: Immer) {58 const scope = new ImmerScope(ImmerScope.current_, immer)59 ImmerScope.current_ = scope60 return scope61 }62}6364function revoke(draft: Drafted) {65 const state: ImmerState = draft[DRAFT_STATE]66 if (67 state.type_ === ProxyTypeProxyObject ||68 state.type_ === ProxyTypeProxyArray69 )70 state.revoke_()71 else state.revoked_ = true72}