Skip to content
Myr

scope.ts

This file

Path

src/core/scope.ts

74 lines · 1.5 KiB · TypeScript

Last changed by

Tests for pluginsMichel Weststratebefore adoptiondb16520

History of this file

Take it

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 1–8why, one step back
1import {
2 Patch,3 PatchListener,4 Drafted,5 Immer,6 DRAFT_STATE,7 ImmerState,8 ProxyTypeProxyObject,

Tests for pluginsMichel Weststratebefore adoptiondb16520

lines 9–10why, one step back
9 ProxyTypeProxyArray,
10 getPlugin

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 11–38why, one step back
11} from "../internal"
1213/** Each scope represents a `produce` call. */14// TODO: non-class?15export class ImmerScope {16 static current_?: ImmerScope1718 patches_?: Patch[]19 inversePatches_?: Patch[]20 canAutoFreeze_: boolean21 drafts_: any[]22 parent_?: ImmerScope23 patchListener_?: PatchListener24 immer_: Immer25 unfinalizedDrafts_ = 02627 constructor(parent: ImmerScope | undefined, immer: Immer) {28 this.drafts_ = []29 this.parent_ = parent30 this.immer_ = immer3132 // Whenever the modified draft contains a draft from another scope, we33 // need to prevent auto-freezing so the unowned draft can be finalized.34 this.canAutoFreeze_ = true35 }3637 usePatches_(patchListener?: PatchListener) {38 if (patchListener) {

Tests for pluginsMichel Weststratebefore adoptiondb16520

line 39why, one step back
39 getPlugin("Patches") // assert we have the plugin

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 40–74why, one step back
40 this.patches_ = []
41 this.inversePatches_ = []42 this.patchListener_ = patchListener43 }44 }4546 revoke_() {47 this.leave_()48 this.drafts_.forEach(revoke)49 // @ts-ignore50 this.drafts_ = null51 }5253 leave_() {54 if (this === ImmerScope.current_) {55 ImmerScope.current_ = this.parent_56 }57 }5859 static enter_(immer: Immer) {60 const scope = new ImmerScope(ImmerScope.current_, immer)61 ImmerScope.current_ = scope62 return scope63 }64}6566function revoke(draft: Drafted) {67 const state: ImmerState = draft[DRAFT_STATE]68 if (69 state.type_ === ProxyTypeProxyObject ||70 state.type_ === ProxyTypeProxyArray71 )72 state.revoke_()73 else state.revoked_ = true74}