Skip to content

Commit 7a1f189

Browse files
committed
add more generic method from replacing fromPromise
1 parent b83ef18 commit 7a1f189

File tree

6 files changed

+26
-7
lines changed

6 files changed

+26
-7
lines changed

src/__tests__/fantasyx-test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('FantasyX', () => {
3535
intent.next(2)
3636

3737
let res = FlatMap.Xstream.flatMap(
38-
(x: number) => Xstream.fromPromise<"ArrayStream", number, number>(new Id({ count: x + 1 }))
38+
(x: number) => Xstream.from(new Id({ count: x + 1 }))
3939
, Xstream.fromIntent<"ArrayStream", number>())
4040
.toFantasyX()
4141
.toStream(intent)
@@ -59,8 +59,8 @@ describe('FantasyX', () => {
5959
it('concat promise', () => {
6060

6161
let res = Semigroup.Xstream.concat(
62-
Xstream.fromPromise(new Id({ count1: 1 }))
63-
, Xstream.fromPromise(new Id({ count2: 2 })))
62+
Xstream.from(new Id({ count1: 1 }))
63+
, Xstream.from(new Id({ count2: 2 })))
6464
.toFantasyX()
6565
.toStream(intent)
6666
.reduce((acc, f: any) => f(acc), { count: 0 })

src/fantasy/xstream.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Functor, map } from './typeclasses/functor'
1+
import { Functor, FunctorInstances, map } from './typeclasses/functor'
22
import { Cartesian, product } from './typeclasses/cartesian'
33
import { Apply } from './typeclasses/apply'
44
import { FlatMap, flatMap } from './typeclasses/flatmap'
@@ -55,6 +55,13 @@ export class Xstream<S extends Stream, I, A> {
5555
})))
5656
}
5757

58+
static from<F extends Stream, I, A, G extends FunctorInstances>(p: $<G, A>) {
59+
return new Xstream<F, I, A>(new State((intent$: Subject<F, I>) => ({
60+
s: intent$,
61+
a: streamOps.from(p) as $<F, A>
62+
})))
63+
}
64+
5865
toFantasyX<St>() {
5966
type itentStream = Subject<S, I>
6067
type updateStream = $<S, State<St, A>>

src/xs/array.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@ StreamOps.prototype.fromPromise = function(p) {
7070
}
7171
return [p.valueOf()]
7272
}
73+
74+
StreamOps.prototype.from = function(fa) {
75+
return [fa.valueOf()]
76+
}

src/xs/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { _, $ } from '../fantasy/typeclasses'
2+
import { FunctorInstances } from '../fantasy/typeclasses/functor'
23
export interface S_<A> { }
34
export type Stream = keyof S_<any>
45

@@ -17,6 +18,7 @@ export class StreamOps<F extends Stream> { }
1718
export interface StreamOps<F extends Stream> {
1819
empty<A>(): $<F, A>
1920
fromPromise<A>(p: Promise<A>): $<F, A>
21+
from<G extends FunctorInstances, A>(fa: $<G, A>): $<F, A>
2022
just<A>(a: A): $<F, A>
2123
merge<A, B>(
2224
a: $<F, A>,

src/xs/most.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Stream as MostStream, empty, just, combineArray, combine, flatMap, fromPromise } from 'most'
1+
import { Stream as MostStream, empty, just, combineArray, combine, flatMap, fromPromise, from } from 'most'
22
import { sync, SyncSubject, Subject } from 'most-subject'
33
import { Subscription, StreamOps } from '.'
44

@@ -51,7 +51,10 @@ StreamOps.prototype.subscribe = function <A>(fa: MostStream<A>, next: (v: A) =>
5151
}).subscribe({ next, error: x => console.error(x), complete }) as Subscription
5252
}
5353

54-
StreamOps.prototype.fromPromise = fromPromise
54+
StreamOps.prototype.fromPromise = fromPromise;
55+
56+
57+
(<any>StreamOps.prototype.from) = from
5558

5659
export const URI = 'Stream'
5760
export type URI = typeof URI

src/xs/rx.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import '@reactivex/rxjs/dist/cjs/add/operator/scan'
88
import '@reactivex/rxjs/dist/cjs/add/operator/catch'
99
import '@reactivex/rxjs/dist/cjs/add/operator/filter'
1010
import '@reactivex/rxjs/dist/cjs/add/observable/empty'
11+
import '@reactivex/rxjs/dist/cjs/add/observable/from'
1112
import '@reactivex/rxjs/dist/cjs/add/observable/of'
1213
import '@reactivex/rxjs/dist/cjs/add/observable/fromPromise'
1314
import '@reactivex/rxjs/dist/cjs/add/observable/combineLatest'
@@ -61,4 +62,6 @@ StreamOps.prototype.merge = function <A, B>(a: RxStream<A>, b: RxStream<B>): RxS
6162
return a.merge<A, B>(b)
6263
}
6364

64-
StreamOps.prototype.fromPromise = RxStream.fromPromise
65+
StreamOps.prototype.fromPromise = RxStream.fromPromise;
66+
67+
(<any>StreamOps.prototype.from) = RxStream.from

0 commit comments

Comments
 (0)