Skip to content
This repository has been archived by the owner on Dec 18, 2019. It is now read-only.

Support Cycle Unified #18

Merged
merged 4 commits into from
Apr 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {DevToolEnabledSource} from '@cycle/run';
import xs, {Stream, MemoryStream} from 'xstream';
import dropRepeats from 'xstream/extra/dropRepeats';
import { adapt } from '@cycle/run/lib/adapt';

export type MainFn<So, Si> = (sources: So) => Si;
export type Reducer<T> = (state: T | undefined) => T | undefined;
Expand All @@ -16,21 +17,23 @@ export type Scope<T, R> = string | number | Lens<T, R>;

export function pick(selector: Selector | string) {
if (typeof selector === 'string') {
return function pickWithString(sinksArray$: Stream<Array<any>>): Stream<Array<any>> {
return sinksArray$.map(sinksArray => sinksArray.map(sinks => sinks[selector]));
return function pickWithString(sinksArray$: any): Stream<Array<any>> {
return adapt((xs.fromObservable(sinksArray$) as Stream<Array<any>>)
.map(sinksArray => sinksArray.map(sinks => sinks[selector])));
};
} else {
return function pickWithFunction(sinksArray$: Stream<Array<any>>): Stream<Array<any>> {
return sinksArray$.map(sinksArray => sinksArray.map(selector));
return function pickWithFunction(sinksArray$: any): Stream<Array<any>> {
return adapt((xs.fromObservable(sinksArray$) as Stream<Array<any>>)
.map(sinksArray => sinksArray.map(selector)));
};
}
}

export function mix(aggregator: Aggregator) {
return function mixOperator(streamArray$: Stream<Array<Stream<any>>>): Stream<any> {
return streamArray$
return function mixOperator(streamArray$: any): Stream<Array<Stream<any>>> {
return adapt((xs.fromObservable(streamArray$) as Stream<Array<Stream<any>>>)
.map(streamArray => aggregator(...streamArray))
.flatten();
.flatten());
}
}

Expand Down Expand Up @@ -105,7 +108,7 @@ export class StateSource<T> {

constructor(stream: Stream<any>, name: string | null) {
this._name = name;
this.state$ = stream.compose(dropRepeats()).remember();
this.state$ = adapt(stream.compose(dropRepeats()).remember());
if (!name) {
return;
}
Expand Down Expand Up @@ -134,7 +137,10 @@ export default function onionify<So, Si>(
.drop(1);
sources[name] = new StateSource<any>(state$, name) as any;
const sinks = main(sources as So);
reducerMimic$.imitate(sinks[name]);
if (sinks[name]) {
const stream$ = xs.fromObservable<Reducer<any>>(sinks[name]);
reducerMimic$.imitate(stream$);
}
return sinks;
}
}