effect 3.4.9 → 3.5.0

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.
Files changed (159) hide show
  1. package/RcMap/package.json +6 -0
  2. package/RcRef/package.json +6 -0
  3. package/dist/cjs/Cause.js +22 -1
  4. package/dist/cjs/Cause.js.map +1 -1
  5. package/dist/cjs/Channel.js.map +1 -1
  6. package/dist/cjs/Config.js.map +1 -1
  7. package/dist/cjs/Console.js.map +1 -1
  8. package/dist/cjs/Data.js +3 -1
  9. package/dist/cjs/Data.js.map +1 -1
  10. package/dist/cjs/Duration.js +23 -2
  11. package/dist/cjs/Duration.js.map +1 -1
  12. package/dist/cjs/Logger.js +11 -1
  13. package/dist/cjs/Logger.js.map +1 -1
  14. package/dist/cjs/PubSub.js.map +1 -1
  15. package/dist/cjs/Random.js +9 -1
  16. package/dist/cjs/Random.js.map +1 -1
  17. package/dist/cjs/RcMap.js +52 -0
  18. package/dist/cjs/RcMap.js.map +1 -0
  19. package/dist/cjs/RcRef.js +51 -0
  20. package/dist/cjs/RcRef.js.map +1 -0
  21. package/dist/cjs/Stream.js +29 -2
  22. package/dist/cjs/Stream.js.map +1 -1
  23. package/dist/cjs/index.js +6 -2
  24. package/dist/cjs/index.js.map +1 -1
  25. package/dist/cjs/internal/cause.js +21 -5
  26. package/dist/cjs/internal/cause.js.map +1 -1
  27. package/dist/cjs/internal/channel/channelExecutor.js.map +1 -1
  28. package/dist/cjs/internal/channel.js.map +1 -1
  29. package/dist/cjs/internal/core-effect.js +0 -5
  30. package/dist/cjs/internal/core-effect.js.map +1 -1
  31. package/dist/cjs/internal/core.js +15 -10
  32. package/dist/cjs/internal/core.js.map +1 -1
  33. package/dist/cjs/internal/defaultServices.js +1 -1
  34. package/dist/cjs/internal/defaultServices.js.map +1 -1
  35. package/dist/cjs/internal/fiberRuntime.js +3 -1
  36. package/dist/cjs/internal/fiberRuntime.js.map +1 -1
  37. package/dist/cjs/internal/logger.js +128 -27
  38. package/dist/cjs/internal/logger.js.map +1 -1
  39. package/dist/cjs/internal/pubsub.js +216 -36
  40. package/dist/cjs/internal/pubsub.js.map +1 -1
  41. package/dist/cjs/internal/random.js +2 -1
  42. package/dist/cjs/internal/random.js.map +1 -1
  43. package/dist/cjs/internal/rcMap.js +129 -0
  44. package/dist/cjs/internal/rcMap.js.map +1 -0
  45. package/dist/cjs/internal/rcRef.js +122 -0
  46. package/dist/cjs/internal/rcRef.js.map +1 -0
  47. package/dist/cjs/internal/stream.js +57 -11
  48. package/dist/cjs/internal/stream.js.map +1 -1
  49. package/dist/cjs/internal/version.js +1 -1
  50. package/dist/dts/Cause.d.ts +40 -1
  51. package/dist/dts/Cause.d.ts.map +1 -1
  52. package/dist/dts/Channel.d.ts +3 -3
  53. package/dist/dts/Channel.d.ts.map +1 -1
  54. package/dist/dts/Config.d.ts +5 -0
  55. package/dist/dts/Config.d.ts.map +1 -1
  56. package/dist/dts/Console.d.ts +2 -4
  57. package/dist/dts/Console.d.ts.map +1 -1
  58. package/dist/dts/Duration.d.ts +5 -0
  59. package/dist/dts/Duration.d.ts.map +1 -1
  60. package/dist/dts/Logger.d.ts +15 -0
  61. package/dist/dts/Logger.d.ts.map +1 -1
  62. package/dist/dts/PubSub.d.ts +15 -4
  63. package/dist/dts/PubSub.d.ts.map +1 -1
  64. package/dist/dts/Random.d.ts +8 -0
  65. package/dist/dts/Random.d.ts.map +1 -1
  66. package/dist/dts/RcMap.d.ts +93 -0
  67. package/dist/dts/RcMap.d.ts.map +1 -0
  68. package/dist/dts/RcRef.d.ts +83 -0
  69. package/dist/dts/RcRef.d.ts.map +1 -0
  70. package/dist/dts/Stream.d.ts +64 -26
  71. package/dist/dts/Stream.d.ts.map +1 -1
  72. package/dist/dts/index.d.ts +8 -0
  73. package/dist/dts/index.d.ts.map +1 -1
  74. package/dist/dts/internal/core-effect.d.ts.map +1 -1
  75. package/dist/dts/internal/fiberRuntime.d.ts.map +1 -1
  76. package/dist/dts/internal/logger.d.ts.map +1 -1
  77. package/dist/dts/internal/random.d.ts +1 -1
  78. package/dist/dts/internal/random.d.ts.map +1 -1
  79. package/dist/dts/internal/rcMap.d.ts +2 -0
  80. package/dist/dts/internal/rcMap.d.ts.map +1 -0
  81. package/dist/dts/internal/rcRef.d.ts +2 -0
  82. package/dist/dts/internal/rcRef.d.ts.map +1 -0
  83. package/dist/dts/internal/stream.d.ts +1 -0
  84. package/dist/dts/internal/stream.d.ts.map +1 -1
  85. package/dist/esm/Cause.js +21 -0
  86. package/dist/esm/Cause.js.map +1 -1
  87. package/dist/esm/Channel.js.map +1 -1
  88. package/dist/esm/Config.js.map +1 -1
  89. package/dist/esm/Console.js.map +1 -1
  90. package/dist/esm/Data.js +3 -1
  91. package/dist/esm/Data.js.map +1 -1
  92. package/dist/esm/Duration.js +20 -0
  93. package/dist/esm/Duration.js.map +1 -1
  94. package/dist/esm/Logger.js +10 -0
  95. package/dist/esm/Logger.js.map +1 -1
  96. package/dist/esm/PubSub.js.map +1 -1
  97. package/dist/esm/Random.js +8 -0
  98. package/dist/esm/Random.js.map +1 -1
  99. package/dist/esm/RcMap.js +44 -0
  100. package/dist/esm/RcMap.js.map +1 -0
  101. package/dist/esm/RcRef.js +43 -0
  102. package/dist/esm/RcRef.js.map +1 -0
  103. package/dist/esm/Stream.js +27 -0
  104. package/dist/esm/Stream.js.map +1 -1
  105. package/dist/esm/index.js +8 -0
  106. package/dist/esm/index.js.map +1 -1
  107. package/dist/esm/internal/cause.js +21 -5
  108. package/dist/esm/internal/cause.js.map +1 -1
  109. package/dist/esm/internal/channel/channelExecutor.js.map +1 -1
  110. package/dist/esm/internal/channel.js.map +1 -1
  111. package/dist/esm/internal/core-effect.js +0 -5
  112. package/dist/esm/internal/core-effect.js.map +1 -1
  113. package/dist/esm/internal/core.js +11 -7
  114. package/dist/esm/internal/core.js.map +1 -1
  115. package/dist/esm/internal/defaultServices.js +1 -1
  116. package/dist/esm/internal/defaultServices.js.map +1 -1
  117. package/dist/esm/internal/fiberRuntime.js +2 -0
  118. package/dist/esm/internal/fiberRuntime.js.map +1 -1
  119. package/dist/esm/internal/logger.js +126 -26
  120. package/dist/esm/internal/logger.js.map +1 -1
  121. package/dist/esm/internal/pubsub.js +216 -36
  122. package/dist/esm/internal/pubsub.js.map +1 -1
  123. package/dist/esm/internal/random.js +2 -1
  124. package/dist/esm/internal/random.js.map +1 -1
  125. package/dist/esm/internal/rcMap.js +120 -0
  126. package/dist/esm/internal/rcMap.js.map +1 -0
  127. package/dist/esm/internal/rcRef.js +112 -0
  128. package/dist/esm/internal/rcRef.js.map +1 -0
  129. package/dist/esm/internal/stream.js +53 -8
  130. package/dist/esm/internal/stream.js.map +1 -1
  131. package/dist/esm/internal/version.js +1 -1
  132. package/package.json +17 -1
  133. package/src/Cause.ts +47 -1
  134. package/src/Channel.ts +3 -3
  135. package/src/Config.ts +6 -0
  136. package/src/Console.ts +2 -4
  137. package/src/Data.ts +1 -1
  138. package/src/Duration.ts +18 -0
  139. package/src/Logger.ts +19 -0
  140. package/src/PubSub.ts +11 -4
  141. package/src/Random.ts +9 -0
  142. package/src/RcMap.ts +103 -0
  143. package/src/RcRef.ts +91 -0
  144. package/src/Stream.ts +83 -26
  145. package/src/index.ts +10 -0
  146. package/src/internal/cause.ts +26 -5
  147. package/src/internal/channel/channelExecutor.ts +1 -1
  148. package/src/internal/channel.ts +4 -3
  149. package/src/internal/core-effect.ts +0 -5
  150. package/src/internal/core.ts +19 -9
  151. package/src/internal/defaultServices.ts +1 -1
  152. package/src/internal/fiberRuntime.ts +6 -0
  153. package/src/internal/logger.ts +133 -27
  154. package/src/internal/pubsub.ts +249 -58
  155. package/src/internal/random.ts +2 -1
  156. package/src/internal/rcMap.ts +213 -0
  157. package/src/internal/rcRef.ts +172 -0
  158. package/src/internal/stream.ts +325 -111
  159. package/src/internal/version.ts +1 -1
@@ -36,16 +36,28 @@ const removeSubscribers = (subscription, pollers) => subscribers => {
36
36
  }
37
37
  };
38
38
  /** @internal */
39
- const bounded = requestedCapacity => (0, _Function.pipe)(core.sync(() => makeBoundedPubSub(requestedCapacity)), core.flatMap(atomicPubSub => makePubSub(atomicPubSub, new BackPressureStrategy())));
39
+ const bounded = capacity => core.suspend(() => {
40
+ const pubsub = makeBoundedPubSub(capacity);
41
+ return makePubSub(pubsub, new BackPressureStrategy());
42
+ });
40
43
  /** @internal */
41
44
  exports.bounded = bounded;
42
- const dropping = requestedCapacity => (0, _Function.pipe)(core.sync(() => makeBoundedPubSub(requestedCapacity)), core.flatMap(atomicPubSub => makePubSub(atomicPubSub, new DroppingStrategy())));
45
+ const dropping = capacity => core.suspend(() => {
46
+ const pubsub = makeBoundedPubSub(capacity);
47
+ return makePubSub(pubsub, new DroppingStrategy());
48
+ });
43
49
  /** @internal */
44
50
  exports.dropping = dropping;
45
- const sliding = requestedCapacity => (0, _Function.pipe)(core.sync(() => makeBoundedPubSub(requestedCapacity)), core.flatMap(atomicPubSub => makePubSub(atomicPubSub, new SlidingStrategy())));
51
+ const sliding = capacity => core.suspend(() => {
52
+ const pubsub = makeBoundedPubSub(capacity);
53
+ return makePubSub(pubsub, new SlidingStrategy());
54
+ });
46
55
  /** @internal */
47
56
  exports.sliding = sliding;
48
- const unbounded = () => (0, _Function.pipe)(core.sync(() => makeUnboundedPubSub()), core.flatMap(atomicPubSub => makePubSub(atomicPubSub, new DroppingStrategy())));
57
+ const unbounded = options => core.suspend(() => {
58
+ const pubsub = makeUnboundedPubSub(options);
59
+ return makePubSub(pubsub, new DroppingStrategy());
60
+ });
49
61
  /** @internal */
50
62
  exports.unbounded = unbounded;
51
63
  const capacity = self => self.capacity();
@@ -76,43 +88,48 @@ const publishAll = exports.publishAll = /*#__PURE__*/(0, _Function.dual)(2, (sel
76
88
  const subscribe = self => self.subscribe;
77
89
  /** @internal */
78
90
  exports.subscribe = subscribe;
79
- const makeBoundedPubSub = requestedCapacity => {
80
- ensureCapacity(requestedCapacity);
81
- if (requestedCapacity === 1) {
82
- return new BoundedPubSubSingle();
83
- } else if ((0, _Number.nextPow2)(requestedCapacity) === requestedCapacity) {
84
- return new BoundedPubSubPow2(requestedCapacity);
91
+ const makeBoundedPubSub = capacity => {
92
+ const options = typeof capacity === "number" ? {
93
+ capacity
94
+ } : capacity;
95
+ ensureCapacity(options.capacity);
96
+ const replayBuffer = options.replay && options.replay > 0 ? new ReplayBuffer(Math.ceil(options.replay)) : undefined;
97
+ if (options.capacity === 1) {
98
+ return new BoundedPubSubSingle(replayBuffer);
99
+ } else if ((0, _Number.nextPow2)(options.capacity) === options.capacity) {
100
+ return new BoundedPubSubPow2(options.capacity, replayBuffer);
85
101
  } else {
86
- return new BoundedPubSubArb(requestedCapacity);
102
+ return new BoundedPubSubArb(options.capacity, replayBuffer);
87
103
  }
88
104
  };
89
105
  /** @internal */
90
- const makeUnboundedPubSub = () => {
91
- return new UnboundedPubSub();
92
- };
106
+ const makeUnboundedPubSub = options => new UnboundedPubSub(options?.replay ? new ReplayBuffer(options.replay) : undefined);
93
107
  /** @internal */
94
108
  const makeSubscription = (pubsub, subscribers, strategy) => core.map(core.deferredMake(), deferred => unsafeMakeSubscription(pubsub, subscribers, pubsub.subscribe(), MutableQueue.unbounded(), deferred, MutableRef.make(false), strategy));
95
109
  /** @internal */
96
- const unsafeMakeSubscription = (pubsub, subscribers, subscription, pollers, shutdownHook, shutdownFlag, strategy) => {
97
- return new SubscriptionImpl(pubsub, subscribers, subscription, pollers, shutdownHook, shutdownFlag, strategy);
98
- };
110
+ const unsafeMakeSubscription = (pubsub, subscribers, subscription, pollers, shutdownHook, shutdownFlag, strategy) => new SubscriptionImpl(pubsub, subscribers, subscription, pollers, shutdownHook, shutdownFlag, strategy, pubsub.replayWindow());
99
111
  /** @internal */
100
112
  exports.unsafeMakeSubscription = unsafeMakeSubscription;
101
113
  class BoundedPubSubArb {
114
+ capacity;
115
+ replayBuffer;
102
116
  array;
103
117
  publisherIndex = 0;
104
118
  subscribers;
105
119
  subscriberCount = 0;
106
120
  subscribersIndex = 0;
107
- capacity;
108
- constructor(requestedCapacity) {
121
+ constructor(capacity, replayBuffer) {
122
+ this.capacity = capacity;
123
+ this.replayBuffer = replayBuffer;
109
124
  this.array = Array.from({
110
- length: requestedCapacity
125
+ length: capacity
111
126
  });
112
127
  this.subscribers = Array.from({
113
- length: requestedCapacity
128
+ length: capacity
114
129
  });
115
- this.capacity = requestedCapacity;
130
+ }
131
+ replayWindow() {
132
+ return this.replayBuffer ? new ReplayWindowImpl(this.replayBuffer) : emptyReplayWindow;
116
133
  }
117
134
  isEmpty() {
118
135
  return this.publisherIndex === this.subscribersIndex;
@@ -133,10 +150,16 @@ class BoundedPubSubArb {
133
150
  this.subscribers[index] = this.subscriberCount;
134
151
  this.publisherIndex += 1;
135
152
  }
153
+ if (this.replayBuffer) {
154
+ this.replayBuffer.offer(value);
155
+ }
136
156
  return true;
137
157
  }
138
158
  publishAll(elements) {
139
159
  if (this.subscriberCount === 0) {
160
+ if (this.replayBuffer) {
161
+ this.replayBuffer.offerAll(elements);
162
+ }
140
163
  return Chunk.empty();
141
164
  }
142
165
  const chunk = Chunk.fromIterable(elements);
@@ -155,6 +178,9 @@ class BoundedPubSubArb {
155
178
  this.array[index] = a;
156
179
  this.subscribers[index] = this.subscriberCount;
157
180
  this.publisherIndex += 1;
181
+ if (this.replayBuffer) {
182
+ this.replayBuffer.offer(a);
183
+ }
158
184
  }
159
185
  return Chunk.drop(chunk, iteratorIndex);
160
186
  }
@@ -165,6 +191,9 @@ class BoundedPubSubArb {
165
191
  this.subscribers[index] = 0;
166
192
  this.subscribersIndex += 1;
167
193
  }
194
+ if (this.replayBuffer) {
195
+ this.replayBuffer.slide();
196
+ }
168
197
  }
169
198
  subscribe() {
170
199
  this.subscriberCount += 1;
@@ -251,22 +280,27 @@ class BoundedPubSubArbSubscription {
251
280
  }
252
281
  /** @internal */
253
282
  class BoundedPubSubPow2 {
283
+ capacity;
284
+ replayBuffer;
254
285
  array;
255
286
  mask;
256
287
  publisherIndex = 0;
257
288
  subscribers;
258
289
  subscriberCount = 0;
259
290
  subscribersIndex = 0;
260
- capacity;
261
- constructor(requestedCapacity) {
291
+ constructor(capacity, replayBuffer) {
292
+ this.capacity = capacity;
293
+ this.replayBuffer = replayBuffer;
262
294
  this.array = Array.from({
263
- length: requestedCapacity
295
+ length: capacity
264
296
  });
265
- this.mask = requestedCapacity - 1;
297
+ this.mask = capacity - 1;
266
298
  this.subscribers = Array.from({
267
- length: requestedCapacity
299
+ length: capacity
268
300
  });
269
- this.capacity = requestedCapacity;
301
+ }
302
+ replayWindow() {
303
+ return this.replayBuffer ? new ReplayWindowImpl(this.replayBuffer) : emptyReplayWindow;
270
304
  }
271
305
  isEmpty() {
272
306
  return this.publisherIndex === this.subscribersIndex;
@@ -287,10 +321,16 @@ class BoundedPubSubPow2 {
287
321
  this.subscribers[index] = this.subscriberCount;
288
322
  this.publisherIndex += 1;
289
323
  }
324
+ if (this.replayBuffer) {
325
+ this.replayBuffer.offer(value);
326
+ }
290
327
  return true;
291
328
  }
292
329
  publishAll(elements) {
293
330
  if (this.subscriberCount === 0) {
331
+ if (this.replayBuffer) {
332
+ this.replayBuffer.offerAll(elements);
333
+ }
294
334
  return Chunk.empty();
295
335
  }
296
336
  const chunk = Chunk.fromIterable(elements);
@@ -309,6 +349,9 @@ class BoundedPubSubPow2 {
309
349
  this.array[index] = elem;
310
350
  this.subscribers[index] = this.subscriberCount;
311
351
  this.publisherIndex += 1;
352
+ if (this.replayBuffer) {
353
+ this.replayBuffer.offer(elem);
354
+ }
312
355
  }
313
356
  return Chunk.drop(chunk, iteratorIndex);
314
357
  }
@@ -319,6 +362,9 @@ class BoundedPubSubPow2 {
319
362
  this.subscribers[index] = 0;
320
363
  this.subscribersIndex += 1;
321
364
  }
365
+ if (this.replayBuffer) {
366
+ this.replayBuffer.slide();
367
+ }
322
368
  }
323
369
  subscribe() {
324
370
  this.subscriberCount += 1;
@@ -406,11 +452,18 @@ class BoundedPubSubPow2Subscription {
406
452
  }
407
453
  /** @internal */
408
454
  class BoundedPubSubSingle {
455
+ replayBuffer;
409
456
  publisherIndex = 0;
410
457
  subscriberCount = 0;
411
458
  subscribers = 0;
412
459
  value = AbsentValue;
413
460
  capacity = 1;
461
+ constructor(replayBuffer) {
462
+ this.replayBuffer = replayBuffer;
463
+ }
464
+ replayWindow() {
465
+ return this.replayBuffer ? new ReplayWindowImpl(this.replayBuffer) : emptyReplayWindow;
466
+ }
414
467
  pipe() {
415
468
  return (0, _Pipeable.pipeArguments)(this, arguments);
416
469
  }
@@ -432,10 +485,16 @@ class BoundedPubSubSingle {
432
485
  this.subscribers = this.subscriberCount;
433
486
  this.publisherIndex += 1;
434
487
  }
488
+ if (this.replayBuffer) {
489
+ this.replayBuffer.offer(value);
490
+ }
435
491
  return true;
436
492
  }
437
493
  publishAll(elements) {
438
494
  if (this.subscriberCount === 0) {
495
+ if (this.replayBuffer) {
496
+ this.replayBuffer.offerAll(elements);
497
+ }
439
498
  return Chunk.empty();
440
499
  }
441
500
  const chunk = Chunk.fromIterable(elements);
@@ -453,6 +512,9 @@ class BoundedPubSubSingle {
453
512
  this.subscribers = 0;
454
513
  this.value = AbsentValue;
455
514
  }
515
+ if (this.replayBuffer) {
516
+ this.replayBuffer.slide();
517
+ }
456
518
  }
457
519
  subscribe() {
458
520
  this.subscriberCount += 1;
@@ -514,6 +576,7 @@ class BoundedPubSubSingleSubscription {
514
576
  }
515
577
  /** @internal */
516
578
  class UnboundedPubSub {
579
+ replayBuffer;
517
580
  publisherHead = {
518
581
  value: AbsentValue,
519
582
  subscribers: 0,
@@ -523,6 +586,12 @@ class UnboundedPubSub {
523
586
  publisherIndex = 0;
524
587
  subscribersIndex = 0;
525
588
  capacity = Number.MAX_SAFE_INTEGER;
589
+ constructor(replayBuffer) {
590
+ this.replayBuffer = replayBuffer;
591
+ }
592
+ replayWindow() {
593
+ return this.replayBuffer ? new ReplayWindowImpl(this.replayBuffer) : emptyReplayWindow;
594
+ }
526
595
  isEmpty() {
527
596
  return this.publisherHead === this.publisherTail;
528
597
  }
@@ -543,6 +612,9 @@ class UnboundedPubSub {
543
612
  this.publisherTail = this.publisherTail.next;
544
613
  this.publisherIndex += 1;
545
614
  }
615
+ if (this.replayBuffer) {
616
+ this.replayBuffer.offer(value);
617
+ }
546
618
  return true;
547
619
  }
548
620
  publishAll(elements) {
@@ -550,6 +622,8 @@ class UnboundedPubSub {
550
622
  for (const a of elements) {
551
623
  this.publish(a);
552
624
  }
625
+ } else if (this.replayBuffer) {
626
+ this.replayBuffer.offerAll(elements);
553
627
  }
554
628
  return Chunk.empty();
555
629
  }
@@ -559,6 +633,9 @@ class UnboundedPubSub {
559
633
  this.publisherHead.value = AbsentValue;
560
634
  this.subscribersIndex += 1;
561
635
  }
636
+ if (this.replayBuffer) {
637
+ this.replayBuffer.slide();
638
+ }
562
639
  }
563
640
  subscribe() {
564
641
  this.publisherTail.subscribers += 1;
@@ -673,8 +750,9 @@ class SubscriptionImpl {
673
750
  shutdownHook;
674
751
  shutdownFlag;
675
752
  strategy;
753
+ replayWindow;
676
754
  [queue.DequeueTypeId] = queue.dequeueVariance;
677
- constructor(pubsub, subscribers, subscription, pollers, shutdownHook, shutdownFlag, strategy) {
755
+ constructor(pubsub, subscribers, subscription, pollers, shutdownHook, shutdownFlag, strategy, replayWindow) {
678
756
  this.pubsub = pubsub;
679
757
  this.subscribers = subscribers;
680
758
  this.subscription = subscription;
@@ -682,6 +760,7 @@ class SubscriptionImpl {
682
760
  this.shutdownHook = shutdownHook;
683
761
  this.shutdownFlag = shutdownFlag;
684
762
  this.strategy = strategy;
763
+ this.replayWindow = replayWindow;
685
764
  }
686
765
  pipe() {
687
766
  return (0, _Pipeable.pipeArguments)(this, arguments);
@@ -693,16 +772,16 @@ class SubscriptionImpl {
693
772
  return !MutableRef.get(this.shutdownFlag);
694
773
  }
695
774
  get size() {
696
- return core.suspend(() => MutableRef.get(this.shutdownFlag) ? core.interrupt : core.succeed(this.subscription.size()));
775
+ return core.suspend(() => MutableRef.get(this.shutdownFlag) ? core.interrupt : core.succeed(this.subscription.size() + this.replayWindow.remaining));
697
776
  }
698
777
  unsafeSize() {
699
778
  if (MutableRef.get(this.shutdownFlag)) {
700
779
  return Option.none();
701
780
  }
702
- return Option.some(this.subscription.size());
781
+ return Option.some(this.subscription.size() + this.replayWindow.remaining);
703
782
  }
704
783
  get isFull() {
705
- return core.map(this.size, size => size === this.capacity());
784
+ return core.suspend(() => MutableRef.get(this.shutdownFlag) ? core.interrupt : core.succeed(this.subscription.size() === this.capacity()));
706
785
  }
707
786
  get isEmpty() {
708
787
  return core.map(this.size, size => size === 0);
@@ -728,6 +807,10 @@ class SubscriptionImpl {
728
807
  if (MutableRef.get(this.shutdownFlag)) {
729
808
  return core.interrupt;
730
809
  }
810
+ if (this.replayWindow.remaining > 0) {
811
+ const message = this.replayWindow.take();
812
+ return core.succeed(message);
813
+ }
731
814
  const message = MutableQueue.isEmpty(this.pollers) ? this.subscription.poll(MutableQueue.EmptyMutableQueue) : MutableQueue.EmptyMutableQueue;
732
815
  if (message === MutableQueue.EmptyMutableQueue) {
733
816
  const deferred = core.deferredUnsafeMake(state.id());
@@ -750,6 +833,9 @@ class SubscriptionImpl {
750
833
  }
751
834
  const as = MutableQueue.isEmpty(this.pollers) ? unsafePollAllSubscription(this.subscription) : Chunk.empty();
752
835
  this.strategy.unsafeOnPubSubEmptySpace(this.pubsub, this.subscribers);
836
+ if (this.replayWindow.remaining > 0) {
837
+ return core.succeed(Chunk.appendAll(this.replayWindow.takeAll(), as));
838
+ }
753
839
  return core.succeed(as);
754
840
  });
755
841
  }
@@ -758,9 +844,17 @@ class SubscriptionImpl {
758
844
  if (MutableRef.get(this.shutdownFlag)) {
759
845
  return core.interrupt;
760
846
  }
847
+ let replay = undefined;
848
+ if (this.replayWindow.remaining >= max) {
849
+ const as = this.replayWindow.takeN(max);
850
+ return core.succeed(as);
851
+ } else if (this.replayWindow.remaining > 0) {
852
+ replay = this.replayWindow.takeAll();
853
+ max = max - replay.length;
854
+ }
761
855
  const as = MutableQueue.isEmpty(this.pollers) ? unsafePollN(this.subscription, max) : Chunk.empty();
762
856
  this.strategy.unsafeOnPubSubEmptySpace(this.pubsub, this.subscribers);
763
- return core.succeed(as);
857
+ return replay ? core.succeed(Chunk.appendAll(replay, as)) : core.succeed(as);
764
858
  });
765
859
  }
766
860
  takeBetween(min, max) {
@@ -887,9 +981,7 @@ class PubSubImpl {
887
981
  const makePubSub = (pubsub, strategy) => core.flatMap(fiberRuntime.scopeMake(), scope => core.map(core.deferredMake(), deferred => unsafeMakePubSub(pubsub, new Map(), scope, deferred, MutableRef.make(false), strategy)));
888
982
  /** @internal */
889
983
  exports.makePubSub = makePubSub;
890
- const unsafeMakePubSub = (pubsub, subscribers, scope, shutdownHook, shutdownFlag, strategy) => {
891
- return new PubSubImpl(pubsub, subscribers, scope, shutdownHook, shutdownFlag, strategy);
892
- };
984
+ const unsafeMakePubSub = (pubsub, subscribers, scope, shutdownHook, shutdownFlag, strategy) => new PubSubImpl(pubsub, subscribers, scope, shutdownHook, shutdownFlag, strategy);
893
985
  /** @internal */
894
986
  exports.unsafeMakePubSub = unsafeMakePubSub;
895
987
  const ensureCapacity = capacity => {
@@ -1100,4 +1192,92 @@ const unsafeStrategyCompleteSubscribers = (strategy, pubsub, subscribers) => {
1100
1192
  }
1101
1193
  }
1102
1194
  };
1195
+ class ReplayBuffer {
1196
+ capacity;
1197
+ constructor(capacity) {
1198
+ this.capacity = capacity;
1199
+ }
1200
+ head = {
1201
+ value: AbsentValue,
1202
+ next: null
1203
+ };
1204
+ tail = this.head;
1205
+ size = 0;
1206
+ index = 0;
1207
+ slide() {
1208
+ this.index++;
1209
+ }
1210
+ offer(a) {
1211
+ this.tail.value = a;
1212
+ this.tail.next = {
1213
+ value: AbsentValue,
1214
+ next: null
1215
+ };
1216
+ this.tail = this.tail.next;
1217
+ if (this.size === this.capacity) {
1218
+ this.head = this.head.next;
1219
+ } else {
1220
+ this.size += 1;
1221
+ }
1222
+ }
1223
+ offerAll(as) {
1224
+ for (const a of as) {
1225
+ this.offer(a);
1226
+ }
1227
+ }
1228
+ }
1229
+ class ReplayWindowImpl {
1230
+ buffer;
1231
+ head;
1232
+ index;
1233
+ remaining;
1234
+ constructor(buffer) {
1235
+ this.buffer = buffer;
1236
+ this.index = buffer.index;
1237
+ this.remaining = buffer.size;
1238
+ this.head = buffer.head;
1239
+ }
1240
+ fastForward() {
1241
+ while (this.index < this.buffer.index) {
1242
+ this.head = this.head.next;
1243
+ this.index++;
1244
+ }
1245
+ }
1246
+ take() {
1247
+ if (this.remaining === 0) {
1248
+ return undefined;
1249
+ } else if (this.index < this.buffer.index) {
1250
+ this.fastForward();
1251
+ }
1252
+ this.remaining--;
1253
+ const value = this.head.value;
1254
+ this.head = this.head.next;
1255
+ return value;
1256
+ }
1257
+ takeN(n) {
1258
+ if (this.remaining === 0) {
1259
+ return Chunk.empty();
1260
+ } else if (this.index < this.buffer.index) {
1261
+ this.fastForward();
1262
+ }
1263
+ const len = Math.min(n, this.remaining);
1264
+ const items = new Array(len);
1265
+ for (let i = 0; i < len; i++) {
1266
+ const value = this.head.value;
1267
+ this.head = this.head.next;
1268
+ items[i] = value;
1269
+ }
1270
+ this.remaining -= len;
1271
+ return Chunk.unsafeFromArray(items);
1272
+ }
1273
+ takeAll() {
1274
+ return this.takeN(this.remaining);
1275
+ }
1276
+ }
1277
+ const emptyReplayWindow = {
1278
+ remaining: 0,
1279
+ take: () => undefined,
1280
+ takeN: () => Chunk.empty(),
1281
+ takeAll: () => Chunk.empty()
1282
+ };
1103
1283
  //# sourceMappingURL=pubsub.js.map