-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs2at-unique-id-manager.js
103 lines (87 loc) · 3.22 KB
/
js2at-unique-id-manager.js
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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* eslint parserOptions: ["sourceType", "module"] */
/* Singleton service that creates and stores unique ids for any event target */
class Js2atUniqueIdManager {
constructor() {
this.targetToUid = new WeakMap();
this.uidToTarget = new Map();
this.counter = 0;
this.uidRemovedCallbacks = [];
window.addEventListener('beforeunload', () => {
for (let [uid, target] of this.uidToTarget.entries())
this.remove(target, uid);
});
}
addUidRemovedListener(callback) {
this.uidRemovedCallbacks.push(callback);
}
add(target, uid) {
if (target instanceof EventTarget === false)
throw new Error('The provided target must be an EventTarget, such as an Element.');
this.targetToUid.set(target, uid);
this.uidToTarget.set(uid, target);
if (target.parentNode) {
// Observe removed children. Can't be done on root since there is no parent,
// but once the root is removed that means the document has gone away,
// in which case this singleton will be destroyed and the memory released.
if (!this.mutationObserver)
this.mutationObserver = new MutationObserver((mutationsList) => { this.onParentMutation(mutationsList); });
this.mutationObserver.observe(target.parentNode, { childList: true });
}
}
onParentMutation(mutationsList) {
// An element with a unique id may have been removed, because its parent
// has reported a change in its child nodes.
for(var mutation of mutationsList)
for (let removedNode of mutation.removedNodes)
if (this.targetToUid.has(removedNode))
this.removeTarget(removedNode);
}
remove(target, uid) {
if (target instanceof EventTarget === false)
throw new Error('The provided target must be an EventTarget, such as an Element.');
this.targetToUid.delete(target);
this.uidToTarget.delete(uid);
for (let uidRemovedCallback of this.uidRemovedCallbacks)
uidRemovedCallback(uid);
}
removeTarget(target) {
this.remove(target, this.getUid(target));
}
removeUid(uid) {
this.remove(this.getTarget(uid), uid);
}
getTarget(uid) {
return this.uidToTarget.get(uid);
}
getUid(target) {
if (target instanceof EventTarget === false)
throw new Error('The provided target must be an EventTarget, such as an Element.');
return this.targetToUid.get(target);
}
getOrCreateUid(target) {
const uid = this.getUid(target);
if (uid)
return uid.toString();
const newUid = this.createUid();
this.add(target, newUid);
return newUid;
}
createUid() {
++ this.counter;
return this.counter.toString();
}
}
export default new Js2atUniqueIdManager();