mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-28 08:17:54 +08:00
Add dispatchr typing
This commit is contained in:
17
types/dispatchr/addons/BaseStore.d.ts
vendored
Normal file
17
types/dispatchr/addons/BaseStore.d.ts
vendored
Normal 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
15
types/dispatchr/addons/createStore.d.ts
vendored
Normal 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 = _;
|
||||
28
types/dispatchr/dispatchr-test.ts
Normal file
28
types/dispatchr/dispatchr-test.ts
Normal 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
62
types/dispatchr/index.d.ts
vendored
Normal 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;
|
||||
25
types/dispatchr/tsconfig.json
Normal file
25
types/dispatchr/tsconfig.json
Normal 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"
|
||||
]
|
||||
}
|
||||
1
types/dispatchr/tslint.json
Normal file
1
types/dispatchr/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dtslint.json" }
|
||||
Reference in New Issue
Block a user