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 var current: Trace = null;
10 var traces: Trace[] = [];
12 export type Edge = [string, string];
14 export default class Trace {
15 private _edges: { [key: string]: Edge } = {};
16 private _stack: string[] = [];
17 private _name: string;
19 constructor(line: Line<any>) {
20 this._name = this._formatLine(line);
25 if (current._stack.length != 0) {
26 current._addEdge([ current._previousEdge(), this._name ]);
29 current._stack.push(this._name);
32 static add(id: string) {
35 current._addEdge([ current._previousEdge(), id ]);
40 if (current === this) {
46 get traceList(): readonly Edge[] {
47 return Object.values(this._edges);
50 private _addEdge(e: Edge) {
51 this._edges[`${e[0]}|${e[1]}`] = e;
54 private _previousEdge(): string {
55 return this._stack[this._stack.length - 1];
58 private _formatLine(line: Line<any>): string {
59 const description = line.description ? ` (${line.description})` : '';
60 if (line.form === undefined)
61 return `${line.constructor.name}${description}`;
62 return `${line.form.name}-${line.id}${description}`;
66 export function getLastTraceList(): readonly Edge[] {
67 if (traces.length == 0)
69 return traces[traces.length - 1].traceList;