-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add toObservable function #9
Draft
cprecioso
wants to merge
4
commits into
mostjs-community:master
Choose a base branch
from
cprecioso:toObservable
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+756
−5,589
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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 { resolve as r } from "path"; | ||
import typescript from "rollup-plugin-typescript2"; | ||
import * as pkg from "./package.json"; | ||
|
||
export default { | ||
input: r(__dirname, "src/index.ts"), | ||
output: [ | ||
{ file: r(__dirname, "dist/index.js"), format: "cjs", exports: "named" }, | ||
{ file: r(__dirname, "dist/index.mjs"), format: "esm", exports: "named" } | ||
], | ||
plugins: [ | ||
typescript({ | ||
typescript: require("typescript"), | ||
cacheRoot: r(__dirname, "node_modules/.cache/rollup-plugin-typescript2") | ||
}) | ||
], | ||
external: [ | ||
...Object.keys(pkg.dependencies), | ||
...Object.keys(pkg.peerDependencies) | ||
] | ||
}; |
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 { currentTime } from "@most/scheduler"; | ||
import { Scheduler, Sink, Stream, Time } from "@most/types"; | ||
import observableSymbol from "symbol-observable"; | ||
import { ObservableCompatible, ObservableLike } from "./observableTypes"; | ||
|
||
const fromObservable = <T>(observable: ObservableCompatible<T>): Stream<T> => | ||
new ObservableStream(observable); | ||
|
||
const tryEvent = <T>(t: Time, x: T, sink: Sink<T>) => { | ||
try { | ||
sink.event(t, x); | ||
} catch (e) { | ||
sink.error(t, e); | ||
} | ||
}; | ||
|
||
const getObservable = <T>(o?: ObservableCompatible<T>) => { | ||
if (o) { | ||
const method = (o as any)[observableSymbol] as | ||
| (() => ObservableLike<T>) | ||
| undefined; | ||
|
||
if (typeof method === "function") { | ||
const obs = method.call(o); | ||
if (!(obs && typeof obs.subscribe === "function")) { | ||
throw new TypeError("invalid observable " + obs); | ||
} | ||
return obs; | ||
} | ||
} | ||
}; | ||
|
||
class ObservableStream<T> implements Stream<T> { | ||
constructor(protected readonly observable: ObservableCompatible<T>) {} | ||
|
||
run(sink: Sink<T>, scheduler: Scheduler) { | ||
const send = (e: T) => tryEvent(currentTime(scheduler), e, sink); | ||
|
||
const subscription = getObservable(this.observable)!.subscribe({ | ||
next: send, | ||
error: e => sink.error(currentTime(scheduler), e), | ||
complete: () => sink.end(currentTime(scheduler)) | ||
}); | ||
|
||
const dispose = () => subscription.unsubscribe(); | ||
|
||
return { dispose }; | ||
} | ||
} | ||
|
||
export { fromObservable }; |
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
export { fromObservable as default, fromObservable } from "./fromObservable"; | ||
export { toObservable } from "./toObservable"; |
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,19 @@ | ||
// types ported from "https://github.com/bigslycat/es-observable" | ||
|
||
export interface ObservableCompatible<T> { | ||
[Symbol.observable](): ObservableLike<T>; | ||
} | ||
|
||
export interface ObserverLike<T> { | ||
next: (value: T) => void; | ||
error: (errorValue: Error) => void; | ||
complete: () => void; | ||
} | ||
|
||
export interface ObservableLike<T> extends ObservableCompatible<T> { | ||
subscribe: (observer: Partial<ObserverLike<T>>) => SubscriptionLike; | ||
} | ||
|
||
export interface SubscriptionLike { | ||
unsubscribe(): void; | ||
} |
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,72 @@ | ||
import { run } from "@most/core"; | ||
import { Disposable, Scheduler, Sink, Stream, Time } from "@most/types"; | ||
import observableSymbol from "symbol-observable"; | ||
import { | ||
ObservableLike, | ||
ObserverLike, | ||
SubscriptionLike | ||
} from "./observableTypes"; | ||
|
||
const toObservable = <T>( | ||
stream: Stream<T>, | ||
scheduler: Scheduler | ||
): ObservableLike<T> => { | ||
return new StreamObservable(stream, scheduler); | ||
}; | ||
|
||
class StreamObservable<T> implements ObservableLike<T> { | ||
constructor( | ||
protected readonly stream: Stream<T>, | ||
protected readonly scheduler: Scheduler | ||
) {} | ||
|
||
[Symbol.observable](): this; | ||
[observableSymbol]() { | ||
return this; | ||
} | ||
|
||
subscribe(observer: Partial<ObserverLike<T>>) { | ||
const disposable = run( | ||
new ObserverSink(observer), | ||
this.scheduler, | ||
this.stream | ||
); | ||
return new StreamSubscription(disposable); | ||
} | ||
} | ||
|
||
class StreamSubscription implements SubscriptionLike { | ||
constructor(protected readonly disposable: Disposable) {} | ||
|
||
unsubscribe() { | ||
this.disposable.dispose(); | ||
} | ||
} | ||
|
||
const noop = () => {}; | ||
|
||
class ObserverSink<T> implements Sink<T> { | ||
constructor(protected readonly observer: Partial<ObserverLike<T>>) {} | ||
|
||
obsNext = this.observer.next ? this.observer.next.bind(this.observer) : noop; | ||
obsError = this.observer.error | ||
? this.observer.error.bind(this.observer) | ||
: noop; | ||
obsComplete = this.observer.complete | ||
? this.observer.complete.bind(this.observer) | ||
: noop; | ||
|
||
event(_time: Time, v: T) { | ||
this.obsNext(v); | ||
} | ||
|
||
error(_time: Time, e: Error) { | ||
this.obsError(e); | ||
} | ||
|
||
end(_time: Time) { | ||
this.obsComplete(); | ||
} | ||
} | ||
|
||
export { toObservable }; |
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,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"allowSyntheticDefaultImports": true, | ||
"strict": true, | ||
"declaration": true | ||
}, | ||
"include": ["src/**/*"] | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fromObservable gets exported both as a named export and as a default export in order not to break compatibility, but the default export should be deprecated in order to maintain symmetry.