@linxin666/dsh-pet 0.4.6 → 0.4.8
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/README.i18n.yaml +2 -2
- package/README.md +3 -1
- package/README.zh.md +3 -1
- package/lib/client.js +223 -79
- package/lib/client.js.map +1 -1
- package/lib/index.js +454 -1
- package/lib/live2d-vendor.js +129 -95
- package/lib/live2d-vendor.js.map +1 -1
- package/lib/types/client/PetSprite.d.ts +6 -4
- package/lib/types/client/PetSprite.d.ts.map +1 -1
- package/lib/types/client/PetSprite.js +100 -50
- package/lib/types/client/index.js +1 -1
- package/lib/types/client/renderers/frames2d.d.ts +5 -3
- package/lib/types/client/renderers/frames2d.d.ts.map +1 -1
- package/lib/types/client/renderers/frames2d.js +139 -33
- package/lib/types/client/sequences.d.ts +9 -2
- package/lib/types/client/sequences.d.ts.map +1 -1
- package/lib/types/client/sequences.js +25 -14
- package/lib/types/client/spritesheet.d.ts +22 -1
- package/lib/types/client/spritesheet.d.ts.map +1 -1
- package/lib/types/client/spritesheet.js +26 -2
- package/package.json +26 -30
- package/src/client/PetSprite.test.tsx +262 -26
- package/src/client/PetSprite.tsx +99 -50
- package/src/client/index.ts +1 -1
- package/src/client/pet-css.test.ts +12 -0
- package/src/client/pet.module.css +9 -3
- package/src/client/renderers/frames2d.test.ts +103 -1
- package/src/client/renderers/frames2d.ts +143 -30
- package/src/client/sequences.test.ts +37 -1
- package/src/client/sequences.ts +42 -16
- package/src/client/spritesheet.test.ts +81 -0
- package/src/client/spritesheet.ts +27 -2
- package/src/registry.test.ts +4 -2
- package/lib/invariant.js +0 -34
- package/lib/state-NvDEoCln.js +0 -455
- package/lib/types/invariant.d.ts +0 -10
- package/lib/types/invariant.d.ts.map +0 -1
- package/lib/types/invariant.js +0 -27
- package/src/invariant.ts +0 -40
package/src/client/PetSprite.tsx
CHANGED
|
@@ -22,7 +22,7 @@ import type { GameplayBus } from './gameplay-hud.ts'
|
|
|
22
22
|
import type { PetRoamDirection } from '../gameplay.ts'
|
|
23
23
|
import type { DecorationView } from '../contracts/status-decoration.ts'
|
|
24
24
|
import type { PetFeedback } from './pet-store.ts'
|
|
25
|
-
import { framePosition, rowOfTrack, trimTrack } from './spritesheet.ts'
|
|
25
|
+
import { framePosition, rowOfTrack, scaledCellSize, trimTrack } from './spritesheet.ts'
|
|
26
26
|
import { createSequenceTimeline } from './sequences.ts'
|
|
27
27
|
import { animationForPhase, type ActivityPhase, type PetAnimation } from '../state.ts'
|
|
28
28
|
import { NS } from './locales.ts'
|
|
@@ -250,10 +250,12 @@ function UsageAnnouncementBubble(props: { announcement: PetAnnouncement }): Reac
|
|
|
250
250
|
}
|
|
251
251
|
|
|
252
252
|
/**
|
|
253
|
-
* The floating pet. The spritesheet frame advances on
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
253
|
+
* The floating pet. The spritesheet frame advances on a timer scheduled for the
|
|
254
|
+
* current frame's remaining time (per-frame durations come from the
|
|
255
|
+
* definition's tracks), so the loop wakes once per frame change instead of once
|
|
256
|
+
* per screen refresh; the atlas image is loaded once and the background
|
|
257
|
+
* position is written straight to the sprite element (no per-frame React
|
|
258
|
+
* state). Roaming writes the sprite's position the same way (issue #17).
|
|
257
259
|
*/
|
|
258
260
|
export function PetSprite(props: PetSpriteProps): ReactPortal {
|
|
259
261
|
const { snapshot, definition, display, feedback } = props
|
|
@@ -376,6 +378,10 @@ export function PetSprite(props: PetSpriteProps): ReactPortal {
|
|
|
376
378
|
const animation = snapshot?.animation ?? 'idle'
|
|
377
379
|
const scaleRef = useRef(spriteScale)
|
|
378
380
|
scaleRef.current = spriteScale
|
|
381
|
+
// The frame the loop painted last. A settled non-looping track leaves the
|
|
382
|
+
// loop stopped, so a later scale change repaints it from here instead of
|
|
383
|
+
// waking the loop.
|
|
384
|
+
const paintedRef = useRef<{ row: number; col: number } | null>(null)
|
|
379
385
|
useEffect(() => {
|
|
380
386
|
if (props.visual !== undefined) return
|
|
381
387
|
const reduceMotion = typeof window !== 'undefined'
|
|
@@ -398,35 +404,46 @@ export function PetSprite(props: PetSpriteProps): ReactPortal {
|
|
|
398
404
|
const leadAnimation = sequence?.[0] ?? animation
|
|
399
405
|
const row = rowOfTrack(leadAnimation)
|
|
400
406
|
const track = trimTrack(tracks[leadAnimation], rows[row] ?? tracks[leadAnimation].frames.length)
|
|
407
|
+
// Write one frame's offset to the sprite element, skipping the assignment
|
|
408
|
+
// when the coordinates did not change. lastPosStr is the cache the whole
|
|
409
|
+
// loop shares, so a tick that changes nothing costs no style write.
|
|
410
|
+
let lastPosStr = ''
|
|
411
|
+
const paint = (frameRow: number, col: number): void => {
|
|
412
|
+
paintedRef.current = { row: frameRow, col }
|
|
413
|
+
const pos = framePosition(cell, frameRow, col, scaleRef.current)
|
|
414
|
+
const posStr = pos.x + 'px ' + pos.y + 'px'
|
|
415
|
+
if (posStr === lastPosStr) return
|
|
416
|
+
lastPosStr = posStr
|
|
417
|
+
if (spriteRef.current !== null) {
|
|
418
|
+
spriteRef.current.style.backgroundPosition = posStr
|
|
419
|
+
}
|
|
420
|
+
}
|
|
401
421
|
// Paint one static sprite frame up front either way, so the pet is never
|
|
402
422
|
// blank while the loop heat-up runs.
|
|
403
|
-
|
|
404
|
-
const lead = framePosition(cell, row, leadCol, scaleRef.current)
|
|
405
|
-
let lastPosStr = lead.x + 'px ' + lead.y + 'px'
|
|
406
|
-
if (spriteRef.current !== null) {
|
|
407
|
-
spriteRef.current.style.backgroundPosition = lastPosStr
|
|
408
|
-
}
|
|
423
|
+
paint(row, track.frames[0]!)
|
|
409
424
|
if (reduceMotion) return
|
|
410
|
-
let
|
|
425
|
+
let timer = 0
|
|
411
426
|
let last = performance.now()
|
|
412
427
|
let sequenceElapsed = 0
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
428
|
+
// Sleep to the next frame boundary instead of waking on every screen
|
|
429
|
+
// refresh: track durations are 300-700 ms, so a 60 Hz loop spent the vast
|
|
430
|
+
// majority of its ticks proving that nothing had changed -- the same rule
|
|
431
|
+
// StatusOrnament already follows. A late wake (throttled tab, jank) still
|
|
432
|
+
// catches up every due frame, so the track keeps its own clock.
|
|
433
|
+
const schedule = (delay: number): void => {
|
|
434
|
+
timer = window.setTimeout(tick, Math.max(1, delay))
|
|
435
|
+
}
|
|
436
|
+
const tick = (): void => {
|
|
437
|
+
timer = 0
|
|
438
|
+
const now = performance.now()
|
|
439
|
+
const delta = now - last
|
|
440
|
+
last = now
|
|
416
441
|
if (timeline !== undefined && sequenceItems !== undefined) {
|
|
417
442
|
sequenceElapsed += delta
|
|
418
443
|
const current = timeline.frameAt(sequenceElapsed)
|
|
419
444
|
const item = sequenceItems.get(current.animation)!
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
const posStr = pos.x + 'px ' + pos.y + 'px'
|
|
423
|
-
if (posStr !== lastPosStr) {
|
|
424
|
-
lastPosStr = posStr
|
|
425
|
-
if (spriteRef.current !== null) {
|
|
426
|
-
spriteRef.current.style.backgroundPosition = posStr
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
raf = requestAnimationFrame(tick)
|
|
445
|
+
paint(item.row, item.track.frames[current.frameIndex]!)
|
|
446
|
+
schedule(timeline.remainingAt(sequenceElapsed))
|
|
430
447
|
return
|
|
431
448
|
}
|
|
432
449
|
// row/track come from the effect scope: they were computed once above
|
|
@@ -445,28 +462,33 @@ export function PetSprite(props: PetSpriteProps): ReactPortal {
|
|
|
445
462
|
st.index += 1
|
|
446
463
|
}
|
|
447
464
|
if (st.elapsed >= (track.durations[st.index] ?? 0)) {
|
|
448
|
-
if (track.loop) {
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
st.index
|
|
453
|
-
|
|
454
|
-
}
|
|
455
|
-
const col = track.frames[st.index]!
|
|
456
|
-
const pos = framePosition(cell, row, col, scaleRef.current)
|
|
457
|
-
const posStr = pos.x + 'px ' + pos.y + 'px'
|
|
458
|
-
if (posStr !== lastPosStr) {
|
|
459
|
-
lastPosStr = posStr
|
|
460
|
-
if (spriteRef.current !== null) {
|
|
461
|
-
spriteRef.current.style.backgroundPosition = posStr
|
|
465
|
+
if (!track.loop) {
|
|
466
|
+
// Hold the final frame; the host owns the switch to the fallback, so
|
|
467
|
+
// stop scheduling instead of re-painting the same frame forever.
|
|
468
|
+
st.index = maxIndex
|
|
469
|
+
paint(row, track.frames[st.index]!)
|
|
470
|
+
return
|
|
462
471
|
}
|
|
472
|
+
st.elapsed = 0
|
|
473
|
+
st.index = 0
|
|
463
474
|
}
|
|
464
|
-
|
|
475
|
+
paint(row, track.frames[st.index]!)
|
|
476
|
+
schedule((track.durations[st.index] ?? 0) - st.elapsed)
|
|
465
477
|
}
|
|
466
|
-
|
|
467
|
-
return () =>
|
|
478
|
+
schedule(0)
|
|
479
|
+
return () => window.clearTimeout(timer)
|
|
468
480
|
}, [animation, phase, cell, columns, rows, tracks, sequences, props.visual])
|
|
469
481
|
|
|
482
|
+
// A stopped loop is woken by nothing, so a sprite scale change repaints the
|
|
483
|
+
// frame it settled on: without this a resized pet would keep the old scale's
|
|
484
|
+
// cell offsets and show a neighbouring cell until the track changed.
|
|
485
|
+
useLayoutEffect(() => {
|
|
486
|
+
const painted = paintedRef.current
|
|
487
|
+
if (props.visual !== undefined || painted === null || spriteRef.current === null) return
|
|
488
|
+
const pos = framePosition(cell, painted.row, painted.col, spriteScale)
|
|
489
|
+
spriteRef.current.style.backgroundPosition = pos.x + 'px ' + pos.y + 'px'
|
|
490
|
+
}, [spriteScale, cell, props.visual])
|
|
491
|
+
|
|
470
492
|
// Auto-clear the feedback bubble after its CSS animation. The callback
|
|
471
493
|
// rides a ref so re-renders never reset the timer: the 2s poll rebuilds
|
|
472
494
|
// `props` every tick, and depending on it would starve the timeout.
|
|
@@ -496,10 +518,13 @@ export function PetSprite(props: PetSpriteProps): ReactPortal {
|
|
|
496
518
|
|
|
497
519
|
const onPointerDown = (e: ReactPointerEvent<HTMLDivElement>): void => {
|
|
498
520
|
if (props.dragDisabled === true) return
|
|
521
|
+
// Read the live position before ending a walk: the walk keeps it in a ref
|
|
522
|
+
// rather than React state, so a drag grabbed mid-walk starts where the pet
|
|
523
|
+
// actually is instead of snapping back to where the walk began.
|
|
524
|
+
const current = walkTargetRef.current ?? dragPosRef.current ?? { right: display.right, bottom: display.bottom }
|
|
499
525
|
endWalk(false)
|
|
500
526
|
e.preventDefault()
|
|
501
527
|
;(e.target as HTMLElement).setPointerCapture?.(e.pointerId)
|
|
502
|
-
const current = dragPos ?? { right: display.right, bottom: display.bottom }
|
|
503
528
|
dragRef.current = { startX: e.clientX, startY: e.clientY, ...current }
|
|
504
529
|
draggedRef.current = false
|
|
505
530
|
setHovered(false)
|
|
@@ -524,10 +549,6 @@ export function PetSprite(props: PetSpriteProps): ReactPortal {
|
|
|
524
549
|
if (dragPos !== null) props.onDragEnd(dragPos.right, dragPos.bottom)
|
|
525
550
|
}
|
|
526
551
|
|
|
527
|
-
const pos = dragPos ?? { right: display.right, bottom: display.bottom }
|
|
528
|
-
const spriteWidth = Math.round(cell.width * spriteScale)
|
|
529
|
-
const spriteHeight = Math.round(cell.height * spriteScale)
|
|
530
|
-
|
|
531
552
|
// --- roaming ---------------------------------------------------------
|
|
532
553
|
// The gameplay HUD rolls when the pet may wander; this side owns the motion
|
|
533
554
|
// (clamped to the viewport), the walk art's facing and the persisted resting
|
|
@@ -536,10 +557,24 @@ export function PetSprite(props: PetSpriteProps): ReactPortal {
|
|
|
536
557
|
// the side view); every other track stays unmirrored.
|
|
537
558
|
const [facingRight, setFacingRight] = useState(false)
|
|
538
559
|
const walkRafRef = useRef(0)
|
|
560
|
+
// Live position while a walk is in flight. The walk writes the element's
|
|
561
|
+
// right/bottom directly and mirrors them here instead of calling setState:
|
|
562
|
+
// a walk is 1-3 s of 60 fps motion, and each frame used to re-render the
|
|
563
|
+
// whole sprite tree (bubble stack, dock panel) for a position the DOM already
|
|
564
|
+
// knew. Keeping it in a ref means a render that lands mid-walk (a poll, a
|
|
565
|
+
// hover) still sees where the sprite actually is (issue #17).
|
|
539
566
|
const walkTargetRef = useRef<{ right: number; bottom: number } | null>(null)
|
|
540
567
|
const dragPosRef = useRef(dragPos)
|
|
541
568
|
dragPosRef.current = dragPos
|
|
542
569
|
|
|
570
|
+
const pos = walkTargetRef.current ?? dragPos ?? { right: display.right, bottom: display.bottom }
|
|
571
|
+
// One integral cell size for the sprite box, the atlas grid and every frame
|
|
572
|
+
// offset: background-size is this size times the column/row count, so the
|
|
573
|
+
// scaled atlas shares a pixel grid with the box that shows one cell (#17).
|
|
574
|
+
const scaled = scaledCellSize(cell, spriteScale)
|
|
575
|
+
const spriteWidth = scaled.width
|
|
576
|
+
const spriteHeight = scaled.height
|
|
577
|
+
|
|
543
578
|
/** Stop an active walk; `persist` lands the pet on the spot it reached. */
|
|
544
579
|
const endWalk = (persist: boolean): void => {
|
|
545
580
|
if (walkRafRef.current === 0) return
|
|
@@ -548,7 +583,12 @@ export function PetSprite(props: PetSpriteProps): ReactPortal {
|
|
|
548
583
|
setFacingRight(false)
|
|
549
584
|
const settled = walkTargetRef.current
|
|
550
585
|
walkTargetRef.current = null
|
|
551
|
-
if (
|
|
586
|
+
if (settled === null) return
|
|
587
|
+
// Hand the reached spot back to React state: the walk's own frames never
|
|
588
|
+
// updated it, and a render (or a drag that starts here) would otherwise
|
|
589
|
+
// put the sprite back where the walk began.
|
|
590
|
+
setDragPos(settled)
|
|
591
|
+
if (persist) props.onDragEnd(settled.right, settled.bottom)
|
|
552
592
|
}
|
|
553
593
|
|
|
554
594
|
useEffect(() => {
|
|
@@ -589,7 +629,15 @@ export function PetSprite(props: PetSpriteProps): ReactPortal {
|
|
|
589
629
|
bottom: current.bottom + (target.bottom - current.bottom) * t,
|
|
590
630
|
}
|
|
591
631
|
walkTargetRef.current = next
|
|
592
|
-
|
|
632
|
+
// The position goes straight to the element: a walk is the one motion
|
|
633
|
+
// that moved every frame, and a setState here re-rendered the bubble
|
|
634
|
+
// stack and the dock panel for a value the DOM already knew (issue
|
|
635
|
+
// #17). React state takes over once, on the landing frame.
|
|
636
|
+
const el = floatRef.current
|
|
637
|
+
if (el !== null) {
|
|
638
|
+
el.style.right = next.right + 'px'
|
|
639
|
+
el.style.bottom = next.bottom + 'px'
|
|
640
|
+
}
|
|
593
641
|
if (t < 1) {
|
|
594
642
|
walkRafRef.current = window.requestAnimationFrame(step)
|
|
595
643
|
return
|
|
@@ -597,6 +645,7 @@ export function PetSprite(props: PetSpriteProps): ReactPortal {
|
|
|
597
645
|
walkRafRef.current = 0
|
|
598
646
|
walkTargetRef.current = null
|
|
599
647
|
setFacingRight(false)
|
|
648
|
+
setDragPos(next)
|
|
600
649
|
props.onDragEnd(next.right, next.bottom)
|
|
601
650
|
}
|
|
602
651
|
walkRafRef.current = window.requestAnimationFrame(step)
|
|
@@ -736,7 +785,7 @@ export function PetSprite(props: PetSpriteProps): ReactPortal {
|
|
|
736
785
|
...(props.visual === undefined
|
|
737
786
|
? {
|
|
738
787
|
backgroundImage: imageReady ? 'url(' + definition.atlasUrl + ')' : undefined,
|
|
739
|
-
backgroundSize: (
|
|
788
|
+
backgroundSize: (scaled.width * columns) + 'px ' + (scaled.height * (definition.atlasRows ?? rows.length)) + 'px',
|
|
740
789
|
backgroundRepeat: 'no-repeat',
|
|
741
790
|
backgroundPosition: '0 0',
|
|
742
791
|
}
|
package/src/client/index.ts
CHANGED
|
@@ -141,7 +141,7 @@ const BINDER_ATTEMPTS = 24
|
|
|
141
141
|
|
|
142
142
|
/** The snapshot an unresolved (or resolvable-but-unbound) form publishes. */
|
|
143
143
|
function pendingSnapshot<T>(status: 'loading' | 'unavailable'): ConfigFormSnapshot<T> {
|
|
144
|
-
return { status, value: undefined, base: undefined, user: undefined, revision: undefined, writable: false
|
|
144
|
+
return { status, value: undefined, base: undefined, user: undefined, revision: undefined, writable: false }
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
/**
|
|
@@ -25,6 +25,18 @@ describe('pet bubble typography (#1549)', () => {
|
|
|
25
25
|
})
|
|
26
26
|
})
|
|
27
27
|
|
|
28
|
+
describe('pet floating surfaces (issue #17)', () => {
|
|
29
|
+
it('keeps backdrop-filter off every surface that rides the sprite', () => {
|
|
30
|
+
// A blurred fixed layer makes the region behind it re-composite whenever
|
|
31
|
+
// the sprite animates or roams, and the blur runs on the CPU path under
|
|
32
|
+
// software compositing and power saving. Both the status bubble and the
|
|
33
|
+
// hover panel are 90%+ opaque, so the filter was invisible work. Comments
|
|
34
|
+
// are stripped first: the rules explain WHY the filter is gone.
|
|
35
|
+
const declared = css.replace(/\/\*[\s\S]*?\*\//g, '')
|
|
36
|
+
expect(declared).not.toContain('backdrop-filter')
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
28
40
|
describe('pet hover panel css', () => {
|
|
29
41
|
it('anchors the panel below the pet', () => {
|
|
30
42
|
const panel = css.match(/\.panel\s*\{([^}]*)\}/)?.[1] ?? ''
|
|
@@ -63,7 +63,10 @@
|
|
|
63
63
|
0 4px 12px rgba(2, 6, 23, 0.35),
|
|
64
64
|
inset 0 1px 0 rgba(226, 232, 255, 0.1),
|
|
65
65
|
0 0 12px rgba(77, 107, 254, 0.18);
|
|
66
|
-
backdrop-filter: blur
|
|
66
|
+
/* No backdrop-filter (issue #17): the glass is 90-94% opaque, so the blur was
|
|
67
|
+
invisible while forcing this permanently-mounted layer to re-composite the
|
|
68
|
+
page behind it on every sprite frame change; a blurred fixed layer costs
|
|
69
|
+
CPU on software-compositing and power-saving paths. */
|
|
67
70
|
letter-spacing: 0.02em;
|
|
68
71
|
animation: pet-bubble-in 240ms ease-out;
|
|
69
72
|
}
|
|
@@ -242,7 +245,8 @@
|
|
|
242
245
|
0 8px 24px rgba(2, 6, 23, 0.45),
|
|
243
246
|
0 0 0 1px rgba(77, 107, 254, 0.1),
|
|
244
247
|
inset 0 1px 0 rgba(226, 232, 255, 0.08);
|
|
245
|
-
backdrop-filter: blur
|
|
248
|
+
/* No backdrop-filter (issue #17): at 95-97% opacity the blur never showed,
|
|
249
|
+
and the panel overlaid the sprite's own animation while it was open. */
|
|
246
250
|
display: flex;
|
|
247
251
|
flex-direction: column;
|
|
248
252
|
gap: 7px;
|
|
@@ -657,7 +661,9 @@
|
|
|
657
661
|
0 4px 12px rgba(2, 6, 23, 0.35),
|
|
658
662
|
inset 0 1px 0 rgba(226, 232, 255, 0.1),
|
|
659
663
|
0 0 12px rgba(77, 107, 254, 0.18);
|
|
660
|
-
backdrop-filter
|
|
664
|
+
/* No backdrop-filter (issue #17), same reason as .bubbleStatus: a 92-95%
|
|
665
|
+
opaque pill gains nothing visible from the blur but pays for the filter on
|
|
666
|
+
every frame the pet animates. */
|
|
661
667
|
letter-spacing: 0.02em;
|
|
662
668
|
animation: pet-usage-in 280ms cubic-bezier(0.22, 1, 0.36, 1);
|
|
663
669
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* override, fallback settling, and the stall watchdog, all on fake timers.
|
|
5
5
|
*/
|
|
6
6
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
7
|
-
import { frames2dRenderer, type Frames2dRendererHandle, type PetFrames2dConfig } from './frames2d.ts'
|
|
7
|
+
import { frames2dRenderer, type Frames2dRendererHandle, type Frames2dTrackConfig, type PetFrames2dConfig } from './frames2d.ts'
|
|
8
8
|
import { createPhaseStream, type PhaseStream } from '../phase-stream.ts'
|
|
9
9
|
import type { PetRendererContext } from '../../contracts/renderer.ts'
|
|
10
10
|
import type { ActivityPhase } from '../../state.ts'
|
|
@@ -400,4 +400,106 @@ describe('frames2dRenderer canvas bitmap path', () => {
|
|
|
400
400
|
expect(memoFetches).toBeGreaterThan(0)
|
|
401
401
|
handle.dispose()
|
|
402
402
|
})
|
|
403
|
+
|
|
404
|
+
/** Decoded frames still resident: created minus closed. */
|
|
405
|
+
const retained = (): number => bitmaps.filter(bmp => bmp.close.mock.calls.length === 0).length
|
|
406
|
+
|
|
407
|
+
const fetchCount = (): number => (fetch as ReturnType<typeof vi.fn>).mock.calls.length
|
|
408
|
+
const fetchedUrls = (): string[] =>
|
|
409
|
+
(fetch as ReturnType<typeof vi.fn>).mock.calls.map(call => String(call[0]))
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Install a decode stub whose frames claim `side * side * 4` bytes each, so a
|
|
413
|
+
* retention budget measured in bytes is reachable without allocating it.
|
|
414
|
+
*/
|
|
415
|
+
const stubFramesOf = (side: number): void => {
|
|
416
|
+
vi.stubGlobal('createImageBitmap', vi.fn(async () => {
|
|
417
|
+
const bmp = { width: side, height: side, close: vi.fn() }
|
|
418
|
+
bitmaps.push(bmp)
|
|
419
|
+
return bmp
|
|
420
|
+
}))
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
it('serves a looping track from the cache without re-decoding it', async () => {
|
|
424
|
+
const { ctx } = canvasSetup()
|
|
425
|
+
const handle = frames2dRenderer.mount(ctx, frames2dRenderer.validateConfig(CONFIG)) as Frames2dRendererHandle
|
|
426
|
+
await flush()
|
|
427
|
+
// Mount decodes the idle frame plus its one-frame look-ahead window.
|
|
428
|
+
expect(fetchCount()).toBe(2)
|
|
429
|
+
// Two whole cycles of the two-frame track: every frame is already resident.
|
|
430
|
+
vi.advanceTimersByTime(400)
|
|
431
|
+
await flush()
|
|
432
|
+
expect(fetchCount()).toBe(2)
|
|
433
|
+
expect(retained()).toBe(2)
|
|
434
|
+
handle.dispose()
|
|
435
|
+
})
|
|
436
|
+
|
|
437
|
+
it('releases frames outside the playing window and re-decodes them on demand', async () => {
|
|
438
|
+
// 4096x4096 puts a decoded frame at 64 MB, so the 128 MiB budget holds two
|
|
439
|
+
// of them and a 20-frame track has to evict as the window advances.
|
|
440
|
+
stubFramesOf(4096)
|
|
441
|
+
const longTrack: PetFrames2dConfig = {
|
|
442
|
+
tracks: {
|
|
443
|
+
idle: {
|
|
444
|
+
frames: Array.from({ length: 20 }, (_, index) => `/pet/miku/idle/${index}.webp`),
|
|
445
|
+
durations: Array.from({ length: 20 }, () => 100),
|
|
446
|
+
loop: true,
|
|
447
|
+
},
|
|
448
|
+
},
|
|
449
|
+
phases: { idle: 'idle' },
|
|
450
|
+
}
|
|
451
|
+
const { ctx } = canvasSetup()
|
|
452
|
+
const handle = frames2dRenderer.mount(ctx, frames2dRenderer.validateConfig(longTrack)) as Frames2dRendererHandle
|
|
453
|
+
await flush(80)
|
|
454
|
+
vi.advanceTimersByTime(1500)
|
|
455
|
+
await flush(80)
|
|
456
|
+
// The run decoded the whole track, but only the playing look-ahead window
|
|
457
|
+
// of it stays resident; the rest was really closed, not just unmapped.
|
|
458
|
+
expect(bitmaps.length).toBeGreaterThanOrEqual(20)
|
|
459
|
+
const closedWhilePlaying = bitmaps.filter(bmp => bmp.close.mock.calls.length > 0).length
|
|
460
|
+
expect(closedWhilePlaying).toBeGreaterThanOrEqual(10)
|
|
461
|
+
expect(retained()).toBeLessThanOrEqual(13)
|
|
462
|
+
expect(retained()).toBeLessThan(bitmaps.length)
|
|
463
|
+
// Looping back onto a released frame re-decodes it instead of drawing a
|
|
464
|
+
// closed bitmap.
|
|
465
|
+
vi.advanceTimersByTime(600)
|
|
466
|
+
await flush(80)
|
|
467
|
+
expect(fetchedUrls().filter(url => url === '/pet/miku/idle/0.webp').length).toBeGreaterThanOrEqual(2)
|
|
468
|
+
handle.dispose()
|
|
469
|
+
await flush()
|
|
470
|
+
expect(retained()).toBe(0)
|
|
471
|
+
for (const bmp of bitmaps) expect(bmp.close).toHaveBeenCalled()
|
|
472
|
+
})
|
|
473
|
+
|
|
474
|
+
it('reclaims earlier tracks so a long multi-track run stops growing', async () => {
|
|
475
|
+
stubFramesOf(4096)
|
|
476
|
+
const tracks: Record<string, Frames2dTrackConfig> = {}
|
|
477
|
+
for (let index = 0; index < 6; index += 1) {
|
|
478
|
+
tracks['t' + index] = {
|
|
479
|
+
frames: Array.from({ length: 6 }, (_, frame) => `/pet/miku/t${index}/${frame}.webp`),
|
|
480
|
+
durations: Array.from({ length: 6 }, () => 100),
|
|
481
|
+
loop: true,
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
const multiTrack: PetFrames2dConfig = { tracks, phases: { idle: 't0' } }
|
|
485
|
+
const { ctx } = canvasSetup()
|
|
486
|
+
const handle = frames2dRenderer.mount(ctx, frames2dRenderer.validateConfig(multiTrack)) as Frames2dRendererHandle
|
|
487
|
+
await flush(60)
|
|
488
|
+
const residentAfterEachTrack: number[] = []
|
|
489
|
+
for (let index = 0; index < 6; index += 1) {
|
|
490
|
+
handle.setState('t' + index)
|
|
491
|
+
vi.advanceTimersByTime(600)
|
|
492
|
+
await flush(60)
|
|
493
|
+
residentAfterEachTrack.push(retained())
|
|
494
|
+
}
|
|
495
|
+
// Six tracks were decoded in full, and the resident set stays at the playing
|
|
496
|
+
// track plus its window: it does not grow with the number of tracks played.
|
|
497
|
+
expect(bitmaps.length).toBeGreaterThanOrEqual(30)
|
|
498
|
+
expect(Math.max(...residentAfterEachTrack)).toBeLessThanOrEqual(13)
|
|
499
|
+
expect(retained()).toBeLessThan(bitmaps.length)
|
|
500
|
+
handle.dispose()
|
|
501
|
+
await flush()
|
|
502
|
+
expect(retained()).toBe(0)
|
|
503
|
+
for (const bmp of bitmaps) expect(bmp.close).toHaveBeenCalled()
|
|
504
|
+
})
|
|
403
505
|
})
|