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

Simplifies vanilla StoreApi #2507

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ If you want to contribute to the [documentation](./docs/):
As a temporary measure, you can do the following
(don't commit any changes made in the pmndrs/website repo):
- In your own Zustand fork, create a new working branch
(further related to as `[your-branch]`);
(referred to as `[your-branch]` going forward);
- Inside website codebase, open `src/data/libraries.ts`;
- Within the `zustand` key,
change `docs: 'pmndrs/zustand/main/docs'`
Expand Down
42 changes: 21 additions & 21 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
type SetStateInternal<T> = {
_(
partial: T | Partial<T> | { _(state: T): T | Partial<T> }['_'],
replace?: boolean | undefined,
// Shows users better types sooner than globalThis.Partial
type Partial<T> = never | { [K in keyof T]+?: T[K] }

export interface StoreApi<T> extends Deprecated.Api {
getState(): T
setState(
partial: Partial<T> | ((state: T) => Partial<T>),
replace?: boolean,
): void
}['_']

export interface StoreApi<T> {
setState: SetStateInternal<T>
getState: () => T
getInitialState: () => T
subscribe: (listener: (state: T, prevState: T) => void) => () => void
/**
* @deprecated Use `unsubscribe` returned by `subscribe`
*/
destroy: () => void
subscribe(listener: (state: T, prevState: T) => void): () => void
getInitialState(): T
}

type Get<T, K, F> = K extends keyof T ? T[K] : F
Expand Down Expand Up @@ -65,12 +60,7 @@ const createStoreImpl: CreateStoreImpl = (createState) => {
const listeners: Set<Listener> = new Set()

const setState: StoreApi<TState>['setState'] = (partial, replace) => {
// TODO: Remove type assertion once https:/microsoft/TypeScript/issues/37663 is resolved
// https:/microsoft/TypeScript/issues/37663#issuecomment-759728342
const nextState =
typeof partial === 'function'
? (partial as (state: TState) => TState)(state)
: partial
const nextState = typeof partial === 'function' ? partial(state) : partial
if (!Object.is(nextState, state)) {
const previousState = state
state =
Expand Down Expand Up @@ -183,3 +173,13 @@ export type GetState<T extends State> = () => T
* @deprecated Use `StoreApi<T>['destroy']` instead of `Destroy`.
*/
export type Destroy = () => void

/* eslint-disable-next-line @typescript-eslint/no-namespace */
declare namespace Deprecated {
interface Api {
/**
* @deprecated Use `unsubscribe` returned by `subscribe`
*/
destroy(): void
}
}
Loading