forked from lumeland/lume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scopes.ts
64 lines (53 loc) · 1.51 KB
/
scopes.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
import type { Entry } from "../core.ts";
/**
* Define independent updates scopes
* This optimize the update process after any change
*/
export default class Scopes {
scopes = new Set<ScopeFilter>();
/** Returns a function to filter the pages that must be rebuild */
getFilter(changedFiles: Iterable<string>): (entry: Entry) => boolean {
// There's no any scope, so rebuild all pages
if (this.scopes.size === 0) {
return () => true;
}
let noScoped = false;
const changed = new Set<ScopeFilter>();
for (const file of changedFiles) {
let found = false;
for (const scopeFn of this.scopes) {
if (scopeFn(file)) {
changed.add(scopeFn);
found = true;
break;
}
}
if (!found) {
noScoped = true;
}
}
// Calculate scoped extensions that didn't change
const notChanged: ScopeFilter[] = [];
for (const scopeFn of this.scopes) {
if (!changed.has(scopeFn)) {
notChanged.push(scopeFn);
}
}
// Generate the filter function
return function (entry) {
// Ignore directories
if (entry.type === "directory") {
return true;
}
// It matches with any scope that has changed
for (const scopeFn of changed) {
if (scopeFn(entry.path)) {
return true;
}
}
// It's not scoped
return noScoped && notChanged.every((scopeFn) => !scopeFn(entry.path));
};
}
}
export type ScopeFilter = (path: string) => boolean;