@xstate/store
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -67,3 +67,3 @@ import { EventPayloadMap, Store, ExtractEventsFromPayloadMap, StoreSnapshot, StoreAssigner, StorePropertyAssigner, StoreContext } from "./types.js"; | ||
*/ | ||
export declare function createStoreWithProducer<TContext extends StoreContext, TEventPayloadMap extends EventPayloadMap>(producer: (context: TContext, recipe: (context: TContext) => void) => TContext, initialContext: TContext, transitions: { | ||
export declare function createStoreWithProducer<TContext extends StoreContext, TEventPayloadMap extends EventPayloadMap>(producer: NoInfer<(context: TContext, recipe: (context: TContext) => void) => TContext>, initialContext: TContext, transitions: { | ||
[K in keyof TEventPayloadMap & string]: (context: NoInfer<TContext>, event: { | ||
@@ -78,2 +78,8 @@ type: K; | ||
} | ||
/** | ||
* Creates a store function, which is a function that accepts the current snapshot and an event and returns a new snapshot. | ||
* @param transitions | ||
* @param updater | ||
* @returns | ||
*/ | ||
export declare function createStoreTransition<TContext extends StoreContext, TEventPayloadMap extends EventPayloadMap>(transitions: { | ||
@@ -80,0 +86,0 @@ [K in keyof TEventPayloadMap & string]: StoreAssigner<NoInfer<TContext>, { |
@@ -124,2 +124,8 @@ 'use strict'; | ||
} | ||
/** | ||
* Creates a store function, which is a function that accepts the current snapshot and an event and returns a new snapshot. | ||
* @param transitions | ||
* @param updater | ||
* @returns | ||
*/ | ||
function createStoreTransition(transitions, updater) { | ||
@@ -126,0 +132,0 @@ return (snapshot, event) => { |
@@ -120,2 +120,8 @@ const symbolObservable = (() => typeof Symbol === 'function' && Symbol.observable || '@@observable')(); | ||
} | ||
/** | ||
* Creates a store function, which is a function that accepts the current snapshot and an event and returns a new snapshot. | ||
* @param transitions | ||
* @param updater | ||
* @returns | ||
*/ | ||
function createStoreTransition(transitions, updater) { | ||
@@ -122,0 +128,0 @@ return (snapshot, event) => { |
{ | ||
"name": "@xstate/store", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Simple stores", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -26,9 +26,6 @@ # `@xstate/store` | ||
// 1. Create a store | ||
export const donutStore = createStore( | ||
{ donuts: 0, favoriteFlavor: 'chocolate' }, | ||
{ | ||
donuts: 0, | ||
favoriteFlavor: 'chocolate' | ||
}, | ||
{ | ||
addDonut: { | ||
@@ -46,25 +43,14 @@ donuts: (context) => context.donuts + 1 | ||
console.log(store.getSnapshot()); | ||
// { | ||
// status: 'active', | ||
// context: { | ||
// donuts: 0, | ||
// favoriteFlavor: 'chocolate' | ||
// } | ||
// } | ||
// 2. Subscribe to the store | ||
store.subscribe((snapshot) => { | ||
donutStore.subscribe((snapshot) => { | ||
console.log(snapshot.context); | ||
}); | ||
// 3. Send events | ||
store.send({ type: 'addDonut' }); | ||
// logs { donuts: 1, favoriteFlavor: 'chocolate' } | ||
donutStore.send({ type: 'addDonut' }); | ||
// => { donuts: 1, favoriteFlavor: 'chocolate' } | ||
store.send({ | ||
donutStore.send({ | ||
type: 'changeFlavor', | ||
flavor: 'strawberry' // Strongly-typed! | ||
}); | ||
// logs { donuts; 1, favoriteFlavor: 'strawberry' } | ||
// => { donuts: 1, favoriteFlavor: 'chocolate' } | ||
``` | ||
@@ -71,0 +57,0 @@ |
29856
631
163