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

Support async CSF stories with createAsyncCallback #258

Merged
merged 2 commits into from
Jul 24, 2020
Merged

Conversation

oblador
Copy link
Owner

@oblador oblador commented Jul 24, 2020

Usage

import createAsyncCallback from '@loki/create-async-callback';
export const AsyncStory = () => <MyComponent onDone={createAsyncCallback()} />;

Fixes #254 and #195

Not sure if the name is the best, but struggling to find a better alternative.

@oblador oblador requested a review from techeverri July 24, 2020 08:44
@@ -34,6 +35,9 @@ storiesOf('Asynchronous render', module)
.add('Logo with 1s delay', () => <Logo delay={1000} />)
.lokiAsync('lokiAsync() with 1s delay', ({ done }) => (
<DelayedComponent delay={1000} onDone={done} />
))
.add('createAsyncCallback() with 1s delay', () => (
<DelayedComponent delay={1000} onDone={createAsyncCallback()} />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this line mean that createAsyncCalkback can be used with both Component Story Format (CSF) and storiesOf? 🤔

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! 🚀

module.exports = function createAsyncCallback(win = window) {
const registerPendingPromise = win.loki && win.loki.registerPendingPromise;
let resolveAsyncStory;
if (registerPendingPromise) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it throw an error when registerPendingPromise is not available? 🤔

Don't really see this happening often but I still can imagine that some misconfiguration can lead to missing properties in the window object for example.

I guess such case calling createAsyncCallback will return a function that returns undefined and resolves nothing

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a console warning in those cases now, don't think it warrants throwing an error. And yes you're right, I figured returning a no-op would be the best thing to do as components might expect this being sent in as props, so wanted to be as graceful as possible.

@oblador oblador merged commit bc5131c into master Jul 24, 2020
@oblador oblador deleted the async-csf branch July 24, 2020 17:51
@pedrohmorais
Copy link

How can i use createAsyncCalkback in CSF format ?

@oblador
Copy link
Owner Author

oblador commented Dec 12, 2020

@pedrohmorais Just like normal, createAsyncCallback was created primarily to support CSF stories, and even the PR description shows an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to use lokiAsync in CPF fotmat?
3 participants