add missing types and functions to fluxible (#28729)

* add types for package 'fluxible'

* add fluxible router types

* fix tests

* fixed annotation

* add missing types

* fix tsconfig.json

* fix for lint

* fix lint

* fix name of a package

* add missing description

* delete bad copypaste

* add test file

* add tests

* fix getStore type check
This commit is contained in:
Max 2018-10-09 20:53:16 +03:00 committed by Andy
parent fcaad045de
commit 3d4028cde1
6 changed files with 211 additions and 2 deletions

View File

@ -0,0 +1,52 @@
import { DispatcherInterface } from 'dispatchr';
import { connectToStores, provideContext } from 'fluxible-addons-react';
import { Fluxible, ComponentContext } from 'fluxible';
import BaseStore = require('fluxible/addons/BaseStore');
import * as React from 'react';
interface HomeProps {
stringValue: string;
}
class Home extends React.Component<HomeProps, any> {
constructor(props: HomeProps) {
super(props);
}
}
class ExtendedStore extends BaseStore {
constructor(public dispatcher: DispatcherInterface) {
super(dispatcher);
this.data = "";
}
static storeName = 'ExtendedStore';
static handlers = {
ACTION_NAME: 'actionHandler'
};
private data: string;
actionHandler() {
this.emitChange();
}
setData(data: string): void {
this.data = data;
}
getData(): string {
return this.data;
}
}
// connecting Home react component to ExtendedStore
const ConnectedComponent = connectToStores(Home, [ExtendedStore], (context: ComponentContext, props: HomeProps) => {
return {
stringValue: context.getStore(ExtendedStore).getData(),
};
});
const ConnectedComponentWithContext = provideContext(ConnectedComponent);

34
types/fluxible-addons-react/index.d.ts vendored Normal file
View File

@ -0,0 +1,34 @@
// Type definitions for fluxible-addons-react 0.2
// Project: https://github.com/yahoo/fluxible#readme
// Definitions by: xbim <https://github.com/xbim>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
/// <reference types="node" />
import * as React from 'react';
import BaseStore = require('fluxible/addons/BaseStore');
import { ComponentContext } from "fluxible";
/**
* Registers change listeners and retrieves state from stores using the `getStateFromStores`
* method
* @param Component component to pass state as props to.
* @param stores List of stores to listen for changes
* @param getStateFromStores function that receives all stores and should return
* the full state object. Receives `stores` hash and component `props` as arguments
* @param customContextTypes additional `contextTypes` that could be accessed from your `getStateFromStores`
* function
* @returns React.Component
*/
export function connectToStores(
Component: typeof React.Component,
stores: Array<typeof BaseStore> | string[],
getStateFromStores: (context: ComponentContext, props: any) => any,
customContextTypes?: any): typeof React.Component;
/**
* Provides context prop to all children as React context
* @param component to wrap
* @param customContextTypes Custom contextTypes to add
* @returns React.Component
*/
export function provideContext(Component: typeof React.Component, customContextTypes?: any): typeof React.Component;

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"esModuleInterop": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"fluxible-addons-react-tests.ts"
]
}

View File

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

View File

@ -11,6 +11,10 @@ import BaseStore = require('fluxible/addons/BaseStore');
export class NavLink extends React.Component<NavLinkProps, any> { }
export class RouteStore extends BaseStore {
dehydrate(context?: FluxibleContext): any;
rehydrate(state: any): void;
static withStaticRoutes(routes: object): typeof RouteStore;
}

View File

@ -73,7 +73,7 @@ export class Fluxible {
/**
* Registers a store to the dispatcher so it can listen for actions
*/
registerStore(store: StoreClass| typeof BaseStore): void;
registerStore(store: StoreClass | typeof BaseStore): void;
/**
* Creates a serializable state of the application and a given context for sending to the client
@ -109,6 +109,24 @@ export class FluxibleContext {
*/
plug(plugin: any): void;
/**
* Returns the context for action controllers
* @return Action context information
*/
getActionContext(): ActionContext;
/**
* Returns the context for action controllers
* @return Component context information
*/
getComponentContext(): ComponentContext;
/**
* Returns the context for stores
* @return Store context information
*/
getStoreContext(): StoreContext;
/**
* Returns a serializable context state
*/
@ -122,5 +140,81 @@ export class FluxibleContext {
/**
* Getter for store from dispatcher
*/
getStore<T extends BaseStore>(store: { new(dispatcher?: DispatcherInterface): T; }): T;
getStore<T extends BaseStore>(store: { new(dispatcher: DispatcherInterface): T; }): T;
}
export class ActionContext {
/**
* Dispatches a payload to all registered callbacks.
* @param action Action name
* @param payload
*/
dispatch(action: string, payload?: any): void;
/**
* Proxy function to execute action
* @param action function that will be executed
* @param payload
* @param callback
*/
executeAction(action: (context: ActionContext, params: object, callback?: () => void) => void, payload?: any, callback?: any): void;
/**
* Getter for store from dispatcher
*/
getStore<T extends BaseStore>(store: { new(dispatcher: DispatcherInterface): T; }): T;
/**
* Data service. available only if fetch plugin is added
*/
service?: {
/**
* GET request to the server
* @param resource name of resourse
* @param params query string parameters as key-value object
* @param callback
*/
read: (resource: string, params: any, callback: (error: Error, data: any) => void) => void;
/**
* POST request to the server
* @param resource name of resourse
* @param params query string parameters as key-value object
* @param body json request body
* @param callback
*/
create: (resource: string, params: any, body: any, callback: (error: Error, data: any) => void) => void;
/**
*
* @param resource name of resourse
* @param params query string parameters as key-value object
* @param body json request body
* @param callback
*/
update: (resource: string, params: any, body: any, callback: (error: Error, data: any) => void) => void;
/**
*
* @param resource name of resourse
* @param params query string parameters as key-value object
* @param callback
*/
delete: (resource: string, params: any, callback: (error: Error, data: any) => void) => void;
};
}
export class ComponentContext {
/**
* Proxy function to execute action
* @param action function that will be executed
* @param payload
* @param callback
*/
executeAction(action: (context: ActionContext, params: object, callback?: () => void) => void, payload?: any): void;
/**
* Getter for store from dispatcher
*/
getStore<T extends BaseStore>(store: { new(dispatcher: DispatcherInterface): T; }): T;
}
export class StoreContext {
}