@glasshome/sync-layer 0.7.0 → 0.7.2

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.
@@ -13,7 +13,7 @@ export declare const MAX_HISTORY_POINTS = 5760;
13
13
  export declare function isHistoryTracked(entityId: EntityId): boolean;
14
14
  /** Start tracking history for an entity. Fetches initial backfill. */
15
15
  export declare function trackEntityHistory(entityId: EntityId, options: EntityHistoryQueryOptions): Promise<EntityHistoryData>;
16
- /** Stop tracking history for an entity and clear its data. */
16
+ /** Release one tracker. The entity's data is cleared only when the last one goes. */
17
17
  export declare function untrackEntityHistory(entityId: EntityId): void;
18
18
  /**
19
19
  * Append a state change to an entity's history.
package/dist/index.js CHANGED
@@ -379,23 +379,31 @@ function historyStateToTimeline(s) {
379
379
  //#region src/history/query.ts
380
380
  /** Hard cap on retained points per entity timeline. */
381
381
  const MAX_HISTORY_POINTS = 5760;
382
- /** Set of entity IDs with active history tracking. */
383
- const trackedEntities = /* @__PURE__ */ new Set();
382
+ /** Live tracker count per entity. Ref-counted because a timeline is shared: two
383
+ * widgets on the same entity must not let the first unmount delete the store
384
+ * data the second is still rendering (appendHistoryPoint would then silently
385
+ * drop every update and the survivor's chart would freeze). */
386
+ const trackedEntities = /* @__PURE__ */ new Map();
384
387
  /** Check if an entity has active history tracking. */
385
388
  function isHistoryTracked(entityId) {
386
389
  return trackedEntities.has(entityId);
387
390
  }
388
391
  /** Start tracking history for an entity. Fetches initial backfill. */
389
392
  async function trackEntityHistory(entityId, options) {
390
- trackedEntities.add(entityId);
393
+ trackedEntities.set(entityId, (trackedEntities.get(entityId) ?? 0) + 1);
391
394
  const historyData = await fetchEntityHistory(entityId, options);
392
395
  setState(produce((s) => {
393
396
  s.history[entityId] = historyData;
394
397
  }));
395
398
  return historyData;
396
399
  }
397
- /** Stop tracking history for an entity and clear its data. */
400
+ /** Release one tracker. The entity's data is cleared only when the last one goes. */
398
401
  function untrackEntityHistory(entityId) {
402
+ const remaining = (trackedEntities.get(entityId) ?? 0) - 1;
403
+ if (remaining > 0) {
404
+ trackedEntities.set(entityId, remaining);
405
+ return;
406
+ }
399
407
  trackedEntities.delete(entityId);
400
408
  setState(produce((s) => {
401
409
  delete s.history[entityId];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glasshome/sync-layer",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "description": "Type-safe, reactive state synchronization layer for Home Assistant",
5
5
  "repository": {
6
6
  "type": "git",
@@ -51,7 +51,7 @@
51
51
  },
52
52
  "dependencies": {
53
53
  "home-assistant-js-websocket": "^9.6.0",
54
- "@glasshome/widget-contract": "0.1.0"
54
+ "@glasshome/widget-contract": "0.2.0"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@glasshome/ha-types": "0.1.2",