Skip to content

Commit

Permalink
add svelte bindings and example. FIX #53
Browse files Browse the repository at this point in the history
  • Loading branch information
bordoley committed Jun 3, 2020
1 parent 322b40c commit accab19
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 34 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ yarn-error.log
*.key
*.re
*.rei
*.svelte
*.tsbuildinfo

.DS_Store
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ language: node_js
cache:
yarn: true
node_js:
- '14'
- "14"

matrix:
fast_finish: true
Expand All @@ -22,4 +22,4 @@ install:
script:
- yarn run build
- yarn run test-coverage
- cat ./coverage/lcov.info | ./node_modules/.bin/coveralls
- cat ./coverage/lcov.info | ./node_modules/.bin/coveralls
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"mod-ts": "yarn mod-ts-copy && yarn mod-ts-replace",
"mod-ts-copy": "cp -r ./src/ ./mod",
"mod-ts-replace": "replace 'from \"(\\.[.\\-/\\w]+)\";' 'from \"$1.ts\";' ./mod -r --include=\"*.ts\"",
"mod-rm-unsupported": "rm -rf ./mod/experimental/testing.jest.* ./mod/experimental/node.* ./mod/experimental/internal/node ./mod/node.* ./mod/react.* ./mod/internal/node ./mod/perf",
"mod-rm-unsupported": "rm -rf ./mod/experimental/testing.jest.* ./mod/experimental/node.* ./mod/experimental/svelte.* ./mod/experimental/internal/node ./mod/node.* ./mod/react.* ./mod/internal/node ./mod/perf",
"pkg": "yarn clean-pkg && yarn pkg-mjs && yarn pkg-js && yarn pkg-types && yarn pkg-rm-unsupport && yarn pkg-cp-files",
"pkg-js": "cp -r ./build/cjs/ ./packages/core",
"pkg-mjs": "yarn pkg-mjs-copy && yarn pkg-mjs-rename && yarn pkg-mjs-replace",
Expand Down Expand Up @@ -72,6 +72,7 @@
"replace": "^1.2.0",
"rxjs": "^6.5.3",
"scheduler": "^0.17.0",
"svelte": "^3.0.0",
"ts-jest": "^24.1.0",
"typedoc": "0.17.0-3",
"typedoc-plugin-markdown": "^2.2.11",
Expand Down
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
},
"dependencies": {
"react": "^16.11.0",
"scheduler": "^0.17.0"
"scheduler": "^0.17.0",
"svelte": "^3.0.0"
},
"publishConfig": {
"access": "public"
Expand Down
14 changes: 8 additions & 6 deletions packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@
"devDependencies": {
"@rollup/plugin-commonjs": "^11.0.2",
"@rollup/plugin-replace": "^2.2.1",
"@rollup/plugin-node-resolve": "^8.0.0",
"@types/mime-db": "^1.27.1",
"@types/mime-types": "^2.1.0",
"@types/node": "13.13.2",
"@types/react": "^16.9.11",
"@types/react-dom": "^16.9.4",
"rollup": "^1.27.4",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.1.2"
"rollup-plugin-terser": "^5.1.2",
"rollup-plugin-svelte": "^5.0.3",
"svelte": "^3.0.0"
},
"scripts": {
"build": "yarn build-server && yarn build-client && yarn rollup",
"build": "yarn build-server && yarn build-react && yarn build-svelte",
"build-server": "tsc -p tsconfig.server.json",
"build-client": "tsc -p tsconfig.client.json",
"clean": "rm -rf build",
"rollup": "rollup -c"
"build-react": "tsc -p tsconfig.react.json && rollup -c rollup.react.js",
"build-svelte": "rollup -c rollup.svelte.js",
"clean": "rm -rf build"
}
}
4 changes: 2 additions & 2 deletions packages/example/index.html → packages/example/react.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Example Rx React App</title>
<title>Example React App</title>
</head>
<body>
<div id="root"></div>
Expand All @@ -15,6 +15,6 @@
crossorigin
src="https://unpkg.com/[email protected]/umd/react-dom.development.js"
></script>
<script src="build/bundle.js"></script>
<script src="build/react.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// rollup.config.js

import resolve from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
import resolve from "rollup-plugin-node-resolve";
import { terser } from "rollup-plugin-terser";

export default {
external: ["react", "react-dom", "scheduler"],

input: "build/esm/example-client.js",
input: "build/esm/react/example.js",
output: {
file: "build/bundle.js",
file: "build/react.js",
format: "iife",
name: "ExampleReact",
globals: {
Expand All @@ -24,10 +22,10 @@ export default {
replace({
"process.env.NODE_ENV": JSON.stringify("production"),
}),
//terser({
//output: {
//comments: false,
//},
//}),
terser({
output: {
comments: false,
},
}),
],
};
28 changes: 28 additions & 0 deletions packages/example/rollup.svelte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import svelte from "rollup-plugin-svelte";
import { terser } from "rollup-plugin-terser";

export default {
input: "src/svelte/example.js",
output: {
format: "iife",
name: "ExampleSvelte",
file: "build/svelte.js",
},
plugins: [
svelte({
dev: false,
}),
resolve({
browser: true,
dedupe: ["svelte"],
}),
commonjs(),
terser({
output: {
comments: false,
},
}),
],
};
File renamed without changes.
8 changes: 8 additions & 0 deletions packages/example/src/svelte/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import App from "./example.svelte";

const app = new App({
target: document.body,
props: {},
});

export default app;
46 changes: 46 additions & 0 deletions packages/example/src/svelte/example.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<script>
import {
pipe,
returns,
increment,
compose,
} from "@reactive-js/core/functions";
import { generate, throttle } from "@reactive-js/core/observable";
import { fromObservable } from "@reactive-js/core/flowable";
import {
createHostScheduler,
toPriorityScheduler,
toSchedulerWithPriority,
} from "@reactive-js/core/scheduler";
import { writable } from "svelte/store";
import { stream } from "@reactive-js/core/experimental/svelte";
const scheduler = pipe(
createHostScheduler(),
toPriorityScheduler,
toSchedulerWithPriority(1),
);
const [value, setMode] = pipe(
generate(increment, returns(0)),
throttle(15),
fromObservable(),
stream(scheduler),
);
let mode = 2;
let label = "RESUME";
const onClick = () => {
mode = mode === 1 ? 2 : 1;
label = mode === 2 ? "RESUME" : "PAUSE";
setMode(mode);
};
</script>

<main>
<h1>{$value || 0}</h1>

<button on:click={onClick}>{label}</button>
</main>
15 changes: 15 additions & 0 deletions packages/example/svelte.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />

<title>Example Svelte App</title>

<link rel="stylesheet" href="build/svelte.css" />

<script defer src="build/svelte.js"></script>
</head>

<body></body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"rootDir": "./src",
"target": "ES2019"
},
"include": ["./src/example-client.tsx"]
"include": ["./src/react/example.tsx"]
}
74 changes: 74 additions & 0 deletions src/experimental/svelte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { onDestroy, onMount } from "svelte";
import { writable, Readable, Writable } from "svelte/store";
import { dispose } from "../disposable";
import { Function1, Factory, pipe, defer, SideEffect1 } from "../functions";
import {
dispatch as dispatchToStream,
ObservableLike,
subscribe as subscribeObs,
StreamLike,
throttle,
} from "../observable";
import { Option, none, isSome } from "../option";
import { SchedulerLike } from "../scheduler";
import { StreamableLike, lift, onNotify, stream as streamableStream } from "../streamable";

class ObservableSvelteStore<T> implements Readable<Option<T>> {
constructor(
private readonly observable: ObservableLike<T>,
private readonly scheduler: SchedulerLike,
) {}

subscribe(callback: (next: Option<T>) => void): Factory<void> {
callback(none);
const subscription = pipe(
this.observable,
onNotify(callback),
subscribeObs(this.scheduler),
);

return defer(subscription, dispose);
}
}

export const subscribe = <T>(
scheduler: SchedulerLike,
): Function1<ObservableLike<T>, Readable<Option<T>>> => obs =>
new ObservableSvelteStore(obs, scheduler);

export const stream = <TReq, T>(
scheduler: SchedulerLike,
options: {
replay?: number;
} = {},
): Function1<
StreamableLike<TReq, T>,
[Readable<Option<T>>, SideEffect1<TReq>]
> => streamable => {
const store: Writable<Option<T>> = writable(none);
const liftedStreamable = pipe(
streamable,
lift(throttle(8)),
onNotify(v => store.set(v)),
);

let stream: Option<StreamLike<TReq, T>> = none;

onMount(() => {
stream = streamableStream(liftedStreamable, scheduler, options);
});

onDestroy(() => {
if (isSome(stream)) {
dispose(stream);
}
});

const dispatch = (req: TReq) => {
if (isSome(stream)) {
dispatchToStream(stream, req);
}
};

return [store, dispatch];
};
Loading

0 comments on commit accab19

Please sign in to comment.