Skip to content

Commit

Permalink
Merge branch 'type-safe-dispatch'
Browse files Browse the repository at this point in the history
  • Loading branch information
zewa666 committed Aug 9, 2018
2 parents 9671fd9 + 4020b93 commit db83186
Show file tree
Hide file tree
Showing 13 changed files with 1,737 additions and 1,730 deletions.
3,186 changes: 1,593 additions & 1,593 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
"test": "cross-env jest",
"test-ci": "cross-env jest && cat ./test/coverage-jest/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
"test-watch": "concurrently \"tsc --watch\" \"jest --watch\"",
"build:amd": "cross-env tsc --outDir dist/amd --module amd",
"build:commonjs": "cross-env tsc --outDir dist/commonjs --module commonjs",
"build:es2015": "cross-env tsc --outDir dist/es2015 --module es2015",
"build:es2017": "cross-env tsc --outDir dist/es2017 --module es2015 --target es2017",
"build:native-modules": "cross-env tsc --outDir dist/native-modules --module es2015",
"build:system": "cross-env tsc --outDir dist/system --module system",
"build:amd": "cross-env tsc --p tsconfig-build.json --outDir dist/amd --module amd",
"build:commonjs": "cross-env tsc --p tsconfig-build.json --outDir dist/commonjs --module commonjs",
"build:es2015": "cross-env tsc --p tsconfig-build.json --outDir dist/es2015 --module es2015",
"build:es2017": "cross-env tsc --p tsconfig-build.json --outDir dist/es2017 --module es2015 --target es2017",
"build:native-modules": "cross-env tsc --p tsconfig-build.json --outDir dist/native-modules --module es2015",
"build:system": "cross-env tsc --p tsconfig-build.json --outDir dist/system --module system",
"prebuild": "cross-env rimraf dist",
"build": "concurrently \"npm run build:amd\" \"npm run build:commonjs\" \"npm run build:es2015\" \"npm run build:es2017\" \"npm run build:native-modules\" \"npm run build:system\"",
"precommit": "npm run lint",
"predoc": "cross-env rimraf doc/api.json && rimraf dist/doc-temp && tsc --module amd --outFile dist/doc-temp/aurelia-store.js && node doc/shape-defs && copyfiles tsconfig.json dist/doc-temp",
"predoc": "cross-env rimraf doc/api.json && rimraf dist/doc-temp && tsc --p tsconfig-build.json --module amd --outFile dist/doc-temp/aurelia-store.js && node doc/shape-defs && copyfiles tsconfig.json dist/doc-temp",
"doc": "cross-env typedoc --json doc/api.json --excludeExternals --includeDeclarations --mode modules --target ES6 --name aurelia-store-docs dist/doc-temp/",
"postdoc": "cross-env node doc/shape-doc && rimraf dist/doc-temp",
"changelog": "cross-env conventional-changelog -p angular -i doc/CHANGELOG.md -s",
Expand Down Expand Up @@ -121,7 +121,7 @@
"ts-jest": "^22.4.6",
"tslint": "^5.9.1",
"typedoc": "^0.11.1",
"typescript": "^2.8.3"
"typescript": "^3.0.1"
},
"aurelia": {
"import": {
Expand Down
12 changes: 6 additions & 6 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { jump, applyLimits, HistoryOptions, isStateHistory } from "./history";
import { Middleware, MiddlewarePlacement, CallingAction } from "./middleware";
import { LogDefinitions, LogLevel, getLogType, LoggerIndexed } from "./logging";

export type Reducer<T> = (state: T, ...params: any[]) => T | false | Promise<T | false>;
export type Reducer<T, P extends any[] = any[]> = (state: T, ...params: P) => T | false | Promise<T | false>;

export enum PerformanceMeasurement {
StartEnd = "startEnd",
Expand Down Expand Up @@ -110,8 +110,8 @@ export class Store<T> {
return this.actions.has(reducer);
}

public dispatch(reducer: Reducer<T> | string, ...params: any[]) {
let action: Reducer<T>;
public dispatch<P extends any[]>(reducer: Reducer<T, P> | string, ...params: P) {
let action: Reducer<T, P>;

if (typeof reducer === "string") {
const result = Array.from(this.actions)
Expand All @@ -125,7 +125,7 @@ export class Store<T> {
}

return new Promise<void>((resolve, reject) => {
this.dispatchQueue.push({ reducer: action, params, resolve, reject });
this.dispatchQueue.push({ reducer: action, params, resolve, reject } as any);
if (this.dispatchQueue.length === 1) {
this.handleQueue();
}
Expand Down Expand Up @@ -296,10 +296,10 @@ export class Store<T> {
}
}

export function dispatchify<T>(action: Reducer<T> | string) {
export function dispatchify<T, P extends any[]>(action: Reducer<T, P> | string) {
const store = Container.instance.get(Store);

return function (...params: any[]) {
return function (...params: P) {
return store.dispatch(action, ...params) as Promise<void>;
}
}
Loading

0 comments on commit db83186

Please sign in to comment.