Skip to content
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
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 0 additions & 4 deletions build.js

This file was deleted.

17 changes: 12 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@
"name": "most-observable",
"version": "0.0.1",
"description": "Create @most/core compatible streams from observables",
"main": "dist/main.js",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"author": "Kanit Sharma",
"license": "MIT",
"scripts": {
"build": "node build.js"
"build": "rollup -c"
},
"dependencies": {
"symbol-observable": "^1.2.0"
},
"peerDependencies": {
"@most/core": "^1.3.2"
"@most/core": "^1.3.2",
"@most/scheduler": "^1.2.2"
},
"devDependencies": {
"@lectro/core": "^1.1.2-alpha-21",
"@lectro/enhancer-buildutils": "^0.1.0-alpha-7"
"@most/core": "^1.3.2",
"@most/scheduler": "^1.2.2",
"@most/types": "^1.0.1",
"rollup": "^1.15.6",
"rollup-plugin-typescript2": "^0.21.1",
"typescript": "^3.5.2"
}
}
10 changes: 8 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# most-observable

Create `@most/core` streams from [ES observables](https://github.com/tc39/proposal-observable).
Convert `@most/core` streams to/from [ES observables](https://github.com/tc39/proposal-observable).

## Install

Expand All @@ -13,7 +13,7 @@ Create `@most/core` streams from [ES observables](https://github.com/tc39/propos
Creating state stream from redux store as it is an Observable but any [ES observable](https://github.com/tc39/proposal-observable) should work.

```javascript
import fromObservable from "most-observable";
import { fromObservable } from "most-observable";
import { runEffects, tap } from "@most/core";
import { newDefaultScheduler } from "@most/scheduler";

Expand All @@ -31,3 +31,9 @@ runEffects(tap(console.log, state$), newDefaultScheduler());
```javascript
const stream = fromObservable(observable);
```

- toObservable

```javascript
const observable = toObservable(stream);
```
21 changes: 21 additions & 0 deletions rollup.config.js
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)
]
};
51 changes: 51 additions & 0 deletions src/fromObservable.ts
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 };
47 changes: 0 additions & 47 deletions src/index.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { fromObservable as default, fromObservable } from "./fromObservable";
Copy link
Author

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.

export { toObservable } from "./toObservable";
19 changes: 19 additions & 0 deletions src/observableTypes.ts
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;
}
72 changes: 72 additions & 0 deletions src/toObservable.ts
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 };
11 changes: 11 additions & 0 deletions tsconfig.json
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/**/*"]
}
9 changes: 0 additions & 9 deletions webpack.config.js

This file was deleted.

Loading