From a95a88b189b98690eb23b14692c2621864a6c0c3 Mon Sep 17 00:00:00 2001 From: Steve Baker <_steve_@outlook.com> Date: Fri, 14 Nov 2014 08:07:24 +0100 Subject: [PATCH] flux definitions --- flux/flux-tests.ts | 81 ++++++++++++++++++++++++++++++++++++++++++++++ flux/flux.d.ts | 63 ++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 flux/flux-tests.ts create mode 100644 flux/flux.d.ts diff --git a/flux/flux-tests.ts b/flux/flux-tests.ts new file mode 100644 index 000000000..f41d15aff --- /dev/null +++ b/flux/flux-tests.ts @@ -0,0 +1,81 @@ +/// + +import flux = require('flux') + +// +// Basic dispatcher usage +// + +var basicDispatcher = new flux.Dispatcher() + +// register(callback: (payload: any) => void): string +var id: string = basicDispatcher.register((payload) => { + // payload is type: any + payload.anything +}) + +// unregister(id: string): void +basicDispatcher.unregister(id) + +// waitFor(ids: string[]): void +basicDispatcher.waitFor([id]) + +// dispatch(payload: any): void +basicDispatcher.dispatch({ msg: 'hello' }) + +// isDispatching(): boolean +var dispatcherIsDispatching: boolean = basicDispatcher.isDispatching() + +// +// Typed payload +// + +enum ActionSource { Server, View } +enum ActionType { Create, Update, Delete } + +interface Action { + source: ActionSource + type: ActionType + data: Object +} + +var typedDispatcher = new flux.Dispatcher() + +var typedPayload: Action + +var typedStore = { + dispatcherID: typedDispatcher.register((payload) => { + typedPayload = payload + }) +} + +typedDispatcher.dispatch(typedPayload) + +// +// Derived dispatcher +// + +class CustomDispatcher extends flux.Dispatcher { + + // Dispatch an action with server as source + handleServerAction(type: ActionType, data: Object) { + this.dispatch({ + source: ActionSource.Server, + type: type, + data: data, + }) + } + + // Dispatch an action with view as source + handleViewAction(type: ActionType, data: Object) { + this.dispatch({ + source: ActionSource.View, + type: type, + data: data, + }) + } +} + +var customDispatcher = new CustomDispatcher() + +export = customDispatcher \ No newline at end of file diff --git a/flux/flux.d.ts b/flux/flux.d.ts new file mode 100644 index 000000000..8a9dbf967 --- /dev/null +++ b/flux/flux.d.ts @@ -0,0 +1,63 @@ +// Type definitions for Flux +// Project: http://facebook.github.io/flux/ +// Definitions by: Steve Baker +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module 'flux' { + + /** + * Dispatcher class + * Create an instance to use throughout the application. + * Or extend it to create a derived dispatcher class. + * + * Specify a type in the 'TPayload' generic argument to use strongly-typed payloads, + * otherwise specify 'any' + * + * Examples: + * var dispatcher = new flux.Dispatcher() + * var typedDispatcher = new flux.Dispatcher() + * class DerivedDispatcher extends flux.Dispatcher { } + */ + export class Dispatcher { + + /** + * Create an instance of the Dispatcher class to use throughout the application. + * + * Specify a type in the 'TPayload' generic argument to use strongly-typed payloads, + * otherwise specify 'any' + * + * Examples: + * var dispatcher = new flux.Dispatcher() + * var typedDispatcher = new flux.Dispatcher() + */ + constructor() + + /** + * Registers a callback that will be invoked with every payload sent to the dispatcher. + * Returns a string token to identify the callback to be used with waitFor() or unregister. + */ + register(callback: (payload: TPayload) => void): string + + /** + * Unregisters a callback with the given ID token + */ + unregister(id: string): void + + /** + * Waits for the callbacks with the specified IDs to be invoked before continuing execution + * of the current callback. This method should only be used by a callback in response + * to a dispatched payload. + */ + waitFor(IDs: string[]): void + + /** + * Dispatches a payload to all registered callbacks + */ + dispatch(payload: TPayload): void + + /** + * Gets whether the dispatcher is currently dispatching + */ + isDispatching(): boolean + } +} \ No newline at end of file