Comparing version 0.14.3 to 0.14.4
@@ -65,3 +65,4 @@ import { konsole } from './Konsole'; | ||
route (m: M) { | ||
return this.getRoute(m) | ||
return this | ||
.getRoute(m) | ||
.do(route => konsole.log("route: returned a route", route)) | ||
@@ -86,9 +87,8 @@ .flatMap(route => toObservable(route.action())) | ||
super(m => router$ | ||
.concatMap( | ||
(router, i) => { | ||
konsole.log(`first: trying router #${i}`); | ||
return router.getRoute(m) | ||
.do(n => konsole.log(`first: router #${i} succeeded`, n)); | ||
} | ||
) | ||
.concatMap((router, i) => { | ||
konsole.log(`first: trying router #${i}`); | ||
return router | ||
.getRoute(m) | ||
.do(n => konsole.log(`first: router #${i} succeeded`, n)); | ||
}) | ||
.take(1) // so that we don't keep going through routers after we find one that matches | ||
@@ -103,7 +103,2 @@ ); | ||
const minRoute: Route = { | ||
score: 0, | ||
action: () => console.warn("This should never be called") | ||
} | ||
export function toScore (score: number) { | ||
@@ -114,6 +109,13 @@ return score == null ? 1 : score; | ||
export class BestRouter <M extends Routable> extends Router<M> { | ||
private static minRoute: Route = { | ||
score: 0, | ||
action: () => { | ||
console.warn("BestRouter.minRoute.action should never be called"); | ||
} | ||
} | ||
constructor(... routersOrHandlers: RouterOrHandler<M>[]) { | ||
const router$ = Observable.from(Router.routersFrom(routersOrHandlers)); | ||
super(m => new Observable<Route>(observer => { | ||
let bestRoute: Route = minRoute; | ||
let bestRoute: Route = BestRouter.minRoute; | ||
@@ -167,14 +169,3 @@ const subscription = router$ | ||
export function routeWithCombinedScore(route: Route, newScore: number) { | ||
const score = toScore(newScore) * toScore(route.score); | ||
return toScore(route.score) === score | ||
? route | ||
: { | ||
... route, | ||
score | ||
} as Route; | ||
} | ||
export class ifTrueRouter <M extends Routable> extends Router<M> { | ||
export class IfTrueRouter <M extends Routable> extends Router<M> { | ||
constructor ( | ||
@@ -201,4 +192,4 @@ predicate: Predicate<M>, | ||
elseRouterOrHandler?: RouterOrHandler<M> | ||
): ifTrueRouter<M> { | ||
return new ifTrueRouter(predicate, thenRouterOrHandler, elseRouterOrHandler); | ||
): IfTrueRouter<M> { | ||
return new IfTrueRouter(predicate, thenRouterOrHandler, elseRouterOrHandler); | ||
} | ||
@@ -210,3 +201,14 @@ | ||
export class ifMatchesRouter <M extends Routable, N extends Routable> extends Router<M> { | ||
export class IfMatchesRouter <M extends Routable, N extends Routable> extends Router<M> { | ||
private static routeWithCombinedScore(route: Route, newScore: number) { | ||
const score = toScore(newScore) * toScore(route.score); | ||
return toScore(route.score) === score | ||
? route | ||
: { | ||
... route, | ||
score | ||
} as Route; | ||
} | ||
constructor ( | ||
@@ -222,5 +224,7 @@ matcher: Matcher<M, N>, | ||
.flatMap(n => n | ||
? thenRouter.getRoute(n) | ||
.map(route => routeWithCombinedScore(route, n.score)) | ||
: elseRouter.getRoute(m) | ||
? thenRouter | ||
.getRoute(n) | ||
.map(route => IfMatchesRouter.routeWithCombinedScore(route, n.score)) | ||
: elseRouter | ||
.getRoute(m) | ||
) | ||
@@ -235,4 +239,4 @@ ); | ||
elseRouterOrHandler?: RouterOrHandler<M> | ||
): ifMatchesRouter<M, N> { | ||
return new ifMatchesRouter(matcher, thenRouterOrHandler, elseRouterOrHandler); | ||
): IfMatchesRouter<M, N> { | ||
return new IfMatchesRouter(matcher, thenRouterOrHandler, elseRouterOrHandler); | ||
} | ||
@@ -250,3 +254,4 @@ | ||
export function catchRoute <M extends Routable> (routerOrHandler: RouterOrHandler<M>): Router<M> { | ||
return new Router<M>(m => Router.from(routerOrHandler) | ||
return new Router<M>(m => Router | ||
.from(routerOrHandler) | ||
.getRoute(m) | ||
@@ -260,3 +265,5 @@ .filter(route => !route.thrown) | ||
const router = Router.from(routerOrHandler); | ||
super(m => router.getRoute(m) | ||
super(m => router | ||
.getRoute(m) | ||
.map(route => ({ | ||
@@ -274,3 +281,5 @@ ... route, | ||
const router = Router.from(routerOrHandler); | ||
super(m => router.getRoute(m) | ||
super(m => router | ||
.getRoute(m) | ||
.map(route => ({ | ||
@@ -277,0 +286,0 @@ ... route, |
@@ -5,7 +5,15 @@ import { Observable } from 'rxjs'; | ||
export declare function toFilteredObservable<T>(t: Observableable<T>): Observable<T>; | ||
export interface Route { | ||
export interface BaseRoute { | ||
score?: number; | ||
thrown?: true; | ||
} | ||
export interface ActionRoute extends BaseRoute { | ||
type: 'action'; | ||
action: () => Observableable<any>; | ||
} | ||
export interface AbstractRoute extends BaseRoute { | ||
type: 'abstract'; | ||
name: string; | ||
value?: object; | ||
} | ||
export declare type Route = ActionRoute | AbstractRoute; | ||
export interface Routable { | ||
@@ -18,5 +26,12 @@ score?: number; | ||
export declare type RouterOrHandler<M extends Routable = {}> = Router<M> | Handler<M>; | ||
export interface AbstractRouteNameValue { | ||
name: string; | ||
value?: object; | ||
} | ||
export interface GetRoute<M extends Routable> { | ||
(m: M): Observable<Route>; | ||
} | ||
export declare class Router<M extends Routable> { | ||
getRoute: (m: M) => Observable<Route>; | ||
constructor(getRoute: (m: M) => Observable<Route>); | ||
getRoute: GetRoute<M>; | ||
constructor(getRoute: GetRoute<M>); | ||
static fromHandler<M extends Routable>(handler: Handler<M>): Router<M>; | ||
@@ -26,2 +41,6 @@ static null: Router<any>; | ||
static routersFrom<M extends Routable>(routersOrHandlers: RouterOrHandler<M>[]): Router<M>[]; | ||
static abstractRouteWarning: (route: AbstractRoute) => (m: any) => void; | ||
static abstractRoute<M extends Routable>(name: string, value?: object): Router<M>; | ||
static abstractRoute<M extends Routable>(getAbstractRouteNameValue: (m: M) => Observableable<AbstractRouteNameValue>): Router<M>; | ||
catchAbstractRoute(getRouter: (route: AbstractRoute) => RouterOrHandler<M>): Router<M>; | ||
route(m: M): Observable<any>; | ||
@@ -37,2 +56,3 @@ doBefore(handler: Handler<M>): any; | ||
export declare class BestRouter<M extends Routable> extends Router<M> { | ||
private static minRoute; | ||
constructor(...routersOrHandlers: RouterOrHandler<M>[]); | ||
@@ -48,16 +68,14 @@ } | ||
} | ||
export declare function routeWithCombinedScore(route: Route, newScore: number): Route; | ||
export declare class ifTrueRouter<M extends Routable> extends Router<M> { | ||
export declare class IfTrueRouter<M extends Routable> extends Router<M> { | ||
constructor(predicate: Predicate<M>, thenRouterOrHandler: RouterOrHandler<M>, elseRouterOrHandler?: RouterOrHandler<M>); | ||
} | ||
export declare function ifTrue<M extends Routable>(predicate: Predicate<M>, thenRouterOrHandler: RouterOrHandler<M>, elseRouterOrHandler?: RouterOrHandler<M>): ifTrueRouter<M>; | ||
export declare function ifTrue<M extends Routable>(predicate: Predicate<M>, thenRouterOrHandler: RouterOrHandler<M>, elseRouterOrHandler?: RouterOrHandler<M>): IfTrueRouter<M>; | ||
export interface Matcher<M extends Routable = {}, Z extends Routable = {}> { | ||
(m: M): Observableable<Z>; | ||
} | ||
export declare class ifMatchesRouter<M extends Routable, N extends Routable> extends Router<M> { | ||
export declare class IfMatchesRouter<M extends Routable, N extends Routable> extends Router<M> { | ||
private static routeWithCombinedScore(route, newScore); | ||
constructor(matcher: Matcher<M, N>, thenRouterOrHandler: RouterOrHandler<N>, elseRouterOrHandler?: RouterOrHandler<M>); | ||
} | ||
export declare function ifMatches<M extends Routable, N extends Routable>(matcher: Matcher<M, N>, thenRouterOrHandler: RouterOrHandler<N>, elseRouterOrHandler?: RouterOrHandler<M>): ifMatchesRouter<M, N>; | ||
export declare function throwRoute<M extends Routable>(): Router<M>; | ||
export declare function catchRoute<M extends Routable>(routerOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function ifMatches<M extends Routable, N extends Routable>(matcher: Matcher<M, N>, thenRouterOrHandler: RouterOrHandler<N>, elseRouterOrHandler?: RouterOrHandler<M>): IfMatchesRouter<M, N>; | ||
export declare class BeforeRouter<M extends Routable> extends Router<M> { | ||
@@ -64,0 +82,0 @@ constructor(beforeHandler: Handler<M>, routerOrHandler: RouterOrHandler<M>); |
@@ -47,2 +47,3 @@ "use strict"; | ||
return new Router(function (m) { return rxjs_1.Observable.of({ | ||
type: 'action', | ||
action: function () { return handler(m); } | ||
@@ -62,4 +63,28 @@ }); }); | ||
}; | ||
Router.abstractRoute = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return new Router(typeof args[0] === 'string' | ||
? function (m) { return rxjs_1.Observable.of({ | ||
type: 'abstract', | ||
name: args[0], | ||
value: args[1] | ||
}); } | ||
: function (m) { return toObservable(args[0](m)) | ||
.map(function (nameValue) { return (__assign({ type: 'abstract' }, nameValue)); }); }); | ||
}; | ||
Router.prototype.catchAbstractRoute = function (getRouter) { | ||
var _this = this; | ||
return new Router(function (m) { return _this | ||
.getRoute(m) | ||
.flatMap(function (route) { return route.type === "abstract" | ||
? Router.from(getRouter(route)).getRoute(m) | ||
: rxjs_1.Observable.of(route); }); }); | ||
}; | ||
Router.prototype.route = function (m) { | ||
return this.getRoute(m) | ||
return this | ||
.catchAbstractRoute(Router.abstractRouteWarning) | ||
.getRoute(m) | ||
.do(function (route) { return Konsole_1.konsole.log("route: returned a route", route); }) | ||
@@ -76,2 +101,5 @@ .flatMap(function (route) { return toObservable(route.action()); }) | ||
Router.null = new Router(function (m) { return rxjs_1.Observable.empty(); }); | ||
Router.abstractRouteWarning = function (route) { return function (m) { | ||
console.warn("An attempt was made to execute an abstract route named " + route.name + " with value " + route.value); | ||
}; }; | ||
return Router; | ||
@@ -92,3 +120,4 @@ }()); | ||
Konsole_1.konsole.log("first: trying router #" + i); | ||
return router.getRoute(m) | ||
return router | ||
.getRoute(m) | ||
.do(function (n) { return Konsole_1.konsole.log("first: router #" + i + " succeeded", n); }); | ||
@@ -111,6 +140,2 @@ }) | ||
exports.first = first; | ||
var minRoute = { | ||
score: 0, | ||
action: function () { return console.warn("This should never be called"); } | ||
}; | ||
function toScore(score) { | ||
@@ -130,3 +155,3 @@ return score == null ? 1 : score; | ||
_this = _super.call(this, function (m) { return new rxjs_1.Observable(function (observer) { | ||
var bestRoute = minRoute; | ||
var bestRoute = BestRouter.minRoute; | ||
var subscription = router$ | ||
@@ -154,2 +179,9 @@ .takeWhile(function (_) { return toScore(bestRoute.score) < 1; }) | ||
} | ||
BestRouter.minRoute = { | ||
type: 'action', | ||
score: 0, | ||
action: function () { | ||
console.warn("BestRouter.minRoute.action should never be called"); | ||
} | ||
}; | ||
return BestRouter; | ||
@@ -179,12 +211,5 @@ }(Router)); | ||
exports.run = run; | ||
function routeWithCombinedScore(route, newScore) { | ||
var score = toScore(newScore) * toScore(route.score); | ||
return toScore(route.score) === score | ||
? route | ||
: __assign({}, route, { score: score }); | ||
} | ||
exports.routeWithCombinedScore = routeWithCombinedScore; | ||
var ifTrueRouter = (function (_super) { | ||
__extends(ifTrueRouter, _super); | ||
function ifTrueRouter(predicate, thenRouterOrHandler, elseRouterOrHandler) { | ||
var IfTrueRouter = (function (_super) { | ||
__extends(IfTrueRouter, _super); | ||
function IfTrueRouter(predicate, thenRouterOrHandler, elseRouterOrHandler) { | ||
var _this = this; | ||
@@ -199,12 +224,12 @@ var thenRouter = Router.from(thenRouterOrHandler); | ||
} | ||
return ifTrueRouter; | ||
return IfTrueRouter; | ||
}(Router)); | ||
exports.ifTrueRouter = ifTrueRouter; | ||
exports.IfTrueRouter = IfTrueRouter; | ||
function ifTrue(predicate, thenRouterOrHandler, elseRouterOrHandler) { | ||
return new ifTrueRouter(predicate, thenRouterOrHandler, elseRouterOrHandler); | ||
return new IfTrueRouter(predicate, thenRouterOrHandler, elseRouterOrHandler); | ||
} | ||
exports.ifTrue = ifTrue; | ||
var ifMatchesRouter = (function (_super) { | ||
__extends(ifMatchesRouter, _super); | ||
function ifMatchesRouter(matcher, thenRouterOrHandler, elseRouterOrHandler) { | ||
var IfMatchesRouter = (function (_super) { | ||
__extends(IfMatchesRouter, _super); | ||
function IfMatchesRouter(matcher, thenRouterOrHandler, elseRouterOrHandler) { | ||
var _this = this; | ||
@@ -215,28 +240,22 @@ var thenRouter = Router.from(thenRouterOrHandler); | ||
.flatMap(function (n) { return n | ||
? thenRouter.getRoute(n) | ||
.map(function (route) { return routeWithCombinedScore(route, n.score); }) | ||
: elseRouter.getRoute(m); }); }) || this; | ||
? thenRouter | ||
.getRoute(n) | ||
.map(function (route) { return IfMatchesRouter.routeWithCombinedScore(route, n.score); }) | ||
: elseRouter | ||
.getRoute(m); }); }) || this; | ||
return _this; | ||
} | ||
return ifMatchesRouter; | ||
IfMatchesRouter.routeWithCombinedScore = function (route, newScore) { | ||
var score = toScore(newScore) * toScore(route.score); | ||
return toScore(route.score) === score | ||
? route | ||
: __assign({}, route, { score: score }); | ||
}; | ||
return IfMatchesRouter; | ||
}(Router)); | ||
exports.ifMatchesRouter = ifMatchesRouter; | ||
exports.IfMatchesRouter = IfMatchesRouter; | ||
function ifMatches(matcher, thenRouterOrHandler, elseRouterOrHandler) { | ||
return new ifMatchesRouter(matcher, thenRouterOrHandler, elseRouterOrHandler); | ||
return new IfMatchesRouter(matcher, thenRouterOrHandler, elseRouterOrHandler); | ||
} | ||
exports.ifMatches = ifMatches; | ||
var thrownRoute = { | ||
thrown: true, | ||
action: function () { } | ||
}; | ||
function throwRoute() { | ||
return new Router(function (m) { return rxjs_1.Observable.of(thrownRoute); }); | ||
} | ||
exports.throwRoute = throwRoute; | ||
function catchRoute(routerOrHandler) { | ||
return new Router(function (m) { return Router.from(routerOrHandler) | ||
.getRoute(m) | ||
.filter(function (route) { return !route.thrown; }); }); | ||
} | ||
exports.catchRoute = catchRoute; | ||
var BeforeRouter = (function (_super) { | ||
@@ -246,4 +265,7 @@ __extends(BeforeRouter, _super); | ||
var _this = this; | ||
var router = Router.from(routerOrHandler); | ||
_this = _super.call(this, function (m) { return router.getRoute(m) | ||
var router = Router | ||
.from(routerOrHandler) | ||
.catchAbstractRoute(Router.abstractRouteWarning); | ||
_this = _super.call(this, function (m) { return router | ||
.getRoute(m) | ||
.map(function (route) { return (__assign({}, route, { action: function () { return toObservable(beforeHandler(m)) | ||
@@ -260,4 +282,7 @@ .flatMap(function (_) { return toObservable(route.action()); }); } })); }); }) || this; | ||
var _this = this; | ||
var router = Router.from(routerOrHandler); | ||
_this = _super.call(this, function (m) { return router.getRoute(m) | ||
var router = Router | ||
.from(routerOrHandler) | ||
.catchAbstractRoute(Router.abstractRouteWarning); | ||
_this = _super.call(this, function (m) { return router | ||
.getRoute(m) | ||
.map(function (route) { return (__assign({}, route, { action: function () { return toObservable(route.action()) | ||
@@ -264,0 +289,0 @@ .flatMap(function (_) { return toObservable(afterHandler(m)); }); } })); }); }) || this; |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.14.3", | ||
"version": "0.14.4", | ||
"description": "rules-based app engine", | ||
@@ -10,0 +10,0 @@ "main": "dist/prague.js", |
@@ -6,3 +6,3 @@ "use strict"; | ||
const expect = chai.expect; | ||
const { toObservable, toFilteredObservable, Router, first, best, run, toScore, routeWithCombinedScore, ifTrue, ifMatches, throwRoute, catchRoute, before, after } = require('../dist/prague.js'); | ||
const { toObservable, toFilteredObservable, Router, first, best, run, toScore, IfMatchesRouter, ifTrue, ifMatches, throwRoute, catchRoute, before, after } = require('../dist/prague.js'); | ||
const { Observable } = require('rxjs'); | ||
@@ -662,5 +662,5 @@ | ||
describe('routeWithCombinedScore', () => { | ||
describe('IfMatchesRouter.routeWithCombinedScore', () => { | ||
it("should return score=1 with both scores undefined", () => { | ||
expect(toScore(routeWithCombinedScore( | ||
expect(toScore(IfMatchesRouter.routeWithCombinedScore( | ||
{ | ||
@@ -673,3 +673,3 @@ action: () => {} | ||
it("should return supplied score when route score undefined", () => { | ||
expect(toScore(routeWithCombinedScore( | ||
expect(toScore(IfMatchesRouter.routeWithCombinedScore( | ||
{ | ||
@@ -683,3 +683,3 @@ action: () => {} | ||
it("should return route score when supplied score undefined", () => { | ||
expect(toScore(routeWithCombinedScore( | ||
expect(toScore(IfMatchesRouter.routeWithCombinedScore( | ||
{ | ||
@@ -693,3 +693,3 @@ score: .13, | ||
it("should return combined score when both scores supplied", () => { | ||
expect(toScore(routeWithCombinedScore( | ||
expect(toScore(IfMatchesRouter.routeWithCombinedScore( | ||
{ | ||
@@ -696,0 +696,0 @@ score: .4, |
Sorry, the diff of this file is not supported yet
183391
2849