|
Last change
on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.2 KB
|
| Line | |
|---|
| 1 | import {
|
|---|
| 2 | die,
|
|---|
| 3 | isDraft,
|
|---|
| 4 | shallowCopy,
|
|---|
| 5 | each,
|
|---|
| 6 | DRAFT_STATE,
|
|---|
| 7 | set,
|
|---|
| 8 | ImmerState,
|
|---|
| 9 | isDraftable,
|
|---|
| 10 | isFrozen
|
|---|
| 11 | } from "../internal"
|
|---|
| 12 |
|
|---|
| 13 | /** Takes a snapshot of the current state of a draft and finalizes it (but without freezing). This is a great utility to print the current state during debugging (no Proxies in the way). The output of current can also be safely leaked outside the producer. */
|
|---|
| 14 | export function current<T>(value: T): T
|
|---|
| 15 | export function current(value: any): any {
|
|---|
| 16 | if (!isDraft(value)) die(10, value)
|
|---|
| 17 | return currentImpl(value)
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | function currentImpl(value: any): any {
|
|---|
| 21 | if (!isDraftable(value) || isFrozen(value)) return value
|
|---|
| 22 | const state: ImmerState | undefined = value[DRAFT_STATE]
|
|---|
| 23 | let copy: any
|
|---|
| 24 | let strict = true // Default to strict for compatibility
|
|---|
| 25 | if (state) {
|
|---|
| 26 | if (!state.modified_) return state.base_
|
|---|
| 27 | // Optimization: avoid generating new drafts during copying
|
|---|
| 28 | state.finalized_ = true
|
|---|
| 29 | copy = shallowCopy(value, state.scope_.immer_.useStrictShallowCopy_)
|
|---|
| 30 | strict = state.scope_.immer_.shouldUseStrictIteration()
|
|---|
| 31 | } else {
|
|---|
| 32 | copy = shallowCopy(value, true)
|
|---|
| 33 | }
|
|---|
| 34 | // recurse
|
|---|
| 35 | each(
|
|---|
| 36 | copy,
|
|---|
| 37 | (key, childValue) => {
|
|---|
| 38 | set(copy, key, currentImpl(childValue))
|
|---|
| 39 | },
|
|---|
| 40 | strict
|
|---|
| 41 | )
|
|---|
| 42 | if (state) {
|
|---|
| 43 | state.finalized_ = false
|
|---|
| 44 | }
|
|---|
| 45 | return copy
|
|---|
| 46 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.