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 fileTake it
Moving files into foldersMichel Weststratebefore adoptionab49edc
lines 1–8why, one step back1import {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 back9 ProxyTypeProxyArray,10 getPluginMoving files into foldersMichel Weststratebefore adoptionab49edc
line 11why, one step back11} from "../internal"unclassed scopeMichel Weststratebefore adoption7a38af9
line 12why, one step back12import {die} from "../utils/errors"Moving files into foldersMichel Weststratebefore adoptionab49edc
lines 13–15why, one step back1314/** Each scope represents a `produce` call. */15unclassed scopeMichel Weststratebefore adoption7a38af9
line 16why, one step back16export interface ImmerScope {Moving files into foldersMichel Weststratebefore adoptionab49edc
lines 17–23why, one step back17 patches_?: Patch[]18 inversePatches_?: Patch[]19 canAutoFreeze_: boolean20 drafts_: any[]21 parent_?: ImmerScope22 patchListener_?: PatchListener23 immer_: Immerunclassed scopeMichel Weststratebefore adoption7a38af9
lines 24–25why, one step back24 unfinalizedDrafts_: number25}Moving files into foldersMichel Weststratebefore adoptionab49edc
line 26why, one step back26unclassed scopeMichel Weststratebefore adoption7a38af9
line 27why, one step back27let currentScope: ImmerScope | undefinedMoving files into foldersMichel Weststratebefore adoptionab49edc
line 28why, one step back28unclassed scopeMichel Weststratebefore adoption7a38af9
lines 29–41why, one step back29export 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 back42 // Whenever the modified draft contains a draft from another scope, we43 // need to prevent auto-freezing so the unowned draft can be finalized.unclassed scopeMichel Weststratebefore adoption7a38af9
lines 44–45why, one step back44 canAutoFreeze_: true,45 unfinalizedDrafts_: 0Moving files into foldersMichel Weststratebefore adoptionab49edc
line 46why, one step back46 }unclassed scopeMichel Weststratebefore adoption7a38af9
line 47why, one step back47}Moving files into foldersMichel Weststratebefore adoptionab49edc
line 48why, one step back48unclassed scopeMichel Weststratebefore adoption7a38af9
lines 49–57why, one step back49export 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_ = patchListenerMoving files into foldersMichel Weststratebefore adoptionab49edc
line 58why, one step back58 }unclassed scopeMichel Weststratebefore adoption7a38af9
line 59why, one step back59}Moving files into foldersMichel Weststratebefore adoptionab49edc
line 60why, one step back60unclassed scopeMichel Weststratebefore adoption7a38af9
lines 61–66why, one step back61export 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 back67unclassed scopeMichel Weststratebefore adoption7a38af9
lines 68–70why, one step back68export function leaveScope(scope: ImmerScope) {69 if (scope === currentScope) {70 currentScope = scope.parent_Moving files into foldersMichel Weststratebefore adoptionab49edc
line 71why, one step back71 }unclassed scopeMichel Weststratebefore adoption7a38af9
line 72why, one step back72}Moving files into foldersMichel Weststratebefore adoptionab49edc
line 73why, one step back73unclassed scopeMichel Weststratebefore adoption7a38af9
lines 74–75why, one step back74export function enterScope(immer: Immer) {75 return (currentScope = createScope(currentScope, immer))Moving files into foldersMichel Weststratebefore adoptionab49edc
lines 76–77why, one step back76}77unclassed scopeMichel Weststratebefore adoption7a38af9
line 78why, one step back78function revokeDraft(draft: Drafted) {Moving files into foldersMichel Weststratebefore adoptionab49edc
lines 79–86why, one step back79 const state: ImmerState = draft[DRAFT_STATE]80 if (81 state.type_ === ProxyTypeProxyObject ||82 state.type_ === ProxyTypeProxyArray83 )84 state.revoke_()85 else state.revoked_ = true86}