--- /dev/null
+import { makeStore } from '../../../src'
+import { userStore } from './user'
+
+export const cartStore = makeStore(
+ 'cart',
+ () => ({
+ rawItems: [] as string[],
+ }),
+ {
+ items: state =>
+ state.rawItems.reduce((items, item) => {
+ const existingItem = items.find(it => it.name === item)
+
+ if (!existingItem) {
+ items.push({ name: item, amount: 1 })
+ } else {
+ existingItem.amount++
+ }
+
+ return items
+ }, [] as { name: string; amount: number }[]),
+ },
+ {}
+)
+
+export function addItem(name: string) {
+ const store = cartStore.useStore()
+ store.state.rawItems.push(name)
+}
+
+export function removeItem(name: string) {
+ const store = cartStore.useStore()
+ const i = store.state.rawItems.indexOf(name)
+ if (i > -1) store.state.rawItems.splice(i, 1)
+}
+
+export async function purchaseItems() {
+ const cart = cartStore.useStore()
+ const user = userStore.useStore()
+ if (!user.state.name) return
+
+ console.log('Purchasing', cart.items.value)
+ const n = cart.items.value.length
+ cart.state.rawItems = []
+
+ return n
+}
--- /dev/null
+import { makeStore } from '../../../src'
+
+export const userStore = makeStore(
+ 'user',
+ () => ({
+ name: 'Eduardo',
+ isAdmin: true as boolean,
+ }),
+ {},
+ {}
+)
+
+export function logout() {
+ const store = userStore.useStore()
+
+ store.patch({
+ name: '',
+ isAdmin: false,
+ })
+
+ // we could do other stuff like redirecting the user
+}
+
+function apiLogin(a: string, p: string) {
+ if (a === 'ed' && p === 'ed') return Promise.resolve({ isAdmin: true })
+ return Promise.reject(new Error('invalid credentials'))
+}
+
+export async function login(user: string, password: string) {
+ const store = userStore.useStore()
+ const userData = await apiLogin(user, password)
+
+ store.patch({
+ name: user,
+ ...userData,
+ })
+}