-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Observable } from '../Observable'; | ||
|
||
export const of = function (...list) { | ||
return new Observable((observer) => { | ||
Array.from(list).forEach((value) => { | ||
observer.next(value); | ||
}); | ||
|
||
observer.complete(); | ||
}); | ||
}; | ||
|
||
Observable.of = of; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Observable } from '../Observable'; | ||
|
||
export const delay = function (source$, time) { | ||
return Observable.create((observer) => { | ||
let subscription; | ||
const timerId = setTimeout(() => { | ||
subscription = source$.subscribe(observer.next, observer.error, observer.complete); | ||
}, time); | ||
|
||
return () => { | ||
clearTimeout(timerId); | ||
if (subscription) { | ||
subscription.unsubscribe(); | ||
} | ||
}; | ||
}); | ||
}; | ||
|
||
Observable.prototype.delay = function (time) { | ||
return delay(this, time); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Observable } from '../Observable'; | ||
import { isObservable } from '../utilities'; | ||
import '../observables/of'; | ||
import './map'; | ||
|
||
export const flatMap = function (source$, mapCallback) { | ||
return new Observable((observer) => { | ||
let subscription = { isComplete: false }; | ||
const nextSubscriptionList = []; | ||
const onComplete = () => { | ||
const nextComplete = nextSubscriptionList.reduce( | ||
(curr, sub) => curr && sub.isComplete, | ||
true | ||
); | ||
|
||
if (subscription.complete && nextComplete) { | ||
observer.complete(); | ||
} | ||
}; | ||
|
||
subscription = source$ | ||
.map(mapCallback) | ||
.map((nextValue) => { | ||
let nextValue$ = nextValue; | ||
|
||
if (!isObservable(nextValue$)) { | ||
nextValue$ = Observable.of(nextValue$); | ||
} | ||
|
||
return nextValue$ | ||
}) | ||
.subscribe((nextValue$) => { | ||
const nextSubscription = nextValue$.subscribe( | ||
observer.next, | ||
observer.error, | ||
onComplete | ||
); | ||
|
||
nextSubscriptionList.push(nextSubscription); | ||
}, observer.error, onComplete); | ||
|
||
return () => { | ||
nextSubscriptionList.forEach((nextSub) => nextSub.unsubscribe()); | ||
subscription.unsubscribe(); | ||
}; | ||
}); | ||
}; | ||
|
||
Observable.prototype.flatMap = function (mapCallback) { | ||
return flatMap(this, mapCallback); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Observable } from '../Observable'; | ||
import { isObservable } from '../utilities'; | ||
import '../observables/of'; | ||
import './map'; | ||
|
||
export const switchMap = function (source$, mapCallback) { | ||
return new Observable((observer) => { | ||
let currentSubscription; | ||
let nextSubscription; | ||
|
||
currentSubscription = source$ | ||
.map(mapCallback) | ||
.map((nextValue) => { | ||
let nextValue$ = nextValue; | ||
|
||
if (!isObservable(nextValue$)) { | ||
nextValue$ = Observable.of(nextValue$); | ||
} | ||
|
||
return nextValue$ | ||
}) | ||
.subscribe((nextValue$) => { | ||
// unsubscribe to the previous subscription if a new value comes in | ||
if (nextSubscription) { | ||
nextSubscription.unsubscribe(); | ||
} | ||
|
||
nextSubscription = nextValue$.subscribe((value) => { | ||
observer.next(value); | ||
}, observer.error, observer.complete); | ||
}, observer.error, observer.complete); | ||
|
||
return () => { | ||
if (nextSubscription) { | ||
nextSubscription.unsubscribe(); | ||
} | ||
currentSubscription.unsubscribe(); | ||
}; | ||
}); | ||
}; | ||
|
||
Observable.prototype.switchMap = function (mapCallback) { | ||
return switchMap(this, mapCallback); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export const noop = () => null; | ||
|
||
export const isPromise = (p) => p !== null && typeof p === 'object' && typeof p.then === 'function'; | ||
|
||
export const isObservable = (o$) => o$ !== null && typeof o$ === 'object' && typeof o$.subscribe === 'function'; |