Skip to content

Commit

Permalink
fix: add useAsync hook
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Oct 14, 2020
1 parent 48576a6 commit f37b1ba
Show file tree
Hide file tree
Showing 20 changed files with 443 additions and 393 deletions.
4 changes: 2 additions & 2 deletions core/config/test/__snapshots__/stories.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ Array [
"/Users/atanasster/component-controls/ui/blocks/src/Subtitle/Subtitle.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/TagsList/TagsList.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/Title/Title.stories.tsx",
"/Users/atanasster/component-controls/examples/stories/src/stories_native/async-stories.stories.tsx",
"/Users/atanasster/component-controls/examples/stories/src/stories_native/dynamic-stories.stories.tsx",
"/Users/atanasster/component-controls/examples/stories/src/stories/controls-editors-starter.stories.tsx",
"/Users/atanasster/component-controls/examples/stories/src/stories/controls-editors.stories.jsx",
"/Users/atanasster/component-controls/examples/stories/src/stories/inherit-from-doc.stories.tsx",
"/Users/atanasster/component-controls/examples/stories/src/stories/smart-prop-type.stories.js",
"/Users/atanasster/component-controls/examples/stories/src/stories/smart-typescript.stories.js",
"/Users/atanasster/component-controls/examples/stories/src/stories/stories-async.stories.tsx",
"/Users/atanasster/component-controls/examples/stories/src/stories/stories-esm.stories.js",
]
`;
Expand Down Expand Up @@ -139,13 +139,13 @@ Array [
"/Users/atanasster/component-controls/ui/blocks/src/Subtitle/Subtitle.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/TagsList/TagsList.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/Title/Title.stories.tsx",
"/Users/atanasster/component-controls/examples/stories/src/stories_native/async-stories.stories.tsx",
"/Users/atanasster/component-controls/examples/stories/src/stories_native/dynamic-stories.stories.tsx",
"/Users/atanasster/component-controls/examples/stories/src/stories/controls-editors-starter.stories.tsx",
"/Users/atanasster/component-controls/examples/stories/src/stories/controls-editors.stories.jsx",
"/Users/atanasster/component-controls/examples/stories/src/stories/inherit-from-doc.stories.tsx",
"/Users/atanasster/component-controls/examples/stories/src/stories/smart-prop-type.stories.js",
"/Users/atanasster/component-controls/examples/stories/src/stories/smart-typescript.stories.js",
"/Users/atanasster/component-controls/examples/stories/src/stories/stories-async.stories.tsx",
"/Users/atanasster/component-controls/examples/stories/src/stories/stories-esm.stories.js",
]
`;
88 changes: 50 additions & 38 deletions core/core/README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/core/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type FrameworkRenderFn = (
story: Story,
doc?: Document,
options?: any,
) => Promise<ReactElement>;
) => ReactElement;

/**
* story type pages can have multiple tabs with separate page configurations.
Expand Down
5 changes: 0 additions & 5 deletions core/core/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,6 @@ export type Story = {
* it is set internally and will be used to create a story URL
*/
dynamicId?: string;

/**
* if the story is declared as an async function
*/
async?: boolean;
} & StoryProps;

/**
Expand Down
45 changes: 44 additions & 1 deletion core/core/src/utility.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { MouseEvent } from 'react';
import React, { MouseEvent, useEffect, useState, useCallback } from 'react';

/**
* position in the stories source code
Expand Down Expand Up @@ -178,3 +178,46 @@ export interface ActionItem {
}

export type ActionItems = ActionItem[];

// source https://usehooks.com/useAsync/
export const useAsync = <T, E = string>(
asyncFunction: () => Promise<T>,
immediate = true,
) => {
const [status, setStatus] = useState<
'idle' | 'pending' | 'success' | 'error'
>('idle');
const [value, setValue] = useState<T | null>(null);
const [error, setError] = useState<E | null>(null);

// The execute function wraps asyncFunction and
// handles setting state for pending, value, and error.
// useCallback ensures the below useEffect is not called
// on every render, but only if asyncFunction changes.
const execute = useCallback(() => {
setStatus('pending');
setValue(null);
setError(null);
return asyncFunction()
.then((response: any) => {
setStatus('success');
setValue(response);
})
.catch((error: any) => {
setError(error);
setStatus('error');
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

// Call execute if we want to fire it right away.
// Otherwise execute can be called later, such as
// in an onClick handler.
useEffect(() => {
if (immediate) {
execute();
}
}, [execute, immediate]);

return { execute, status, value, error };
};
6 changes: 0 additions & 6 deletions core/instrument/src/babel/esm-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ export const extractCSFStories = (
name,
id: name,
};
if (declaration.init.async) {
story.async = true;
}
traverse(path.node, extractFunctionParameters(story), path.scope);
return story;
}
Expand All @@ -55,9 +52,6 @@ export const extractCSFStories = (
name,
id: name,
};
if (declaration.async) {
story.async = true;
}
traverse(path.node, extractFunctionParameters(story), path.scope);
return story;
}
Expand Down
9 changes: 0 additions & 9 deletions core/instrument/src/babel/mdx-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,6 @@ export const extractMDXStories = (props: any) => (
name,
id,
};
if (
expression &&
expression.expression &&
expression.expression.type === 'ArrowFunctionExpression'
) {
if (expression.expression.async) {
story.async = true;
}
}
if (
expression &&
(expression.expression.type === 'CallExpression' ||
Expand Down
4 changes: 0 additions & 4 deletions core/instrument/test/__snapshots__/esm-async.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ Object {
"stories": Object {
"myStory": Object {
"arguments": Array [],
"async": true,
"id": "myStory",
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -140,7 +139,6 @@ Object {
"stories": Object {
"asyncStory": Object {
"arguments": Array [],
"async": true,
"id": "asyncStory",
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -237,7 +235,6 @@ Object {
"stories": Object {
"myStory": Object {
"arguments": Array [],
"async": true,
"id": "myStory",
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -348,7 +345,6 @@ Object {
"value": "props",
},
],
"async": true,
"id": "myStory",
"loc": Object {
"end": Object {
Expand Down
1 change: 0 additions & 1 deletion core/instrument/test/__snapshots__/mdx-async.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ Object {
"value": "props",
},
],
"async": true,
"id": "storyWithProps",
"loc": Object {
"end": Object {
Expand Down
6 changes: 3 additions & 3 deletions core/render/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ _defined in [@component-controls/render/src/index.ts](https://github.com/ccontro

### properties

| Name | Type | Description |
| -------- | ----------------------------------------------------------------------------------------------------------------------------------- | ----------- |
| `react*` | **function** (`story`\*: [Story](#story), `doc`: [Document](#document), `options`: any): Promise&lt;[ReactElement](#reactelement)>; | |
| Name | Type | Description |
| -------- | ----------------------------------------------------------------------------------------------------------------------- | ----------- |
| `react*` | **function** (`story`\*: [Story](#story), `doc`: [Document](#document), `options`: any): [ReactElement](#reactelement); | |

<!-- END-TSDOC-TYPESCRIPT -->
16 changes: 2 additions & 14 deletions core/render/src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ import {
FrameworkRenderFn,
} from '@component-controls/core';

const isPromise = (obj: any) => obj instanceof Promise;

export const render: FrameworkRenderFn = async (
story,
doc,
options: any = {},
) => {
export const render: FrameworkRenderFn = (story, doc, options: any = {}) => {
if (!story) {
throw new Error(`Invalid story`);
}
Expand Down Expand Up @@ -47,12 +41,6 @@ export const render: FrameworkRenderFn = async (
);
}
let node: any = null;
if (renderFn) {
if (story.async || isPromise(renderFn)) {
node = await (renderFn as StoryRenderFn)(values, context);
} else {
node = () => (renderFn as StoryRenderFn)(values, context);
}
}
node = () => (renderFn as StoryRenderFn)(values, context);
return createElement(node);
};
Loading

0 comments on commit f37b1ba

Please sign in to comment.