Skip to content

Commit

Permalink
fix(createSingleLoading, useReactive): use create option to avoid i…
Browse files Browse the repository at this point in the history
…mport and ESM require error
  • Loading branch information
vikiboss committed Jul 2, 2024
1 parent 40c11c7 commit 2b8548c
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 26 deletions.
6 changes: 3 additions & 3 deletions examples/vite-unocss-csr/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { useMount } from '@shined/react-use'
// import { PartSix } from './parts/part-six'
// import { PartFive } from './parts/part-five'
// import { PartFour } from './parts/part-four'
// import { PartOne } from './parts/part-one'
// import { PartThree } from './parts/part-three'
// import { PartTwo } from './parts/part-two'
// import { Playground } from './playground'
import { useMount } from '@shined/react-use'
import { Playground } from './playground'

export function App() {
useMount(() => {})

return (
<div className="min-h-screen w-screen flex justify-center">
<div className="flex flex-col gap-2 w-800px">
{/* <Playground /> */}
<Playground />
{/* <PartSix /> */}
{/* <PartFive /> */}
{/* <PartFour /> */}
Expand Down
3 changes: 2 additions & 1 deletion src/create-single-loading/demo.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Button, Card, KeyValue, OTP, Zone, wait as mockFetch } from '@/components'
import { createSingleLoading } from '@shined/react-use'
import { create } from '@shined/reactive'

const pageLoading = createSingleLoading()
const pageLoading = createSingleLoading({ create })

const fetchOutsideReact = pageLoading.bind(async (otp: string) => {
await mockFetch()
Expand Down
3 changes: 2 additions & 1 deletion src/create-single-loading/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import { Source } from '@/components'
## API

```tsx
const pageLoading = createSingleLoading(options)
import { create } from '@shined/reactive'
const pageLoading = createSingleLoading({ create })
const { set, get, bind, useLoding, useAsyncFn } = pageLoading
```

Expand Down
16 changes: 12 additions & 4 deletions src/create-single-loading/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useAsyncFn as useAsyncFnOrigin } from '../use-async-fn'
import { isFunction } from '../utils/basic'

import type { create } from '@shined/reactive'
import type { UseAsyncFnReturns } from '../use-async-fn'
Expand Down Expand Up @@ -51,6 +52,10 @@ export interface CreateSingleLoadingReturns {
}

export interface CreateSingleLoadingOptions {
/**
* `Create` function of Reactive
*/
create: typeof create
/**
* Whether set to false on error
*
Expand All @@ -75,11 +80,14 @@ export interface CreateSingleLoadingOptions {
*
* @see {@link https://sheinsight.github.io/reactive/ | Reactive - Documentation}
*/
export function createSingleLoading(options: CreateSingleLoadingOptions = {}): CreateSingleLoadingReturns {
const { resetOnError = true, initialState = false } = options
export function createSingleLoading(options: CreateSingleLoadingOptions): CreateSingleLoadingReturns {
const { resetOnError = true, create, initialState = false } = options

if (!isFunction(create)) {
throw new Error('Please provide the `create` function from `@shined/reactive`')
}

const createStore = require('@shined/reactive').create as typeof create
const store = createStore({ loading: initialState })
const store = create({ loading: initialState })

function bind<T extends AnyFunc>(func: T): T {
return (async (...args: Parameters<T>) => {
Expand Down
3 changes: 2 additions & 1 deletion src/create-single-loading/index_zh-cn.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import { Source } from '@/components'
## API

```tsx
const pageLoading = createSingleLoading(options)
import { create } from '@shined/reactive'
const pageLoading = createSingleLoading({ create })
const { set, get, bind, useLoading, useAsyncFn } = pageLoading
```

Expand Down
14 changes: 9 additions & 5 deletions src/use-reactive/demo.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Button, Card, KeyValue, OTP, Zone } from '@/components'
import { useReactive } from '@shined/react-use'
import { create } from '@shined/reactive'

export function App() {
const [state, mutate] = useReactive({
name: 'Bob',
age: 20,
hobbies: ['running', 'coding'],
})
const [state, mutate] = useReactive(
{
name: 'Bob',
age: 20,
hobbies: ['running', 'coding'],
},
{ create },
)

return (
<Card>
Expand Down
3 changes: 2 additions & 1 deletion src/use-reactive/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import { Source } from '@/components'
## API

```tsx
const [state, mutate] = useReactive(initialState, options)
import { create } from '@shined/reactive';
const [state, mutate] = useReactive(initialState, { create })
```

### InitialState
Expand Down
23 changes: 14 additions & 9 deletions src/use-reactive/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCreation } from '../use-creation'
import { isFunction } from '../utils/basic'

import type { create } from '@shined/reactive'

Expand All @@ -11,19 +12,23 @@ interface SnapshotOptions<StateSlice> {
/**
* A React Hook that helps to use [Reactive](https://sheinsight.github.io/reactive) in React with ease.
*/
export function useReactive<State extends object>(initialState: State): readonly [State, State]
export function useReactive<State extends object>(
initialState: State,
options?: SnapshotOptions<State>,
): readonly [State, State]
export function useReactive<State extends object>(
initialState: State,
options: SnapshotOptions<State> = {},
options: SnapshotOptions<State> & { create: typeof create },
): readonly [State, State] {
const store = useCreation(() => {
const createStore = require('@shined/reactive').create as typeof create
return createStore(initialState)
if (!isFunction(options.create)) {
throw new Error('Please provide the `create` function from `@shined/reactive`')
}

return options.create(initialState)
})

return [store.useSnapshot(options), store.mutate] as const
return [
store.useSnapshot({
sync: options.sync,
isEqual: options.isEqual,
}),
store.mutate,
] as const
}
3 changes: 2 additions & 1 deletion src/use-reactive/index_zh-cn.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import { Source } from '@/components'
## API

```tsx
const [state, mutate] = useReactive(initialState, options)
import { create } from '@shined/reactive';
const [state, mutate] = useReactive(initialState, { create })
```

### InitialState
Expand Down

0 comments on commit 2b8548c

Please sign in to comment.