@thi.ng/rstream 9.1.0 → 9.2.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-12-04T15:08:17Z
3
+ - **Last updated**: 2024-12-12T10:11:58Z
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,14 @@ 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
+ ## [9.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/rstream@9.2.0) (2024-12-05)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add fromTuple(), update StreamObj impl ([ef691cc](https://github.com/thi-ng/umbrella/commit/ef691cc))
17
+ - update docs
18
+ - add tests
19
+
12
20
  ## [9.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/rstream@9.1.0) (2024-12-04)
13
21
 
14
22
  #### 🚀 Features
package/README.md CHANGED
@@ -215,7 +215,7 @@ For Node.js REPL:
215
215
  const rs = await import("@thi.ng/rstream");
216
216
  ```
217
217
 
218
- Package sizes (brotli'd, pre-treeshake): ESM: 6.28 KB
218
+ Package sizes (brotli'd, pre-treeshake): ESM: 6.34 KB
219
219
 
220
220
  ## Dependencies
221
221
 
@@ -451,6 +451,7 @@ s.next(42);
451
451
  - [fromPromise()](https://docs.thi.ng/umbrella/rstream/functions/fromPromise.html) - single value stream from promise
452
452
  - [fromPromises()](https://docs.thi.ng/umbrella/rstream/functions/fromPromises.html) - results from multiple promise
453
453
  - [fromRAF()](https://docs.thi.ng/umbrella/rstream/functions/fromRAF.html) - requestAnimationFrame() counter (w/ node fallback)
454
+ - [fromTuple()](https://docs.thi.ng/umbrella/rstream/functions/fromTuple.html) - tuple/vector per-component streams
454
455
  - [fromView()](https://docs.thi.ng/umbrella/rstream/functions/fromView.html) - derived view value changes (see [@thi.ng/atom](https://github.com/thi-ng/umbrella/tree/develop/packages/atom))
455
456
  - [fromWorker()](https://docs.thi.ng/umbrella/rstream/functions/fromWorker.html) - messages received from worker
456
457
  - [toggle()](https://docs.thi.ng/umbrella/rstream/functions/toggle.html) - on/off switch-like stream
package/object.d.ts CHANGED
@@ -38,36 +38,26 @@ export interface StreamObjOpts<T, K extends Keys<T>> extends CommonOpts {
38
38
  }
39
39
  /**
40
40
  * Takes an arbitrary object `src` and object of options (see
41
- * {@link StreamObjOpts}). Creates a new object and for each selected
42
- * key creates a new stream, optionally seeded with the key's value in
43
- * `src`. Returns new object of streams.
41
+ * {@link StreamObjOpts}). Creates a new object and for each selected key
42
+ * creates a new subscription, optionally seeded with the key's value in `src`.
43
+ * Returns new {@link StreamObj}.
44
44
  *
45
45
  * @remarks
46
- * The structure of the returned object is
47
- * {@link StreamObj | as follows}:
48
- *
49
- * ```text
50
- * {
51
- * streams: { ... },
52
- * next(x): void;
53
- * done(): void;
54
- * }
55
- * ```
56
- *
57
- * All streams will be stored under `streams`. The `next()` and `done()`
58
- * functions/methods allow the object itself to be used as subscriber
59
- * for an upstream subscribable (see 2nd example below):
60
- *
61
- * - `next()` - takes a object of same type as `src` and feeds each
62
- * key's new value into its respective stream. If the `defaults`
63
- * option is given, `undefined` key values are replaced with their
64
- * specified default. If `dedupe` is enabled (default) only changed
65
- * values (as per `equiv` predicate option) will be propagated
66
- * downstream.
67
- * - `done()` - calls {@link ISubscriber.done} on all streams
68
- *
69
- * The optional `opts` arg is used to customize overall behavior of
70
- * `fromObject` and specify shared options for *all* created streams.
46
+ * The options arg is used to customize overall behavior of `fromObject` and
47
+ * specify shared options for *all* created streams.
48
+ *
49
+ * A {@link StreamObj} is a full {@link Subscription}, in which additionally all
50
+ * configured key streams are exposed under `streams`. The
51
+ * {@link StreamObj.next} and {@link StreamObj.done} methods allow the
52
+ * {@link StreamObj} itself to be used as subscriber for an upstream
53
+ * subscribable (see 2nd example below):
54
+ *
55
+ * {@link StreamObj.next} receives an object of same type as `src` and feeds
56
+ * each key's new value into its respective {@link StreamObj.streams}. If the
57
+ * {@link StreamObjOpts.defaults} option is given, `undefined` key values are
58
+ * replaced with their specified default. If {@link StreamObjOpts.dedupe} is
59
+ * enabled (default) only changed values (as per {@link StreamObjOpts.equiv}
60
+ * predicate option) will be propagated downstream.
71
61
  *
72
62
  * @example
73
63
  * ```ts tangle:../export/from-object.ts
@@ -99,7 +89,7 @@ export interface StreamObjOpts<T, K extends Keys<T>> extends CommonOpts {
99
89
  * obj.streams.b.subscribe(trace("b"));
100
90
  *
101
91
  * const src = subscription<Foo, Foo>();
102
- * // use as subscriber
92
+ * // use `obj` as subscriber itself
103
93
  * src.subscribe(obj);
104
94
  *
105
95
  * src.next({ a: 1, b: "foo" });
@@ -111,6 +101,44 @@ export interface StreamObjOpts<T, K extends Keys<T>> extends CommonOpts {
111
101
  * @param opts -
112
102
  */
113
103
  export declare const fromObject: <T extends object, K extends Keys<T>>(src: T, opts?: Partial<StreamObjOpts<T, K>>) => StreamObj<T, K>;
104
+ /**
105
+ * Syntax sugar for {@link fromObject} for tuple/arrays. Returns a
106
+ * {@link StreamObj} which provides individual subscriptions for each tuple
107
+ * element, i.e. for 1:N fanout.
108
+ *
109
+ * @remarks
110
+ * This construct is very useful for UI purposes, helping to provide both
111
+ * finegrained and tuple-based reactive state for UI components used to edit
112
+ * tuple/vector values (e.g. via individual per-tuple-element input
113
+ * fields/controls).
114
+ *
115
+ * @example
116
+ * ```ts tangle:../export/from-tuple.ts
117
+ * import { fromTuple, subscription, trace } from "@thi.ng/rstream";
118
+ *
119
+ * const tup = fromTuple([10, 20, 30]);
120
+ *
121
+ * tup.streams[0].subscribe(trace("[0]:"));
122
+ * tup.streams[1].subscribe(trace("[1]:"));
123
+ * tup.streams[2].subscribe(trace("[2]:"));
124
+ *
125
+ * // [0]: 10
126
+ * // [1]: 20
127
+ * // [2]: 30
128
+ *
129
+ * tup.next([100,20,30]);
130
+ *
131
+ * // [0]: 100
132
+ * // (the two other streams didn't update since their values haven't changed)
133
+ * ```
134
+ *
135
+ * @param src
136
+ * @param opts
137
+ */
138
+ export declare const fromTuple: <T>(src: T[], opts?: Partial<StreamObjOpts<T[], number>>) => StreamObj<T[], number>;
139
+ /**
140
+ * See {@link fromObject} for details.
141
+ */
114
142
  export declare class StreamObj<T extends object, K extends Keys<T>> extends Subscription<T, T> {
115
143
  /**
116
144
  * Object of managed & typed streams for registered keys.
@@ -120,16 +148,18 @@ export declare class StreamObj<T extends object, K extends Keys<T>> extends Subs
120
148
  defaults?: Partial<T>;
121
149
  constructor(src: T, opts?: Partial<StreamObjOpts<T, K>>);
122
150
  /**
123
- * Feeds new values from `x` to each registered key's stream.
124
- * Satifies {@link ISubscriber.next} interface.
151
+ * Receives an object of configured type and feeds each key's new value into
152
+ * its respective {@link StreamObj.streams}. If the
153
+ * {@link StreamObjOpts.defaults} option is given, `undefined` key values
154
+ * are replaced with their specified default. If
155
+ * {@link StreamObjOpts.dedupe} is enabled (default) only changed values (as
156
+ * per {@link StreamObjOpts.equiv} predicate option) will be propagated
157
+ * downstream.
125
158
  *
126
159
  * @param x -
127
160
  */
128
161
  next(x: T): void;
129
- /**
130
- * Calls {@link ISubscriber.done} for all streams created. Satifies
131
- * {@link ISubscriber.done} interface.
132
- */
133
162
  done(): void;
163
+ unsubscribe(sub?: ISubscription<T, any> | undefined): boolean;
134
164
  }
135
165
  //# sourceMappingURL=object.d.ts.map
package/object.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import { dedupe } from "@thi.ng/transducers/dedupe";
2
+ import { range } from "@thi.ng/transducers/range";
2
3
  import { __optsWithID } from "./idgen.js";
3
4
  import { Subscription, subscription } from "./subscription.js";
4
5
  const fromObject = (src, opts = {}) => new StreamObj(src, opts);
6
+ const fromTuple = (src, opts) => new StreamObj(src, { keys: [...range(src.length)], ...opts });
5
7
  class StreamObj extends Subscription {
6
8
  /**
7
9
  * Object of managed & typed streams for registered keys.
@@ -26,8 +28,13 @@ class StreamObj extends Subscription {
26
28
  opts.initial !== false && this.next(src);
27
29
  }
28
30
  /**
29
- * Feeds new values from `x` to each registered key's stream.
30
- * Satifies {@link ISubscriber.next} interface.
31
+ * Receives an object of configured type and feeds each key's new value into
32
+ * its respective {@link StreamObj.streams}. If the
33
+ * {@link StreamObjOpts.defaults} option is given, `undefined` key values
34
+ * are replaced with their specified default. If
35
+ * {@link StreamObjOpts.dedupe} is enabled (default) only changed values (as
36
+ * per {@link StreamObjOpts.equiv} predicate option) will be propagated
37
+ * downstream.
31
38
  *
32
39
  * @param x -
33
40
  */
@@ -39,18 +46,25 @@ class StreamObj extends Subscription {
39
46
  this.defaults && val === void 0 ? this.defaults[k] : val
40
47
  );
41
48
  }
49
+ super.next(x);
42
50
  }
43
- /**
44
- * Calls {@link ISubscriber.done} for all streams created. Satifies
45
- * {@link ISubscriber.done} interface.
46
- */
47
51
  done() {
48
52
  for (let k of this.keys) {
49
53
  this.streams[k].done();
50
54
  }
55
+ super.done();
56
+ }
57
+ unsubscribe(sub) {
58
+ if (!sub) {
59
+ for (let k of this.keys) {
60
+ this.streams[k].unsubscribe();
61
+ }
62
+ }
63
+ return super.unsubscribe(sub);
51
64
  }
52
65
  }
53
66
  export {
54
67
  StreamObj,
55
- fromObject
68
+ fromObject,
69
+ fromTuple
56
70
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/rstream",
3
- "version": "9.1.0",
3
+ "version": "9.2.1",
4
4
  "description": "Reactive streams & subscription primitives for constructing dataflow graphs / pipelines",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -40,19 +40,19 @@
40
40
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
41
41
  },
42
42
  "dependencies": {
43
- "@thi.ng/api": "^8.11.13",
44
- "@thi.ng/arrays": "^2.10.6",
45
- "@thi.ng/associative": "^7.0.15",
46
- "@thi.ng/atom": "^5.3.13",
47
- "@thi.ng/checks": "^3.6.15",
48
- "@thi.ng/errors": "^2.5.19",
49
- "@thi.ng/logger": "^3.0.23",
50
- "@thi.ng/transducers": "^9.2.9"
43
+ "@thi.ng/api": "^8.11.14",
44
+ "@thi.ng/arrays": "^2.10.7",
45
+ "@thi.ng/associative": "^7.0.16",
46
+ "@thi.ng/atom": "^5.3.14",
47
+ "@thi.ng/checks": "^3.6.16",
48
+ "@thi.ng/errors": "^2.5.20",
49
+ "@thi.ng/logger": "^3.0.24",
50
+ "@thi.ng/transducers": "^9.2.10"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@microsoft/api-extractor": "^7.48.0",
54
54
  "esbuild": "^0.24.0",
55
- "typedoc": "^0.26.11",
55
+ "typedoc": "^0.27.4",
56
56
  "typescript": "^5.7.2"
57
57
  },
58
58
  "keywords": [
@@ -219,5 +219,5 @@
219
219
  ],
220
220
  "year": 2017
221
221
  },
222
- "gitHead": "7f97d3a454bb605afabf785af1736cb155ecced4\n"
222
+ "gitHead": "34ac95538d96f046090fef5fd3a1dc36d54663d5\n"
223
223
  }