Skip to content
Open
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
10 changes: 5 additions & 5 deletions packages/store/src/create-computed/create-computed-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ type ComputedDefToMethods<TComputedDef> = TComputedDef extends (
input: infer TReadInput
) => infer TReadValue
? {
use: (input: VoidIfUnknown<TReadInput>) => TReadValue;
get: (input: VoidIfUnknown<TReadInput>) => TReadValue;
use: (input: VoidIfUnknown<TReadInput>) => Readonly<TReadValue>;
get: (input: VoidIfUnknown<TReadInput>) => Readonly<TReadValue>;
}
: TComputedDef extends {
read: (input: infer TReadInput) => infer TReadValue;
write?: (value: infer TWriteInput) => void;
}
? {
use: (input: VoidIfUnknown<TReadInput>) => TReadValue;
get: (input: VoidIfUnknown<TReadInput>) => TReadValue;
use: (input: VoidIfUnknown<TReadInput>) => Readonly<TReadValue>;
get: (input: VoidIfUnknown<TReadInput>) => Readonly<TReadValue>;
set: TComputedDef['write'] extends undefined
? never
: (value: TWriteInput) => void;
Expand All @@ -39,7 +39,7 @@ export type ComputedMethods<TComputedProps extends ComputedProps> = {
};

export type ComputedBuilder<TStore, TComputedProps extends ComputedProps> = (
store: TStore
store: Readonly<TStore>
) => TComputedProps;

function hasWrite<TReadValue, TWriteInput, TReadInput>(
Expand Down
13 changes: 6 additions & 7 deletions packages/store/src/create-state-methods/state.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import { EqualityChecker, Simplify, StateValue } from '../types';

type ReadonlyState<TSelector, TStateValue> = TSelector extends (state: TStateValue) => infer TReturnType
? Readonly<TReturnType>
: Readonly<TStateValue>;

export type State<TStateValue extends StateValue> = (TStateValue extends object
? {
[TKey in keyof TStateValue]: State<TStateValue[TKey]>;
Expand All @@ -28,9 +32,7 @@ export type State<TStateValue extends StateValue> = (TStateValue extends object
) => TStateValue,
>(
selector?: TSelector
) => TSelector extends (state: TStateValue) => infer TReturnType
? TReturnType
: TStateValue;
) => ReadonlyState<TSelector, TStateValue>;
/**
* Set a new state for the entire store using Immer
* @param fn A function that mutates the current state
Expand All @@ -56,10 +58,7 @@ export type State<TStateValue extends StateValue> = (TStateValue extends object
>(
selector?: TSelector,
equalityFn?: EqualityChecker<TStateValue>
) => TSelector extends (state: TStateValue) => infer TReturnType
? TReturnType
: TStateValue;

) => ReadonlyState<TSelector, TStateValue>;
/**
* Subscribe to changes in the store
* @param callback A callback that is called whenever the store changes
Expand Down
19 changes: 2 additions & 17 deletions packages/store/src/store-builder/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ComputedProps,
createComputedMethods,
} from '../create-computed/create-computed-methods';
import { createEffectMethods } from '../create-effects';
import { createStoreApiProxy } from '../create-store/create-store-proxy';
import { createStore } from '../create-store/create-zustand-store';

Expand Down Expand Up @@ -83,23 +82,9 @@ export const store = <TState extends StateValue>(
export function getDefaultStoreDef<TState extends StateValue>(
initialState?: TState
) {
const _def = {
return new StoreDef({
initialState,
extensions: [],
options: {},
get name() {
const options = _def.options as StoreOptions<TState>;
const name = options.name as string;
if (name) return name;

const stateString = _def.initialState
? JSON.stringify(_def.initialState)
: 'no-state';
const defaultName = `(davstack/store)initialState=${stateString}`;
Object.assign(_def.options, { name: defaultName });
return defaultName as string;
},
};

return _def satisfies StoreDef<TState>;
})
}
25 changes: 23 additions & 2 deletions packages/store/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,32 @@ export type StateValue = unknown;

export type { StoreApi as ZustandStoreApi } from 'zustand';

export interface StoreDef<TStateValue extends StateValue = {}> {
name: string;
export class StoreDef<TStateValue extends StateValue = {}> {

extensions: Array<(store: StoreApi<TStateValue>) => Record<string, any>>;
options: StoreOptions<TStateValue>;
initialState: TStateValue | undefined;

constructor(def: Omit<StoreDef<TStateValue>, "name">) {
this.extensions = def.extensions;
this.options = def.options;
this.initialState = def.initialState
}

get name() {
const _def = this;
const options = _def.options as StoreOptions<TStateValue>;
const name = options.name as string;
if (name) return name;

const stateString = _def.initialState
? JSON.stringify(_def.initialState)
: 'no-state';
const defaultName = `(davstack/store)initialState=${stateString}`;
Object.assign(_def.options, { name: defaultName });
return defaultName as string;
}

}

export type StoreBuilderMethods<
Expand Down