Add dispatchr typing

This commit is contained in:
Ragg
2018-02-28 01:26:21 +09:00
parent 0128360a9d
commit 58296325aa
6 changed files with 148 additions and 0 deletions

17
types/dispatchr/addons/BaseStore.d.ts vendored Normal file
View File

@@ -0,0 +1,17 @@
// TypeScript Version: 2.3
/// <reference types="node" />
import { Dispatcher, DispatcherInterface, DispatcherContext, Store } from 'dispatchr';
import { EventEmitter } from 'events';
declare class BaseStore<S = {}> extends EventEmitter implements Store<S> {
constructor(dispatcher: DispatcherInterface);
initialize?: () => void;
getContext(): DispatcherContext;
addChangeListener(callback: () => void): void;
removeChangeListener(callback: () => void): void;
shouldDehydrate(): boolean;
emitChange(): void;
}
export = BaseStore;

15
types/dispatchr/addons/createStore.d.ts vendored Normal file
View File

@@ -0,0 +1,15 @@
// TypeScript Version: 2.3
import { StoreClass, Store } from 'dispatchr';
interface StoreOptions {
initialize?(): void;
storeName: string;
handlers: { [event: string]: string };
}
type CreateStoreOption = ThisType<Store> & StoreOptions & { [key: string]: any };
type CreateStore = (options: CreateStoreOption) => StoreClass;
declare const _: CreateStore;
export = _;

View File

@@ -0,0 +1,28 @@
import { createDispatcher, Store } from 'dispatchr';
import * as createStore from 'dispatchr/addons/createStore';
const TestStore = createStore({
storeName: 'TestStore',
handlers: {
ACTION_NAME: 'actionHandler'
},
initialize() { },
actionHandler() {
this.emitChange();
}
});
const dispatcher = createDispatcher({
errorHandler(e, context) {
e.meta;
e.type;
e.message;
},
stores: [TestStore]
});
const context = dispatcher.createContext({});
context.dispatch('ACTION_NAME', {});

62
types/dispatchr/index.d.ts vendored Normal file
View File

@@ -0,0 +1,62 @@
// TypeScript Version: 2.3
/// <reference types="node" />
import { EventEmitter } from 'events';
export interface DispatcherState {
stores: { [storeName: string]: any };
}
export interface DispatcherOption {
stores?: StoreClass[];
errorHandler?: (e: DispatcherError, context: DispatcherContext) => void;
}
export interface StoreClass {
storeName: string;
new(): Store;
}
export interface Store<S = {}> extends EventEmitter {
dehydrate?(): S;
rehydrate?(state: S): void;
shouldDehydrate?(): boolean;
emitChange(): void;
}
export interface Dispatcher {
createContext(context: {}): DispatcherContext;
registerStore(store: StoreClass): void;
isRegistered(store: StoreClass | string): boolean;
getStoreName(store: StoreClass | string): string;
}
export interface DispatcherInterface {
getContext(): DispatcherContext;
getStore: DispatcherContext['getStore'];
waitFor: DispatcherContext['waitFor'];
}
export interface DispatcherContext {
getStore(name: string): Store;
getStore<T extends StoreClass>(name: T): T;
dispatch(actionName: string, payload: any): void;
dehydrate(): DispatcherState;
rehydrate(dispatcherState: DispatcherState): void;
waitFor(stores: Array<string|StoreClass>, callback: () => void): void;
dispatcherInterface: DispatcherInterface;
}
export interface DispatcherError {
message: string;
type: string;
meta: {
actionName?: string,
payload?: any,
error: Error
};
}
export function createDispatcher(options: DispatcherOption): Dispatcher;

View File

@@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"addons/BaseStore.d.ts",
"addons/createStore.d.ts",
"dispatchr-test.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dtslint.json" }