reactor-core-ts 2.1.2 → 2.1.4

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.md CHANGED
@@ -336,10 +336,20 @@ Flux.just('a', 'b', 'c')
336
336
  | Method | Description |
337
337
  |---|---|
338
338
  | `.cache()` | Subscribe to the source once; replay all items to subsequent subscribers. |
339
+ | `.share()` | Hot multicast with refCounting — connects on first subscriber, disconnects when the last one leaves. Items are delivered best-effort (no buffering). Re-subscribes to upstream when a new subscriber arrives after all others have left. |
339
340
  | `.switchIfEmpty(alternative)` | Switch to `alternative` if source completes without emitting. |
340
341
  | `.delaySubscription(ms)` | Delay the actual subscription to the source by `ms` milliseconds. |
341
342
  | `.pipe(producer, onRequest, onUnsubscribe)` | Low-level escape hatch for building custom downstream operators. |
342
343
 
344
+ ```typescript
345
+ // share() — multiple subscribers observe the same live upstream
346
+ const hot = Flux.interval(500).share();
347
+
348
+ hot.subscribe(v => console.log('A:', v));
349
+ setTimeout(() => hot.subscribe(v => console.log('B:', v)), 1200);
350
+ // A: 0, A: 1, A: 2, B: 2, A: 3, B: 3, …
351
+ ```
352
+
343
353
  ### FluxSink
344
354
 
345
355
  `Flux.create(emitter => …)` hands the `emitter` callback a `FluxSink<T>` with:
@@ -413,6 +423,7 @@ sub.unsubscribe(); // cancel at any time
413
423
  | `Mono.empty()` | Complete immediately without emitting. |
414
424
  | `Mono.error(err)` | Signal `onError` immediately. |
415
425
  | `Mono.firstWithValue(...sources)` | Race: emit the first value from any of the given `Mono` sources. |
426
+ | `Mono.when(...sources)` | `Mono<void>` that completes when **all** given publishers complete. Values are ignored; the first error is propagated and all other sources are cancelled. |
416
427
 
417
428
  ```typescript
418
429
  import { Mono } from 'reactor-core-ts';
@@ -429,6 +440,12 @@ Mono.justOrEmpty(null).subscribe(
429
440
  () => console.log('empty'),
430
441
  );
431
442
  // empty
443
+
444
+ // Mono.when — wait for multiple async operations to finish
445
+ Mono.when(
446
+ Mono.fromPromise(saveUser(user)),
447
+ Mono.fromPromise(sendEmail(user)),
448
+ ).subscribe(undefined, undefined, () => console.log('all done'));
432
449
  ```
433
450
 
434
451
  ### Mono Transformation
@@ -439,6 +456,8 @@ Mono.justOrEmpty(null).subscribe(
439
456
  | `.mapNotNull(fn)` | Transform `T → R | null | undefined`; if result is null/undefined, complete empty. |
440
457
  | `.flatMap(fn)` | Map the value to a `Mono<R>`, then subscribe to that inner Mono. |
441
458
  | `.flatMapMany(fn)` | Map the value to a `Flux<R>` or `Mono<R>`, returning a `Flux<R>`. |
459
+ | `.then()` | `Mono<void>` — ignore the emitted value; complete when source completes. |
460
+ | `.then(other)` | Ignore the emitted value; subscribe to `other` after source completes and return `other`'s result. |
442
461
  | `.thenReturn(value)` | Ignore the emitted value; emit `value` after source completes. |
443
462
  | `.delayElement(ms)` | Delay the emitted value by `ms` milliseconds. |
444
463
  | `.delayUntil(triggerFn)` | Hold the emitted value until the trigger publisher fires, then forward it. |
@@ -453,6 +472,18 @@ Mono.just(5)
453
472
  .flatMapMany(n => Flux.range(0, n))
454
473
  .subscribe(v => console.log(v));
455
474
  // 0 1 2 3 4
475
+
476
+ // then() — sequence operations, ignore intermediate values
477
+ Mono.just('step 1')
478
+ .then(Mono.just('step 2'))
479
+ .then(Mono.just('step 3'))
480
+ .subscribe(v => console.log(v));
481
+ // 'step 3'
482
+
483
+ // then() with no argument — completion signal only
484
+ Mono.fromPromise(deleteRecord(id))
485
+ .then()
486
+ .subscribe(undefined, undefined, () => console.log('deleted'));
456
487
  ```
457
488
 
458
489
  ### Mono Filtering
@@ -547,10 +578,16 @@ const value: number | null = await Mono.just(42).toPromise();
547
578
 
548
579
  ## Sinks
549
580
 
550
- Sinks are imperative bridges that let external code push values into a reactive stream. The `Sinks` factory returns a `SinkPublisher<T>` — an object that implements both `Sink<T>` (push API) and `Publisher<T>` (subscribe API).
581
+ Sinks are imperative bridges that let external code push values into a reactive stream. The `Sinks` factory returns a `SinkPublisher<T>` — an object that implements both `Sink<T>` (push API) and `Publisher<T>` (subscribe API), plus a convenience `asFlux()` method that wraps the sink as a full-featured `Flux<T>` with all operators available.
551
582
 
552
583
  ```typescript
553
584
  import { Sinks, Flux } from 'reactor-core-ts';
585
+
586
+ // asFlux() gives access to all Flux operators
587
+ const sink = Sinks.many().replay().all<number>();
588
+ sink.next(1);
589
+ sink.next(2);
590
+ sink.asFlux().map(n => n * 10).subscribe(v => console.log(v)); // 10 20
554
591
  ```
555
592
 
556
593
  ### `Sinks.empty<T>()`
package/dist/index.d.mts CHANGED
@@ -569,6 +569,56 @@ interface FluxSink<T> extends Sink<T> {
569
569
  onDispose(fn: () => void): FluxSink<T>;
570
570
  }
571
571
 
572
+ /**
573
+ * Minimal interface compatible with the browser `WebSocket` API and the `ws` npm package.
574
+ *
575
+ * Any object that exposes `onmessage`, `onerror`, `onclose`, and `close()` satisfies
576
+ * this interface and can be passed to {@link Flux.fromWebSocket}.
577
+ *
578
+ * @typeParam T - The type of `event.data` received in each message.
579
+ */
580
+ interface WebSocketLike<T = unknown> {
581
+ /** Assigned when a message arrives; `event.data` carries the payload. */
582
+ onmessage: ((event: {
583
+ data: T;
584
+ }) => void) | null;
585
+ /** Assigned when a transport-level error occurs. */
586
+ onerror: ((event: unknown) => void) | null;
587
+ /** Assigned when the connection is closed. */
588
+ onclose: ((event: {
589
+ code: number;
590
+ reason: string | Buffer;
591
+ }) => void) | null;
592
+ /** Close the connection, optionally with a status code and reason. */
593
+ close(code?: number, reason?: string | Buffer): void;
594
+ /** Current connection state (0 = CONNECTING, 1 = OPEN, 2 = CLOSING, 3 = CLOSED). */
595
+ readonly readyState: number;
596
+ }
597
+ /**
598
+ * Minimal DOM-style event target interface (`addEventListener` / `removeEventListener`).
599
+ * Satisfied by browser `EventTarget`, `HTMLElement`, `window`, `document`, etc.
600
+ *
601
+ * @typeParam T - The event type emitted by the target.
602
+ */
603
+ interface DOMEventTargetLike<T = Event> {
604
+ addEventListener(type: string, listener: (event: T) => void): void;
605
+ removeEventListener(type: string, listener: (event: T) => void): void;
606
+ }
607
+ /**
608
+ * Minimal Node.js-style event emitter interface (`on` / `off`).
609
+ * Satisfied by Node.js `EventEmitter` and any object with the same shape.
610
+ *
611
+ * @typeParam T - The event data type emitted by the emitter.
612
+ */
613
+ interface NodeEventEmitterLike<T = unknown> {
614
+ on(event: string | symbol, listener: (data: T) => void): void;
615
+ off(event: string | symbol, listener: (data: T) => void): void;
616
+ }
617
+ /**
618
+ * Union of {@link DOMEventTargetLike} and {@link NodeEventEmitterLike}.
619
+ * Accepted by {@link Flux.fromEvent}.
620
+ */
621
+ type EventSourceLike<T = unknown> = DOMEventTargetLike<T> | NodeEventEmitterLike<T>;
572
622
  /**
573
623
  * A cold publisher of 0 to N items with full backpressure support.
574
624
  *
@@ -743,6 +793,68 @@ declare class Flux<T> extends AbstractPipePublisher<T, Flux<T>> implements PipeP
743
793
  * ```
744
794
  */
745
795
  static using<T, R>(resourceSupplier: () => R, sourceFactory: (resource: R) => Publisher<T>, cleanup: (resource: R) => void): Flux<T>;
796
+ /**
797
+ * Creates a `Flux<T>` that emits each message received on a `WebSocket`.
798
+ *
799
+ * - Each message's `data` is emitted as the next item.
800
+ * - The stream **completes** when the socket is closed (any close code).
801
+ * - A transport-level error event terminates the stream with an `Error`.
802
+ * - **Unsubscribing** closes the socket gracefully (sends a normal-closure frame).
803
+ *
804
+ * Compatible with the browser `WebSocket` API and the `ws` npm package. Any object
805
+ * satisfying the {@link WebSocketLike} interface can be passed.
806
+ *
807
+ * @param ws - An open (or opening) WebSocket instance.
808
+ * @returns A `Flux<T>` backed by the WebSocket message stream.
809
+ *
810
+ * @example
811
+ * ```typescript
812
+ * // Browser
813
+ * Flux.fromWebSocket<string>(new WebSocket('wss://example.com/chat'))
814
+ * .map(msg => JSON.parse(msg))
815
+ * .subscribe(msg => console.log(msg));
816
+ *
817
+ * // Node.js (ws package)
818
+ * import WebSocket from 'ws';
819
+ * Flux.fromWebSocket<Buffer>(new WebSocket('ws://localhost:8080'))
820
+ * .map(buf => buf.toString())
821
+ * .subscribe(text => console.log(text));
822
+ * ```
823
+ */
824
+ static fromWebSocket<T = unknown>(ws: WebSocketLike<T>): Flux<T>;
825
+ /**
826
+ * Creates an infinite `Flux<T>` that emits every event of type `eventName` fired on
827
+ * `target`. The stream never completes on its own — call `unsubscribe()` or chain a
828
+ * limiting operator (e.g. `.take(n)`, `.takeUntilOther(stop$)`) to terminate it.
829
+ *
830
+ * Works with:
831
+ * - **DOM targets** (`EventTarget`: `HTMLElement`, `window`, `document`, …) — uses
832
+ * `addEventListener` / `removeEventListener`.
833
+ * - **Node.js emitters** (any object with `on` / `off`) — uses those methods directly.
834
+ *
835
+ * Events fired while downstream has no demand are **buffered** (via {@link Flux.create})
836
+ * and delivered once demand is available.
837
+ *
838
+ * @param target - A DOM `EventTarget` or Node.js-style event emitter.
839
+ * @param eventName - The event name / type to listen for.
840
+ * @returns An infinite cold `Flux<T>` of matching events.
841
+ *
842
+ * @example
843
+ * ```typescript
844
+ * // Browser — click events on a button
845
+ * Flux.fromEvent<MouseEvent>(document.getElementById('btn')!, 'click')
846
+ * .map(e => ({ x: e.clientX, y: e.clientY }))
847
+ * .subscribe(pos => console.log(pos));
848
+ *
849
+ * // Node.js EventEmitter
850
+ * import { EventEmitter } from 'events';
851
+ * const emitter = new EventEmitter();
852
+ * Flux.fromEvent<string>(emitter, 'data')
853
+ * .take(5)
854
+ * .subscribe(v => console.log(v));
855
+ * ```
856
+ */
857
+ static fromEvent<T = Event>(target: EventSourceLike<T>, eventName: string): Flux<T>;
746
858
  /**
747
859
  * Merges N publishers by subscribing to all of them concurrently and interleaving
748
860
  * their items as they arrive.
@@ -2096,6 +2208,27 @@ declare class Mono<T> extends AbstractPipePublisher<T, Mono<T>> implements PipeP
2096
2208
  * ```
2097
2209
  */
2098
2210
  thenReturn<R>(value: R): Mono<R>;
2211
+ /**
2212
+ * Ignores the value emitted by this `Mono` and, upon completion, subscribes to `other`
2213
+ * and forwards its result downstream.
2214
+ *
2215
+ * If `other` is omitted, returns a `Mono<void>` that completes when this `Mono` completes.
2216
+ * Errors from this `Mono` are propagated without subscribing to `other`.
2217
+ *
2218
+ * @param other - Optional `Mono<V>` to subscribe to after this `Mono` completes.
2219
+ * @returns `Mono<void>` when called with no argument; `Mono<V>` when `other` is provided.
2220
+ *
2221
+ * @example
2222
+ * ```typescript
2223
+ * // Sequencing: run A, then run B and return B's value
2224
+ * Mono.just('ignored').then(Mono.just(42)).subscribe(v => console.log(v)); // 42
2225
+ *
2226
+ * // Completion signal only
2227
+ * Mono.just('ignored').then().subscribe(undefined, undefined, () => console.log('done'));
2228
+ * ```
2229
+ */
2230
+ then(): Mono<void>;
2231
+ then<V>(other: Mono<V>): Mono<V>;
2099
2232
  /**
2100
2233
  * Delays delivery of the emitted value by `ms` milliseconds.
2101
2234
  *
@@ -2399,4 +2532,4 @@ declare const Sinks: {
2399
2532
  };
2400
2533
  };
2401
2534
 
2402
- export { type CancellableScheduler, Flux, type FluxSink, type GroupedFlux, Mono, type Publisher, type Scheduler, Schedulers, Signal, type Sink, type SinkPublisher, Sinks, type Subscriber, type Subscription };
2535
+ export { type CancellableScheduler, type DOMEventTargetLike, type EventSourceLike, Flux, type FluxSink, type GroupedFlux, Mono, type NodeEventEmitterLike, type Publisher, type Scheduler, Schedulers, Signal, type Sink, type SinkPublisher, Sinks, type Subscriber, type Subscription, type WebSocketLike };
package/dist/index.d.ts CHANGED
@@ -569,6 +569,56 @@ interface FluxSink<T> extends Sink<T> {
569
569
  onDispose(fn: () => void): FluxSink<T>;
570
570
  }
571
571
 
572
+ /**
573
+ * Minimal interface compatible with the browser `WebSocket` API and the `ws` npm package.
574
+ *
575
+ * Any object that exposes `onmessage`, `onerror`, `onclose`, and `close()` satisfies
576
+ * this interface and can be passed to {@link Flux.fromWebSocket}.
577
+ *
578
+ * @typeParam T - The type of `event.data` received in each message.
579
+ */
580
+ interface WebSocketLike<T = unknown> {
581
+ /** Assigned when a message arrives; `event.data` carries the payload. */
582
+ onmessage: ((event: {
583
+ data: T;
584
+ }) => void) | null;
585
+ /** Assigned when a transport-level error occurs. */
586
+ onerror: ((event: unknown) => void) | null;
587
+ /** Assigned when the connection is closed. */
588
+ onclose: ((event: {
589
+ code: number;
590
+ reason: string | Buffer;
591
+ }) => void) | null;
592
+ /** Close the connection, optionally with a status code and reason. */
593
+ close(code?: number, reason?: string | Buffer): void;
594
+ /** Current connection state (0 = CONNECTING, 1 = OPEN, 2 = CLOSING, 3 = CLOSED). */
595
+ readonly readyState: number;
596
+ }
597
+ /**
598
+ * Minimal DOM-style event target interface (`addEventListener` / `removeEventListener`).
599
+ * Satisfied by browser `EventTarget`, `HTMLElement`, `window`, `document`, etc.
600
+ *
601
+ * @typeParam T - The event type emitted by the target.
602
+ */
603
+ interface DOMEventTargetLike<T = Event> {
604
+ addEventListener(type: string, listener: (event: T) => void): void;
605
+ removeEventListener(type: string, listener: (event: T) => void): void;
606
+ }
607
+ /**
608
+ * Minimal Node.js-style event emitter interface (`on` / `off`).
609
+ * Satisfied by Node.js `EventEmitter` and any object with the same shape.
610
+ *
611
+ * @typeParam T - The event data type emitted by the emitter.
612
+ */
613
+ interface NodeEventEmitterLike<T = unknown> {
614
+ on(event: string | symbol, listener: (data: T) => void): void;
615
+ off(event: string | symbol, listener: (data: T) => void): void;
616
+ }
617
+ /**
618
+ * Union of {@link DOMEventTargetLike} and {@link NodeEventEmitterLike}.
619
+ * Accepted by {@link Flux.fromEvent}.
620
+ */
621
+ type EventSourceLike<T = unknown> = DOMEventTargetLike<T> | NodeEventEmitterLike<T>;
572
622
  /**
573
623
  * A cold publisher of 0 to N items with full backpressure support.
574
624
  *
@@ -743,6 +793,68 @@ declare class Flux<T> extends AbstractPipePublisher<T, Flux<T>> implements PipeP
743
793
  * ```
744
794
  */
745
795
  static using<T, R>(resourceSupplier: () => R, sourceFactory: (resource: R) => Publisher<T>, cleanup: (resource: R) => void): Flux<T>;
796
+ /**
797
+ * Creates a `Flux<T>` that emits each message received on a `WebSocket`.
798
+ *
799
+ * - Each message's `data` is emitted as the next item.
800
+ * - The stream **completes** when the socket is closed (any close code).
801
+ * - A transport-level error event terminates the stream with an `Error`.
802
+ * - **Unsubscribing** closes the socket gracefully (sends a normal-closure frame).
803
+ *
804
+ * Compatible with the browser `WebSocket` API and the `ws` npm package. Any object
805
+ * satisfying the {@link WebSocketLike} interface can be passed.
806
+ *
807
+ * @param ws - An open (or opening) WebSocket instance.
808
+ * @returns A `Flux<T>` backed by the WebSocket message stream.
809
+ *
810
+ * @example
811
+ * ```typescript
812
+ * // Browser
813
+ * Flux.fromWebSocket<string>(new WebSocket('wss://example.com/chat'))
814
+ * .map(msg => JSON.parse(msg))
815
+ * .subscribe(msg => console.log(msg));
816
+ *
817
+ * // Node.js (ws package)
818
+ * import WebSocket from 'ws';
819
+ * Flux.fromWebSocket<Buffer>(new WebSocket('ws://localhost:8080'))
820
+ * .map(buf => buf.toString())
821
+ * .subscribe(text => console.log(text));
822
+ * ```
823
+ */
824
+ static fromWebSocket<T = unknown>(ws: WebSocketLike<T>): Flux<T>;
825
+ /**
826
+ * Creates an infinite `Flux<T>` that emits every event of type `eventName` fired on
827
+ * `target`. The stream never completes on its own — call `unsubscribe()` or chain a
828
+ * limiting operator (e.g. `.take(n)`, `.takeUntilOther(stop$)`) to terminate it.
829
+ *
830
+ * Works with:
831
+ * - **DOM targets** (`EventTarget`: `HTMLElement`, `window`, `document`, …) — uses
832
+ * `addEventListener` / `removeEventListener`.
833
+ * - **Node.js emitters** (any object with `on` / `off`) — uses those methods directly.
834
+ *
835
+ * Events fired while downstream has no demand are **buffered** (via {@link Flux.create})
836
+ * and delivered once demand is available.
837
+ *
838
+ * @param target - A DOM `EventTarget` or Node.js-style event emitter.
839
+ * @param eventName - The event name / type to listen for.
840
+ * @returns An infinite cold `Flux<T>` of matching events.
841
+ *
842
+ * @example
843
+ * ```typescript
844
+ * // Browser — click events on a button
845
+ * Flux.fromEvent<MouseEvent>(document.getElementById('btn')!, 'click')
846
+ * .map(e => ({ x: e.clientX, y: e.clientY }))
847
+ * .subscribe(pos => console.log(pos));
848
+ *
849
+ * // Node.js EventEmitter
850
+ * import { EventEmitter } from 'events';
851
+ * const emitter = new EventEmitter();
852
+ * Flux.fromEvent<string>(emitter, 'data')
853
+ * .take(5)
854
+ * .subscribe(v => console.log(v));
855
+ * ```
856
+ */
857
+ static fromEvent<T = Event>(target: EventSourceLike<T>, eventName: string): Flux<T>;
746
858
  /**
747
859
  * Merges N publishers by subscribing to all of them concurrently and interleaving
748
860
  * their items as they arrive.
@@ -2096,6 +2208,27 @@ declare class Mono<T> extends AbstractPipePublisher<T, Mono<T>> implements PipeP
2096
2208
  * ```
2097
2209
  */
2098
2210
  thenReturn<R>(value: R): Mono<R>;
2211
+ /**
2212
+ * Ignores the value emitted by this `Mono` and, upon completion, subscribes to `other`
2213
+ * and forwards its result downstream.
2214
+ *
2215
+ * If `other` is omitted, returns a `Mono<void>` that completes when this `Mono` completes.
2216
+ * Errors from this `Mono` are propagated without subscribing to `other`.
2217
+ *
2218
+ * @param other - Optional `Mono<V>` to subscribe to after this `Mono` completes.
2219
+ * @returns `Mono<void>` when called with no argument; `Mono<V>` when `other` is provided.
2220
+ *
2221
+ * @example
2222
+ * ```typescript
2223
+ * // Sequencing: run A, then run B and return B's value
2224
+ * Mono.just('ignored').then(Mono.just(42)).subscribe(v => console.log(v)); // 42
2225
+ *
2226
+ * // Completion signal only
2227
+ * Mono.just('ignored').then().subscribe(undefined, undefined, () => console.log('done'));
2228
+ * ```
2229
+ */
2230
+ then(): Mono<void>;
2231
+ then<V>(other: Mono<V>): Mono<V>;
2099
2232
  /**
2100
2233
  * Delays delivery of the emitted value by `ms` milliseconds.
2101
2234
  *
@@ -2399,4 +2532,4 @@ declare const Sinks: {
2399
2532
  };
2400
2533
  };
2401
2534
 
2402
- export { type CancellableScheduler, Flux, type FluxSink, type GroupedFlux, Mono, type Publisher, type Scheduler, Schedulers, Signal, type Sink, type SinkPublisher, Sinks, type Subscriber, type Subscription };
2535
+ export { type CancellableScheduler, type DOMEventTargetLike, type EventSourceLike, Flux, type FluxSink, type GroupedFlux, Mono, type NodeEventEmitterLike, type Publisher, type Scheduler, Schedulers, Signal, type Sink, type SinkPublisher, Sinks, type Subscriber, type Subscription, type WebSocketLike };