Skip to content

Commit

Permalink
fix(types): fixed types for PartialState #2
Browse files Browse the repository at this point in the history
  • Loading branch information
l-Leniac-l committed Feb 17, 2022
1 parent dfe7fca commit f222525
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
12 changes: 3 additions & 9 deletions src/createGlobalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import { Observable } from "./Observable"
import { isBrowser } from "./isBrowser"
import { isEqual } from "./isEqual"
import { rehydrate } from "./rehydrate"
import {
GlobalState,
GlobalStateOptions,
PartialState,
SetStateOptions,
} from "./types"
import { GlobalState, GlobalStateOptions, SetStateOptions } from "./types"
import updater from "./updater"
import { createHook } from "./createHook"
import { createReadOnlyHook } from "./createReadOnlyHook"
Expand Down Expand Up @@ -43,9 +38,8 @@ export const createGlobalState = <TState>(

const useReadOnlyState = createReadOnlyHook(state$, state => state)

const createPartialState = <TPartial>(
project: (state: TState) => PartialState<TState, TPartial>
) => createReadOnlyHook<TState, TPartial>(state$, project)
const createPartialState = <TPartial>(project: (state: TState) => TPartial) =>
createReadOnlyHook<TState, TPartial>(state$, project)

return {
useGlobalState,
Expand Down
3 changes: 1 addition & 2 deletions src/createReadOnlyHook.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useEffect, useState } from "react"

import { Observable } from "./Observable"
import { PartialState } from "./types"

export function createReadOnlyHook<TState, TPartial>(
state$: Observable<TState>,
project: (state: TState) => PartialState<TState, TPartial>
project: (state: TState) => TPartial
) {
return () => {
const [state, setState] = useState(project(state$.value))
Expand Down
8 changes: 3 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ export interface GlobalStateOptions {
persistence?: PersistenceOptions
}

export type PartialState<TState, TPartial> = TState | TPartial

export interface GlobalState<TState> {
useGlobalState: () => readonly [
TState,
(state: SetStateAction<TState>, options?: SetStateOptions) => void
]
useReadOnlyState: () => PartialState<TState, TState>
useReadOnlyState: () => TState
createPartialState: <TPartial>(
project: (state: TState) => PartialState<TState, TPartial>
) => () => PartialState<TState, TPartial>
project: (state: TState) => TPartial
) => () => TPartial
getGlobalState: () => TState
setGlobalState: (
state: SetStateAction<TState>,
Expand Down

0 comments on commit f222525

Please sign in to comment.