-
Notifications
You must be signed in to change notification settings - Fork 809
/
portalOpenInstances.js
71 lines (62 loc) · 1.82 KB
/
portalOpenInstances.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
// Tracks portals that are open and emits events to subscribers
class PortalOpenInstances {
constructor() {
this.openInstances = [];
this.subscribers = [];
}
register = openInstance => {
if (this.openInstances.indexOf(openInstance) !== -1) {
if (process.env.NODE_ENV !== "production") {
// eslint-disable-next-line no-console
console.warn(
`React-Modal: Cannot register modal instance that's already open`
);
}
return;
}
this.openInstances.push(openInstance);
this.emit("register");
};
deregister = openInstance => {
const index = this.openInstances.indexOf(openInstance);
if (index === -1) {
if (process.env.NODE_ENV !== "production") {
// eslint-disable-next-line no-console
console.warn(
`React-Modal: Unable to deregister ${openInstance} as ` +
`it was never registered`
);
}
return;
}
this.openInstances.splice(index, 1);
this.emit("deregister");
};
subscribe = callback => {
this.subscribers.push(callback);
};
emit = eventType => {
this.subscribers.forEach(subscriber =>
subscriber(
eventType,
// shallow copy to avoid accidental mutation
this.openInstances.slice()
)
);
};
}
let portalOpenInstances = new PortalOpenInstances();
/* eslint-disable no-console */
/* istanbul ignore next */
export function log() {
console.log("portalOpenInstances ----------");
console.log(portalOpenInstances.openInstances.length);
portalOpenInstances.openInstances.forEach(p => console.log(p));
console.log("end portalOpenInstances ----------");
}
/* istanbul ignore next */
export function resetState() {
portalOpenInstances = new PortalOpenInstances();
}
/* eslint-enable no-console */
export default portalOpenInstances;