@gaulatti/bleecker 0.1.44 → 0.1.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -61,27 +61,61 @@ export function Alert({ className, duration = 5000, message, onClose, type = 'in
61
61
  }
62
62
  let alertsState = [];
63
63
  let listeners = [];
64
+ // Each alert is shown by exactly one container: the first one mounted when it
65
+ // is raised, or, when none is mounted yet, the first one to mount. An alert
66
+ // stays with its container until it closes or the container unmounts.
67
+ const ownerOf = new Map();
68
+ function alertsOwnedBy(listener) {
69
+ return alertsState.filter((alert) => ownerOf.get(alert.id) === listener);
70
+ }
64
71
  function notifyListeners() {
65
- listeners.forEach((listener) => listener([...alertsState]));
72
+ listeners.forEach((listener) => listener(alertsOwnedBy(listener)));
66
73
  }
67
74
  export function showAlert(message, type = 'info') {
68
75
  const id = Math.random().toString(36).substring(2, 9);
69
76
  alertsState.push({ id, message, type });
77
+ if (listeners.length > 0) {
78
+ ownerOf.set(id, listeners[0]);
79
+ }
70
80
  notifyListeners();
71
81
  }
72
82
  export function AlertContainer() {
73
83
  const [alerts, setAlerts] = React.useState([]);
84
+ // One listener per container instance, kept across React StrictMode's
85
+ // simulated remount so the container keeps the alerts it already claimed.
86
+ const listenerRef = React.useRef(null);
87
+ listenerRef.current ??= (nextAlerts) => setAlerts(nextAlerts);
74
88
  React.useEffect(() => {
75
- const listener = (nextAlerts) => {
76
- setAlerts(nextAlerts);
77
- };
89
+ const listener = listenerRef.current;
78
90
  listeners.push(listener);
91
+ // Claim the alerts raised while no container was mounted.
92
+ for (const alert of alertsState) {
93
+ if (!ownerOf.has(alert.id)) {
94
+ ownerOf.set(alert.id, listener);
95
+ }
96
+ }
97
+ listener(alertsOwnedBy(listener));
79
98
  return () => {
80
99
  listeners = listeners.filter((entry) => entry !== listener);
100
+ // Drop what this container showed once it has really unmounted; a
101
+ // StrictMode remount subscribes the same listener again first.
102
+ queueMicrotask(() => {
103
+ if (listeners.includes(listener)) {
104
+ return;
105
+ }
106
+ alertsState = alertsState.filter((alert) => {
107
+ if (ownerOf.get(alert.id) !== listener) {
108
+ return true;
109
+ }
110
+ ownerOf.delete(alert.id);
111
+ return false;
112
+ });
113
+ });
81
114
  };
82
115
  }, []);
83
116
  const removeAlert = (id) => {
84
117
  alertsState = alertsState.filter((alert) => alert.id !== id);
118
+ ownerOf.delete(id);
85
119
  notifyListeners();
86
120
  };
87
121
  return (_jsx(_Fragment, { children: alerts.map((alert) => (_jsx(Alert, { message: alert.message, onClose: () => removeAlert(alert.id), type: alert.type }, alert.id))) }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gaulatti/bleecker",
3
- "version": "0.1.44",
3
+ "version": "0.1.45",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",