Skip to content

Instantly share code, notes, and snippets.

@maxsei
Created February 14, 2024 14:21
Show Gist options
  • Select an option

  • Save maxsei/b925e56fc881cd2e829bd90e9cf02c64 to your computer and use it in GitHub Desktop.

Select an option

Save maxsei/b925e56fc881cd2e829bd90e9cf02c64 to your computer and use it in GitHub Desktop.
attempt at using an object with merge function
import { labeled, pairs, mapA } from "@thi.ng/transducers";
import {
merge,
TransformableOpts,
ISubscribable,
Subscription,
} from "@thi.ng/rstream";
import { IObjectOf, Pair } from "@thi.ng/api";
type MappedSubcribable<K, V> = {
[P in K]: ISubscribable<V>;
};
export const mergeKeyed = <K, V>(
obj: MappedSubcribable<K, V>,
): Subscription<any, Pair<K, V>> =>
merge({ src: mapA(([k, v]) => v.transform(labeled(k)), pairs(x)) });
mergeKeyed({ a: reactive(42), b: reactive("abc") }).map((x) => {
switch (k) {
case "a":
x; // (parameter) x: Pair<"a" | "b", unknown>
case "b":
default:
throw new Error("unreachable");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment