(value)
| 38 | * @returns {T} |
| 39 | */ |
| 40 | export function proxy(value) { |
| 41 | // if non-proxyable, or is already a proxy, return `value` |
| 42 | if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) { |
| 43 | return value; |
| 44 | } |
| 45 | |
| 46 | const prototype = get_prototype_of(value); |
| 47 | |
| 48 | if (prototype !== object_prototype && prototype !== array_prototype) { |
| 49 | return value; |
| 50 | } |
| 51 | |
| 52 | /** @type {Map<any, Source<any>>} */ |
| 53 | var sources = new Map(); |
| 54 | var is_proxied_array = is_array(value); |
| 55 | var version = source(0); |
| 56 | |
| 57 | var stack = DEV && tracing_mode_flag ? get_error('created at') : null; |
| 58 | var parent_version = update_version; |
| 59 | |
| 60 | /** |
| 61 | * Executes the proxy in the context of the reaction it was originally created in, if any |
| 62 | * @template T |
| 63 | * @param {() => T} fn |
| 64 | */ |
| 65 | var with_parent = (fn) => { |
| 66 | if (update_version === parent_version) { |
| 67 | return fn(); |
| 68 | } |
| 69 | |
| 70 | // child source is being created after the initial proxy — |
| 71 | // prevent it from being associated with the current reaction |
| 72 | var reaction = active_reaction; |
| 73 | var version = update_version; |
| 74 | |
| 75 | set_active_reaction(null); |
| 76 | set_update_version(parent_version); |
| 77 | |
| 78 | var result = fn(); |
| 79 | |
| 80 | set_active_reaction(reaction); |
| 81 | set_update_version(version); |
| 82 | |
| 83 | return result; |
| 84 | }; |
| 85 | |
| 86 | if (is_proxied_array) { |
| 87 | // We need to create the length source eagerly to ensure that |
| 88 | // mutations to the array are properly synced with our proxy |
| 89 | sources.set('length', source(/** @type {any[]} */ (value).length, stack)); |
| 90 | if (DEV) { |
| 91 | value = /** @type {any} */ (inspectable_array(/** @type {any[]} */ (value))); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** Used in dev for $inspect.trace() */ |
| 96 | var path = ''; |
| 97 | let updating = false; |
no test coverage detected