scope.ts
This file
Path
src/scope.ts
68 lines · 1.5 KiB · TypeScript
Last changed by
Optimization: stop recursing if there are no drafts leftMichel Weststratebefore adoptionebda6f8
History of this fileTake it
Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 1why, one step back1import {Patch, PatchListener, Drafted, Immer, DRAFT_STATE} from "./internal"fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 2why, one step back2import {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 3–6why, one step back3 ImmerState,4 ProxyTypeProxyObject,5 ProxyTypeProxyArray6} from "./types-internal"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 7–8why, one step back78/** Each scope represents a `produce` call. */use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 9why, one step back9// TODO: non-class?feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 10why, one step back10export class ImmerScope {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 11why, one step back11 static current_?: ImmerScopefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 12why, one step back12Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 13–19why, one step back13 patches_?: Patch[]14 inversePatches_?: Patch[]15 canAutoFreeze_: boolean16 drafts_: any[]17 parent_?: ImmerScope18 patchListener_?: PatchListener19 immer_: ImmerOptimization: stop recursing if there are no drafts leftMichel Weststratebefore adoptionebda6f8
line 20why, one step back20 unfinalizedDrafts_ = 0feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 21why, one step back21feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 22why, one step back22 constructor(parent: ImmerScope | undefined, immer: Immer) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 23–25why, one step back23 this.drafts_ = []24 this.parent_ = parent25 this.immer_ = immerfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 26–28why, one step back2627 // Whenever the modified draft contains a draft from another scope, we28 // need to prevent auto-freezing so the unowned draft can be finalized.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 29why, one step back29 this.canAutoFreeze_ = truefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 30–31why, one step back30 }31Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 32why, one step back32 usePatches_(patchListener?: PatchListener) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 33why, one step back33 if (patchListener) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 34–36why, one step back34 this.patches_ = []35 this.inversePatches_ = []36 this.patchListener_ = patchListenerfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 37–39why, one step back37 }38 }39Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 40–42why, one step back40 revoke_() {41 this.leave_()42 this.drafts_.forEach(revoke)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 43why, one step back43 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 44why, one step back44 this.drafts_ = nullfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 45–46why, one step back45 }46Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 47–49why, one step back47 leave_() {48 if (this === ImmerScope.current_) {49 ImmerScope.current_ = this.parent_feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 50–52why, one step back50 }51 }52Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 53–55why, one step back53 static enter_(immer: Immer) {54 const scope = new ImmerScope(ImmerScope.current_, immer)55 ImmerScope.current_ = scopefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 56–59why, one step back56 return scope57 }58}59feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 60why, one step back60function revoke(draft: Drafted) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 61why, one step back61 const state: ImmerState = draft[DRAFT_STATE]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 62why, one step back62 if (Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 63–64why, one step back63 state.type_ === ProxyTypeProxyObject ||64 state.type_ === ProxyTypeProxyArrayfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 65why, one step back65 )Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 66–67why, one step back66 state.revoke_()67 else state.revoked_ = truefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 68why, one step back68}