Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than ba24aab point at it.

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 1–7why, one step back
1import {
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 back
8 ArchType,

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 9–13why, one step back
9 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 back
14 PluginPatches,
15 ArrayMethodsPlugin,16 PluginArrayMethods

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 17–23why, one step back
17} 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 back
24 patchPlugin_?: PatchesPlugin
25 mapSetPlugin_?: MapSetPlugin

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

line 26why, one step back
26 arrayMethodsPlugin_?: ArrayMethodsPlugin

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 27–32why, one step back
27 canAutoFreeze_: boolean
28 drafts_: any[]29 parent_?: ImmerScope30 patchListener_?: PatchListener31 immer_: Immer32 unfinalizedDrafts_: number

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 33–34why, one step back
33 handledSet_: Set<any>
34 processedForPatches_: Set<any>

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 35–38why, one step back
35}
3637let currentScope: ImmerScope | undefined38

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 39why, one step back
39export let getCurrentScope = () => currentScope!

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 40why, one step back
40

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 41why, one step back
41let createScope = (

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 42–43why, one step back
42 parent_: ImmerScope | undefined,
43 immer_: Immer

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 44–55why, one step back
44): 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 back
56 : 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 back
59 : undefined
60})

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 61–66why, one step back
61
62export 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 back
67 scope.patchPlugin_ = getPlugin(PluginPatches) // assert we have the plugin

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 68–86why, one step back
68 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}86

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 87–88why, one step back
87export let enterScope = (immer: Immer) =>
88 (currentScope = createScope(currentScope, immer))

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 89–91why, one step back
89
90function revokeDraft(draft: Drafted) {91 const state: ImmerState = draft[DRAFT_STATE]

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 92why, one step back
92 if (state.type_ === ArchType.Object || state.type_ === ArchType.Array)

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 93–95why, one step back
93 state.revoke_()
94 else state.revoked_ = true95}