From e722612c5fd97a09b2b190c780a011c8d4427df0 Mon Sep 17 00:00:00 2001 From: Alexey Svetliakov Date: Wed, 6 Jul 2016 03:02:52 +0200 Subject: [PATCH] Added type definition for redux-optimistic-ui --- .../redux-optimistic-ui-tests.ts | 38 +++++++++ redux-optimistic-ui/redux-optimistic-ui.d.ts | 78 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 redux-optimistic-ui/redux-optimistic-ui-tests.ts create mode 100644 redux-optimistic-ui/redux-optimistic-ui.d.ts diff --git a/redux-optimistic-ui/redux-optimistic-ui-tests.ts b/redux-optimistic-ui/redux-optimistic-ui-tests.ts new file mode 100644 index 0000000000..3e02e0b642 --- /dev/null +++ b/redux-optimistic-ui/redux-optimistic-ui-tests.ts @@ -0,0 +1,38 @@ +/// +/// + +import { Action } from "redux"; +import { OptimisticState, OptimisticAction, BEGIN, optimistic, ensureState, } from "redux-optimistic-ui"; + +interface TestState { + a: number; + b: string; +} + +declare const testState: TestState; +declare const optimisticState: OptimisticState; + +function myReducer(state: TestState, action: Action) { + return state; +} + +const optimisticReducer = optimistic(myReducer); + +const nextState = optimisticReducer(optimisticState, { type: "test" }); +nextState.current.a; +nextState.current.b; +nextState.beforeState; +nextState.history; + +ensureState(testState).a; +ensureState(optimisticState).a; + +const myOptimisticAction: Action & OptimisticAction = { + type: "test", + meta: { + optimistic: { + type: BEGIN, + id: 1 + } + } +} diff --git a/redux-optimistic-ui/redux-optimistic-ui.d.ts b/redux-optimistic-ui/redux-optimistic-ui.d.ts new file mode 100644 index 0000000000..f487a64ffd --- /dev/null +++ b/redux-optimistic-ui/redux-optimistic-ui.d.ts @@ -0,0 +1,78 @@ +// Type definitions for redux-optimistic-ui 0.4.0 +// Project: https://github.com/mattkrick/redux-optimistic-ui +// Definitions by: Alexey Svetliakov +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module "redux-optimistic-ui" { + import { Reducer } from "redux"; + + /** + * Enhanced state by redux-optimistic-ui + */ + export interface OptimisticState { + /** + * History list + * List is Immutable.List<> + */ + history: any; + /** + * Before state + */ + beforeState: TState; + /** + * Current state + */ + current: TState; + } + + /** + * Enhances and wraps your root reducer into optimistic state + */ + export function optimistic(reducer: Reducer): Reducer>; + + + /** + * Returns your current state or state if it wasn't enhanced yet + */ + export function ensureState(state: OptimisticState | TState): TState; + + /** + * Optimistic action slice + */ + export interface OptimisticAction { + meta: { + /** + * Optimistic information + */ + optimistic: { + /** + * Type, BEGIN, COMMIT or REVERT + * + * @type {string} + */ + type: string; + /** + * Transaction id. Id should be unique for each optimistic action + * + * @type {number} + */ + id: number; + } + } + } + + /** + * Start optimistic action + */ + export const BEGIN: string; + /** + * Finish optimistic action and commit results + */ + export const COMMIT: string; + /** + * Revert optimistic action + */ + export const REVERT: string; +}