notifier.client.spec.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Notifier: microtask/frame batching, rebuild-before-notify ordering,
  3. * no-listener laziness, synchronous notifyNow, and unsubscribe.
  4. */
  5. import { afterEach, describe, expect, it, vi } from 'vitest'
  6. import { Notifier } from '../src/client/sessions/notifier.ts'
  7. const microtask = (): Promise<void> => new Promise((resolve) => { queueMicrotask(resolve) })
  8. afterEach(() => {
  9. vi.unstubAllGlobals()
  10. })
  11. describe('Session notifier', () => {
  12. it('collapses N markDirty calls into one flush, rebuilding before notifying', async () => {
  13. const order: string[] = []
  14. const notifier = new Notifier(() => order.push('rebuild'))
  15. notifier.subscribe(() => order.push('notify'))
  16. notifier.markDirty()
  17. notifier.markDirty()
  18. notifier.markDirty()
  19. expect(order).toEqual([]) // nothing until the microtask boundary
  20. await microtask()
  21. expect(order).toEqual(['rebuild', 'notify'])
  22. })
  23. it('skips rebuild with zero listeners and ensureFresh rebuilds lazily exactly once', async () => {
  24. let rebuilds = 0
  25. const notifier = new Notifier(() => { rebuilds++ })
  26. notifier.markDirty()
  27. await microtask()
  28. expect(rebuilds).toBe(0) // lazy: kept dirty
  29. notifier.ensureFresh()
  30. expect(rebuilds).toBe(1)
  31. notifier.ensureFresh()
  32. expect(rebuilds).toBe(1) // clean: no second rebuild
  33. })
  34. it('notifyNow runs listeners synchronously (controlled-input contract)', () => {
  35. const order: string[] = []
  36. const notifier = new Notifier(() => order.push('rebuild'))
  37. notifier.subscribe(() => order.push('notify'))
  38. notifier.notifyNow()
  39. expect(order).toEqual(['rebuild', 'notify']) // before returning, no microtask needed
  40. })
  41. it('notifyNow with zero listeners stays lazy like markDirty', () => {
  42. let rebuilds = 0
  43. const notifier = new Notifier(() => { rebuilds++ })
  44. notifier.notifyNow()
  45. expect(rebuilds).toBe(0)
  46. notifier.ensureFresh()
  47. expect(rebuilds).toBe(1)
  48. })
  49. it('a scheduled flush after notifyNow already flushed is a no-op', async () => {
  50. let rebuilds = 0
  51. const notifier = new Notifier(() => { rebuilds++ })
  52. notifier.subscribe(() => undefined)
  53. notifier.markDirty() // schedules the microtask flush
  54. notifier.notifyNow() // flushes synchronously, clears dirty
  55. await microtask() // the scheduled flush finds dirty=false
  56. expect(rebuilds).toBe(1)
  57. })
  58. it('collapses frame-dirty changes into one cumulative frame publication', () => {
  59. const frames: FrameRequestCallback[] = []
  60. vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => {
  61. frames.push(callback)
  62. return frames.length
  63. })
  64. const order: string[] = []
  65. const notifier = new Notifier(() => order.push('rebuild'))
  66. notifier.subscribe(() => order.push('notify'))
  67. notifier.markFrameDirty()
  68. notifier.markFrameDirty()
  69. notifier.markFrameDirty()
  70. expect(order).toEqual([])
  71. expect(frames).toHaveLength(1)
  72. frames.shift()!(0)
  73. expect(order).toEqual(['rebuild', 'notify'])
  74. })
  75. it('lets a structural microtask publication supersede a pending frame', async () => {
  76. const frames: FrameRequestCallback[] = []
  77. vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => {
  78. frames.push(callback)
  79. return frames.length
  80. })
  81. let notifications = 0
  82. const notifier = new Notifier(() => undefined)
  83. notifier.subscribe(() => { notifications++ })
  84. notifier.markFrameDirty()
  85. notifier.markDirty()
  86. await microtask()
  87. expect(notifications).toBe(1)
  88. frames.shift()!(0)
  89. expect(notifications).toBe(1)
  90. })
  91. it('falls back to microtask batching when animation frames are unavailable', async () => {
  92. let notifications = 0
  93. const notifier = new Notifier(() => undefined)
  94. notifier.subscribe(() => { notifications++ })
  95. notifier.markFrameDirty()
  96. notifier.markFrameDirty()
  97. expect(notifications).toBe(0)
  98. await microtask()
  99. expect(notifications).toBe(1)
  100. })
  101. it('unsubscribed listeners stop receiving notifications', async () => {
  102. let calls = 0
  103. const notifier = new Notifier(() => undefined)
  104. const unsubscribe = notifier.subscribe(() => { calls++ })
  105. notifier.notifyNow()
  106. expect(calls).toBe(1)
  107. unsubscribe()
  108. notifier.markDirty()
  109. await microtask()
  110. notifier.notifyNow()
  111. expect(calls).toBe(1)
  112. })
  113. })