@thi.ng/rstream 8.3.20 → 8.4.1

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2024-04-08T14:59:30Z
3
+ - **Last updated**: 2024-04-20T14:42:45Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,18 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ### [8.4.1](https://github.com/thi-ng/umbrella/tree/@thi.ng/rstream@8.4.1) (2024-04-20)
13
+
14
+ #### ♻️ Refactoring
15
+
16
+ - update type usage ([86be8b7](https://github.com/thi-ng/umbrella/commit/86be8b7))
17
+
18
+ ## [8.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/rstream@8.4.0) (2024-04-11)
19
+
20
+ #### 🚀 Features
21
+
22
+ - add fromAsync() / asAsync() converters & tests ([df57056](https://github.com/thi-ng/umbrella/commit/df57056))
23
+
12
24
  ### [8.3.20](https://github.com/thi-ng/umbrella/tree/@thi.ng/rstream@8.3.20) (2024-04-08)
13
25
 
14
26
  #### ♻️ Refactoring
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
8
 
9
9
  > [!NOTE]
10
- > This is one of 191 standalone projects, maintained as part
10
+ > This is one of 192 standalone projects, maintained as part
11
11
  > of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
12
12
  > and anti-framework.
13
13
  >
@@ -203,10 +203,10 @@ import * as rs from "@thi.ng/rstream";
203
203
  Browser ESM import:
204
204
 
205
205
  ```html
206
- <script type="module" src="https://cdn.skypack.dev/@thi.ng/rstream"></script>
206
+ <script type="module" src="https://esm.run/@thi.ng/rstream"></script>
207
207
  ```
208
208
 
209
- [Skypack documentation](https://docs.skypack.dev/)
209
+ [JSDelivr documentation](https://www.jsdelivr.com/)
210
210
 
211
211
  For Node.js REPL:
212
212
 
@@ -214,7 +214,7 @@ For Node.js REPL:
214
214
  const rs = await import("@thi.ng/rstream");
215
215
  ```
216
216
 
217
- Package sizes (brotli'd, pre-treeshake): ESM: 6.16 KB
217
+ Package sizes (brotli'd, pre-treeshake): ESM: 6.32 KB
218
218
 
219
219
  ## Dependencies
220
220
 
package/api.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn, Fn0, IDeref, IID } from "@thi.ng/api";
1
+ import type { Fn, Fn0, IDeref, IID, Maybe } from "@thi.ng/api";
2
2
  import type { Transducer } from "@thi.ng/transducers";
3
3
  import type { Stream } from "./stream.js";
4
4
  export declare enum State {
@@ -75,7 +75,7 @@ export interface TransformableOpts<A, B> extends CommonOpts, WithTransform<A, B>
75
75
  export type ErrorHandler = Fn<any, boolean>;
76
76
  export interface WithErrorHandler {
77
77
  /**
78
- * Optional error handler to use for this
78
+ * Optional error handler to use for this stream/subscription
79
79
  */
80
80
  error: ErrorHandler;
81
81
  }
@@ -127,7 +127,7 @@ export interface ISubscriber<T> {
127
127
  __owner?: ISubscription<any, any>;
128
128
  [id: string]: any;
129
129
  }
130
- export interface ISubscribable<A> extends IDeref<A | undefined>, IID<string> {
130
+ export interface ISubscribable<A> extends IDeref<Maybe<A>>, IID<string> {
131
131
  /**
132
132
  * Adds given `sub` as child subscription.
133
133
  *
@@ -174,7 +174,7 @@ export interface ITransformable<B> {
174
174
  transform<C>(opts: WithTransform<B, C> & Partial<WithErrorHandlerOpts>): ISubscription<B, C>;
175
175
  map<C>(fn: Fn<B, C>, opts?: Partial<WithErrorHandlerOpts>): ISubscription<B, C>;
176
176
  }
177
- export interface ISubscription<A, B> extends IDeref<B | undefined>, ISubscriber<A>, ISubscribable<B>, ITransformable<B> {
177
+ export interface ISubscription<A, B> extends IDeref<Maybe<B>>, ISubscriber<A>, ISubscribable<B>, ITransformable<B> {
178
178
  parent?: ISubscription<any, A>;
179
179
  getState(): State;
180
180
  }
package/async.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { type ISubscription, type WithErrorHandlerOpts } from "./api.js";
2
+ /**
3
+ * Creates a new {@link stream} from given async iterable `src` and `opts`.
4
+ *
5
+ * @param src
6
+ * @param opts
7
+ */
8
+ export declare const fromAsync: <T>(src: AsyncIterable<T>, opts?: Partial<WithErrorHandlerOpts>) => import("./stream.js").Stream<T>;
9
+ /**
10
+ * Reverse operation of {@link fromAsync}. Returns an async iterator which
11
+ * subscribes to given `src` sub and yields values as long as the `src` is
12
+ * intact.
13
+ *
14
+ * @param src
15
+ */
16
+ export declare function asAsync<T>(src: ISubscription<any, T>): AsyncGenerator<Awaited<T>, void, unknown>;
17
+ //# sourceMappingURL=async.d.ts.map
package/async.js ADDED
@@ -0,0 +1,57 @@
1
+ import { SEMAPHORE } from "@thi.ng/api/api";
2
+ import { State } from "./api.js";
3
+ import { stream } from "./stream.js";
4
+ const fromAsync = (src, opts) => stream(($stream) => {
5
+ let active = true;
6
+ (async () => {
7
+ try {
8
+ for await (let x of src) {
9
+ if (!active)
10
+ return;
11
+ $stream.next(x);
12
+ }
13
+ $stream.done();
14
+ } catch (e) {
15
+ $stream.error(e);
16
+ }
17
+ })();
18
+ return () => active = false;
19
+ }, opts);
20
+ async function* asAsync(src) {
21
+ let resolve;
22
+ let initial;
23
+ const $newInitial = () => {
24
+ return initial = new Promise(($resolve) => {
25
+ resolve = $resolve;
26
+ });
27
+ };
28
+ const promises = [$newInitial()];
29
+ src.subscribe({
30
+ next(x) {
31
+ if (initial) {
32
+ resolve(x);
33
+ initial = void 0;
34
+ } else
35
+ promises.push(Promise.resolve(x));
36
+ },
37
+ done() {
38
+ if (initial) {
39
+ resolve(SEMAPHORE);
40
+ initial = void 0;
41
+ } else
42
+ promises.push(Promise.resolve(SEMAPHORE));
43
+ }
44
+ });
45
+ while (promises.length && src.getState() < State.ERROR) {
46
+ const res = await promises.shift();
47
+ if (res === SEMAPHORE)
48
+ break;
49
+ if (!promises.length)
50
+ promises[0] = $newInitial();
51
+ yield res;
52
+ }
53
+ }
54
+ export {
55
+ asAsync,
56
+ fromAsync
57
+ };
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./api.js";
2
2
  export * from "./asidechain.js";
3
+ export * from "./async.js";
3
4
  export * from "./atom.js";
4
5
  export * from "./bisect.js";
5
6
  export * from "./checks.js";
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./api.js";
2
2
  export * from "./asidechain.js";
3
+ export * from "./async.js";
3
4
  export * from "./atom.js";
4
5
  export * from "./bisect.js";
5
6
  export * from "./checks.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/rstream",
3
- "version": "8.3.20",
3
+ "version": "8.4.1",
4
4
  "description": "Reactive streams & subscription primitives for constructing dataflow graphs / pipelines",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -40,14 +40,14 @@
40
40
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
41
41
  },
42
42
  "dependencies": {
43
- "@thi.ng/api": "^8.10.0",
44
- "@thi.ng/arrays": "^2.9.2",
45
- "@thi.ng/associative": "^6.3.55",
46
- "@thi.ng/atom": "^5.2.42",
47
- "@thi.ng/checks": "^3.6.0",
48
- "@thi.ng/errors": "^2.5.3",
49
- "@thi.ng/logger": "^3.0.8",
50
- "@thi.ng/transducers": "^9.0.0"
43
+ "@thi.ng/api": "^8.11.0",
44
+ "@thi.ng/arrays": "^2.9.4",
45
+ "@thi.ng/associative": "^6.3.57",
46
+ "@thi.ng/atom": "^5.2.44",
47
+ "@thi.ng/checks": "^3.6.2",
48
+ "@thi.ng/errors": "^2.5.5",
49
+ "@thi.ng/logger": "^3.0.10",
50
+ "@thi.ng/transducers": "^9.0.2"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@microsoft/api-extractor": "^7.43.0",
@@ -94,6 +94,9 @@
94
94
  "./asidechain": {
95
95
  "default": "./asidechain.js"
96
96
  },
97
+ "./async": {
98
+ "default": "./async.js"
99
+ },
97
100
  "./atom": {
98
101
  "default": "./atom.js"
99
102
  },
@@ -216,5 +219,5 @@
216
219
  ],
217
220
  "year": 2017
218
221
  },
219
- "gitHead": "85ac4bd4d6d89f8e3689e2863d5bea0cecdb371c\n"
222
+ "gitHead": "8339d05ecc857e529c7325a9839c0063b89e728d\n"
220
223
  }
package/stream.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { Maybe } from "@thi.ng/api";
1
2
  import { type CommonOpts, type IStream, type ISubscriber, type ISubscription, type StreamCancel, type StreamSource, type TransformableOpts, type WithErrorHandlerOpts } from "./api.js";
2
3
  import { Subscription } from "./subscription.js";
3
4
  /**
@@ -71,7 +72,7 @@ export declare const reactive: <T>(val: T, opts?: Partial<CommonOpts>) => Stream
71
72
  */
72
73
  export declare class Stream<T> extends Subscription<T, T> implements IStream<T> {
73
74
  src?: StreamSource<T>;
74
- protected _cancel: StreamCancel | undefined;
75
+ protected _cancel: Maybe<StreamCancel>;
75
76
  protected _inited: boolean;
76
77
  constructor(opts?: Partial<WithErrorHandlerOpts>);
77
78
  constructor(src: StreamSource<T>, opts?: Partial<WithErrorHandlerOpts>);
package/subscription.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn } from "@thi.ng/api";
1
+ import type { Fn, Maybe } from "@thi.ng/api";
2
2
  import type { Reduced, Reducer, Transducer } from "@thi.ng/transducers";
3
3
  import { CloseMode, State, type CommonOpts, type ISubscriber, type ISubscription, type SubscriptionOpts, type TransformableOpts, type WithErrorHandlerOpts, type WithTransform } from "./api.js";
4
4
  /**
@@ -66,7 +66,7 @@ export declare class Subscription<A, B> implements ISubscription<A, B> {
66
66
  protected state: State;
67
67
  protected subs: Partial<ISubscriber<B>>[];
68
68
  constructor(wrapped?: Partial<ISubscriber<B>> | undefined, opts?: Partial<SubscriptionOpts<A, B>>);
69
- deref(): B | undefined;
69
+ deref(): Maybe<B>;
70
70
  getState(): State;
71
71
  protected setState(state: State): void;
72
72
  /**