Skip to content
Myr

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 file

Take it

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 1–8why, one step back
1import {
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 back
9
10/** Each scope represents a `produce` call. */

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 11why, one step back
11// TODO: non-class?

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 12–20why, one step back
12export class ImmerScope {
13 static current?: ImmerScope1415 patches?: Patch[]16 inversePatches?: Patch[]17 canAutoFreeze: boolean18 drafts: any[]19 parent?: ImmerScope20 patchListener?: PatchListener

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 21why, one step back
21 immer: Immer

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 22why, one step back
22

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 23why, one step back
23 constructor(parent: ImmerScope | undefined, immer: Immer) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 24–25why, one step back
24 this.drafts = []
25 this.parent = parent

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 26why, one step back
26 this.immer = immer

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 27–32why, one step back
27
28 // 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 }32

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 33why, one step back
33 usePatches(patchListener?: PatchListener) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 34–44why, one step back
34 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-ignore

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 45why, one step back
45 this.drafts = null

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 46–53why, one step back
46 }
4748 leave() {49 if (this === ImmerScope.current) {50 ImmerScope.current = this.parent51 }52 }53

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 54–55why, one step back
54 static enter(immer: Immer) {
55 const scope = new ImmerScope(ImmerScope.current, immer)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 56–60why, one step back
56 ImmerScope.current = scope
57 return scope58 }59}60

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 61–68why, one step back
61function 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 = true

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 69why, one step back
69}