[wip] so close; check the ws messages
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
import { merge, Observable } from "kefir";
|
||||
|
||||
export type ValueWithin<O extends Observable<any, any>> = Parameters<
|
||||
Parameters<O["map"]>[0]
|
||||
>[0];
|
||||
|
||||
type Mutation<A, O extends Observable<any, any>> = [
|
||||
O,
|
||||
(prev: A, value: ValueWithin<O>) => A
|
||||
];
|
||||
|
||||
export const multiScan = <A, M extends Mutation<A, any>[]>(
|
||||
initValue: A,
|
||||
...mutations: M
|
||||
): Observable<A, any> =>
|
||||
merge(
|
||||
mutations.map(([source, mutation]) =>
|
||||
source.map((event) => ({ event, mutation }))
|
||||
)
|
||||
).scan((prev, { event, mutation }) => mutation(prev, event), initValue);
|
||||
|
||||
export const partition =
|
||||
<C extends readonly [...string[]], T, E>(
|
||||
classes: C,
|
||||
partitionFn: (v: T) => C[number]
|
||||
) =>
|
||||
(obs: Observable<T, E>) => {
|
||||
const assigned = obs.map((obj) => ({ obj, cls: partitionFn(obj) }));
|
||||
return Object.fromEntries(
|
||||
classes.map((C) => [
|
||||
C,
|
||||
assigned.filter(({ cls }) => cls == C).map(({ obj }) => obj),
|
||||
])
|
||||
);
|
||||
};
|
||||
|
||||
export const isEmpty = (container: { length: number }) => container.length == 0;
|
||||
52
pkg/shared/kefirs.ts
Normal file
52
pkg/shared/kefirs.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { merge, Observable } from "kefir";
|
||||
import Bus from "kefir-bus";
|
||||
|
||||
export type ValueWithin<O extends Observable<any, any>> = Parameters<
|
||||
Parameters<O["map"]>[0]
|
||||
>[0];
|
||||
|
||||
type Mutation<A, O extends Observable<any, any>> = [
|
||||
O,
|
||||
(prev: A, value: ValueWithin<O>) => A
|
||||
];
|
||||
|
||||
export const multiScan = <A, M extends Mutation<A, any>[]>(
|
||||
initValue: A,
|
||||
...mutations: M
|
||||
): Observable<A, any> =>
|
||||
merge(
|
||||
mutations.map(([source, mutation]) =>
|
||||
source.map((event) => ({ event, mutation }))
|
||||
)
|
||||
).scan((prev, { event, mutation }) => mutation(prev, event), initValue);
|
||||
|
||||
export const partition = <
|
||||
C extends readonly [...string[]],
|
||||
T extends { [key: string]: any },
|
||||
E
|
||||
>(
|
||||
classes: C,
|
||||
obs: Observable<T, E>
|
||||
) => {
|
||||
const classBuses = Object.fromEntries(classes.map((c) => [c, Bus()]));
|
||||
obs.onValue((v) => {
|
||||
for (const _class of classes) {
|
||||
if (_class in v) {
|
||||
classBuses[_class].emit(v);
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
return classBuses;
|
||||
};
|
||||
|
||||
export const isEmpty = (container: { length: number }) => container.length == 0;
|
||||
|
||||
export const setDiff = <T>(
|
||||
sets: [Set<T>, s2: Set<T>]
|
||||
): { added: T[]; removed: T[] } => ({
|
||||
added: [...sets[1].difference(sets[0])],
|
||||
removed: [...sets[0].difference(sets[1])],
|
||||
});
|
||||
|
||||
export const set = <T>(arr: T[]) => new Set<T>(arr);
|
||||
@@ -3,9 +3,11 @@
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"kefir": "^3.8.8",
|
||||
"kefir-bus": "^2.3.1",
|
||||
"object-hash": "^3.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/kefir": "^3.8.11"
|
||||
"@types/kefir": "^3.8.11",
|
||||
"ts-xor": "^1.3.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"esModuleInterop": true
|
||||
"esModuleInterop": true,
|
||||
"target": "esnext",
|
||||
"moduleResolution": "nodenext",
|
||||
"module": "nodenext"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user