scope.ts
This file
Path
src/scope.ts
69 lines · 1.3 KiB · TypeScript
Last changed by
use invariant -321 bytesMichel Weststratebefore adoptionac57da5
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"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 9–10why, one step back910/** Each scope represents a `produce` call. */use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 11why, one step back11// TODO: non-class?feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 12–20why, one step back12export class ImmerScope {13 static current?: ImmerScope1415 patches?: Patch[]16 inversePatches?: Patch[]17 canAutoFreeze: boolean18 drafts: any[]19 parent?: ImmerScope20 patchListener?: PatchListenerfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 21why, one step back21 immer: Immerfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 22why, one step back22feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 23why, one step back23 constructor(parent: ImmerScope | undefined, immer: Immer) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 24–25why, one step back24 this.drafts = []25 this.parent = parentfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 26why, one step back26 this.immer = immerfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 27–32why, one step back2728 // Whenever the modified draft contains a draft from another scope, we29 // need to prevent auto-freezing so the unowned draft can be finalized.30 this.canAutoFreeze = true31 }32feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 33why, one step back33 usePatches(patchListener?: PatchListener) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 34–44why, one step back34 if (patchListener) {35 this.patches = []36 this.inversePatches = []37 this.patchListener = patchListener38 }39 }4041 revoke() {42 this.leave()43 this.drafts.forEach(revoke)44 // @ts-ignorefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 45why, one step back45 this.drafts = nullfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 46–53why, one step back46 }4748 leave() {49 if (this === ImmerScope.current) {50 ImmerScope.current = this.parent51 }52 }53feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 54–55why, one step back54 static enter(immer: Immer) {55 const scope = new ImmerScope(ImmerScope.current, immer)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 56–60why, one step back56 ImmerScope.current = scope57 return scope58 }59}60feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 61–68why, one step back61function revoke(draft: Drafted) {62 const state = draft[DRAFT_STATE]63 if (64 state.type === ProxyType.ProxyObject ||65 state.type === ProxyType.ProxyArray66 )67 state.revoke()68 else state.revoked = truefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 69why, one step back69}