/*--------------------------------------------------------------------------------------------- * Licensed under the MIT License. * * @Author: fantasticsoul *--------------------------------------------------------------------------------------------*/ import { IS_RAW } from '../support/consts'; import { isPrimitive } from '../support/util'; import { deepCopy } from './copy'; import { getDraftMeta } from './meta'; export function original(mayDraftProxy: T): T { if (!mayDraftProxy) { return mayDraftProxy; } const meta = getDraftMeta(mayDraftProxy); // use ternary conditional operator instead of meta?.self // avoid generating redundant compiled code const self = meta ? meta.self : mayDraftProxy; return self as T; } export function current(mayDraftProxy: T): T { if (!mayDraftProxy) { return mayDraftProxy; } const meta = getDraftMeta(mayDraftProxy); if (!meta) { return mayDraftProxy; } return deepCopy(meta.copy || meta.self) as T; } export function markRaw(rawVal: T): T { if (!rawVal || isPrimitive(rawVal)) return rawVal; (rawVal as any)[IS_RAW] = true; return rawVal; }