Skip to content
Myr

scope.ts

This file

Path

src/core/scope.ts

86 lines · 1.7 KiB · TypeScript

Last changed by

unclassed scopeMichel Weststratebefore adoption7a38af9

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

line 11why, one step back
11} from "../internal"

unclassed scopeMichel Weststratebefore adoption7a38af9

line 12why, one step back
12import {die} from "../utils/errors"

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 13–15why, one step back
13
14/** Each scope represents a `produce` call. */15

unclassed scopeMichel Weststratebefore adoption7a38af9

line 16why, one step back
16export interface ImmerScope {

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 17–23why, one step back
17 patches_?: Patch[]
18 inversePatches_?: Patch[]19 canAutoFreeze_: boolean20 drafts_: any[]21 parent_?: ImmerScope22 patchListener_?: PatchListener23 immer_: Immer

unclassed scopeMichel Weststratebefore adoption7a38af9

lines 24–25why, one step back
24 unfinalizedDrafts_: number
25}

Moving files into foldersMichel Weststratebefore adoptionab49edc

line 26why, one step back
26

unclassed scopeMichel Weststratebefore adoption7a38af9

line 27why, one step back
27let currentScope: ImmerScope | undefined

Moving files into foldersMichel Weststratebefore adoptionab49edc

line 28why, one step back
28

unclassed scopeMichel Weststratebefore adoption7a38af9

lines 29–41why, one step back
29export function getCurrentScope() {
30 if (__DEV__ && !currentScope) die(0)31 return currentScope!32}3334function createScope(35 parent_: ImmerScope | undefined,36 immer_: Immer37): ImmerScope {38 return {39 drafts_: [],40 parent_,41 immer_,

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 42–43why, one step back
42 // Whenever the modified draft contains a draft from another scope, we
43 // need to prevent auto-freezing so the unowned draft can be finalized.

unclassed scopeMichel Weststratebefore adoption7a38af9

lines 44–45why, one step back
44 canAutoFreeze_: true,
45 unfinalizedDrafts_: 0

Moving files into foldersMichel Weststratebefore adoptionab49edc

line 46why, one step back
46 }

unclassed scopeMichel Weststratebefore adoption7a38af9

line 47why, one step back
47}

Moving files into foldersMichel Weststratebefore adoptionab49edc

line 48why, one step back
48

unclassed scopeMichel Weststratebefore adoption7a38af9

lines 49–57why, one step back
49export function usePatchesInScope(
50 scope: ImmerScope,51 patchListener?: PatchListener52) {53 if (patchListener) {54 getPlugin("Patches") // assert we have the plugin55 scope.patches_ = []56 scope.inversePatches_ = []57 scope.patchListener_ = patchListener

Moving files into foldersMichel Weststratebefore adoptionab49edc

line 58why, one step back
58 }

unclassed scopeMichel Weststratebefore adoption7a38af9

line 59why, one step back
59}

Moving files into foldersMichel Weststratebefore adoptionab49edc

line 60why, one step back
60

unclassed scopeMichel Weststratebefore adoption7a38af9

lines 61–66why, one step back
61export function revokeScope(scope: ImmerScope) {
62 leaveScope(scope)63 scope.drafts_.forEach(revokeDraft)64 // @ts-ignore65 scope.drafts_ = null66}

Moving files into foldersMichel Weststratebefore adoptionab49edc

line 67why, one step back
67

unclassed scopeMichel Weststratebefore adoption7a38af9

lines 68–70why, one step back
68export function leaveScope(scope: ImmerScope) {
69 if (scope === currentScope) {70 currentScope = scope.parent_

Moving files into foldersMichel Weststratebefore adoptionab49edc

line 71why, one step back
71 }

unclassed scopeMichel Weststratebefore adoption7a38af9

line 72why, one step back
72}

Moving files into foldersMichel Weststratebefore adoptionab49edc

line 73why, one step back
73

unclassed scopeMichel Weststratebefore adoption7a38af9

lines 74–75why, one step back
74export function enterScope(immer: Immer) {
75 return (currentScope = createScope(currentScope, immer))

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 76–77why, one step back
76}
77

unclassed scopeMichel Weststratebefore adoption7a38af9

line 78why, one step back
78function revokeDraft(draft: Drafted) {

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 79–86why, one step back
79 const state: ImmerState = draft[DRAFT_STATE]
80 if (81 state.type_ === ProxyTypeProxyObject ||82 state.type_ === ProxyTypeProxyArray83 )84 state.revoke_()85 else state.revoked_ = true86}