@trops/dash-core 0.1.355 → 0.1.357

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.
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
+ var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
4
+ var _typeof = require('@babel/runtime/helpers/typeof');
3
5
  var _defineProperty = require('@babel/runtime/helpers/defineProperty');
4
6
  var DashReact = require('@trops/dash-react');
5
- var _typeof = require('@babel/runtime/helpers/typeof');
6
7
  var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
7
- var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
8
8
  var React = require('react');
9
9
  var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
10
10
  var _createClass = require('@babel/runtime/helpers/createClass');
@@ -124,12 +124,47 @@ var event = {
124
124
  };
125
125
  var ipcBridgeListener = null;
126
126
  var monitorCallbacks = new Set();
127
+
128
+ // Cache of last-seen event payloads by eventType. Used to replay the most
129
+ // recent value to late subscribers in popout windows — so a widget rendered
130
+ // in a popout can hydrate from current state without requiring the event to
131
+ // re-fire. Populated on-demand by enableIpcBridge({ replay: true }) and kept
132
+ // current via pub() and the broadcast listener.
133
+ var lastEventCache = new Map();
134
+ // Replay is opt-in per window to avoid resurrecting stale or command-style
135
+ // events when the main dashboard is reopened. Popout windows set this true;
136
+ // the main window leaves it false.
137
+ var replayEnabled = false;
127
138
  var DashboardPublisher = {
128
139
  sub: function sub(eventType, action, uuid) {
129
140
  event.on(eventType, action, uuid);
141
+
142
+ // Replay last known payload to this subscriber so popouts can hydrate
143
+ // from current state without needing the event to re-fire. Only active
144
+ // in windows that opted in via enableIpcBridge({ replay: true }).
145
+ if (replayEnabled && lastEventCache.has(eventType) && typeof action === "function") {
146
+ var cached = lastEventCache.get(eventType);
147
+ // Defer so the caller finishes wiring up before the handler runs.
148
+ setTimeout(function () {
149
+ try {
150
+ action({
151
+ message: cached.content,
152
+ event: eventType,
153
+ uuid: uuid,
154
+ replay: true
155
+ });
156
+ } catch (_) {
157
+ // Replay must not break subscription
158
+ }
159
+ }, 0);
160
+ }
130
161
  },
131
162
  pub: function pub(eventType, content) {
132
163
  var _window$mainApi;
164
+ lastEventCache.set(eventType, {
165
+ content: content,
166
+ timestamp: Date.now()
167
+ });
133
168
  event.emit(eventType, content);
134
169
 
135
170
  // Notify monitor callbacks (debugger)
@@ -162,21 +197,51 @@ var DashboardPublisher = {
162
197
  };
163
198
  },
164
199
  enableIpcBridge: function enableIpcBridge() {
165
- var _window$mainApi2;
200
+ var _window$mainApi2, _window$mainApi3;
201
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
166
202
  if (ipcBridgeListener) return;
167
203
  if (!((_window$mainApi2 = window.mainApi) !== null && _window$mainApi2 !== void 0 && _window$mainApi2.on)) return;
204
+ var _options$replay = options.replay,
205
+ replay = _options$replay === void 0 ? false : _options$replay;
206
+ replayEnabled = replay;
168
207
  ipcBridgeListener = function ipcBridgeListener(_e, message) {
208
+ if (replayEnabled && message && message.eventType) {
209
+ lastEventCache.set(message.eventType, {
210
+ content: message.content,
211
+ timestamp: Date.now()
212
+ });
213
+ }
169
214
  event.emit(message.eventType, message.content);
170
215
  };
171
216
  window.mainApi.on("widget-event:broadcast", ipcBridgeListener);
217
+
218
+ // Hydrate this window's cache from the main-process cache so that
219
+ // widgets mounted in a fresh window (popout) can replay events that
220
+ // fired before this window existed. Only for windows that opted in.
221
+ if (replayEnabled && (_window$mainApi3 = window.mainApi) !== null && _window$mainApi3 !== void 0 && (_window$mainApi3 = _window$mainApi3.widgetEvent) !== null && _window$mainApi3 !== void 0 && _window$mainApi3.getLastEvents) {
222
+ window.mainApi.widgetEvent.getLastEvents().then(function (events) {
223
+ if (events && _typeof(events) === "object") {
224
+ for (var _i = 0, _Object$entries = Object.entries(events); _i < _Object$entries.length; _i++) {
225
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
226
+ eventType = _Object$entries$_i[0],
227
+ entry = _Object$entries$_i[1];
228
+ var existing = lastEventCache.get(eventType);
229
+ if (!existing || existing.timestamp < (entry.timestamp || 0)) {
230
+ lastEventCache.set(eventType, entry);
231
+ }
232
+ }
233
+ }
234
+ })["catch"](function () {});
235
+ }
172
236
  },
173
237
  disableIpcBridge: function disableIpcBridge() {
174
- var _window$mainApi3;
238
+ var _window$mainApi4;
175
239
  if (!ipcBridgeListener) return;
176
- if ((_window$mainApi3 = window.mainApi) !== null && _window$mainApi3 !== void 0 && _window$mainApi3.removeListener) {
240
+ if ((_window$mainApi4 = window.mainApi) !== null && _window$mainApi4 !== void 0 && _window$mainApi4.removeListener) {
177
241
  window.mainApi.removeListener("widget-event:broadcast", ipcBridgeListener);
178
242
  }
179
243
  ipcBridgeListener = null;
244
+ replayEnabled = false;
180
245
  },
181
246
  listeners: function listeners() {
182
247
  return event.list;
@@ -2764,7 +2829,16 @@ var DashboardWrapper = function DashboardWrapper(_ref) {
2764
2829
  return w;
2765
2830
  }, [dashApi]);
2766
2831
  React.useEffect(function () {
2767
- DashboardPublisher.enableIpcBridge();
2832
+ var _window$location, _window$location2;
2833
+ // Only popout windows replay cached events on subscribe — keeps the
2834
+ // main dashboard from resurrecting stale state when reopened while
2835
+ // still letting popped-out widgets hydrate from current state.
2836
+ // Electron uses hash routing, so the popout path lives in
2837
+ // window.location.hash (e.g. "#/popout-widget/..."), not pathname.
2838
+ var isPopout = typeof window !== "undefined" && (typeof ((_window$location = window.location) === null || _window$location === void 0 ? void 0 : _window$location.hash) === "string" && window.location.hash.includes("/popout") || typeof ((_window$location2 = window.location) === null || _window$location2 === void 0 ? void 0 : _window$location2.pathname) === "string" && window.location.pathname.includes("/popout"));
2839
+ DashboardPublisher.enableIpcBridge({
2840
+ replay: isPopout
2841
+ });
2768
2842
  return function () {
2769
2843
  return DashboardPublisher.disableIpcBridge();
2770
2844
  };