|
| 1 | +## ❗️ In Development ❗️ |
| 2 | + |
| 3 | +The project is still in development. Hoping to release it right before Svelte 5 release. You can find usable examples in `src/routes/+page.svelte`. |
| 4 | + |
| 5 | +# katai |
| 6 | + |
| 7 | +Kaṭai (meaning store in Tamil) is a simple and lightweight store implementation for Svelte 5. |
| 8 | + |
| 9 | +The basics of Katai is a few primitives that can be used to build any type of store. You can also build the store type you need with the help of these primitives. We also provide a few pre-built variations to choose from the Stores options. Whichever feels the most suitable can be used. |
| 10 | + |
| 11 | +We do not wish to restrict you to a particular pattern for all your stores. You can copy the one of the current stores implementation as a base and build from on top it. |
| 12 | + |
| 13 | +## Primitives |
| 14 | + |
| 15 | +### createStore |
| 16 | + |
| 17 | +The function `createStore` creates a primitive store with a specified name and initial state. |
| 18 | + |
| 19 | +- @param {string} storeName - The `storeName` parameter is a string that represents the name of the store being created. It is a required parameter for creating a new store. |
| 20 | +- @param {InferedState} storeState - The `storeState` parameter in the `createStore` function represents the initial state or value that will be stored in the created store. It is the data that the store will manage and provide access to. |
| 21 | +- @param {StoreOptions} [options] - The `options` parameter in the `createStore` function is an optional parameter that allows you to provide additional configuration options for creating the store. It is of type `StoreOptions`, which likely contains properties or settings that can be used to customize the behavior of the store creation process like adding cache adapters. |
| 22 | +- @returns A PrimitiveStore object with the store name and a getter function for the store value. |
| 23 | + |
| 24 | +#### EXAMPLE |
| 25 | + |
| 26 | +```ts |
| 27 | +export const counterCoreStore = createStore('test', { |
| 28 | + counter: 0 |
| 29 | +}); |
| 30 | + |
| 31 | +// Returns |
| 32 | +PrimitiveStore<{ |
| 33 | + counter: number; |
| 34 | +}>; |
| 35 | +``` |
| 36 | + |
| 37 | +### get |
| 38 | + |
| 39 | +The `get` function takes a store and a derivation function, and returns a getter function that applies the derivation function to the store's value. |
| 40 | + |
| 41 | +- @param store - `PrimitiveStore<T>` is a generic type representing a store that holds a value of type `T`. It seems like the `store` parameter is expected to be an instance of this `PrimitiveStore` type. |
| 42 | +- @param derivation - The `derivation` parameter is a function that takes the current state of type `T` as input and returns a value of type `U`. It is used to derive a new value based on the current state stored in the `PrimitiveStore`. |
| 43 | +- @returns A `Getter<U>` function is being returned. This function takes no arguments and returns a value of type `U`. The value returned is the result of applying the `derivation` function to the `store.value`. |
| 44 | + |
| 45 | +### update |
| 46 | + |
| 47 | +The function `update` takes a store, a mutator function, and a payload, and updates the store's value using the mutator function while handling caching if applicable. |
| 48 | + |
| 49 | +- @param store - The `store` parameter is a PrimitiveStore object that holds a value of type T. |
| 50 | +- @param mutator - The `mutator` parameter is a function that takes the current state of the store (`T`) and a payload of type `C`, and returns a new state of type `U`. It is used to update the state of the store based on the provided payload. |
| 51 | +- @returns The `update` function returns an `Updater` function that takes a value of type `C` as an argument. |
| 52 | + |
| 53 | +### subscribe |
| 54 | + |
| 55 | +The `subscribe` function allows for subscribing to a primitive store with specified subscribers and an effect to be executed. |
| 56 | + |
| 57 | +- @param store - The `store` parameter is a PrimitiveStore that holds the state of type T. |
| 58 | +- @param subscribers - Subscribers are functions that subscribe to changes in the store's state. They are typically used to extract specific pieces of state from the store and react to changes in those pieces of state. |
| 59 | +- @param effect - The `effect` parameter in the `subscribe` function is a function that takes a `MapSources` object as its argument and performs some action based on the states provided in the `MapSources` object. |
| 60 | +- @returns The `subscribe` function returns a cleanup function that can be used to unsubscribe the effect and remove it from the list of subscribers. |
| 61 | + |
| 62 | +#### EXAMPLE |
| 63 | + |
| 64 | +```ts |
| 65 | +export const test = createStore('test', { |
| 66 | + counter: 0 |
| 67 | +}); |
| 68 | + |
| 69 | +type StoreType = (typeof test)['value']; |
| 70 | + |
| 71 | +export const testStore = { |
| 72 | + get $value() { |
| 73 | + return test.value; |
| 74 | + }, |
| 75 | + get: <U extends unknown>(derivation: (val: StoreType) => U) => get(test, () => derivation(test.value)), |
| 76 | + subscribe: <T extends Subscribers<StoreType>>(states: [...T], effect: (states: MapSources<T, StoreType>) => void) => |
| 77 | + subscribe(test, states, effect) |
| 78 | +}; |
| 79 | + |
| 80 | +testStore.subscribe([(state) => state.counter], ([value]) => { |
| 81 | + console.log('counter', value); |
| 82 | +}); |
| 83 | + |
| 84 | +const interval = setInterval(() => { |
| 85 | + testStore.$value.counter = testStore.$value.counter + 1; |
| 86 | +}, 2000); |
| 87 | + |
| 88 | +// cleanup |
| 89 | +onDestroy(() => { |
| 90 | + clearInterval(interval); |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +## Stores |
| 95 | + |
| 96 | +### Basic Store |
| 97 | + |
| 98 | +The function `createBasicStore` creates a basic store with state, getters, and actions based on the provided options. |
| 99 | + |
| 100 | +- @param {string} storeName - The `storeName` parameter is a string that represents the name of the store being created. |
| 101 | +- @param options - The `options` parameter in the `createBasicStore` function is an object that contains the following properties: |
| 102 | +- @param {StoreOptions} [settings] - The `settings` parameter in the `createBasicStore` function is an optional parameter of type `StoreOptions`. It allows you to provide additional settings or configurations for the store creation process. These settings can include options such as the store's persistence mechanism using cache adapters. |
| 103 | +- @returns The `createBasicStore` function returns an object of type `BasicStore<S, G, A>`, which includes the state, getters, actions, and additional methods like `clearCache` and `subscribe`. |
| 104 | + |
| 105 | +#### EXAMPLE |
| 106 | + |
| 107 | +```ts |
| 108 | +const newStore = createBasicStore('tester', { |
| 109 | + state: { |
| 110 | + counter: 0, |
| 111 | + count: 0 |
| 112 | + }, |
| 113 | + getters: { |
| 114 | + getCounter: (state) => state.counter, |
| 115 | + getCount: (state) => String(state.count) |
| 116 | + }, |
| 117 | + actions: { |
| 118 | + updateCounter: (state, payload: number) => { |
| 119 | + state.counter += payload; |
| 120 | + }, |
| 121 | + updateCount: (state, payload: number) => { |
| 122 | + state.count += payload; |
| 123 | + } |
| 124 | + } |
| 125 | +}); |
| 126 | + |
| 127 | +const interval = setInterval(() => { |
| 128 | + newStore.updateCounter(1); |
| 129 | +}, 2000); |
| 130 | + |
| 131 | +// cleanup |
| 132 | +onDestroy(() => { |
| 133 | + clearInterval(interval); |
| 134 | +}); |
| 135 | + |
| 136 | +newStore.subscribe([(state) => state.counter], (states) => { |
| 137 | + console.log('newStore', states[0]); |
| 138 | +}); |
| 139 | +``` |
| 140 | + |
| 141 | +### Writable Store |
| 142 | + |
| 143 | +The `createWritable` function creates a writable store with initial value and provides methods for getting, setting, updating, subscribing to changes, and clearing cache. |
| 144 | + |
| 145 | +- @param {T} initalValue - The `initalValue` parameter is the initial value that will be stored in the writable store. It should be an object of type `T`, which extends `Record<string, any>`. This initial value will be used as the starting value for the store. |
| 146 | +- @param storeName - The `storeName` parameter is a string that represents the name of the store where the data will be stored. If no `storeName` is provided, a random string will be generated for the store name. |
| 147 | +- @param {StoreOptions} [storeOptions] - The `storeOptions` parameter in the `createWritable` function is an optional parameter that allows you to specify additional options for the store creation. These options can include configuration settings or options specific to the underlying store implementation. If provided, these options will be used when creating the store using the `create |
| 148 | +- @returns An object is being returned with the following properties: |
| 149 | +- - get: a function that retrieves the current value from the store |
| 150 | +- - set: a function that updates the value in the store |
| 151 | +- - update: a function that takes a callback to update the value in the store |
| 152 | +- - subscribe: a function that subscribes to changes in the store and calls a subscriber function |
| 153 | +- - clearCache: a function that |
0 commit comments