1 // Copyright 2020 Blue Static <https://www.bluestatic.org>
2 // This program is free software licensed under the GNU General Public License,
3 // version 3.0. The full text of the license can be found in LICENSE.txt.
4 // SPDX-License-Identifier: GPL-3.0-only
6 import { Line } from './Line';
8 export type Edge = [string, string];
10 type Trace = { [key: string]: Edge };
12 var stack: string[] = [];
14 var current: Trace = null;
15 var previous: Trace = null;
17 export function begin(line: Line<any>) {
18 const name = formatLine(line);
21 current = {} as Trace;
23 if (stack.length != 0)
24 addEdge([ previousEdge(), name ]);
29 export function mark(id: string) {
32 addEdge([ previousEdge(), id ]);
35 export function end() {
37 if (stack.length == 0) {
42 export function reset() {
48 export function getLastTraceList(): readonly Edge[] {
49 if (previous === null)
51 return Object.values(previous);
54 function addEdge(e: Edge) {
55 current[`${e[0]}|${e[1]}`] = e;
58 function previousEdge(): string {
59 return stack[stack.length - 1];
62 function formatLine(line: Line<any>): string {
63 const description = line.description ? ` (${line.description})` : '';
64 if (line.form === undefined)
65 return `${line.constructor.name}${description}`;
66 return `${line.form.name}-${line.id}${description}`;