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

refactor(vanilla/types): replace hardcoded types with StoreApi type #1459

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
ref: replace hardcoded types with StoreApi type
Add binding of createStoreImpl methods types to StoreApi without hardcoding.
Makes it easier to read and reason about where the type comes from and belongs to.
Removing arrow function types reduces number of arrows, and makes it easier to read.
  • Loading branch information
Arsikod authored Dec 3, 2022
commit b07d80dcc6e490bf337068496b0283d466f037c1
8 changes: 4 additions & 4 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const createStoreImpl: CreateStoreImpl = (createState) => {
let state: TState
const listeners: Set<Listener> = new Set()

const setState: SetStateInternal<TState> = (partial, replace) => {
const setState: StoreApi<TState>['setState'] = (partial, replace) => {
// TODO: Remove type assertion once https://github.com/microsoft/TypeScript/issues/37663 is resolved
// https://github.com/microsoft/TypeScript/issues/37663#issuecomment-759728342
const nextState =
Expand All @@ -77,15 +77,15 @@ const createStoreImpl: CreateStoreImpl = (createState) => {
}
}

const getState: () => TState = () => state
const getState: StoreApi<TState>['getState'] = () => state

const subscribe: (listener: Listener) => () => void = (listener) => {
const subscribe: StoreApi<TState>['subscribe'] = (listener) => {
listeners.add(listener)
// Unsubscribe
return () => listeners.delete(listener)
}

const destroy: () => void = () => listeners.clear()
const destroy: StoreApi<TState>['destroy'] = () => listeners.clear()
const api = { setState, getState, subscribe, destroy }
state = createState(setState, getState, api)
return api as any
Expand Down