Comparing version 0.7.0 to 0.7.1
@@ -1,2 +0,2 @@ | ||
import { Match, IRule, BaseRule, RuleResult, Observableable, toObservable, toFilteredObservable } from './Rules'; | ||
import { Match, IRule, RuleResult, Observableable, toObservable, toFilteredObservable } from './Rules'; | ||
import { Observable } from 'rxjs'; | ||
@@ -17,4 +17,4 @@ import { konsole } from './Konsole'; | ||
export interface IDialogRootMatch { | ||
beginChildDialog<DIALOGARGS extends object = any>(name: string, args?: DIALOGARGS): Observableable<void>; | ||
clearChildDialog(): Observableable<void>; | ||
beginChildDialog<DIALOGARGS extends object = any>(name: string, args?: DIALOGARGS): Promise<void>; | ||
clearChildDialog(): Promise<void>; | ||
} | ||
@@ -27,4 +27,4 @@ | ||
dialogStack: DialogInstance[]; | ||
replaceThisDialog<DIALOGARGS = any>(name: string, args?: DIALOGARGS, response?: DIALOGRESPONSE): Observableable<void>; | ||
endThisDialog(response?: DIALOGRESPONSE): Observableable<void>; | ||
replaceThisDialog<DIALOGARGS = any>(name: string, args?: DIALOGARGS, response?: DIALOGRESPONSE): Promise<void>; | ||
endThisDialog(response?: DIALOGRESPONSE): Promise<void>; | ||
} | ||
@@ -92,20 +92,13 @@ | ||
runChildIfActive<ANYMATCH extends Match = M>(responders?: DialogResponders<ANYMATCH>): IRule<ANYMATCH>; | ||
runChildIfActive<ANYMATCH extends Match = M>(... args: any[]): IRule<ANYMATCH> { | ||
const dialogs = this.dialogs, | ||
rootDialogInstance: RootDialogInstance<ANYMATCH> = this.rootDialogInstance; | ||
runChildIfActive<ANYMATCH extends Match = M>(arg0: any, arg1?: any): IRule<ANYMATCH> { | ||
const dialogResponders: DialogResponders<ANYMATCH> = arg0 | ||
? arg1 | ||
? { [arg0]: arg1 } | ||
: typeof arg0 === "object" | ||
? arg0 | ||
: { [arg0]: () => {} } | ||
: undefined; | ||
return new class extends BaseRule<ANYMATCH & IDialogMatch> { | ||
dialogResponders: DialogResponders<ANYMATCH>; | ||
constructor(... args: any[]) { | ||
super(); | ||
if (args.length === 1) | ||
this.dialogResponders = typeof args[0] === "object" | ||
? args[0] | ||
: { [args[0]]: () => {} } | ||
else if (args.length === 2) | ||
this.dialogResponders = { [args[0]]: args[1] }; | ||
} | ||
tryMatch(match: ANYMATCH & IDialogMatch): Observable<RuleResult> { | ||
return { | ||
tryMatch: (match: ANYMATCH & IDialogMatch) => { | ||
console.log("runIfActive.tryMatch", match); | ||
@@ -122,3 +115,3 @@ | ||
} | ||
odi = toObservable(rootDialogInstance.get(match)); | ||
odi = toObservable(this.rootDialogInstance.get(match)); | ||
} | ||
@@ -130,4 +123,4 @@ | ||
let dialogResponder: DialogResponder<ANYMATCH> = () => {}; | ||
if (this.dialogResponders) { | ||
dialogResponder = this.dialogResponders[dialogInstance.name]; | ||
if (dialogResponders) { | ||
dialogResponder = dialogResponders[dialogInstance.name]; | ||
if (!dialogResponder) | ||
@@ -137,3 +130,3 @@ return Observable.empty<RuleResult>(); | ||
const dialog = dialogs[dialogInstance.name]; | ||
const dialog = this.dialogs[dialogInstance.name]; | ||
if (!dialog) { | ||
@@ -151,3 +144,4 @@ console.warn(`The stack references a dialog named "${dialogInstance.name}", which doesn't exist.`); | ||
})) | ||
.flatMap(_ => toObservable(match.beginChildDialog(name, args))), | ||
.toPromise() | ||
.then(() => match.beginChildDialog(name, args)), | ||
endThisDialog: <DIALOGRESPONSE extends object = any>(dialogResponse?: DIALOGRESPONSE) => | ||
@@ -158,7 +152,8 @@ toObservable(dialogResponder({ | ||
})) | ||
.flatMap(_ => toObservable(match.clearChildDialog())) | ||
.toPromise() | ||
.then(() => match.clearChildDialog()), | ||
}); | ||
}); | ||
} | ||
}(... args) as IRule<ANYMATCH>; | ||
} as IRule<ANYMATCH>; | ||
} | ||
@@ -171,4 +166,5 @@ | ||
this.invokeDialog(match, name, args) | ||
.flatMap(dialogInstance => toObservable(this.rootDialogInstance.set(match, dialogInstance))), | ||
clearChildDialog: () => this.rootDialogInstance.set(match) | ||
.flatMap(dialogInstance => toObservable(this.rootDialogInstance.set(match, dialogInstance))) | ||
.toPromise(), | ||
clearChildDialog: () => toObservable(this.rootDialogInstance.set(match)).toPromise(), | ||
} | ||
@@ -208,7 +204,8 @@ } | ||
this.invokeDialog(match, name, args) | ||
.flatMap(dialogInstance => { | ||
.toPromise() | ||
.then(dialogInstance => { | ||
dialogData.childDialogInstance = dialogInstance; | ||
return Observable.of({}); | ||
return; | ||
}), | ||
clearChildDialog: () => dialogData.childDialogInstance = undefined, | ||
clearChildDialog: () => new Promise<void>(() => dialogData.childDialogInstance = undefined), | ||
}) | ||
@@ -215,0 +212,0 @@ .map(ruleResult => ({ |
import { Observable } from 'rxjs'; | ||
import { ITextMatch } from './Text'; | ||
import { konsole } from './Konsole'; | ||
import { IRule, RuleResult, BaseRule, SimpleRule, Matcher, Handler, Match, Observableable, toFilteredObservable, ruleize } from './Rules'; | ||
import { IRule, RuleResult, simpleRule, Handler, Match, Observableable, toFilteredObservable, ruleize } from './Rules'; | ||
import 'isomorphic-fetch'; | ||
@@ -190,4 +190,24 @@ | ||
best<M extends Match & ITextMatch = any>(luisRules: LuisRules<M>): IRule<M> { | ||
return new BestMatchingLuisRule<M>(match => this.match(match), luisRules); | ||
best<M extends Match & ITextMatch = any>(luisRules: LuisRules<M>) { | ||
return { | ||
tryMatch: (match: M) => | ||
toFilteredObservable(this.match(match)) | ||
.flatMap(m => | ||
Observable.from(m.luisResponse.intents) | ||
.flatMap( | ||
luisIntent => | ||
Observable.of(luisRules[luisIntent.intent]) | ||
.filter(rule => !!rule) | ||
.flatMap(rule => | ||
ruleize(rule).tryMatch({ | ||
... match as any, | ||
score: luisIntent.score, | ||
... entityFields(m.luisResponse.entities), | ||
}) | ||
), | ||
1 | ||
) | ||
.take(1) // stop with first intent that appears in the rules | ||
) | ||
} as IRule<M>; | ||
} | ||
@@ -206,31 +226,1 @@ | ||
} | ||
class BestMatchingLuisRule<M extends Match & ITextMatch> extends BaseRule<M> { | ||
constructor( | ||
private matchModel: Matcher<M, M & { luisResponse: LuisResponse }>, | ||
private luisRules: LuisRules<M> | ||
) { | ||
super(); | ||
} | ||
tryMatch(match: M): Observable<RuleResult> { | ||
return toFilteredObservable(this.matchModel(match)) | ||
.flatMap(m => | ||
Observable.from(m.luisResponse.intents) | ||
.flatMap( | ||
luisIntent => | ||
Observable.of(this.luisRules[luisIntent.intent]) | ||
.filter(rule => !!rule) | ||
.flatMap(rule => | ||
ruleize(rule).tryMatch({ | ||
... match as any, | ||
score: luisIntent.score, | ||
... entityFields(m.luisResponse.entities), | ||
}) | ||
), | ||
1 | ||
) | ||
.take(1) // stop with first intent that appears in the rules | ||
) | ||
} | ||
} |
import { konsole } from './Konsole'; | ||
import { IRule, SimpleRule, Observableable, RuleResult, BaseRule, Matcher, Handler, Match, combineMatchers } from './Rules'; | ||
import { IRule, Handler, Match, rule } from './Rules'; | ||
import { Observable } from 'rxjs'; | ||
import { ITextMatch } from './Text'; | ||
export interface PromptRules<M> { | ||
[promptKey: string]: IRule<M>; | ||
} | ||
export class Prompts<M extends Match = any> extends BaseRule<M> { | ||
constructor( | ||
private rules: PromptRules<M>, | ||
private getPromptKey: (match: M) => string, | ||
private setPromptKey: (match: M, promptKey?: string) => void | ||
) { | ||
super(); | ||
} | ||
tryMatch(match: M): Observable<RuleResult> { | ||
return Observable.of(this.getPromptKey(match)) | ||
.do(promptKey => konsole.log("promptKey", promptKey)) | ||
.filter(promptKey => promptKey !== undefined) | ||
.map(promptKey => this.rules[promptKey]) | ||
.filter(rule => rule !== undefined) | ||
.flatMap(rule => | ||
rule.tryMatch(match) | ||
.map(ruleResult => ({ | ||
... ruleResult, | ||
action: () => { | ||
this.setPromptKey(match, undefined); | ||
return ruleResult.action(); | ||
} | ||
})) | ||
); | ||
} | ||
setPrompt(match, promptKey: string) { | ||
this.setPromptKey(match, promptKey); | ||
} | ||
} | ||
export interface IChatPromptConfirmMatch { | ||
confirm: boolean, | ||
} | ||
export interface IChatPromptChoiceMatch { | ||
@@ -50,32 +10,23 @@ choice: string, | ||
export class TextPrompts<M extends Match & ITextMatch> extends Prompts<M> { | ||
matchChoice(choices: string[]): Matcher<M, M & IChatPromptChoiceMatch> { | ||
return match => | ||
Observable.of(choices.find(choice => choice.toLowerCase() === match.text.toLowerCase())) | ||
.filter(choice => !!choice) | ||
.map(choice => ({ | ||
... match as any, // remove "as any" when TypeScript fixes this bug | ||
choice | ||
})); | ||
} | ||
matchConfirm(): Matcher<M & IChatPromptChoiceMatch, M & IChatPromptConfirmMatch> { | ||
return match => ({ | ||
export const matchChoice = (choices: string[]) => | ||
<M extends Match & ITextMatch = any>(match: M) => { | ||
const choice = choices.find(choice => choice.toLowerCase() === match.text.toLowerCase()); | ||
return choice && { | ||
... match as any, // remove "as any" when TypeScript fixes this bug | ||
confirm: match.choice === 'Yes' | ||
}); | ||
choice | ||
} as M & IChatPromptChoiceMatch | ||
} | ||
choice(choices: string[], handler: Handler<M & IChatPromptChoiceMatch>): IRule<M> { | ||
return new SimpleRule<M>(this.matchChoice(choices), handler); | ||
export const promptChoice = <M extends Match & ITextMatch = any>(choices: string[], ruleOrHandler: Handler<M & IChatPromptChoiceMatch> | IRule<M & IChatPromptChoiceMatch>) => { | ||
return rule(matchChoice(choices), ruleOrHandler) as IRule<M>; | ||
} | ||
export const matchConfirm = () => | ||
<M extends Match & ITextMatch = any>(match: M) => { | ||
const m = matchChoice(['Yes', 'No'])(match); | ||
return m.choice === 'Yes' && match; | ||
} | ||
confirm(handler: Handler<M & IChatPromptConfirmMatch>): IRule<M> { | ||
const choices = ['Yes', 'No']; | ||
return new SimpleRule<M>( | ||
this.matchChoice(choices), | ||
this.matchConfirm(), | ||
handler | ||
); | ||
} | ||
export const promptConfirm = <M extends Match & ITextMatch = any>(ruleOrHandler: Handler<M> | IRule<M>) => { | ||
return rule(matchConfirm(), ruleOrHandler) as IRule<M>; | ||
} |
import { Observable } from 'rxjs'; | ||
import { konsole } from './Konsole'; | ||
import { ITextMatch } from './Text'; | ||
import { Match, IRule, SimpleRule, Matcher, Handler, arrayize } from './Rules'; | ||
import { Match, IRule, rule, Handler, arrayize } from './Rules'; | ||
@@ -10,4 +10,4 @@ export interface IRegExpMatch { | ||
export const matchRegExp = <M extends Match & ITextMatch = any>(intents: RegExp | RegExp[]): Matcher<M, M & IRegExpMatch> => | ||
(match) => | ||
export const matchRegExp = (intents: RegExp | RegExp[]) => | ||
<M extends Match & ITextMatch = any>(match) => | ||
Observable.from(arrayize(intents)) | ||
@@ -23,10 +23,7 @@ .do(_ => konsole.log("RegExp.match matching", match)) | ||
groups | ||
})); | ||
} as M & IRegExpMatch)); | ||
// Either call as re(intent, action) or re([intent, intent, ...], action) | ||
export const re = <M extends Match & ITextMatch = any>(intents: RegExp | RegExp[], handler: Handler<M & IRegExpMatch>) => { | ||
return new SimpleRule( | ||
matchRegExp(intents), | ||
handler | ||
) as IRule<M>; | ||
export const re = <M extends Match & ITextMatch = any>(intents: RegExp | RegExp[], ruleOrHandler: Handler<M & IRegExpMatch> | IRule<M & IRegExpMatch>) => { | ||
return rule(matchRegExp(intents), ruleOrHandler) as IRule<M>; | ||
} |
@@ -17,4 +17,2 @@ import { konsole } from './Konsole'; | ||
tryMatch(match: M): Observable<RuleResult>; | ||
callHandlerIfMatch(match: M): Observable<any>; | ||
prependMatcher<L extends Match = any>(matcher: Matcher<L, M>): IRule<L> | ||
} | ||
@@ -55,3 +53,3 @@ | ||
export const ruleize = <M extends Match = any>(r: IRule<M> | Handler<M>) => { | ||
return isRule(r) ? r : new SimpleRule(r) as IRule<M>; | ||
return isRule(r) ? r : simpleRule(r); | ||
} | ||
@@ -61,23 +59,14 @@ | ||
// we want to allow any matcher to be a predicate (return a boolean) | ||
// if so, the 'falsey' case will be filtered out by observize, | ||
// if so, the 'falsey' case will be filtered out by toFilteredObservable, | ||
// so we just need to catch the case where it is precisely true | ||
return toFilteredObservable(matcher(match)) | ||
.map(m => typeof(m) === 'boolean' ? match : m); | ||
.map(m => typeof m === 'boolean' ? match : m); | ||
} | ||
export abstract class BaseRule<M extends Match = any> implements IRule<M> { | ||
abstract tryMatch(match: M): Observable<RuleResult>; | ||
export const callActionIfMatch = <M extends Match = any>(match: M, rule: IRule<M>) => | ||
rule.tryMatch(match) | ||
.do(result => konsole.log("handle: matched a rule", result)) | ||
.flatMap(result => toObservable(result.action())) | ||
.do(_ => konsole.log("handle: called action")); | ||
callHandlerIfMatch(match: M): Observable<any> { | ||
return this.tryMatch(match) | ||
.do(result => konsole.log("handle: matched a rule", result)) | ||
.flatMap(result => toObservable(result.action())) | ||
.do(_ => konsole.log("handle: called action")); | ||
} | ||
prependMatcher<L>(matcher: Matcher<L, M>): IRule<L> { | ||
return new RuleWithPrependedMatcher(matcher, this); | ||
} | ||
} | ||
export function combineMatchers<M extends Match = any, N extends Match = any>(m1: Matcher<M, N>): Matcher<M, N> | ||
@@ -105,98 +94,40 @@ export function combineMatchers<M extends Match = any, N extends Match = any, O extends Match = any>(m1: Matcher<M, N>, m2: Matcher<N, O>): Matcher<M, O> | ||
export class SimpleRule<M extends Match = any> extends BaseRule<M> { | ||
private matchers: Matcher[] = []; | ||
private handler: Handler; | ||
export const simpleRule = <M extends Match = any>(handler: Handler<M>) => ({ | ||
tryMatch: (match: M) => Observable.of({ | ||
score: match.score, | ||
action: () => handler(match) | ||
} as RuleResult) | ||
}) as IRule<M>; | ||
constructor(... args: (Matcher | Predicate | Handler)[]) { | ||
super(); | ||
if (args.length < 1) { | ||
konsole.error("rules must at least have a handler"); | ||
return; | ||
} | ||
if (args.length > 1) | ||
this.matchers = args.slice(0, args.length - 1) as Matcher[]; | ||
this.handler = args[args.length - 1] as Handler; | ||
} | ||
export const first = <M extends Match = any>(... rules: (IRule<M> | Handler<M>)[]) => { | ||
const rule$ = Observable.from(rules) | ||
.filter(rule => !!rule) | ||
.map(rule => ruleize(rule)); | ||
tryMatch(match: M): Observable<RuleResult> { | ||
konsole.log("SimpleRule.tryMatch", this.matchers); | ||
if (this.matchers.length === 0) | ||
return Observable.of({ | ||
score: match.score, | ||
action: () => this.handler(match) | ||
} as RuleResult); | ||
return ( | ||
this.matchers.length === 1 | ||
? matchize(this.matchers[0], match) | ||
: combineMatchers<M>(... this.matchers)(match) as Observable<Match> | ||
return { | ||
tryMatch: (match: M) => | ||
rule$.flatMap( | ||
(rule, i) => { | ||
konsole.log(`Rule.first: trying rule #${i}`); | ||
return rule.tryMatch(match) | ||
.do(m => konsole.log(`Rule.first: rule #${i} succeeded`, m)); | ||
}, | ||
1 | ||
) | ||
.do(m => konsole.log("match", m)) | ||
.map(m => ({ | ||
score: m.score, | ||
action: () => this.handler(m) | ||
} as RuleResult)); | ||
} | ||
prependMatcher<L extends Match = any>(matcher: Matcher<L, M>) { | ||
konsole.log("SimpleRule.prependMatcher", matcher); | ||
return new SimpleRule<L>( | ||
matcher, | ||
... this.matchers, | ||
this.handler | ||
); | ||
} | ||
.take(1) // so that we don't keep going through rules after we find one that matches | ||
} as IRule<M>; | ||
} | ||
export class FirstMatchingRule<M extends Match = any> extends BaseRule<M> { | ||
private rule$: Observable<IRule<M>>; | ||
export const prependMatcher = <L extends Match = any, M extends Match = any>(matcher: Matcher<L, M>, rule: IRule<M>) => ({ | ||
tryMatch: (match: L) => | ||
matchize(matcher, match) | ||
.flatMap((m: M) => rule.tryMatch(m)) | ||
} as IRule<L>); | ||
constructor(... rules: (IRule<M> | Handler<M>)[]) { | ||
super(); | ||
konsole.log("FirstMatchingRule.constructor: rules", rules); | ||
this.rule$ = Observable.from(rules) | ||
.filter(rule => !!rule) | ||
.map(rule => ruleize(rule)); | ||
} | ||
export const run = <M extends Match = any>(handler: Handler<M>) => ({ | ||
tryMatch: (match: M) => | ||
toObservable(handler(match)) | ||
.map(_ => null) | ||
} as IRule<M>); | ||
tryMatch(match: M): Observable<RuleResult> { | ||
konsole.log("FirstMatchingRule.tryMatch", match); | ||
return this.rule$ | ||
.flatMap( | ||
(rule, i) => { | ||
konsole.log(`Rule.first: trying rule #${i}`); | ||
return rule.tryMatch(match) | ||
.do(m => konsole.log(`Rule.first: rule #${i} succeeded`, m)); | ||
}, | ||
1 | ||
) | ||
.take(1) // so that we don't keep going through rules after we find one that matches | ||
} | ||
} | ||
export class RuleWithPrependedMatcher<L extends Match = any, M extends Match = any> extends BaseRule<L> { | ||
// TO DO: let this take multiple matchers | ||
constructor(private matcher: Matcher<L, M>, private rule: IRule<M>) { | ||
super(); | ||
} | ||
tryMatch(match: L): Observable<RuleResult> { | ||
return matchize(this.matcher, match) | ||
.flatMap((m: M) => this.rule.tryMatch(m)); | ||
} | ||
} | ||
export class RunRule<M extends Match = any> extends BaseRule<M> { | ||
constructor(private handler: Handler<M>) { | ||
super(); | ||
} | ||
tryMatch(match: M): Observable<RuleResult> { | ||
return toFilteredObservable(this.handler(match)) | ||
.map(_ => null); | ||
} | ||
} | ||
// These are left over from previous versions of the API and need to be updated to the latest hotness | ||
@@ -260,20 +191,11 @@ | ||
export function rule<M extends Match = any>(... args: (Predicate | Matcher | Handler | IRule)[]): IRule<M> { | ||
const ruleOrHandler = args[args.length - 1] as Handler | IRule; | ||
if (isRule(ruleOrHandler)) { | ||
switch (args.length) { | ||
case 1: | ||
return ruleOrHandler; | ||
case 2: | ||
return ruleOrHandler.prependMatcher(args[0] as Matcher); | ||
default: | ||
return ruleOrHandler.prependMatcher(combineMatchers(... args.slice(0, args.length - 1) as Matcher[])); | ||
} | ||
const ruleOrHandler = ruleize(args[args.length - 1] as Handler | IRule); | ||
switch (args.length) { | ||
case 1: | ||
return ruleOrHandler; | ||
case 2: | ||
return prependMatcher(args[0] as Matcher, ruleOrHandler); | ||
default: | ||
return prependMatcher(combineMatchers(... args.slice(0, args.length - 1) as Matcher[]), ruleOrHandler); | ||
} | ||
return new SimpleRule(... args as Matcher[]) as IRule<M>; | ||
} | ||
export function first<M extends Match = any>(... rules: (IRule<M> | Handler<M>)[]): IRule<M> { | ||
return new FirstMatchingRule(... rules); | ||
} | ||
export const run = <M extends Match = any>(handler: Handler<M>) => new RunRule<M>(handler) as IRule<M>; |
@@ -11,4 +11,4 @@ import { Match, IRule, RuleResult, Observableable } from './Rules'; | ||
export interface IDialogRootMatch { | ||
beginChildDialog<DIALOGARGS extends object = any>(name: string, args?: DIALOGARGS): Observableable<void>; | ||
clearChildDialog(): Observableable<void>; | ||
beginChildDialog<DIALOGARGS extends object = any>(name: string, args?: DIALOGARGS): Promise<void>; | ||
clearChildDialog(): Promise<void>; | ||
} | ||
@@ -21,4 +21,4 @@ export declare type IDialogData<DIALOGDATA extends object> = { | ||
dialogStack: DialogInstance[]; | ||
replaceThisDialog<DIALOGARGS = any>(name: string, args?: DIALOGARGS, response?: DIALOGRESPONSE): Observableable<void>; | ||
endThisDialog(response?: DIALOGRESPONSE): Observableable<void>; | ||
replaceThisDialog<DIALOGARGS = any>(name: string, args?: DIALOGARGS, response?: DIALOGRESPONSE): Promise<void>; | ||
endThisDialog(response?: DIALOGRESPONSE): Promise<void>; | ||
} | ||
@@ -25,0 +25,0 @@ export interface IDialogArgsMatch<DIALOGARGS extends object> { |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
@@ -41,27 +31,12 @@ for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
}; | ||
Dialogs.prototype.runChildIfActive = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var dialogs = this.dialogs, rootDialogInstance = this.rootDialogInstance; | ||
return new ((_a = (function (_super) { | ||
__extends(class_1, _super); | ||
function class_1() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var _this = _super.call(this) || this; | ||
if (args.length === 1) | ||
_this.dialogResponders = typeof args[0] === "object" | ||
? args[0] | ||
: (_a = {}, _a[args[0]] = function () { }, _a); | ||
else if (args.length === 2) | ||
_this.dialogResponders = (_b = {}, _b[args[0]] = args[1], _b); | ||
return _this; | ||
var _a, _b; | ||
} | ||
class_1.prototype.tryMatch = function (match) { | ||
var _this = this; | ||
Dialogs.prototype.runChildIfActive = function (arg0, arg1) { | ||
var _this = this; | ||
var dialogResponders = arg0 | ||
? arg1 | ||
? (_a = {}, _a[arg0] = arg1, _a) : typeof arg0 === "object" | ||
? arg0 | ||
: (_b = {}, _b[arg0] = function () { }, _b) | ||
: undefined; | ||
return { | ||
tryMatch: function (match) { | ||
console.log("runIfActive.tryMatch", match); | ||
@@ -75,3 +50,3 @@ var odi; | ||
match = __assign({}, match, { dialogStack: [] }); | ||
odi = Rules_1.toObservable(rootDialogInstance.get(match)); | ||
odi = Rules_1.toObservable(_this.rootDialogInstance.get(match)); | ||
} | ||
@@ -82,8 +57,8 @@ return odi | ||
var dialogResponder = function () { }; | ||
if (_this.dialogResponders) { | ||
dialogResponder = _this.dialogResponders[dialogInstance.name]; | ||
if (dialogResponders) { | ||
dialogResponder = dialogResponders[dialogInstance.name]; | ||
if (!dialogResponder) | ||
return rxjs_1.Observable.empty(); | ||
} | ||
var dialog = dialogs[dialogInstance.name]; | ||
var dialog = _this.dialogs[dialogInstance.name]; | ||
if (!dialog) { | ||
@@ -95,12 +70,13 @@ console.warn("The stack references a dialog named \"" + dialogInstance.name + "\", which doesn't exist."); | ||
return Rules_1.toObservable(dialogResponder(__assign({}, match, { dialogResponse: dialogResponse }))) | ||
.flatMap(function (_) { return Rules_1.toObservable(match.beginChildDialog(name, args)); }); | ||
.toPromise() | ||
.then(function () { return match.beginChildDialog(name, args); }); | ||
}, endThisDialog: function (dialogResponse) { | ||
return Rules_1.toObservable(dialogResponder(__assign({}, match, { dialogResponse: dialogResponse }))) | ||
.flatMap(function (_) { return Rules_1.toObservable(match.clearChildDialog()); }); | ||
.toPromise() | ||
.then(function () { return match.clearChildDialog(); }); | ||
} })); | ||
}); | ||
}; | ||
return class_1; | ||
}(Rules_1.BaseRule))).bind.apply(_a, [void 0].concat(args)))(); | ||
var _a; | ||
} | ||
}; | ||
var _a, _b; | ||
}; | ||
@@ -111,4 +87,5 @@ Dialogs.prototype.matchRootDialog = function (match) { | ||
return _this.invokeDialog(match, name, args) | ||
.flatMap(function (dialogInstance) { return Rules_1.toObservable(_this.rootDialogInstance.set(match, dialogInstance)); }); | ||
}, clearChildDialog: function () { return _this.rootDialogInstance.set(match); } }); | ||
.flatMap(function (dialogInstance) { return Rules_1.toObservable(_this.rootDialogInstance.set(match, dialogInstance)); }) | ||
.toPromise(); | ||
}, clearChildDialog: function () { return Rules_1.toObservable(_this.rootDialogInstance.set(match)).toPromise(); } }); | ||
}; | ||
@@ -132,7 +109,8 @@ Dialogs.prototype.addLocal = function (name, rule, init) { | ||
return _this.invokeDialog(match, name, args) | ||
.flatMap(function (dialogInstance) { | ||
.toPromise() | ||
.then(function (dialogInstance) { | ||
dialogData.childDialogInstance = dialogInstance; | ||
return rxjs_1.Observable.of({}); | ||
return; | ||
}); | ||
}, clearChildDialog: function () { return dialogData.childDialogInstance = undefined; } })) | ||
}, clearChildDialog: function () { return new Promise(function () { return dialogData.childDialogInstance = undefined; }); } })) | ||
.map(function (ruleResult) { return (__assign({}, ruleResult, { action: function () { | ||
@@ -139,0 +117,0 @@ return Rules_1.toObservable(ruleResult.action()) |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
@@ -159,3 +149,19 @@ for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
var _this = this; | ||
return new BestMatchingLuisRule(function (match) { return _this.match(match); }, luisRules); | ||
return { | ||
tryMatch: function (match) { | ||
return Rules_1.toFilteredObservable(_this.match(match)) | ||
.flatMap(function (m) { | ||
return rxjs_1.Observable.from(m.luisResponse.intents) | ||
.flatMap(function (luisIntent) { | ||
return rxjs_1.Observable.of(luisRules[luisIntent.intent]) | ||
.filter(function (rule) { return !!rule; }) | ||
.flatMap(function (rule) { | ||
return Rules_1.ruleize(rule).tryMatch(__assign({}, match, { score: luisIntent.score }, entityFields(m.luisResponse.entities))); | ||
}); | ||
}, 1) | ||
.take(1); | ||
} // stop with first intent that appears in the rules | ||
); | ||
} | ||
}; | ||
}; | ||
@@ -173,28 +179,2 @@ LuisModel.findEntity = function (entities, type) { | ||
exports.LuisModel = LuisModel; | ||
var BestMatchingLuisRule = (function (_super) { | ||
__extends(BestMatchingLuisRule, _super); | ||
function BestMatchingLuisRule(matchModel, luisRules) { | ||
var _this = _super.call(this) || this; | ||
_this.matchModel = matchModel; | ||
_this.luisRules = luisRules; | ||
return _this; | ||
} | ||
BestMatchingLuisRule.prototype.tryMatch = function (match) { | ||
var _this = this; | ||
return Rules_1.toFilteredObservable(this.matchModel(match)) | ||
.flatMap(function (m) { | ||
return rxjs_1.Observable.from(m.luisResponse.intents) | ||
.flatMap(function (luisIntent) { | ||
return rxjs_1.Observable.of(_this.luisRules[luisIntent.intent]) | ||
.filter(function (rule) { return !!rule; }) | ||
.flatMap(function (rule) { | ||
return Rules_1.ruleize(rule).tryMatch(__assign({}, match, { score: luisIntent.score }, entityFields(m.luisResponse.entities))); | ||
}); | ||
}, 1) | ||
.take(1); | ||
} // stop with first intent that appears in the rules | ||
); | ||
}; | ||
return BestMatchingLuisRule; | ||
}(Rules_1.BaseRule)); | ||
//# sourceMappingURL=LUIS.js.map |
@@ -1,26 +0,9 @@ | ||
import { IRule, RuleResult, BaseRule, Matcher, Handler, Match } from './Rules'; | ||
import { Observable } from 'rxjs'; | ||
import { IRule, Handler, Match } from './Rules'; | ||
import { ITextMatch } from './Text'; | ||
export interface PromptRules<M> { | ||
[promptKey: string]: IRule<M>; | ||
} | ||
export declare class Prompts<M extends Match = any> extends BaseRule<M> { | ||
private rules; | ||
private getPromptKey; | ||
private setPromptKey; | ||
constructor(rules: PromptRules<M>, getPromptKey: (match: M) => string, setPromptKey: (match: M, promptKey?: string) => void); | ||
tryMatch(match: M): Observable<RuleResult>; | ||
setPrompt(match: any, promptKey: string): void; | ||
} | ||
export interface IChatPromptConfirmMatch { | ||
confirm: boolean; | ||
} | ||
export interface IChatPromptChoiceMatch { | ||
choice: string; | ||
} | ||
export declare class TextPrompts<M extends Match & ITextMatch> extends Prompts<M> { | ||
matchChoice(choices: string[]): Matcher<M, M & IChatPromptChoiceMatch>; | ||
matchConfirm(): Matcher<M & IChatPromptChoiceMatch, M & IChatPromptConfirmMatch>; | ||
choice(choices: string[], handler: Handler<M & IChatPromptChoiceMatch>): IRule<M>; | ||
confirm(handler: Handler<M & IChatPromptConfirmMatch>): IRule<M>; | ||
} | ||
export declare const matchChoice: (choices: string[]) => <M extends Match & ITextMatch = any>(match: M) => M & IChatPromptChoiceMatch; | ||
export declare const promptChoice: <M extends Match & ITextMatch = any>(choices: string[], ruleOrHandler: Handler<M & IChatPromptChoiceMatch> | IRule<M & IChatPromptChoiceMatch>) => IRule<M>; | ||
export declare const matchConfirm: () => <M extends Match & ITextMatch = any>(match: M) => M; | ||
export declare const promptConfirm: <M extends Match & ITextMatch = any>(ruleOrHandler: Handler<M> | IRule<M>) => IRule<M>; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
@@ -21,61 +11,22 @@ for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Konsole_1 = require("./Konsole"); | ||
var Rules_1 = require("./Rules"); | ||
var rxjs_1 = require("rxjs"); | ||
var Prompts = (function (_super) { | ||
__extends(Prompts, _super); | ||
function Prompts(rules, getPromptKey, setPromptKey) { | ||
var _this = _super.call(this) || this; | ||
_this.rules = rules; | ||
_this.getPromptKey = getPromptKey; | ||
_this.setPromptKey = setPromptKey; | ||
return _this; | ||
} | ||
Prompts.prototype.tryMatch = function (match) { | ||
var _this = this; | ||
return rxjs_1.Observable.of(this.getPromptKey(match)) | ||
.do(function (promptKey) { return Konsole_1.konsole.log("promptKey", promptKey); }) | ||
.filter(function (promptKey) { return promptKey !== undefined; }) | ||
.map(function (promptKey) { return _this.rules[promptKey]; }) | ||
.filter(function (rule) { return rule !== undefined; }) | ||
.flatMap(function (rule) { | ||
return rule.tryMatch(match) | ||
.map(function (ruleResult) { return (__assign({}, ruleResult, { action: function () { | ||
_this.setPromptKey(match, undefined); | ||
return ruleResult.action(); | ||
} })); }); | ||
}); | ||
exports.matchChoice = function (choices) { | ||
return function (match) { | ||
var choice = choices.find(function (choice) { return choice.toLowerCase() === match.text.toLowerCase(); }); | ||
return choice && __assign({}, match, { // remove "as any" when TypeScript fixes this bug | ||
choice: choice }); | ||
}; | ||
Prompts.prototype.setPrompt = function (match, promptKey) { | ||
this.setPromptKey(match, promptKey); | ||
}; | ||
exports.promptChoice = function (choices, ruleOrHandler) { | ||
return Rules_1.rule(exports.matchChoice(choices), ruleOrHandler); | ||
}; | ||
exports.matchConfirm = function () { | ||
return function (match) { | ||
var m = exports.matchChoice(['Yes', 'No'])(match); | ||
return m.choice === 'Yes' && match; | ||
}; | ||
return Prompts; | ||
}(Rules_1.BaseRule)); | ||
exports.Prompts = Prompts; | ||
var TextPrompts = (function (_super) { | ||
__extends(TextPrompts, _super); | ||
function TextPrompts() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
TextPrompts.prototype.matchChoice = function (choices) { | ||
return function (match) { | ||
return rxjs_1.Observable.of(choices.find(function (choice) { return choice.toLowerCase() === match.text.toLowerCase(); })) | ||
.filter(function (choice) { return !!choice; }) | ||
.map(function (choice) { return (__assign({}, match, { // remove "as any" when TypeScript fixes this bug | ||
choice: choice })); }); | ||
}; | ||
}; | ||
TextPrompts.prototype.matchConfirm = function () { | ||
return function (match) { return (__assign({}, match, { confirm: match.choice === 'Yes' })); }; | ||
}; | ||
TextPrompts.prototype.choice = function (choices, handler) { | ||
return new Rules_1.SimpleRule(this.matchChoice(choices), handler); | ||
}; | ||
TextPrompts.prototype.confirm = function (handler) { | ||
var choices = ['Yes', 'No']; | ||
return new Rules_1.SimpleRule(this.matchChoice(choices), this.matchConfirm(), handler); | ||
}; | ||
return TextPrompts; | ||
}(Prompts)); | ||
exports.TextPrompts = TextPrompts; | ||
}; | ||
exports.promptConfirm = function (ruleOrHandler) { | ||
return Rules_1.rule(exports.matchConfirm(), ruleOrHandler); | ||
}; | ||
//# sourceMappingURL=Prompt.js.map |
@@ -0,7 +1,8 @@ | ||
import { Observable } from 'rxjs'; | ||
import { ITextMatch } from './Text'; | ||
import { Match, IRule, Matcher, Handler } from './Rules'; | ||
import { Match, IRule, Handler } from './Rules'; | ||
export interface IRegExpMatch { | ||
groups: RegExpExecArray; | ||
} | ||
export declare const matchRegExp: <M extends Match & ITextMatch = any>(intents: RegExp | RegExp[]) => Matcher<M, M & IRegExpMatch>; | ||
export declare const re: <M extends Match & ITextMatch = any>(intents: RegExp | RegExp[], handler: Handler<M & IRegExpMatch>) => IRule<M>; | ||
export declare const matchRegExp: (intents: RegExp | RegExp[]) => <M extends Match & ITextMatch = any>(match: any) => Observable<M & IRegExpMatch>; | ||
export declare const re: <M extends Match & ITextMatch = any>(intents: RegExp | RegExp[], ruleOrHandler: Handler<M & IRegExpMatch> | IRule<M & IRegExpMatch>) => IRule<M>; |
@@ -28,5 +28,5 @@ "use strict"; | ||
// Either call as re(intent, action) or re([intent, intent, ...], action) | ||
exports.re = function (intents, handler) { | ||
return new Rules_1.SimpleRule(exports.matchRegExp(intents), handler); | ||
exports.re = function (intents, ruleOrHandler) { | ||
return Rules_1.rule(exports.matchRegExp(intents), ruleOrHandler); | ||
}; | ||
//# sourceMappingURL=RegExp.js.map |
@@ -12,4 +12,2 @@ import { Observable } from 'rxjs'; | ||
tryMatch(match: M): Observable<RuleResult>; | ||
callHandlerIfMatch(match: M): Observable<any>; | ||
prependMatcher<L extends Match = any>(matcher: Matcher<L, M>): IRule<L>; | ||
} | ||
@@ -24,7 +22,3 @@ export declare type Matcher<A extends Match = any, Z extends Match = any> = (match: A) => Observableable<Z>; | ||
export declare const matchize: <M extends Match = any>(matcher: Matcher<M, any>, match: M) => Observable<any>; | ||
export declare abstract class BaseRule<M extends Match = any> implements IRule<M> { | ||
abstract tryMatch(match: M): Observable<RuleResult>; | ||
callHandlerIfMatch(match: M): Observable<any>; | ||
prependMatcher<L>(matcher: Matcher<L, M>): IRule<L>; | ||
} | ||
export declare const callActionIfMatch: <M extends Match = any>(match: M, rule: IRule<M>) => Observable<any>; | ||
export declare function combineMatchers<M extends Match = any, N extends Match = any>(m1: Matcher<M, N>): Matcher<M, N>; | ||
@@ -35,25 +29,6 @@ export declare function combineMatchers<M extends Match = any, N extends Match = any, O extends Match = any>(m1: Matcher<M, N>, m2: Matcher<N, O>): Matcher<M, O>; | ||
export declare function combineMatchers<M extends Match = any>(...matchers: Matcher[]): Matcher<M, any>; | ||
export declare class SimpleRule<M extends Match = any> extends BaseRule<M> { | ||
private matchers; | ||
private handler; | ||
constructor(...args: (Matcher | Predicate | Handler)[]); | ||
tryMatch(match: M): Observable<RuleResult>; | ||
prependMatcher<L extends Match = any>(matcher: Matcher<L, M>): SimpleRule<L>; | ||
} | ||
export declare class FirstMatchingRule<M extends Match = any> extends BaseRule<M> { | ||
private rule$; | ||
constructor(...rules: (IRule<M> | Handler<M>)[]); | ||
tryMatch(match: M): Observable<RuleResult>; | ||
} | ||
export declare class RuleWithPrependedMatcher<L extends Match = any, M extends Match = any> extends BaseRule<L> { | ||
private matcher; | ||
private rule; | ||
constructor(matcher: Matcher<L, M>, rule: IRule<M>); | ||
tryMatch(match: L): Observable<RuleResult>; | ||
} | ||
export declare class RunRule<M extends Match = any> extends BaseRule<M> { | ||
private handler; | ||
constructor(handler: Handler<M>); | ||
tryMatch(match: M): Observable<RuleResult>; | ||
} | ||
export declare const simpleRule: <M extends Match = any>(handler: Handler<M>) => IRule<M>; | ||
export declare const first: <M extends Match = any>(...rules: (IRule<M> | Handler<M>)[]) => IRule<M>; | ||
export declare const prependMatcher: <L extends Match = any, M extends Match = any>(matcher: Matcher<L, M>, rule: IRule<M>) => IRule<L>; | ||
export declare const run: <M extends Match = any>(handler: Handler<M>) => IRule<M>; | ||
export interface Predicate<M extends Match = any> { | ||
@@ -75,3 +50,1 @@ (match: M): Observableable<boolean>; | ||
export declare function rule<M extends Match = any, N extends Match = any, O extends Match = any, Z extends Match = any>(m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>, handler: Handler<Z> | IRule<Z>): IRule<M>; | ||
export declare function first<M extends Match = any>(...rules: (IRule<M> | Handler<M>)[]): IRule<M>; | ||
export declare const run: <M extends Match = any>(handler: Handler<M>) => IRule<M>; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -40,26 +30,17 @@ var Konsole_1 = require("./Konsole"); | ||
exports.ruleize = function (r) { | ||
return isRule(r) ? r : new SimpleRule(r); | ||
return isRule(r) ? r : exports.simpleRule(r); | ||
}; | ||
exports.matchize = function (matcher, match) { | ||
// we want to allow any matcher to be a predicate (return a boolean) | ||
// if so, the 'falsey' case will be filtered out by observize, | ||
// if so, the 'falsey' case will be filtered out by toFilteredObservable, | ||
// so we just need to catch the case where it is precisely true | ||
return exports.toFilteredObservable(matcher(match)) | ||
.map(function (m) { return typeof (m) === 'boolean' ? match : m; }); | ||
.map(function (m) { return typeof m === 'boolean' ? match : m; }); | ||
}; | ||
var BaseRule = (function () { | ||
function BaseRule() { | ||
} | ||
BaseRule.prototype.callHandlerIfMatch = function (match) { | ||
return this.tryMatch(match) | ||
.do(function (result) { return Konsole_1.konsole.log("handle: matched a rule", result); }) | ||
.flatMap(function (result) { return exports.toObservable(result.action()); }) | ||
.do(function (_) { return Konsole_1.konsole.log("handle: called action"); }); | ||
}; | ||
BaseRule.prototype.prependMatcher = function (matcher) { | ||
return new RuleWithPrependedMatcher(matcher, this); | ||
}; | ||
return BaseRule; | ||
}()); | ||
exports.BaseRule = BaseRule; | ||
exports.callActionIfMatch = function (match, rule) { | ||
return rule.tryMatch(match) | ||
.do(function (result) { return Konsole_1.konsole.log("handle: matched a rule", result); }) | ||
.flatMap(function (result) { return exports.toObservable(result.action()); }) | ||
.do(function (_) { return Konsole_1.konsole.log("handle: called action"); }); | ||
}; | ||
function combineMatchers() { | ||
@@ -85,102 +66,39 @@ var args = []; | ||
exports.combineMatchers = combineMatchers; | ||
var SimpleRule = (function (_super) { | ||
__extends(SimpleRule, _super); | ||
function SimpleRule() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var _this = _super.call(this) || this; | ||
_this.matchers = []; | ||
if (args.length < 1) { | ||
Konsole_1.konsole.error("rules must at least have a handler"); | ||
return _this; | ||
} | ||
if (args.length > 1) | ||
_this.matchers = args.slice(0, args.length - 1); | ||
_this.handler = args[args.length - 1]; | ||
return _this; | ||
exports.simpleRule = function (handler) { return ({ | ||
tryMatch: function (match) { return rxjs_1.Observable.of({ | ||
score: match.score, | ||
action: function () { return handler(match); } | ||
}); } | ||
}); }; | ||
exports.first = function () { | ||
var rules = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
rules[_i] = arguments[_i]; | ||
} | ||
SimpleRule.prototype.tryMatch = function (match) { | ||
var _this = this; | ||
Konsole_1.konsole.log("SimpleRule.tryMatch", this.matchers); | ||
if (this.matchers.length === 0) | ||
return rxjs_1.Observable.of({ | ||
score: match.score, | ||
action: function () { return _this.handler(match); } | ||
}); | ||
return (this.matchers.length === 1 | ||
? exports.matchize(this.matchers[0], match) | ||
: combineMatchers.apply(void 0, this.matchers)(match)) | ||
.do(function (m) { return Konsole_1.konsole.log("match", m); }) | ||
.map(function (m) { return ({ | ||
score: m.score, | ||
action: function () { return _this.handler(m); } | ||
}); }); | ||
var rule$ = rxjs_1.Observable.from(rules) | ||
.filter(function (rule) { return !!rule; }) | ||
.map(function (rule) { return exports.ruleize(rule); }); | ||
return { | ||
tryMatch: function (match) { | ||
return rule$.flatMap(function (rule, i) { | ||
Konsole_1.konsole.log("Rule.first: trying rule #" + i); | ||
return rule.tryMatch(match) | ||
.do(function (m) { return Konsole_1.konsole.log("Rule.first: rule #" + i + " succeeded", m); }); | ||
}, 1) | ||
.take(1); | ||
} // so that we don't keep going through rules after we find one that matches | ||
}; | ||
SimpleRule.prototype.prependMatcher = function (matcher) { | ||
Konsole_1.konsole.log("SimpleRule.prependMatcher", matcher); | ||
return new (SimpleRule.bind.apply(SimpleRule, [void 0, matcher].concat(this.matchers, [this.handler])))(); | ||
}; | ||
return SimpleRule; | ||
}(BaseRule)); | ||
exports.SimpleRule = SimpleRule; | ||
var FirstMatchingRule = (function (_super) { | ||
__extends(FirstMatchingRule, _super); | ||
function FirstMatchingRule() { | ||
var rules = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
rules[_i] = arguments[_i]; | ||
} | ||
var _this = _super.call(this) || this; | ||
Konsole_1.konsole.log("FirstMatchingRule.constructor: rules", rules); | ||
_this.rule$ = rxjs_1.Observable.from(rules) | ||
.filter(function (rule) { return !!rule; }) | ||
.map(function (rule) { return exports.ruleize(rule); }); | ||
return _this; | ||
}; | ||
exports.prependMatcher = function (matcher, rule) { return ({ | ||
tryMatch: function (match) { | ||
return exports.matchize(matcher, match) | ||
.flatMap(function (m) { return rule.tryMatch(m); }); | ||
} | ||
FirstMatchingRule.prototype.tryMatch = function (match) { | ||
Konsole_1.konsole.log("FirstMatchingRule.tryMatch", match); | ||
return this.rule$ | ||
.flatMap(function (rule, i) { | ||
Konsole_1.konsole.log("Rule.first: trying rule #" + i); | ||
return rule.tryMatch(match) | ||
.do(function (m) { return Konsole_1.konsole.log("Rule.first: rule #" + i + " succeeded", m); }); | ||
}, 1) | ||
.take(1); // so that we don't keep going through rules after we find one that matches | ||
}; | ||
return FirstMatchingRule; | ||
}(BaseRule)); | ||
exports.FirstMatchingRule = FirstMatchingRule; | ||
var RuleWithPrependedMatcher = (function (_super) { | ||
__extends(RuleWithPrependedMatcher, _super); | ||
// TO DO: let this take multiple matchers | ||
function RuleWithPrependedMatcher(matcher, rule) { | ||
var _this = _super.call(this) || this; | ||
_this.matcher = matcher; | ||
_this.rule = rule; | ||
return _this; | ||
}); }; | ||
exports.run = function (handler) { return ({ | ||
tryMatch: function (match) { | ||
return exports.toObservable(handler(match)) | ||
.map(function (_) { return null; }); | ||
} | ||
RuleWithPrependedMatcher.prototype.tryMatch = function (match) { | ||
var _this = this; | ||
return exports.matchize(this.matcher, match) | ||
.flatMap(function (m) { return _this.rule.tryMatch(m); }); | ||
}; | ||
return RuleWithPrependedMatcher; | ||
}(BaseRule)); | ||
exports.RuleWithPrependedMatcher = RuleWithPrependedMatcher; | ||
var RunRule = (function (_super) { | ||
__extends(RunRule, _super); | ||
function RunRule(handler) { | ||
var _this = _super.call(this) || this; | ||
_this.handler = handler; | ||
return _this; | ||
} | ||
RunRule.prototype.tryMatch = function (match) { | ||
return exports.toFilteredObservable(this.handler(match)) | ||
.map(function (_) { return null; }); | ||
}; | ||
return RunRule; | ||
}(BaseRule)); | ||
exports.RunRule = RunRule; | ||
}); }; | ||
function rule() { | ||
@@ -191,25 +109,13 @@ var args = []; | ||
} | ||
var ruleOrHandler = args[args.length - 1]; | ||
if (isRule(ruleOrHandler)) { | ||
switch (args.length) { | ||
case 1: | ||
return ruleOrHandler; | ||
case 2: | ||
return ruleOrHandler.prependMatcher(args[0]); | ||
default: | ||
return ruleOrHandler.prependMatcher(combineMatchers.apply(void 0, args.slice(0, args.length - 1))); | ||
} | ||
var ruleOrHandler = exports.ruleize(args[args.length - 1]); | ||
switch (args.length) { | ||
case 1: | ||
return ruleOrHandler; | ||
case 2: | ||
return exports.prependMatcher(args[0], ruleOrHandler); | ||
default: | ||
return exports.prependMatcher(combineMatchers.apply(void 0, args.slice(0, args.length - 1)), ruleOrHandler); | ||
} | ||
return new (SimpleRule.bind.apply(SimpleRule, [void 0].concat(args)))(); | ||
} | ||
exports.rule = rule; | ||
function first() { | ||
var rules = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
rules[_i] = arguments[_i]; | ||
} | ||
return new (FirstMatchingRule.bind.apply(FirstMatchingRule, [void 0].concat(rules)))(); | ||
} | ||
exports.first = first; | ||
exports.run = function (handler) { return new RunRule(handler); }; | ||
//# sourceMappingURL=Rules.js.map |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "rules-based app engine", | ||
@@ -10,0 +10,0 @@ "main": "dist/prague.js", |
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
100830
1413