-
Notifications
You must be signed in to change notification settings - Fork 168
/
graph.ts
144 lines (126 loc) · 3.74 KB
/
graph.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
export type Module = {
readonly specifier: string;
readonly version: number;
readonly deps?: ReadonlyArray<DependencyDescriptor>;
readonly inlineCSS?: string;
};
export type DependencyDescriptor = {
readonly specifier: string;
readonly dynamic?: boolean;
};
export class DependencyGraph {
#modules = new Map<string, Module>();
#globalVersion = Date.now();
constructor(modules?: Module[], globalVersion?: number) {
if (modules) {
modules.forEach((item) => {
if (typeof item.specifier === "string" && typeof item.version === "number") {
this.#modules.set(item.specifier, item);
}
});
}
if (globalVersion) {
this.#globalVersion = globalVersion;
}
}
get globalVersion(): number {
return this.#globalVersion;
}
get modules(): Module[] {
const modules: Module[] = [];
this.#modules.forEach((module) => modules.push(module));
return modules;
}
get(specifier: string): Module | undefined {
return this.#modules.get(specifier);
}
mark(specifier: string, props: Partial<Module>): Module {
const prev = this.#modules.get(specifier);
if (prev) {
Object.assign(prev, props);
return prev;
}
const mod: Module = {
specifier,
version: this.#globalVersion,
...props,
};
this.#modules.set(specifier, mod);
return mod;
}
unmark(specifier: string) {
this.#modules.delete(specifier);
}
// version++
update(specifier: string) {
this.#update(specifier);
}
#update(specifier: string, _set = new Set<string>()) {
const module = this.#modules.get(specifier);
if (module) {
// deno-lint-ignore ban-ts-comment
// @ts-ignore
module.version++;
_set.add(specifier);
this.#modules.forEach((module) => {
if (module.deps?.find((dep) => dep.specifier === specifier)) {
if (!_set.has(module.specifier)) {
this.#update(module.specifier, _set);
}
}
});
}
}
lookup(specifier: string, callback: (specifier: string) => void | false) {
this.#lookup(specifier, callback);
}
#lookup(specifier: string, callback: (specifier: string) => void | false, _set = new Set<string>()) {
_set.add(specifier);
for (const module of this.#modules.values()) {
if (module.deps?.find((dep) => dep.specifier === specifier)) {
if (!_set.has(module.specifier) && callback(module.specifier) !== false) {
this.#lookup(module.specifier, callback, _set);
}
}
}
}
shallowWalk(specifier: string | string[], callback: (mod: Module) => void) {
const recorder = new Set<string>();
[specifier].flat().forEach((spec) => {
this.#shallowWalk(spec, callback, recorder);
});
}
#shallowWalk(specifier: string, callback: (mod: Module) => void, recorder: Set<string>) {
if (this.#modules.has(specifier)) {
const mod = this.#modules.get(specifier)!;
callback(mod);
recorder.add(specifier);
mod.deps?.forEach((dep) => {
if (!recorder.has(dep.specifier)) {
this.#shallowWalk(dep.specifier, callback, recorder);
}
});
}
}
walk(specifier: string, callback: (mod: Module, importer?: Module) => void) {
this.#walk(specifier, callback);
}
#walk(
specifier: string,
callback: (mod: Module, importer?: Module) => void,
importer?: Module,
_path: string[] = [],
) {
if (this.#modules.has(specifier)) {
const mod = this.#modules.get(specifier)!;
callback(mod, importer);
_path.push(specifier);
mod.deps?.forEach((dep) => {
if (!_path.includes(dep.specifier)) {
this.#walk(dep.specifier, callback, mod, [..._path]);
}
});
}
}
}
export default new DependencyGraph();