Skip to content

Commit 822a7f7

Browse files
committed
add stories
1 parent 88b8ddd commit 822a7f7

File tree

9 files changed

+5742
-92
lines changed

9 files changed

+5742
-92
lines changed

.storybook/addons.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import '@storybook/addon-actions/register';
2+
import '@storybook/addon-links/register';
3+
import "@storybook/addon-storysource/register";

.storybook/config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { configure } from '@storybook/react';
2+
3+
// automatically import all files ending in *.stories.js
4+
const req = require.context('../stories', true, /\.stories\.js$/);
5+
function loadStories() {
6+
req.keys().forEach(filename => req(filename));
7+
}
8+
9+
configure(loadStories, module);

.storybook/webpack.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = function({ config }) {
2+
config.module.rules.push({
3+
test: /\.stories\.jsx?$/,
4+
loaders: [require.resolve('@storybook/source-loader')],
5+
enforce: 'pre',
6+
});
7+
8+
return config;
9+
};

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,28 @@
1919
"prebuild": "yarn run clean",
2020
"build": "rollup -c rollup.config.js",
2121
"clean": "shx rm -rf ./lib",
22-
"lint": "eslint --ext ts src",
22+
"lint": "eslint --ignore-path .gitignore .",
2323
"prepublishOnly": "yarn run test && yarn run build",
24-
"test": "jest --config jest.config.js --rootDir --color"
24+
"test": "jest --config jest.config.js --color",
25+
"start-storybook": "start-storybook -p 1337",
26+
"build-storybook": "build-storybook"
2527
},
2628
"dependencies": {
2729
"@repeaterjs/repeater": "^2.0.0"
2830
},
2931
"devDependencies": {
32+
"@babel/core": "^7.5.5",
33+
"@storybook/addon-actions": "^5.2.0",
34+
"@storybook/addon-links": "^5.2.0",
35+
"@storybook/addon-storysource": "^5.2.0",
36+
"@storybook/addons": "^5.2.0",
37+
"@storybook/react": "^5.2.0",
3038
"@testing-library/react-hooks": "^2.0.1",
3139
"@types/jest": "^24.0.18",
3240
"@types/react": "^16.9.2",
3341
"@typescript-eslint/eslint-plugin": "^2.0.0",
3442
"@typescript-eslint/parser": "^2.0.0",
43+
"babel-loader": "^8.0.6",
3544
"eslint": "^6.2.1",
3645
"eslint-config-prettier": "^6.1.0",
3746
"eslint-plugin-jest": "^22.15.2",

rollup.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from "path";
22
import typescript from "rollup-plugin-typescript2";
33

44
const root = process.cwd();
5-
const pkg = require(path.join(root, "package.json"));
5+
const pkg = require(path.join(root, "package.json")); // eslint-disable-line
66

77
export default {
88
input: path.join(root, "src/react-hooks.ts"),
@@ -19,4 +19,5 @@ export default {
1919
},
2020
],
2121
plugins: [typescript()],
22+
external: ["react", "@repeaterjs/repeater"],
2223
};

src/react-hooks.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { useEffect, useState } from "react";
22
import { Push, Repeater, RepeaterBuffer, Stop } from "@repeaterjs/repeater";
33

4+
type PrimedRepeaterTuple<T> = [Repeater<T>, Push<T>, Stop];
45
// Repeaters are lazy, hooks are eager.
56
// We need to return push and stop synchronously from the useRepeater hook so
67
// we prime the repeater by calling next immediately.
78
function createPrimedRepeater<T>(
89
buffer?: RepeaterBuffer<T>,
9-
): [Repeater<T>, Push<T>, Stop] {
10+
): PrimedRepeaterTuple<T> {
1011
let push: Push<T>;
1112
let stop: Stop;
1213
const repeater = new Repeater((push1, stop1) => {
@@ -22,7 +23,7 @@ function createPrimedRepeater<T>(
2223

2324
export function useRepeater<T>(
2425
buffer?: RepeaterBuffer<T>,
25-
): [Repeater<T>, Push<T>, Stop] {
26+
): PrimedRepeaterTuple<T> {
2627
const [tuple] = useState(() => createPrimedRepeater(buffer));
2728
return tuple;
2829
}

stories/index.stories.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from "react"; // eslint-disable-line
2+
import { storiesOf } from "@storybook/react";
3+
import { useResult } from "../lib/react-hooks.esm";
4+
5+
storiesOf("@repeaterjs/react-hooks", module)
6+
.add("incrementing counter", () => {
7+
const result = useResult(async function*() {
8+
let i = 0;
9+
while (true) {
10+
yield i++;
11+
await new Promise((resolve) => setTimeout(resolve, 1000));
12+
}
13+
});
14+
15+
if (result == null) {
16+
return null;
17+
}
18+
19+
return <div>Current value: {result.value}</div>;
20+
})
21+
.add("with some emoji", () => {
22+
return <div>Yo world</div>;
23+
});

tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"files": [
3+
"src/react-hooks.ts"
4+
],
25
"compilerOptions": {
36
/* Basic Options */
47
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */

0 commit comments

Comments
 (0)