Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineState } from '@lwc/state';

export default defineState((atom) => (initialName = 'anotherFoo') => ({
export default defineState(({ atom }) => (initialName = 'anotherFoo') => ({
name: atom(initialName),
}));
2 changes: 1 addition & 1 deletion example/src/modules/x/childState/childState.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineState } from '@lwc/state';
import parentStateFactory from 'x/parentState';
import anotherParentStateFactory from 'x/anotherParentState';

export default defineState((atom, _computed, update, fromContext) => (initialName = 'bar') => {
export default defineState(({ atom, update, fromContext }) => (initialName = 'bar') => {
const name = atom(initialName);
const parentState = fromContext(parentStateFactory);
const anotherParentState = fromContext(anotherParentStateFactory);
Expand Down
2 changes: 1 addition & 1 deletion example/src/modules/x/grandChildState/grandChildState.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineState } from '@lwc/state';
import childStateFactory from 'x/childState';

export default defineState((_atom, _computed, _update, fromContext) => () => {
export default defineState(({ fromContext }) => () => {
const childState = fromContext(childStateFactory);

return {
Expand Down
2 changes: 1 addition & 1 deletion example/src/modules/x/parentState/parentState.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineState } from '@lwc/state';

export default defineState((atom, computed, update, fromContext) => (initialName = 'foo') => {
export default defineState(({ atom, update }) => (initialName = 'foo') => {
const name = atom(initialName);

const updateName = update({ name }, (_, newName) => ({
Expand Down
6 changes: 3 additions & 3 deletions packages/@lwc/state/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ yarn add @lwc/state

### defineState

The main function for creating state definitions. It provides four utilities:
The main function for creating state definitions. It provides an object with four utilities:

- `atom<T>(initialValue: T)`: Creates a reactive atomic value
- `computed(signals, computeFn)`: Creates derived state based on provided signals map
Expand All @@ -46,7 +46,7 @@ Create a state definition using `defineState`:
import { defineState } from '@lwc/state';

const useCounter = defineState(
(atom, computed, update) =>
({ atom, computed, update }) =>
(initialValue = 0) => {
// Create reactive atom
const count = atom(initialValue);
Expand Down Expand Up @@ -106,7 +106,7 @@ const context = defineState(
import contextFactory from '<parentState>';

const useTheme = defineState(
(_atom, _computed, _update, fromContext) =>
({ fromContext }) =>
() => {
const theme = fromContext(contextFactory);

Expand Down
2 changes: 1 addition & 1 deletion packages/@lwc/state/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let fruitNameAndCountNotifySpy: any;
// biome-ignore lint: test only
let fruitNameAndCountComputeValueSpy: any;

const state = defineState((atom, computed, update, _fromContext) => (...args) => {
const state = defineState(({ atom, computed, update }) => (...args) => {
const countArg = args[0] as number;
const fruitArg = args[1] as string;

Expand Down
15 changes: 8 additions & 7 deletions packages/@lwc/state/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { connectContext, disconnectContext } from './shared.js';
import type {
Computer,
DefineState,
ExposedAPIs,
ExposedUpdater,
MakeAtom,
MakeComputed,
Expand Down Expand Up @@ -130,12 +131,7 @@ export const defineState: DefineState = <
Args extends unknown[],
ContextShape,
>(
defineStateCallback: (
atom: MakeAtom,
computed: MakeComputed,
update: MakeUpdate,
fromContext: MakeContextHook<ContextShape>,
) => (...args: Args) => InnerStateShape,
defineStateCallback: (api: ExposedAPIs<ContextShape>) => (...args: Args) => InnerStateShape,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does all the type inference continue to work with this change? I'm in favor so long as the types (internal and external) can still be inferred when writing & consuming a state manager.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, the type inference is working!

) => {
const stateDefinition = (...args: Args) => {
class StateManagerSignal extends SignalBaseClass<OuterStateShape> implements ContextManager {
Expand Down Expand Up @@ -194,7 +190,12 @@ export const defineState: DefineState = <
return localContextSignal;
};

this.internalStateShape = defineStateCallback(atom, computed, update, fromContext)(...args);
this.internalStateShape = defineStateCallback({
atom,
computed,
update,
fromContext,
})(...args);

for (const signalOrUpdater of Object.values(this.internalStateShape)) {
if (signalOrUpdater && !isUpdater(signalOrUpdater)) {
Expand Down
14 changes: 8 additions & 6 deletions packages/@lwc/state/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export type ExposedUpdater = (...updaterArgs: unknown[]) => void;

export type ContextSignal<T> = Signal<T> & { id: symbol };

export type ExposedAPIs<ContextShape> = {
atom: MakeAtom;
computed: MakeComputed;
update: MakeUpdate;
fromContext: MakeContextHook<ContextShape>;
};

export type DefineState = <
InnerStateShape extends Record<string, Signal<unknown> | ExposedUpdater>,
OuterStateShape extends {
Expand All @@ -47,10 +54,5 @@ export type DefineState = <
Args extends unknown[],
ContextShape,
>(
defineStateCb: (
atom: MakeAtom,
computed: MakeComputed,
update: MakeUpdate,
fromContext: MakeContextHook<ContextShape>,
) => (...args: Args) => InnerStateShape,
defineStateCb: (api: ExposedAPIs<ContextShape>) => (...args: Args) => InnerStateShape,
) => (...args: Args) => Signal<OuterStateShape>;
Loading