Skip to content

Commit

Permalink
[release] Enable async handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Truong Hoang Dung authored and Truong Hoang Dung committed May 11, 2024
1 parent 69fbd04 commit 48e558f
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 7 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,6 @@ export function TodoApp(props: TodoAppProps) {
},
]);

const handleToggleTodo = (id: number) => {
dispatch('ToggleTodo', { id });
};

return (
<div>
<h2>Todo List:</h2>
Expand All @@ -246,7 +242,7 @@ export function TodoApp(props: TodoAppProps) {
<li
key={todo.id}
style={{ textDecoration: todo.completed ? 'line-through' : 'none' }}
onClick={() => handleToggleTodo(todo.id)}
onClick={() => dispatch('ToggleTodo', { id })}
>
{todo.text}
</li>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@revskill10/use-state",
"description": "",
"version": "0.0.8",
"version": "0.0.9",
"author": "",
"license": "MIT",
"keywords": [],
Expand Down
9 changes: 9 additions & 0 deletions src/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# How to contribute

## Develop locally with storybook

```js
pnpm dev
```

## Create pull request
63 changes: 63 additions & 0 deletions src/examples/Async.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from 'react';
import { state$ } from '../hooks';

export type ExampleProps = {
text?: String;
};
interface Message {
Decrement: {
amount: number;
};
Increment: {};
}
interface AppState {
loading?: boolean;
value: number;
}
export function AsyncExample(props: ExampleProps) {
const [count, dispatch] = state$<AppState, Message>({ value: 0 }, [
{
messages: ['Increment'],
handler: async (c) => {
c.loading.set(true);
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
c.loading.set(false);
c.value.set(c.value.get() + 1);
},
onChange: (c) => {
// eslint-disable-next-line no-console
console.log(`current count is ${c.get()}`);
},
},
{
messages: ['Decrement'],
handler: (c, payload) => {
if ('amount' in payload) {
c.value.set(c.value.get() - payload.amount ?? 1);
}
},
},
]);
return (
<>
{`${props.text} ${count.value}`}
<button
onClick={() => dispatch('Increment', {})}
type="button"
id="increment-button"
disabled={count.loading}
>
Increment
</button>
<button
onClick={() => dispatch('Decrement', { amount: 3 })}
type="button"
id="decrement-button"
>
Decrement
</button>
</>
);
}
2 changes: 1 addition & 1 deletion src/hooks/use-state/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type HandlerCreator<State, T> = (
state: Observable<State>,
message: T[keyof T],
bus?: EventBus<T>
) => void;
) => void | Promise<void>;
type Listener = {
unsubscribe: () => void;
};
Expand Down
18 changes: 18 additions & 0 deletions src/stories/Async.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import { Meta, StoryFn } from '@storybook/react';

import { AsyncExample as Example } from '../examples/Async';

export default {
title: 'AsyncExample',
component: Example,
argTypes: {},
} as Meta<typeof Example>;

const Template: StoryFn<typeof Example> = (args) => <Example {...args} />;

export const Primary = Template.bind({});

Primary.args = {
text: 'Clicked this many times:',
};

0 comments on commit 48e558f

Please sign in to comment.