Skip to content

Commit 90d7ecd

Browse files
authored
Merge pull request #27 from scramjetorg/feature/cleaner-samples
Feature: Cleaner samples
2 parents e2b26dd + 8789167 commit 90d7ecd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3505
-2220
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ lerna-debug.log*
2626
.npm
2727

2828
# Optional eslint cache
29-
.eslintcache
29+
.eslintcache
30+
**/dist/

package-lock.json

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"dependencies": {
3-
"markdownlint": "^0.25.0",
3+
"markdownlint": "^0.25.1",
44
"markdownlint-cli": "^0.30.0"
55
},
66
"scripts": {
7-
"lint":"npx markdownlint '**/*.md' --ignore node_modules -c .markdownlint.json"
7+
"lint":"npx markdownlint '**/*.md' --ignore **/node_modules/** -c .markdownlint.json"
88
}
99
}

samples/crypto-prices/index.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
import { ReadableApp } from "@scramjet/types";
2-
import { PassThrough } from "stream";
32
import fetch from "node-fetch";
43

5-
const getData = async (currency: string, baseCurrency: string) =>
6-
fetch(`https://api.coinbase.com/v2/prices/${currency}-${baseCurrency}/spot`)
7-
.then(res => res.json());
4+
async function defer(interval: number) {
5+
await new Promise(res => setTimeout(res, interval));
6+
}
7+
88
/**
99
* Requests external API every 3 seconds.
1010
* Writes crypto prices to output stream.
1111
*
1212
* @param _stream - dummy input stream
1313
* @param currency currency (default: 'BTC')
1414
* @param baseCurrency currency (default: 'USD')
15+
* @param interval how often to check
1516
*/
16-
const app: ReadableApp<string> = function(_stream, currency = "BTC", baseCurrency = "USD") {
17-
const outputStream = new PassThrough();
18-
19-
setInterval(() => {
20-
getData(currency, baseCurrency)
21-
.then(data => {
22-
outputStream.write(JSON.stringify(data) + "\r\n");
23-
})
24-
}, 3000);
25-
26-
return outputStream;
17+
const app: ReadableApp<string> = async function* (
18+
_stream,
19+
currency = "BTC",
20+
baseCurrency = "USD",
21+
interval = 3000
22+
) {
23+
while (true) {
24+
const ref = defer(interval);
25+
const data = await fetch(`https://api.coinbase.com/v2/prices/${currency}-${baseCurrency}/spot`);
26+
yield JSON.stringify(await data.json()) + "\r\n";
27+
await ref;
28+
}
2729
};
2830

2931
export default app;

samples/crypto-prices/package-lock.json

Lines changed: 233 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/crypto-prices/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"build": "tsc -p tsconfig.json",
8-
"postbuild": "cp -r node_modules package.json dist/"
8+
"postbuild": "cp -r package.json dist/ && (cd dist && npm i --only=production)"
99
},
1010
"author": "",
1111
"license": "ISC",

samples/crypto-prices/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
},
55
"include": [
66
"./index.ts"
7-
],
7+
]
88
}

samples/discord-slack-connection/discord-read/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ npm install
3737
# transpile TS->JS to dist/
3838
npm run build
3939

40-
# prepare standalone JS package
41-
cp -r node_modules package.json dist/
42-
4340
# make a compressed package with sequence
4441
si pack dist
4542

0 commit comments

Comments
 (0)