@linxin666/dsh-pet 0.3.14 → 0.3.16

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/lib/client.js CHANGED
@@ -41,6 +41,7 @@ window.__ModuleLoader__.load({
41
41
  }),
42
42
  actions: {
43
43
  setSnapshot: (draft, snapshot) => {
44
+ if (draft.state === "ready" && draft.error === null && sameSnapshot(draft.snapshot, snapshot)) return;
44
45
  draft.snapshot = snapshot;
45
46
  draft.state = "ready";
46
47
  draft.error = null;
@@ -64,6 +65,15 @@ window.__ModuleLoader__.load({
64
65
  }
65
66
  });
66
67
  }
68
+ /**
69
+ * Content equality for consecutive poll snapshots. JSON compare, not field
70
+ * enumeration: an exact string match is the only way to skip the publish, so
71
+ * a missed field can never freeze the UI — it can only cost the render the
72
+ * optimization exists to save.
73
+ */
74
+ function sameSnapshot(previous, next) {
75
+ return previous !== null && JSON.stringify(previous) === JSON.stringify(next);
76
+ }
67
77
  //#endregion
68
78
  //#region ../../node_modules/.pnpm/clsx@2.1.1/node_modules/clsx/dist/clsx.mjs
69
79
  function r(e) {
@@ -165,27 +175,29 @@ window.__ModuleLoader__.load({
165
175
  }
166
176
  //#endregion
167
177
  //#region src/client/sequences.ts
168
- /** Resolve the active track and frame after elapsed milliseconds of a looping sequence. */
169
- function sequenceFrameAt(sequence, tracks, elapsedMs) {
178
+ /** Build a {@link SequenceTimeline} over one manifest sequence. */
179
+ function createSequenceTimeline(sequence, tracks) {
170
180
  const itemDurations = sequence.map((animation) => tracks[animation].durations.reduce((sum, value) => sum + value, 0));
171
181
  const sequenceDuration = itemDurations.reduce((sum, value) => sum + value, 0);
172
- let offset = Math.max(0, elapsedMs) % sequenceDuration;
173
- let itemIndex = 0;
174
- while (itemIndex < sequence.length - 1 && offset >= itemDurations[itemIndex]) {
175
- offset -= itemDurations[itemIndex];
176
- itemIndex += 1;
177
- }
178
- const animation = sequence[itemIndex];
179
- const track = tracks[animation];
180
- let frameIndex = 0;
181
- while (frameIndex < track.frames.length - 1 && offset >= track.durations[frameIndex]) {
182
- offset -= track.durations[frameIndex];
183
- frameIndex += 1;
184
- }
185
- return {
186
- animation,
187
- frameIndex
188
- };
182
+ return { frameAt(elapsedMs) {
183
+ let offset = Math.max(0, elapsedMs) % sequenceDuration;
184
+ let itemIndex = 0;
185
+ while (itemIndex < sequence.length - 1 && offset >= itemDurations[itemIndex]) {
186
+ offset -= itemDurations[itemIndex];
187
+ itemIndex += 1;
188
+ }
189
+ const animation = sequence[itemIndex];
190
+ const track = tracks[animation];
191
+ let frameIndex = 0;
192
+ while (frameIndex < track.frames.length - 1 && offset >= track.durations[frameIndex]) {
193
+ offset -= track.durations[frameIndex];
194
+ frameIndex += 1;
195
+ }
196
+ return {
197
+ animation,
198
+ frameIndex
199
+ };
200
+ } };
189
201
  }
190
202
  //#endregion
191
203
  //#region \0dsh-css:packages/dsh-pet/src/client/pet.module.css.mjs
@@ -466,6 +478,14 @@ window.__ModuleLoader__.load({
466
478
  if (props.visual !== void 0) return;
467
479
  const reduceMotion = typeof window !== "undefined" && window.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches === true;
468
480
  const sequence = animation === animationForPhase(phase) ? sequences?.[phase] : void 0;
481
+ const timeline = sequence === void 0 ? void 0 : createSequenceTimeline(sequence, tracks);
482
+ const sequenceItems = sequence === void 0 ? void 0 : new Map(sequence.map((itemAnimation) => {
483
+ const itemRow = rowOfTrack(itemAnimation);
484
+ return [itemAnimation, {
485
+ row: itemRow,
486
+ track: trimTrack(tracks[itemAnimation], rows[itemRow] ?? tracks[itemAnimation].frames.length)
487
+ }];
488
+ }));
469
489
  const leadAnimation = sequence?.[0] ?? animation;
470
490
  const row = rowOfTrack(leadAnimation);
471
491
  const track = trimTrack(tracks[leadAnimation], rows[row] ?? tracks[leadAnimation].frames.length);
@@ -480,12 +500,12 @@ window.__ModuleLoader__.load({
480
500
  const tick = (ts) => {
481
501
  const delta = ts - last;
482
502
  last = ts;
483
- if (sequence !== void 0) {
503
+ if (timeline !== void 0 && sequenceItems !== void 0) {
484
504
  sequenceElapsed += delta;
485
- const current = sequenceFrameAt(sequence, tracks, sequenceElapsed);
486
- const currentRow = rowOfTrack(current.animation);
487
- const col = trimTrack(tracks[current.animation], rows[currentRow] ?? tracks[current.animation].frames.length).frames[current.frameIndex];
488
- const pos = framePosition(cell, currentRow, col, scaleRef.current);
505
+ const current = timeline.frameAt(sequenceElapsed);
506
+ const item = sequenceItems.get(current.animation);
507
+ const col = item.track.frames[current.frameIndex];
508
+ const pos = framePosition(cell, item.row, col, scaleRef.current);
489
509
  const posStr = pos.x + "px " + pos.y + "px";
490
510
  if (posStr !== lastPosStr) {
491
511
  lastPosStr = posStr;
@@ -3468,7 +3488,7 @@ window.__ModuleLoader__.load({
3468
3488
  /** The building package's version, when the bundle carries it. */
3469
3489
  function bakedVersion() {
3470
3490
  try {
3471
- return "0.3.14";
3491
+ return "0.3.16";
3472
3492
  } catch {
3473
3493
  return;
3474
3494
  }
@@ -3677,7 +3697,7 @@ window.__ModuleLoader__.load({
3677
3697
  document.removeEventListener("visibilitychange", onVisibility);
3678
3698
  };
3679
3699
  }, "pet: poll");
3680
- ctx.effect(() => {
3700
+ const disposeSessionWatch = ctx.effect(() => {
3681
3701
  return sessions.list.subscribe(() => {
3682
3702
  if (document.visibilityState === "visible") pollNow();
3683
3703
  });
@@ -3762,6 +3782,7 @@ window.__ModuleLoader__.load({
3762
3782
  petRoot.unmount();
3763
3783
  container.remove();
3764
3784
  disposePoll();
3785
+ disposeSessionWatch();
3765
3786
  disposeUi = void 0;
3766
3787
  };
3767
3788
  clearUiTeardown = registerPetUiTeardown(() => {