diff --git a/types/nuclear-js/index.d.ts b/types/nuclear-js/index.d.ts index 75f017cfc7..df4089ff87 100644 --- a/types/nuclear-js/index.d.ts +++ b/types/nuclear-js/index.d.ts @@ -2,7 +2,6 @@ // Project: https://github.com/optimizely/nuclear-js // Definitions by: Pat Lillis // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 import * as _Immutable from 'immutable'; @@ -27,7 +26,15 @@ interface ReactMixin { componentWillUnmount(): void; } -interface Reactor { +/** + * State is stored in NuclearJS Reactors. Reactors contain a `state` object + * which is an Immutable.Map + * + * The only way Reactors can change state is by reacting to messages. To + * update state, Reactor's dispatch messages to all registered stores, and + * the store returns it's new state based on the message + */ +export interface Reactor { prevReactorState: any; reactorState: any; observerState: any; @@ -102,7 +109,7 @@ interface Reactor { /** * Registers stores. */ - registerStores(stores: { [storeName: string]: Store }): void; + registerStores(stores: { [storeName: string]: Store }): void; /** * Replace store implementation (handlers) without modifying the app @@ -110,7 +117,7 @@ interface Reactor { * * Useful for hot reloading */ - replaceStores(stores: { [storeName: string]: Store }): void; + replaceStores(stores: { [storeName: string]: Store }): void; /** * Resets the state of a reactor and returns it back to initial state. @@ -140,7 +147,13 @@ export const Reactor: { (config?: ReactorConfig): Reactor; }; -interface Store extends StoreLike { +/** + * A Store defines how a certain domain of the application should respond to + * actions taken on the whole system. They manage their own section of the + * entire app state and have no knowledge about the other parts of the + * application state. + */ +export interface Store extends StoreLike { /** * Takes a current reactor state, action type and payload, does the * reaction, and returns the new state. @@ -219,7 +232,7 @@ export const Store: { * entire app state and have no knowledge about the other parts of the * application state. */ - new (config: StoreLike): Store; + new (config: StoreLike): Store; /** * A Store defines how a certain domain of the application should respond to @@ -227,7 +240,7 @@ export const Store: { * entire app state and have no knowledge about the other parts of the * application state. */ - (config: StoreLike): Store; + (config: StoreLike): Store; }; /** diff --git a/types/nuclear-js/nuclear-js-tests.ts b/types/nuclear-js/nuclear-js-tests.ts index 71e4e88230..d70df214c8 100644 --- a/types/nuclear-js/nuclear-js-tests.ts +++ b/types/nuclear-js/nuclear-js-tests.ts @@ -19,8 +19,8 @@ Reactor(); new Reactor({ debug: true }); Reactor({ debug: undefined }); // Make sure that type checking succeeds with or without `new`. -const r1 = new Reactor(); -const r2 = Reactor(); +const r1: Reactor = new Reactor(); +const r2: Reactor = Reactor(); r1.dispatch('FETCH_ENTITY_SUCCESS'); r1.dispatch('FETCH_ENTITY_SUCCESS', { data: 5 }); r1.batch(() => null); @@ -101,12 +101,32 @@ r2.ReactMixin.componentWillUnmount(); r2.ReactMixin.getInitialState(); // Callable with or without `new`. -new Store({ getInitialState() {}, initialize() {} }); -Store({ getInitialState() {}, initialize() {} }); -new Store({ getInitialState() {}, initialize() {} }); -Store({ getInitialState() {}, initialize() {} }); +new Store({ + getInitialState() { + return {}; + }, + initialize() {}, +}); +Store({ + getInitialState() { + return {}; + }, + initialize() {}, +}); +new Store({ + getInitialState() { + return {}; + }, + initialize() {}, +}); +Store({ + getInitialState() { + return {}; + }, + initialize() {}, +}); // Make sure that type checking succeeds with or without `new`. -const s1 = new Store({ +const s1: Store = new Store({ getInitialState() { return 5; }, @@ -114,7 +134,7 @@ const s1 = new Store({ this.on('FETCH_THING', (s: number, x: any) => 5); }, }); -const s2 = Store({ +const s2: Store = Store({ getInitialState() { return ''; }, @@ -125,6 +145,18 @@ const s2 = Store({ return '15'; }, }); +const s3: Store = new Store({ + getInitialState() { + return {}; + }, + initialize() {}, +}); +const s4: Store = Store({ + getInitialState() { + return {}; + }, + initialize() {}, +}); s1.getInitialState(); s1.initialize(); s1.handleReset(5); @@ -172,7 +204,12 @@ createReactMixin(r2); import Nuclear = require('nuclear-js'); Nuclear.Immutable.Map({ a: 1 }); Nuclear.Reactor({ debug: true }); -Nuclear.Store({ getInitialState() {}, initialize() {} }); +Nuclear.Store({ + getInitialState() { + return {}; + }, + initialize() {}, +}); Nuclear.isKeyPath({}); Nuclear.isGetter({}); Nuclear.toJS({});