|
| 1 | +import { type ComputeFn } from "../compute"; |
| 2 | +import { type Get, type Readable } from "../typings"; |
| 3 | +import { isReadable, isWritable } from "../utils"; |
| 4 | +import { type WatchEffect } from "../watch"; |
| 5 | + |
| 6 | +export interface TraceConfig { |
| 7 | + name?: string; |
| 8 | + /** @default true */ |
| 9 | + printStack?: boolean; |
| 10 | + /** @default true */ |
| 11 | + defaultExpand?: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +export interface Trace { |
| 15 | + <T extends Readable>($: T, config?: TraceConfig): T; |
| 16 | + (effect: WatchEffect, config?: TraceConfig): WatchEffect; |
| 17 | + (fn: ComputeFn, config?: TraceConfig): ComputeFn; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Console logs trace information about the provided Readable or WatchEffect. |
| 22 | + * @example |
| 23 | + * ```ts |
| 24 | + * import { trace, writable, watch } from "@embra"; |
| 25 | + * const count$ = writable(0); |
| 26 | + * |
| 27 | + * trace(count$); |
| 28 | + * |
| 29 | + * watch(() => count$.value++); |
| 30 | + * |
| 31 | + * count$.set(1); |
| 32 | + * ``` |
| 33 | + */ |
| 34 | +export const trace: Trace = (x: unknown, config?: TraceConfig): any => { |
| 35 | + if (isReadable(x)) { |
| 36 | + return traceReadable(x, config); |
| 37 | + } |
| 38 | + if (typeof x === "function") { |
| 39 | + return traceWatch(x as ComputeFn | WatchEffect, config); |
| 40 | + } |
| 41 | + throw new TypeError("trace expects a Readable, WatchEffect, or ComputeFn as the first argument"); |
| 42 | +}; |
| 43 | + |
| 44 | +interface Info { |
| 45 | + value: any; |
| 46 | + version: number; |
| 47 | +} |
| 48 | + |
| 49 | +class Deps { |
| 50 | + public oldDeps = new Map<Readable, Info>(); |
| 51 | + public newDeps = new Map<Readable, Info>(); |
| 52 | + |
| 53 | + private readonly added: [Readable, Info][] = []; |
| 54 | + private readonly updated: [Readable, Info][] = []; |
| 55 | + private readonly unchanged: [Readable, Info][] = []; |
| 56 | + private readonly removed: [Readable, Info][] = []; |
| 57 | + |
| 58 | + public print(): void { |
| 59 | + for (const [dep, info] of this.newDeps) { |
| 60 | + const oldInfo = this.oldDeps.get(dep); |
| 61 | + if (oldInfo) { |
| 62 | + if (oldInfo.version !== info.version) { |
| 63 | + this.updated.push([dep, info]); |
| 64 | + } else { |
| 65 | + this.unchanged.push([dep, info]); |
| 66 | + } |
| 67 | + } else { |
| 68 | + this.added.push([dep, info]); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + for (const [dep, info] of this.oldDeps) { |
| 73 | + if (!this.newDeps.has(dep)) { |
| 74 | + this.removed.push([dep, info]); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + for (const [dep, info] of this.added) { |
| 79 | + console.log(`\x1b[32m(+)\x1b[0m ${this.getDepName(dep)} ->`, info.value); |
| 80 | + } |
| 81 | + for (const [dep, info] of this.updated) { |
| 82 | + console.log( |
| 83 | + `\x1b[33m(*)\x1b[0m ${this.getDepName(dep)} ->`, |
| 84 | + info.value, |
| 85 | + `<- previous:`, |
| 86 | + this.oldDeps.get(dep)?.value, |
| 87 | + ); |
| 88 | + } |
| 89 | + for (const [dep, info] of this.unchanged) { |
| 90 | + console.log(`\x1b[37m(=) ${this.getDepName(dep)}\x1b[0m ->`, info.value); |
| 91 | + } |
| 92 | + for (const [dep, info] of this.removed) { |
| 93 | + console.log(`\x1b[31m(-)\x1b[0m \x1b[9m${this.getDepName(dep)}\x1b[0m ->`, info.value); |
| 94 | + } |
| 95 | + |
| 96 | + this.added.length = this.updated.length = this.unchanged.length = this.removed.length = 0; |
| 97 | + } |
| 98 | + |
| 99 | + private depNameIndex = 1; |
| 100 | + private readonly depNames = new WeakMap<Readable, string>(); |
| 101 | + private getDepName(dep: Readable): string { |
| 102 | + if (this.depNames.has(dep)) { |
| 103 | + return this.depNames.get(dep)!; |
| 104 | + } |
| 105 | + const depType = isWritable(dep) ? "Writable" : isReadable(dep) ? "Readable" : "Unknown"; |
| 106 | + let name = |
| 107 | + dep.name || |
| 108 | + (this.depNameIndex <= 20 ? String.fromCodePoint(9311 + this.depNameIndex++) : `${this.depNameIndex++}`); |
| 109 | + name = `${depType}(${name})`; |
| 110 | + this.depNames.set(dep, name); |
| 111 | + return name; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +const traceReadable = <T extends Readable>($: T, config?: TraceConfig): T => { |
| 116 | + const deps = new Deps(); |
| 117 | + const $type = isWritable($) ? "Writable" : isReadable($) ? "Readable" : "Unknown"; |
| 118 | + const traceLocation = new Error("LocationDebugError"); |
| 119 | + let lastValue: any = traceLocation; |
| 120 | + |
| 121 | + $.subscribe(() => { |
| 122 | + ((config?.defaultExpand ?? true) ? console.group : console.groupCollapsed)( |
| 123 | + `\x1b[36m[embra]\x1b[0m trace ${$type}${$.name ? `(\x1b[33m${$.name}\x1b[0m)` : ""}${config?.name ? `: ${config.name}` : ""}`, |
| 124 | + ); |
| 125 | + |
| 126 | + if (config?.printStack ?? true) { |
| 127 | + console.groupCollapsed("trace location"); |
| 128 | + console.log(traceLocation); |
| 129 | + console.groupEnd(); |
| 130 | + |
| 131 | + console.groupCollapsed(`effect location`); |
| 132 | + console.trace(); |
| 133 | + console.groupEnd(); |
| 134 | + } |
| 135 | + |
| 136 | + if (lastValue === traceLocation) { |
| 137 | + console.log($.value); |
| 138 | + } else { |
| 139 | + console.log($.value, "<- previous:", lastValue); |
| 140 | + lastValue = $.value; |
| 141 | + } |
| 142 | + |
| 143 | + if ($.deps_) { |
| 144 | + for (const dep of $.deps_.keys()) { |
| 145 | + deps.newDeps.set(dep, { value: dep.value, version: dep.$version }); |
| 146 | + } |
| 147 | + |
| 148 | + deps.print(); |
| 149 | + |
| 150 | + [deps.oldDeps, deps.newDeps] = [deps.newDeps, deps.oldDeps]; |
| 151 | + deps.newDeps.clear(); |
| 152 | + } else { |
| 153 | + deps.oldDeps.clear(); |
| 154 | + deps.newDeps.clear(); |
| 155 | + } |
| 156 | + |
| 157 | + console.groupEnd(); |
| 158 | + }); |
| 159 | + |
| 160 | + return $; |
| 161 | +}; |
| 162 | + |
| 163 | +const traceWatch = <T extends WatchEffect | ComputeFn>(effect: T, config?: TraceConfig): T => { |
| 164 | + const deps = new Deps(); |
| 165 | + const traceLocation = new Error("TraceLocationDebugError"); |
| 166 | + |
| 167 | + return ((get: Get, ...args: [any]): any => { |
| 168 | + const myGet: Get = $ => { |
| 169 | + if (isReadable($)) { |
| 170 | + deps.newDeps.set($, { value: $.value, version: $.$version }); |
| 171 | + } |
| 172 | + return get($); |
| 173 | + }; |
| 174 | + |
| 175 | + const type = typeof args[0] === "function" ? "watch" : "compute"; |
| 176 | + const start = performance.now(); |
| 177 | + const effectResult = effect(myGet, ...args); |
| 178 | + const time = (performance.now() - start).toFixed(2); |
| 179 | + |
| 180 | + ((config?.defaultExpand ?? true) ? console.group : console.groupCollapsed)( |
| 181 | + `\x1b[36m[embra]\x1b[0m trace ${type}${effect.name ? `(\x1b[33m${effect.name}\x1b[0m)` : ""}${config?.name ? `: ${config.name}` : ""}`, |
| 182 | + ); |
| 183 | + |
| 184 | + if (config?.printStack ?? true) { |
| 185 | + console.groupCollapsed("trace location"); |
| 186 | + console.log(traceLocation); |
| 187 | + console.groupEnd(); |
| 188 | + |
| 189 | + console.groupCollapsed(`effect executed in ${time} ms`); |
| 190 | + console.trace(); |
| 191 | + console.groupEnd(); |
| 192 | + } else { |
| 193 | + console.log(`effect executed in ${time} ms`); |
| 194 | + } |
| 195 | + |
| 196 | + deps.print(); |
| 197 | + |
| 198 | + console.groupEnd(); |
| 199 | + |
| 200 | + [deps.oldDeps, deps.newDeps] = [deps.newDeps, deps.oldDeps]; |
| 201 | + deps.newDeps.clear(); |
| 202 | + |
| 203 | + return effectResult; |
| 204 | + }) as T; |
| 205 | +}; |
0 commit comments