-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathCustomElementsRegistry.ts
More file actions
96 lines (79 loc) · 3.25 KB
/
CustomElementsRegistry.ts
File metadata and controls
96 lines (79 loc) · 3.25 KB
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
import getSharedResource from "./getSharedResource.js";
import { getCurrentRuntimeIndex, compareRuntimes, getAllRuntimes } from "./Runtimes.js";
import {
addRegisteredTag,
isTagRegistered,
hasRegisteredTags,
getAllRegisteredTags,
} from "./RegisteredElements.js";
import type { Timeout } from "./types.js";
const Tags = getSharedResource<Map<string, number>>("Tags", new Map());
let Failures = new Map<number, Set<string>>();
let failureTimeout: Timeout | undefined;
const UNKNOWN_RUNTIME = -1;
const registerTag = (tag: string) => {
addRegisteredTag(tag);
Tags.set(tag, getCurrentRuntimeIndex());
};
const recordTagRegistrationFailure = (tag: string) => {
let tagRegRuntimeIndex = Tags.get(tag);
if (tagRegRuntimeIndex === undefined) {
tagRegRuntimeIndex = UNKNOWN_RUNTIME; // If the tag is taken, but not registered in Tags, then a version before 1.1.0 defined it => use the "unknown" key
}
if (!Failures.has(tagRegRuntimeIndex)) {
Failures.set(tagRegRuntimeIndex, new Set());
}
Failures.get(tagRegRuntimeIndex)!.add(tag);
if (!failureTimeout) {
failureTimeout = setTimeout(() => {
displayFailedRegistrations();
Failures = new Map();
failureTimeout = undefined;
}, 1000);
}
};
const displayFailedRegistrations = () => {
const allRuntimes = getAllRuntimes();
const currentRuntimeIndex = getCurrentRuntimeIndex();
const currentRuntime = allRuntimes[currentRuntimeIndex];
let message = `Multiple UI5 Web Components instances detected.`;
if (allRuntimes.length > 1) {
message = `${message}\nLoading order (versions before 1.1.0 not listed): ${allRuntimes.map(runtime => `\n${runtime.description}`).join("")}`;
}
[...Failures.keys()].forEach(otherRuntimeIndex => {
let comparison: number;
let otherRuntime;
if (otherRuntimeIndex === UNKNOWN_RUNTIME) { // version < 1.1.0 defined the tag
comparison = 1; // the current runtime is considered newer
otherRuntime = {
description: `Older unknown runtime`,
};
} else {
comparison = compareRuntimes(currentRuntimeIndex, otherRuntimeIndex);
otherRuntime = allRuntimes[otherRuntimeIndex];
}
let compareWord;
if (comparison > 0) {
compareWord = "an older";
} else if (comparison < 0) {
compareWord = "a newer";
} else {
compareWord = "the same";
}
message = `${message}\n\n"${currentRuntime.description}" failed to define ${Failures.get(otherRuntimeIndex)!.size} tag(s) as they were defined by a runtime of ${compareWord} version "${otherRuntime.description}": ${([...Failures.get(otherRuntimeIndex)!]).sort().join(", ")}.`;
if (comparison > 0) {
message = `${message}\nWARNING! If your code uses features of the above web components, unavailable in ${otherRuntime.description}, it might not work as expected!`;
} else {
message = `${message}\nSince the above web components were defined by the same or newer version runtime, they should be compatible with your code.`;
}
});
message = `${message}\n\nTo prevent other runtimes from defining tags that you use, consider using scoping or have third-party libraries use scoping: https://github.com/UI5/webcomponents/blob/main/docs/2-advanced/06-scoping.md.`;
console.warn(message); // eslint-disable-line
};
export {
registerTag,
isTagRegistered,
hasRegisteredTags,
getAllRegisteredTags,
recordTagRegistrationFailure,
};