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 types #8

Draft
wants to merge 2 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.

16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@
"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/scheduler": "^1.2.2",
"@most/types": "^1.0.1",
"rollup": "^1.15.6",
"rollup-plugin-typescript2": "^0.21.1",
"typescript": "^3.5.2"
}
}
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" },
{ file: r(__dirname, "dist/index.mjs"), format: "esm" }
],
plugins: [
typescript({
typescript: require("typescript"),
cacheRoot: r(__dirname, "node_modules/.cache/rollup-plugin-typescript2")
})
],
external: [
...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies)
]
};
47 changes: 0 additions & 47 deletions src/index.js

This file was deleted.

51 changes: 51 additions & 0 deletions src/index.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 default fromObservable;
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;
}
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