(get_value, inspector, show_stack = false)
| 10 | * @param {boolean} show_stack |
| 11 | */ |
| 12 | export function inspect(get_value, inspector, show_stack = false) { |
| 13 | validate_effect('$inspect'); |
| 14 | |
| 15 | let initial = true; |
| 16 | let error = /** @type {any} */ (UNINITIALIZED); |
| 17 | |
| 18 | // Inspect effects runs synchronously so that we can capture useful |
| 19 | // stack traces. As a consequence, reading the value might result |
| 20 | // in an error (an `$inspect(object.property)` will run before the |
| 21 | // `{#if object}...{/if}` that contains it) |
| 22 | eager_effect(() => { |
| 23 | error = UNINITIALIZED; |
| 24 | |
| 25 | try { |
| 26 | var value = get_value(); |
| 27 | } catch (e) { |
| 28 | error = e; |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | var snap = snapshot(value, true, true); |
| 33 | untrack(() => { |
| 34 | if (show_stack) { |
| 35 | inspector(...snap); |
| 36 | |
| 37 | if (!initial) { |
| 38 | const stack = get_error('$inspect(...)'); |
| 39 | if (stack) { |
| 40 | // eslint-disable-next-line no-console |
| 41 | console.groupCollapsed('stack trace'); |
| 42 | // eslint-disable-next-line no-console |
| 43 | console.log(stack); |
| 44 | // eslint-disable-next-line no-console |
| 45 | console.groupEnd(); |
| 46 | } |
| 47 | } |
| 48 | } else { |
| 49 | inspector(initial ? 'init' : 'update', ...snap); |
| 50 | } |
| 51 | }); |
| 52 | |
| 53 | initial = false; |
| 54 | }); |
| 55 | |
| 56 | // If an error occurs, we store it (along with its stack trace). |
| 57 | // If the render effect subsequently runs, we log the error, |
| 58 | // but if it doesn't run it's because the `$inspect` was |
| 59 | // destroyed, meaning we don't need to bother |
| 60 | render_effect(() => { |
| 61 | try { |
| 62 | // call `get_value` so that this runs alongside the inspect effect |
| 63 | get_value(); |
| 64 | } catch { |
| 65 | // ignore |
| 66 | } |
| 67 | |
| 68 | if (error !== UNINITIALIZED) { |
| 69 | // eslint-disable-next-line no-console |
no test coverage detected