notifier.spec.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Notifier: microtask batching, rebuild-before-notify ordering, no-listener
  3. * laziness, synchronous notifyNow, and unsubscribe.
  4. */
  5. import { describe, expect, it } from 'vitest'
  6. import { Notifier } from '../src/client/sessions/notifier.ts'
  7. const microtask = (): Promise<void> => new Promise((resolve) => { queueMicrotask(resolve) })
  8. describe('Notifier', () => {
  9. it('collapses N markDirty calls into one flush, rebuilding before notifying', async () => {
  10. const order: string[] = []
  11. const notifier = new Notifier(() => order.push('rebuild'))
  12. notifier.subscribe(() => order.push('notify'))
  13. notifier.markDirty()
  14. notifier.markDirty()
  15. notifier.markDirty()
  16. expect(order).toEqual([]) // nothing until the microtask boundary
  17. await microtask()
  18. expect(order).toEqual(['rebuild', 'notify'])
  19. })
  20. it('skips rebuild with zero listeners and ensureFresh rebuilds lazily exactly once', async () => {
  21. let rebuilds = 0
  22. const notifier = new Notifier(() => { rebuilds++ })
  23. notifier.markDirty()
  24. await microtask()
  25. expect(rebuilds).toBe(0) // lazy: kept dirty
  26. notifier.ensureFresh()
  27. expect(rebuilds).toBe(1)
  28. notifier.ensureFresh()
  29. expect(rebuilds).toBe(1) // clean: no second rebuild
  30. })
  31. it('notifyNow runs listeners synchronously (controlled-input contract)', () => {
  32. const order: string[] = []
  33. const notifier = new Notifier(() => order.push('rebuild'))
  34. notifier.subscribe(() => order.push('notify'))
  35. notifier.notifyNow()
  36. expect(order).toEqual(['rebuild', 'notify']) // before returning, no microtask needed
  37. })
  38. it('notifyNow with zero listeners stays lazy like markDirty', () => {
  39. let rebuilds = 0
  40. const notifier = new Notifier(() => { rebuilds++ })
  41. notifier.notifyNow()
  42. expect(rebuilds).toBe(0)
  43. notifier.ensureFresh()
  44. expect(rebuilds).toBe(1)
  45. })
  46. it('a scheduled flush after notifyNow already flushed is a no-op', async () => {
  47. let rebuilds = 0
  48. const notifier = new Notifier(() => { rebuilds++ })
  49. notifier.subscribe(() => undefined)
  50. notifier.markDirty() // schedules the microtask flush
  51. notifier.notifyNow() // flushes synchronously, clears dirty
  52. await microtask() // the scheduled flush finds dirty=false
  53. expect(rebuilds).toBe(1)
  54. })
  55. it('unsubscribed listeners stop receiving notifications', async () => {
  56. let calls = 0
  57. const notifier = new Notifier(() => undefined)
  58. const unsubscribe = notifier.subscribe(() => { calls++ })
  59. notifier.notifyNow()
  60. expect(calls).toBe(1)
  61. unsubscribe()
  62. notifier.markDirty()
  63. await microtask()
  64. notifier.notifyNow()
  65. expect(calls).toBe(1)
  66. })
  67. })