@cristaline/web
v1.0.0
Published
Web Storage API adapter based on the JSON stream format.
Readme
@cristaline/web
Requirements
Installation
npm create vite -- --template vanilla-ts project
cd project
npm install @cristaline/core @cristaline/web zod
touch src/main.tsimport type { EventShape } from "@cristaline/core";
import type { ZodSchema } from "zod";
import { createEventStore, MemoryState } from "@cristaline/core";
import { StorageEvent } from "@cristaline/web";
import { z } from "zod";
const eventSchema = z.object({
id: z.string(),
date: z.date({ coerce: true }),
type: z.literal("TodoAdded"),
version: z.literal(1),
data: z.object({
id: z.string(),
title: z.string()
})
}) satisfies ZodSchema<EventShape>
type Todo = {
id: string,
title: string
}
type State = {
todos: Array<Todo>
}
const eventStore = createEventStore({
event: StorageEvent.for({
storage: localStorage,
key: "events",
parser: eventSchema.parse
}),
state: MemoryState.for({
state: {
todos: []
}
}),
replay: (state, event) => {
return {
...state,
todos: [
...state.todos,
event.data
]
}
}
});
await eventStore.initialize();
console.log(eventStore.getState());Changelogs
Versions
1.0.0
Major changes
- Renamed the
StorageEventAdapterclass toStorageEvent
0.1.0
Major changes
None.
Minor changes
None.
Bug & security fixes
None.
