scope.ts
This file
Path
src/core/scope.ts
95 lines · 2.1 KiB · TypeScript
Last changed by
feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
History of this fileTake it
The walk stopped after 200 commits: lines older than d91a659 point at it.
fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 1–7why, one step back1import {2 Patch,3 PatchListener,4 Drafted,5 Immer,6 DRAFT_STATE,7 ImmerState,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 8why, one step back8 ArchType,perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 9–13why, one step back9 getPlugin,10 PatchesPlugin,11 MapSetPlugin,12 isPluginLoaded,13 PluginMapSet,feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
lines 14–16why, one step back14 PluginPatches,15 ArrayMethodsPlugin,16 PluginArrayMethodsfix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 17–23why, one step back17} from "../internal"1819/** Each scope represents a `produce` call. */2021export interface ImmerScope {22 patches_?: Patch[]23 inversePatches_?: Patch[]perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 24–25why, one step back24 patchPlugin_?: PatchesPlugin25 mapSetPlugin_?: MapSetPluginfeat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
line 26why, one step back26 arrayMethodsPlugin_?: ArrayMethodsPluginfix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 27–32why, one step back27 canAutoFreeze_: boolean28 drafts_: any[]29 parent_?: ImmerScope30 patchListener_?: PatchListener31 immer_: Immer32 unfinalizedDrafts_: numberperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 33–34why, one step back33 handledSet_: Set<any>34 processedForPatches_: Set<any>fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 35–38why, one step back35}3637let currentScope: ImmerScope | undefined38perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 39why, one step back39export let getCurrentScope = () => currentScope!fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
line 40why, one step back40perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 41why, one step back41let createScope = (fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 42–43why, one step back42 parent_: ImmerScope | undefined,43 immer_: Immerperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 44–55why, one step back44): ImmerScope => ({45 drafts_: [],46 parent_,47 immer_,48 // Whenever the modified draft contains a draft from another scope, we49 // need to prevent auto-freezing so the unowned draft can be finalized.50 canAutoFreeze_: true,51 unfinalizedDrafts_: 0,52 handledSet_: new Set(),53 processedForPatches_: new Set(),54 mapSetPlugin_: isPluginLoaded(PluginMapSet)55 ? getPlugin(PluginMapSet)feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
lines 56–58why, one step back56 : undefined,57 arrayMethodsPlugin_: isPluginLoaded(PluginArrayMethods)58 ? getPlugin(PluginArrayMethods)perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 59–60why, one step back59 : undefined60})fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 61–66why, one step back6162export function usePatchesInScope(63 scope: ImmerScope,64 patchListener?: PatchListener65) {66 if (patchListener) {perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 67why, one step back67 scope.patchPlugin_ = getPlugin(PluginPatches) // assert we have the pluginfix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 68–86why, one step back68 scope.patches_ = []69 scope.inversePatches_ = []70 scope.patchListener_ = patchListener71 }72}7374export function revokeScope(scope: ImmerScope) {75 leaveScope(scope)76 scope.drafts_.forEach(revokeDraft)77 // @ts-ignore78 scope.drafts_ = null79}8081export function leaveScope(scope: ImmerScope) {82 if (scope === currentScope) {83 currentScope = scope.parent_84 }85}86perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 87–88why, one step back87export let enterScope = (immer: Immer) =>88 (currentScope = createScope(currentScope, immer))fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 89–91why, one step back8990function revokeDraft(draft: Drafted) {91 const state: ImmerState = draft[DRAFT_STATE]Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 92why, one step back92 if (state.type_ === ArchType.Object || state.type_ === ArchType.Array)fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 93–95why, one step back93 state.revoke_()94 else state.revoked_ = true95}