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
fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 1–8why, one step back1import {2 Patch,3 PatchListener,4 Drafted,5 ProxyType,6 Immer,7 DRAFT_STATE8} from "./internal"Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 9why, one step back9import {ImmerState} from "./types-internal"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 10–11why, one step back1011/** Each scope represents a `produce` call. */use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 12why, one step back12// TODO: non-class?feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 13why, one step back13export class ImmerScope {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 14why, one step back14 static current_?: ImmerScopefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 15why, one step back15Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 16–22why, one step back16 patches_?: Patch[]17 inversePatches_?: Patch[]18 canAutoFreeze_: boolean19 drafts_: any[]20 parent_?: ImmerScope21 patchListener_?: PatchListener22 immer_: Immerfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 23why, one step back23feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 24why, one step back24 constructor(parent: ImmerScope | undefined, immer: Immer) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 25–27why, one step back25 this.drafts_ = []26 this.parent_ = parent27 this.immer_ = immerfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 28–30why, one step back2829 // Whenever the modified draft contains a draft from another scope, we30 // need to prevent auto-freezing so the unowned draft can be finalized.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 31why, one step back31 this.canAutoFreeze_ = truefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 32–33why, one step back32 }33Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 34why, one step back34 usePatches_(patchListener?: PatchListener) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 35why, one step back35 if (patchListener) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 36–38why, one step back36 this.patches_ = []37 this.inversePatches_ = []38 this.patchListener_ = patchListenerfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 39–41why, one step back39 }40 }41Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 42–44why, one step back42 revoke_() {43 this.leave_()44 this.drafts_.forEach(revoke)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 45why, one step back45 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 46why, one step back46 this.drafts_ = nullfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 47–48why, one step back47 }48Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 49–51why, one step back49 leave_() {50 if (this === ImmerScope.current_) {51 ImmerScope.current_ = this.parent_feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 52–54why, one step back52 }53 }54Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 55–57why, one step back55 static enter_(immer: Immer) {56 const scope = new ImmerScope(ImmerScope.current_, immer)57 ImmerScope.current_ = scopefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 58–61why, one step back58 return scope59 }60}61feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 62why, one step back62function revoke(draft: Drafted) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 63why, one step back63 const state: ImmerState = draft[DRAFT_STATE]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 64why, one step back64 if (Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 65–66why, one step back65 state.type_ === ProxyType.ProxyObject ||66 state.type_ === ProxyType.ProxyArrayfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 67why, one step back67 )Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 68–69why, one step back68 state.revoke_()69 else state.revoked_ = truefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 70why, one step back70}