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

[@xstate/store] Add examples + update createStoreWithProducer(…) #5079

Merged
merged 19 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions packages/core/src/inspection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
ActorRefLike,
AnyActorRef,
AnyEventObject,
AnyTransitionDefinition,
Snapshot
Expand Down Expand Up @@ -51,7 +50,7 @@ export interface InspectedEventEvent extends BaseInspectionEventProperties {
// The source might not exist, e.g. when:
// - root init events
// - events sent from external (non-actor) sources
sourceRef: AnyActorRef | undefined;
sourceRef: ActorRefLike | undefined;
Copy link
Member

Choose a reason for hiding this comment

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

u might want to add some changeset for this change as without it u wont release xstate package and @xstore/store won't be able to utilize this improvement

Copy link
Member Author

Choose a reason for hiding this comment

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

event: AnyEventObject; // { type: string, ... }
}

Expand Down
4 changes: 2 additions & 2 deletions packages/xstate-store/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
EnqueueObject,
EventPayloadMap,
ExtractEventsFromPayloadMap,
InspectionEvent,
StoreInspectionEvent,
InteropSubscribable,
Observer,
Recipe,
Expand Down Expand Up @@ -47,7 +47,7 @@ function setter<TContext extends StoreContext>(

const inspectionObservers = new WeakMap<
Store<any, any, any>,
Set<Observer<InspectionEvent>>
Set<Observer<StoreInspectionEvent>>
>();

function createStoreCore<
Expand Down
40 changes: 16 additions & 24 deletions packages/xstate-store/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ export interface Store<
*/
inspect: (
observer:
| Observer<InspectionEvent>
| ((inspectionEvent: InspectionEvent) => void)
| Observer<StoreInspectionEvent>
| ((inspectionEvent: StoreInspectionEvent) => void)
) => Subscription;
sessionId: string;
on: <TEmittedType extends TEmitted['type']>(
Expand Down Expand Up @@ -232,14 +232,12 @@ export type EventObject = {
};
type Values<T> = T[keyof T];

export type InspectionEvent =
| InspectedSnapshotEvent
| InspectedEventEvent
| InspectedActorEvent
| InspectedMicrostepEvent
| InspectedActionEvent;
export type StoreInspectionEvent =
Copy link
Member

Choose a reason for hiding this comment

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

technically this is a breaking change as you have renamed a public export, the same applied to other renamed exports in this file

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a deprecation instead

Copy link
Member Author

Choose a reason for hiding this comment

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

| StoreInspectedSnapshotEvent
| StoreInspectedEventEvent
| StoreInspectedActorEvent;

interface BaseInspectionEventProperties {
interface StoreBaseInspectionEventProperties {
rootId: string; // the session ID of the root
/**
* The relevant actorRef for the inspection event.
Expand All @@ -251,33 +249,26 @@ interface BaseInspectionEventProperties {
actorRef: ActorRefLike;
}

export interface InspectedSnapshotEvent extends BaseInspectionEventProperties {
export interface StoreInspectedSnapshotEvent
extends StoreBaseInspectionEventProperties {
type: '@xstate.snapshot';
event: AnyEventObject; // { type: string, ... }
snapshot: Snapshot<unknown>;
}

interface InspectedMicrostepEvent extends BaseInspectionEventProperties {
type: '@xstate.microstep';
event: AnyEventObject; // { type: string, ... }
snapshot: Snapshot<unknown>;
_transitions: unknown[];
}

export interface InspectedActionEvent extends BaseInspectionEventProperties {
export interface StoreInspectedActionEvent
extends StoreBaseInspectionEventProperties {
type: '@xstate.action';
action: {
type: string;
params: Record<string, unknown>;
};
}

export interface InspectedEventEvent extends BaseInspectionEventProperties {
export interface StoreInspectedEventEvent
extends StoreBaseInspectionEventProperties {
type: '@xstate.event';
// The source might not exist, e.g. when:
// - root init events
// - events sent from external (non-actor) sources
sourceRef: ActorRefLike | undefined;
sourceRef: AnyStore | undefined;
event: AnyEventObject; // { type: string, ... }
}

Expand All @@ -286,7 +277,8 @@ interface AnyEventObject {
[key: string]: any;
}

export interface InspectedActorEvent extends BaseInspectionEventProperties {
export interface StoreInspectedActorEvent
extends StoreBaseInspectionEventProperties {
type: '@xstate.actor';
}

Expand Down