Comparing version 0.8.7 to 0.9.0
# Change Log | ||
This project adheres to [Semantic Versioning](http://semver.org/). | ||
## 0.9 | ||
* Allow to define types for events (by Pawel Majewski). | ||
## 0.8.7 | ||
@@ -5,0 +8,0 @@ * Reduce `devtools/logger` size (by WrinkleJ and Alexey Berezin). |
declare namespace createStore { | ||
export type Dispatch = (event: PropertyKey, data?: unknown) => void; | ||
export type Dispatch<EventsDataTypesMap = any> = <Event extends keyof EventsDataTypesMap>( | ||
event: Event, ...data: (EventsDataTypesMap[Event] extends (never | undefined) ? [never?] : [EventsDataTypesMap[Event]])) => void; | ||
export interface Store<State = unknown> { | ||
readonly on: ( | ||
event: PropertyKey, | ||
handler: (state: Readonly<State>, data: any) => Partial<State> | Promise<void> | null | void | ||
export interface Store<State = unknown, EventsDataTypesMap extends StoreonEvents<State> = any> { | ||
readonly on: <Event extends keyof EventsDataTypesMap>( | ||
event: Event, | ||
handler: (state: Readonly<State>, data: EventsDataTypesMap[Event]) => Partial<State> | Promise<void> | null | void | ||
) => () => void; | ||
readonly dispatch: Dispatch; | ||
readonly dispatch: Dispatch<EventsDataTypesMap>; | ||
readonly get: () => State; | ||
} | ||
export type Module<State> = (store: Store<State>) => void; | ||
export type Module<State, EventsDataTypesMap extends StoreonEvents<State> = any> = (store: Store<State, EventsDataTypesMap>) => void; | ||
export interface StoreonEvents<State> { | ||
'@init': never; | ||
'@changed': Partial<State>; | ||
} | ||
} | ||
declare function createStore<State>( | ||
modules: Array<createStore.Module<State> | false> | ||
): createStore.Store<State>; | ||
declare function createStore<State, EventsDataTypesMap extends createStore.StoreonEvents<State> = any>( | ||
modules: Array<createStore.Module<State, EventsDataTypesMap> | false> | ||
): createStore.Store<State, EventsDataTypesMap>; | ||
export = createStore; |
{ | ||
"name": "storeon", | ||
"version": "0.8.7", | ||
"version": "0.9.0", | ||
"description": "Tiny (173 bytes) event-based Redux-like state manager for React and Preact", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -273,2 +273,45 @@ # Storeon | ||
## TypeScript | ||
Storeon delivers TypeScript declaration which allows to declare type | ||
of state and optionally declare types of events and parameter. | ||
If Storeon store has to be full type safe the event types declaration | ||
interface has to be delivered as second type to `createStore` function. | ||
```typescript | ||
import createStore, { Module, StoreonEvents } from 'storeon' | ||
// State structure | ||
interface State { | ||
counter: number | ||
} | ||
// Events declaration: map of event names to type of event data | ||
interface Events extends StoreonEvents<State> { | ||
// `inc` event which do not goes with any data | ||
'inc': undefined | ||
// `set` event which goes with number as data | ||
'set': number | ||
} | ||
const counterModule: Module<State, Events> = store => { | ||
store.on('@init', () => ({ counter: 0})) | ||
store.on('inc', state => ({ counter: state.counter + 1})) | ||
store.on('set', (state, event) => ({ counter: event})) | ||
} | ||
const store = createStore<State, Events>([counterModule]) | ||
// Correct calls: | ||
store.dispatch('set', 100) | ||
store.dispatch('inc') | ||
// Compilation errors: | ||
store.dispatch('inc', 100) // `inc` doesn’t have data | ||
store.dispatch('set', "100") // `set` event do not expect string data | ||
store.dispatch('dec') // Unknown event | ||
``` | ||
## Testing | ||
@@ -275,0 +318,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29312
454
345