redux-app
Type-safe, DRY and OO redux. Implemented with typescript.
Installation
npm install --save redux-app
Short Example
@component
class App {
counter = new Counter();
}
@component
class Counter {
value = 0;
increment() {
this.value = value + 1;
}
}
const app = new ReduxApp(new App(), devToolsEnhancer(undefined));
console.log(app.root.counter.value);
console.log(app.store.getState());
app.root.counter.increment();
console.log(app.root.counter.value);
console.log(app.store.getState());
Important Notice
You should not mutate the object properties but rather assign them with new values.
That's why we write this.value = value + 1
and not this.value++
.
More Examples
More examples can be found here redux-app-examples.
How it works
For each component
decorated class the library generates an underlying Component
object that holds the same properties and method. The new Component object has it's prototype patched and all of it's methods replaced with dispatch() calls.
The generated Component also has a hidden 'REDUCER' property which is later on used by redux store. The 'REDUCER' property itself is generated from the original object methods, replacing all 'this' values with the current state from the store on each call (using Object.assign and Function.prototype.call).
Component Options
You can supply the following options to the component
decorator.
class SchemaOptions {
public actionNamespace?: boolean;
public uppercaseActions?: boolean;
public updateState?: boolean;
}
Usage:
@component({ uppercaseActions: false })
class Counter {
value = 0;
increment() {
this.value = value + 1;
}
}
Global Options
Available global options:
class GlobalOptions {
logLevel: LogLevel;
schema: SchemaOptions;
}
enum LogLevel {
None = 0,
Verbose = 1,
Debug = 2,
Silent = 10
}
Usage:
ReduxApp.options.logLevel = LogLevel.Debug;