aurelia-store
Advanced tools
Comparing version 0.22.1 to 0.23.0
@@ -33,3 +33,3 @@ import { Observable } from "rxjs/Observable"; | ||
unregisterAction(reducer: Reducer<T>): void; | ||
dispatch(reducer: Reducer<T>, ...params: any[]): Promise<void>; | ||
dispatch(reducer: Reducer<T> | string, ...params: any[]): Promise<void>; | ||
private handleQueue(); | ||
@@ -42,2 +42,2 @@ private internalDispatch(reducer, ...params); | ||
} | ||
export declare function dispatchify<T>(action: Reducer<T>): (...params: any[]) => Promise<void>; | ||
export declare function dispatchify<T>(action: Reducer<T> | string): (...params: any[]) => Promise<void>; |
@@ -92,4 +92,15 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
} | ||
var action; | ||
if (typeof reducer === "string") { | ||
var result = Array.from(this.actions) | ||
.find(function (val) { return val[1].name === reducer; }); | ||
if (result) { | ||
action = result[0]; | ||
} | ||
} | ||
else { | ||
action = reducer; | ||
} | ||
return new Promise(function (resolve, reject) { | ||
_this.dispatchQueue.push({ reducer: reducer, params: params, resolve: resolve, reject: reject }); | ||
_this.dispatchQueue.push({ reducer: action, params: params, resolve: resolve, reject: reject }); | ||
if (_this.dispatchQueue.length === 1) { | ||
@@ -96,0 +107,0 @@ _this.handleQueue(); |
@@ -33,3 +33,3 @@ import { Observable } from "rxjs/Observable"; | ||
unregisterAction(reducer: Reducer<T>): void; | ||
dispatch(reducer: Reducer<T>, ...params: any[]): Promise<void>; | ||
dispatch(reducer: Reducer<T> | string, ...params: any[]): Promise<void>; | ||
private handleQueue(); | ||
@@ -42,2 +42,2 @@ private internalDispatch(reducer, ...params); | ||
} | ||
export declare function dispatchify<T>(action: Reducer<T>): (...params: any[]) => Promise<void>; | ||
export declare function dispatchify<T>(action: Reducer<T> | string): (...params: any[]) => Promise<void>; |
@@ -96,4 +96,15 @@ "use strict"; | ||
} | ||
var action; | ||
if (typeof reducer === "string") { | ||
var result = Array.from(this.actions) | ||
.find(function (val) { return val[1].name === reducer; }); | ||
if (result) { | ||
action = result[0]; | ||
} | ||
} | ||
else { | ||
action = reducer; | ||
} | ||
return new Promise(function (resolve, reject) { | ||
_this.dispatchQueue.push({ reducer: reducer, params: params, resolve: resolve, reject: reject }); | ||
_this.dispatchQueue.push({ reducer: action, params: params, resolve: resolve, reject: reject }); | ||
if (_this.dispatchQueue.length === 1) { | ||
@@ -100,0 +111,0 @@ _this.handleQueue(); |
@@ -33,3 +33,3 @@ import { Observable } from "rxjs/Observable"; | ||
unregisterAction(reducer: Reducer<T>): void; | ||
dispatch(reducer: Reducer<T>, ...params: any[]): Promise<void>; | ||
dispatch(reducer: Reducer<T> | string, ...params: any[]): Promise<void>; | ||
private handleQueue(); | ||
@@ -42,2 +42,2 @@ private internalDispatch(reducer, ...params); | ||
} | ||
export declare function dispatchify<T>(action: Reducer<T>): (...params: any[]) => Promise<void>; | ||
export declare function dispatchify<T>(action: Reducer<T> | string): (...params: any[]) => Promise<void>; |
@@ -94,4 +94,15 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
} | ||
var action; | ||
if (typeof reducer === "string") { | ||
var result = Array.from(this.actions) | ||
.find(function (val) { return val[1].name === reducer; }); | ||
if (result) { | ||
action = result[0]; | ||
} | ||
} | ||
else { | ||
action = reducer; | ||
} | ||
return new Promise(function (resolve, reject) { | ||
_this.dispatchQueue.push({ reducer: reducer, params: params, resolve: resolve, reject: reject }); | ||
_this.dispatchQueue.push({ reducer: action, params: params, resolve: resolve, reject: reject }); | ||
if (_this.dispatchQueue.length === 1) { | ||
@@ -98,0 +109,0 @@ _this.handleQueue(); |
@@ -33,3 +33,3 @@ import { Observable } from "rxjs/Observable"; | ||
unregisterAction(reducer: Reducer<T>): void; | ||
dispatch(reducer: Reducer<T>, ...params: any[]): Promise<void>; | ||
dispatch(reducer: Reducer<T> | string, ...params: any[]): Promise<void>; | ||
private handleQueue(); | ||
@@ -42,2 +42,2 @@ private internalDispatch(reducer, ...params); | ||
} | ||
export declare function dispatchify<T>(action: Reducer<T>): (...params: any[]) => Promise<void>; | ||
export declare function dispatchify<T>(action: Reducer<T> | string): (...params: any[]) => Promise<void>; |
@@ -94,4 +94,15 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
} | ||
var action; | ||
if (typeof reducer === "string") { | ||
var result = Array.from(this.actions) | ||
.find(function (val) { return val[1].name === reducer; }); | ||
if (result) { | ||
action = result[0]; | ||
} | ||
} | ||
else { | ||
action = reducer; | ||
} | ||
return new Promise(function (resolve, reject) { | ||
_this.dispatchQueue.push({ reducer: reducer, params: params, resolve: resolve, reject: reject }); | ||
_this.dispatchQueue.push({ reducer: action, params: params, resolve: resolve, reject: reject }); | ||
if (_this.dispatchQueue.length === 1) { | ||
@@ -98,0 +109,0 @@ _this.handleQueue(); |
@@ -33,3 +33,3 @@ import { Observable } from "rxjs/Observable"; | ||
unregisterAction(reducer: Reducer<T>): void; | ||
dispatch(reducer: Reducer<T>, ...params: any[]): Promise<void>; | ||
dispatch(reducer: Reducer<T> | string, ...params: any[]): Promise<void>; | ||
private handleQueue(); | ||
@@ -42,2 +42,2 @@ private internalDispatch(reducer, ...params); | ||
} | ||
export declare function dispatchify<T>(action: Reducer<T>): (...params: any[]) => Promise<void>; | ||
export declare function dispatchify<T>(action: Reducer<T> | string): (...params: any[]) => Promise<void>; |
@@ -123,4 +123,15 @@ System.register(["rxjs/BehaviorSubject", "aurelia-framework", "./history", "./middleware", "./logging"], function (exports_1, context_1) { | ||
} | ||
var action; | ||
if (typeof reducer === "string") { | ||
var result = Array.from(this.actions) | ||
.find(function (val) { return val[1].name === reducer; }); | ||
if (result) { | ||
action = result[0]; | ||
} | ||
} | ||
else { | ||
action = reducer; | ||
} | ||
return new Promise(function (resolve, reject) { | ||
_this.dispatchQueue.push({ reducer: reducer, params: params, resolve: resolve, reject: reject }); | ||
_this.dispatchQueue.push({ reducer: action, params: params, resolve: resolve, reject: reject }); | ||
if (_this.dispatchQueue.length === 1) { | ||
@@ -127,0 +138,0 @@ _this.handleQueue(); |
@@ -639,4 +639,5 @@ --- | ||
So far we've just created an action and registered it by several means. Now let's look at how we can actually execute one of them to trigger the next state change. | ||
We can use the store method `dispatchAction` to exactly do that. In below example, the function `dispatchDemo`, can be called with an argument `nextFramework`. | ||
Inside we call `store.dispatchAction`, passing it the action itself and all subsequent parameters required. | ||
We can use the store method `dispatch` to exactly do that. In below example, the function `dispatchDemo`, can be called with an argument `nextFramework`. | ||
Inside we call `store.dispatch`, passing it the action itself and all subsequent parameters required. | ||
Alternatively we can also provide the previously registered name instead. | ||
@@ -664,3 +665,6 @@ <code-listing heading="Dispatching an action"> | ||
public dispatchDemo(nextFramework: string) { | ||
this.store.dispatchAction(demoAction, nextFramework); | ||
this.store.dispatch(demoAction, nextFramework); | ||
// or | ||
// this.store.dispatch("DemoAction", nextFramework); | ||
} | ||
@@ -687,3 +691,6 @@ } | ||
public dispatchDemo(nextFramework) { | ||
this.store.dispatchAction(demoAction, nextFramework); | ||
this.store.dispatch(demoAction, nextFramework); | ||
// or | ||
// this.store.dispatch("DemoAction", nextFramework); | ||
} | ||
@@ -694,7 +701,11 @@ } | ||
Now keep in mind that an action might be async, or really any middleware is, you'll learn more about them later, as such if you're depending on the state being updated right after it, make sure to `await` the call to `dispatchAction` | ||
Now keep in mind that an action might be async, or really any middleware is, you'll learn more about them later, as such if you're depending on the state being updated right after it, make sure to `await` the call to `dispatch`. | ||
The choice whether you call by the actual actions function or it's previously registered name is up to you. It might | ||
be less work just forwarding a string. That way you don't need to import the action from wherever you want to dispatch | ||
it. On the other hand exactly this is a helpful mechanism to make sure your app survives a refactoring session. Imagine | ||
you'd rename the registration name and not all the places you're dispatching. A long debugging night might be just around the corner ;) | ||
> Dispatching non-registered actions will result in an error | ||
## Using the dispatchify higher order function | ||
@@ -701,0 +712,0 @@ |
{ | ||
"name": "aurelia-store", | ||
"version": "0.22.1", | ||
"version": "0.23.0", | ||
"description": "Aurelia single state store based on RxJS", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -131,2 +131,4 @@ # aurelia-store | ||
this.store.dispatch(demoAction); | ||
// or by dispatching via the registered action name | ||
this.store.dispatch("DemoAction"); | ||
} | ||
@@ -133,0 +135,0 @@ } |
@@ -88,5 +88,18 @@ import { Observable } from "rxjs/Observable"; | ||
public dispatch(reducer: Reducer<T>, ...params: any[]) { | ||
public dispatch(reducer: Reducer<T> | string, ...params: any[]) { | ||
let action: Reducer<T>; | ||
if (typeof reducer === "string") { | ||
const result = Array.from(this.actions) | ||
.find((val) => val[1].name === reducer); | ||
if (result) { | ||
action = result[0]; | ||
} | ||
} else { | ||
action = reducer; | ||
} | ||
return new Promise<void>((resolve, reject) => { | ||
this.dispatchQueue.push({ reducer, params, resolve, reject }); | ||
this.dispatchQueue.push({ reducer: action, params, resolve, reject }); | ||
if (this.dispatchQueue.length === 1) { | ||
@@ -154,3 +167,3 @@ this.handleQueue(); | ||
} | ||
let resultingState = await this.executeMiddlewares( | ||
@@ -263,3 +276,3 @@ result, | ||
export function dispatchify<T>(action: Reducer<T>) { | ||
export function dispatchify<T>(action: Reducer<T> | string) { | ||
const store = Container.instance.get(Store); | ||
@@ -266,0 +279,0 @@ |
@@ -5,3 +5,2 @@ import { Container } from "aurelia-framework"; | ||
import { | ||
dispatchify, | ||
Store, | ||
@@ -101,36 +100,2 @@ PerformanceMeasurement | ||
it("should help create dispatchifyable functions", done => { | ||
const cont = new Container().makeGlobal(); | ||
const { store } = createTestStore(); | ||
const fakeAction = (currentState: testState, param1: number, param2: number) => { | ||
return Object.assign({}, currentState, { foo: param1 + param2 }) | ||
}; | ||
store.registerAction("FakeAction", fakeAction as any); | ||
cont.registerInstance(Store, store); | ||
dispatchify(fakeAction)("A", "B"); | ||
store.state.skip(1).subscribe((state) => { | ||
expect(state.foo).toEqual("AB"); | ||
done(); | ||
}); | ||
}); | ||
it("should return the promise from dispatchified calls", async () => { | ||
const cont = new Container().makeGlobal(); | ||
const { store } = createTestStore(); | ||
const fakeAction = (currentState: testState, param1: number, param2: number) => { | ||
return Object.assign({}, currentState, { foo: param1 + param2 }) | ||
}; | ||
store.registerAction("FakeAction", fakeAction as any); | ||
cont.registerInstance(Store, store); | ||
const result = dispatchify(fakeAction)("A", "B"); | ||
expect(result.then).toBeDefined(); | ||
await result; | ||
}); | ||
it("should accept reducers taking multiple parameters", done => { | ||
@@ -167,2 +132,18 @@ const { store } = createTestStore(); | ||
it("should the previously registered action name as dispatch argument", done => { | ||
const { store } = createTestStore(); | ||
const modifiedState = { foo: "bert" }; | ||
const fakeAction = (currentState: testState) => Promise.resolve(modifiedState); | ||
const fakeActionRegisteredName = "FakeAction"; | ||
store.registerAction(fakeActionRegisteredName, fakeAction); | ||
store.dispatch(fakeActionRegisteredName); | ||
// since the async action is coming at a later time we need to skip the initial state | ||
store.state.skip(1).subscribe((state) => { | ||
expect(state).toEqual(modifiedState); | ||
done(); | ||
}); | ||
}); | ||
it("should support promised actions", done => { | ||
@@ -169,0 +150,0 @@ const { store } = createTestStore(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
453997
134
5512
514