Comparing version 0.11.3 to 0.12.0
@@ -1,2 +0,2 @@ | ||
import { Router, RouterOrHandler, Predicate, Matcher, tryMatch, toRouter, Route, Observableable, toObservable, toFilteredObservable, first } from './Router'; | ||
import { Router, RouterOrHandler, Predicate, Matcher, tryMatch, toRouter, Route, Observableable, toObservable, toFilteredObservable, first, nullRouter } from './Router'; | ||
import { Observable } from 'rxjs'; | ||
@@ -14,9 +14,8 @@ import { konsole } from './Konsole'; | ||
export interface DialogResponseHandler<M extends object = {}, DIALOGRESPONSE extends object = {}> { | ||
export interface DialogResponseHandler <M extends object = {}, DIALOGRESPONSE extends object = {}> { | ||
(message: M & IDialogResponseHandlerMatch<DIALOGRESPONSE>): Observableable<void>; | ||
} | ||
export interface DialogState<DIALOGSTATE extends object = {}> { | ||
export interface DialogState <DIALOGSTATE extends object = {}> { | ||
state: DIALOGSTATE, | ||
// child: DialogInstance, | ||
activeDialogs: { | ||
@@ -27,3 +26,3 @@ [name: string]: DialogInstance; | ||
export interface DialogConstructorHelper< | ||
export interface DialogConstructorHelper < | ||
M extends object = {}, | ||
@@ -40,3 +39,3 @@ DIALOGARGS extends object = {}, | ||
export interface DialogRouterHelper< | ||
export interface DialogRouterHelper < | ||
M extends object = {}, | ||
@@ -72,3 +71,3 @@ DIALOGRESPONSE extends object = {}, | ||
// first<M extends object>(... routersOrHandlers: (RouterOrHandler<M>)[]): IRouter<M>; | ||
// first<M extends object> (... routersOrHandlers: (RouterOrHandler<M>)[]): IRouter<M>; | ||
@@ -80,24 +79,2 @@ // call this from within a dialog to signal its end and (optionally) pass a response to the dialog response handler | ||
// // stack | ||
// // call this from within a dialog to signal its end and ask the parent dialog to begin another dialog | ||
// replace <DIALOGARGS extends object = {}> ( | ||
// dialogOrName: DialogOrName<M, DIALOGARGS>, | ||
// args?: DIALOGARGS, | ||
// ): Promise<void>; | ||
// setChild( | ||
// dialogInstance: DialogInstance | ||
// ): Promise<void>; | ||
// // shorthand for createInstance(...).then(di => setChild(di)) | ||
// setChild <DIALOGARGS extends object = {}> ( | ||
// dialogOrName: DialogOrName<M, DIALOGARGS>, | ||
// args?: DIALOGARGS, | ||
// ): Promise<void>; | ||
// clearChild(): void; | ||
// routeToChild(): IRouter<M>; | ||
// activation | ||
@@ -113,3 +90,5 @@ | ||
dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, | ||
dialogResponseHandler: DialogResponseHandler<M, DIALOGRESPONSE> | ||
instanceTag: string, | ||
args?: DIALOGARGS, | ||
dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE> | ||
): Promise<void>; | ||
@@ -132,3 +111,3 @@ | ||
dialogOrName: DialogOrName<M, any, DIALOGRESPONSE>, | ||
tag: string, | ||
instanceTag: string, | ||
dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE> | ||
@@ -139,48 +118,31 @@ ): Router<M>; | ||
routeTo <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, N extends object = {}>( | ||
routeTo <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, N extends object = {}> ( | ||
dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, | ||
trigger: Matcher<M, N>, | ||
args?: DIALOGARGS | ((n: N) => Observableable<DIALOGARGS>), | ||
trigger: DialogTrigger<M, DIALOGARGS>, | ||
dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE> | ||
): Router<M>; | ||
routeTo <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>( | ||
routeTo <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}> ( | ||
dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, | ||
trigger: Predicate<M>, | ||
args?: DIALOGARGS | ((m: M) => Observableable<DIALOGARGS>), | ||
args?: DIALOGARGS, | ||
dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE> | ||
): Router<M>; | ||
routeTo <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>( | ||
routeTo <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}> ( | ||
dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, | ||
args: DIALOGARGS, | ||
dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE> | ||
): Router<M>; | ||
routeTo <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, N extends object = {}>( | ||
dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, | ||
instanceTag: string, | ||
trigger: Matcher<M, N>, | ||
args?: DIALOGARGS | ((n: N) => Observableable<DIALOGARGS>), | ||
trigger: DialogTrigger<M, DIALOGARGS>, | ||
dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE> | ||
): Router<M>; | ||
routeTo <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>( | ||
routeTo <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}> ( | ||
dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, | ||
instanceTag: string, | ||
trigger: Predicate<M>, | ||
args?: DIALOGARGS | ((m: M) => Observableable<DIALOGARGS>), | ||
args?: DIALOGARGS, | ||
dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE> | ||
): Router<M>; | ||
routeTo <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>( | ||
dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, | ||
instanceTag: string, | ||
args: DIALOGARGS, | ||
dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE> | ||
): Router<M>; | ||
} | ||
export interface DialogConstructor< | ||
export interface DialogConstructor < | ||
M extends object, | ||
@@ -194,3 +156,3 @@ DIALOGARGS extends object = {}, | ||
export interface DialogRouter< | ||
export interface DialogRouter < | ||
M extends object, | ||
@@ -203,3 +165,3 @@ DIALOGRESPONSE extends object = {}, | ||
export interface DialogRouterOrHandler< | ||
export interface DialogRouterOrHandler < | ||
M extends object = {}, | ||
@@ -210,5 +172,19 @@ DIALOGRESPONSE extends object = {}, | ||
(dialog: DialogRouterHelper<M, DIALOGRESPONSE, DIALOGSTATE>): RouterOrHandler<M>; | ||
} | ||
export interface IDialog< | ||
export type DialogTrigger < | ||
M extends object = {}, | ||
DIALOGARGS extends object = {} | ||
> = Predicate<M> | Matcher<M, DIALOGARGS>; | ||
export function tryDialogTrigger < | ||
M extends object = {}, | ||
DIALOGARGS extends object = {} | ||
> (trigger: DialogTrigger<M, DIALOGARGS>, message): Observable<DIALOGARGS> { | ||
konsole.log("tryDialogTrigger", trigger, message); | ||
return toFilteredObservable(trigger(message) as any) | ||
.map(m => typeof m === 'boolean' ? {} as any : m); | ||
} | ||
export interface IDialog < | ||
M extends object, | ||
@@ -220,6 +196,7 @@ DIALOGARGS extends object = {}, | ||
constructor?: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>, | ||
routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE>, | ||
router?: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE>, | ||
trigger?: DialogTrigger<M, DIALOGARGS>, | ||
} | ||
export interface LocalDialog< | ||
export interface LocalDialog < | ||
M extends object, | ||
@@ -234,5 +211,6 @@ DIALOGARGS extends object = {}, | ||
router: DialogRouter<M, DIALOGRESPONSE, DIALOGSTATE>; | ||
trigger: DialogTrigger<M, DIALOGARGS>; | ||
} | ||
export interface RemoteDialog< | ||
export interface RemoteDialog < | ||
M extends object, | ||
@@ -242,8 +220,8 @@ DIALOGARGS extends object = {}, | ||
> { | ||
localName: string; | ||
remoteUrl: string; | ||
localName: string; | ||
remoteName: string; | ||
} | ||
export type LocalOrRemoteDialog< | ||
export type LocalOrRemoteDialog < | ||
M extends object = {}, | ||
@@ -264,3 +242,3 @@ DIALOGARGS extends object = {}, | ||
export type DialogOrName< | ||
export type DialogOrName < | ||
M extends object = {}, | ||
@@ -272,3 +250,3 @@ DIALOGARGS extends object = {}, | ||
export const localName = (dialogOrName: DialogOrName): string => | ||
export const toLocalName = (dialogOrName: DialogOrName): string => | ||
typeof dialogOrName === 'string' | ||
@@ -363,3 +341,3 @@ ? dialogOrName | ||
export class Dialogs<M extends object> { | ||
export class Dialogs <M extends object> { | ||
@@ -377,3 +355,3 @@ private dialogRegistry: { | ||
private toLocalOrRemoteDialog<DIALOGARGS extends object = {}>( | ||
private toLocalOrRemoteDialog<DIALOGARGS extends object = {}> ( | ||
dialogOrName: DialogOrName<M, DIALOGARGS> | ||
@@ -384,3 +362,3 @@ ): LocalOrRemoteDialog<M, DIALOGARGS> { | ||
const localOrRemoteDialog = this.dialogRegistry[dialogOrName]; | ||
const localOrRemoteDialog: LocalOrRemoteDialog<M, DIALOGARGS> = this.dialogRegistry[dialogOrName] as any; | ||
if (localOrRemoteDialog) | ||
@@ -392,15 +370,3 @@ return localOrRemoteDialog; | ||
// add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>( | ||
// localName: string, | ||
// remoteName: string, | ||
// dialog: IDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE> | ||
// ): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
// add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>( | ||
// localName: string, | ||
// remoteable: boolean, | ||
// dialog: IDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE> | ||
// ): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>( | ||
add <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}> ( | ||
localName: string, | ||
@@ -410,55 +376,25 @@ dialog: IDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE> | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>( | ||
add <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}> ( | ||
localName: string, | ||
remoteName: string, | ||
constructor: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>, | ||
routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE> | ||
dialog: IDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE> | ||
): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>( | ||
add <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}> ( | ||
localName: string, | ||
remoteable: boolean, | ||
constructor: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>, | ||
routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE> | ||
dialog: IDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE> | ||
): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>( | ||
localName: string, | ||
constructor: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>, | ||
routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE> | ||
): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
// add <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}> ( | ||
// localName: string, | ||
// remoteUrl: string, | ||
// remoteName?: string | ||
// ): RemoteDialog<M, DIALOGARGS, DIALOGRESPONSE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>( | ||
add ( | ||
localName: string, | ||
remoteName: string, | ||
routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE> | ||
): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>( | ||
localName: string, | ||
remoteable: boolean, | ||
routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE> | ||
): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>( | ||
localName: string, | ||
routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE> | ||
): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>( | ||
localName: string, | ||
remoteUrl: string, | ||
): RemoteDialog<M, DIALOGARGS, DIALOGRESPONSE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>( | ||
localName: string, | ||
remoteUrl: string, | ||
remoteName: string | ||
): RemoteDialog<M, DIALOGARGS, DIALOGRESPONSE>; | ||
add( | ||
localName: string, | ||
... args | ||
) { | ||
let dialog: LocalOrRemoteDialog; | ||
let dialog: LocalOrRemoteDialog<M>; | ||
@@ -471,9 +407,7 @@ if (typeof args[0] === 'string' && (args.length === 1 || (args.length === 2 && typeof args[1] === 'string'))) { | ||
remoteName: (args.length === 2 && args[1]) || localName | ||
} | ||
} as RemoteDialog<M> | ||
} else { | ||
// local dialog | ||
let remoteName: string; | ||
let constructor; | ||
let routerOrHandler: DialogRouterOrHandler; | ||
let dialogIndex = 1; | ||
let idialog: IDialog<M> = args[1]; | ||
@@ -486,24 +420,12 @@ if (typeof args[0] === 'string') { | ||
} else { | ||
dialogIndex = 0; | ||
idialog = args[0] | ||
} | ||
if (args.length === dialogIndex + 2) { | ||
// init + router | ||
constructor = args[dialogIndex]; | ||
routerOrHandler = args[dialogIndex + 1]; | ||
} else if (args[dialogIndex].router) { | ||
// IDialog | ||
constructor = args[dialogIndex].constructor; | ||
routerOrHandler = args[dialogIndex].routerOrHandler; | ||
} else { | ||
// just router (use default init) | ||
routerOrHandler = args[dialogIndex]; | ||
} | ||
dialog = { | ||
localName, | ||
remoteName, | ||
constructor, | ||
router: dialog => toRouter(routerOrHandler(dialog)) | ||
} | ||
constructor: idialog.constructor || ((dialog, m) => {}), | ||
router: (dialog: DialogRouterHelper<M>) => idialog.router ? toRouter(idialog.router(dialog)) : nullRouter(), | ||
trigger: idialog.trigger || (() => ({})) | ||
} as LocalDialog<M>; | ||
} | ||
@@ -524,3 +446,3 @@ | ||
m: M, | ||
dialogArgs?: object, | ||
dialogArgs: object = {}, | ||
dialogResponseHandler: DialogResponseHandler<M> = () => {} | ||
@@ -535,8 +457,2 @@ ): Observable<DialogInstance> { | ||
if (isLocalDialog(localOrRemoteDialog)) { | ||
if (!localOrRemoteDialog.constructor) | ||
return toObservable(this.localDialogInstances.createInstance(localOrRemoteDialog.localName, { | ||
state: {}, | ||
activeDialogs: {} | ||
} as DialogState)); | ||
let dialogResponse; | ||
@@ -624,3 +540,2 @@ let messageToRoute: M; | ||
end: (dialogResponse?: object) => boolean = () => true, | ||
// replace?: (dialogOrName: DialogOrName, args?: object) => Promise<void> | ||
): Observable<Route> { | ||
@@ -675,3 +590,3 @@ console.log("getRouteFromDialogInstance", dialogInstance, m, dialogResponseHandler, end); // , replace); | ||
// ) | ||
// .flatMap<RemoteTryMatchResponse, Route>(response => { | ||
// .flatMap<RemoteTryMatchResponse, Route> (response => { | ||
// if (response.status === 'error') | ||
@@ -681,3 +596,3 @@ // return Observable.throw(`RemoteDialog.tryMatch returned error "${response.error}".`); | ||
// if (response.status === 'matchless') | ||
// return Observable.empty<Route>(); | ||
// return Observable.empty<Route> (); | ||
@@ -696,3 +611,3 @@ // if (response.status !== 'match') | ||
// konsole.log("tryMatch local", m); | ||
// return toObservable(this.localDialogInstances.getDialogData<DIALOGSTATE>(dialogInstance)) | ||
// return toObservable(this.localDialogInstances.getDialogData<DIALOGSTATE> (dialogInstance)) | ||
// .flatMap(dialogState => | ||
@@ -705,11 +620,11 @@ // dialog.router.getRoute({ | ||
// beginChildDialog: <DIALOGARGS extends object = {}>(dialogOrName: LocalOrRemoteDialog<M, DIALOGARGS> | string, dialogArgs?: DIALOGARGS) => | ||
// beginChildDialog: <DIALOGARGS extends object = {}> (dialogOrName: LocalOrRemoteDialog<M, DIALOGARGS> | string, dialogArgs?: DIALOGARGS) => | ||
// this.activate(dialogOrName, m, dialogArgs) | ||
// .do(dialogInstance => m.dialogState.childDialogInstance = dialogInstance) | ||
// .toPromise(), | ||
// clearChildDialog: () => new Promise<void>((resolve) => { | ||
// clearChildDialog: () => new Promise<void> ((resolve) => { | ||
// dialogState.childDialogInstance = undefined; | ||
// resolve(); | ||
// }), | ||
// replaceThisDialog: <DIALOGARGS extends object = {}>(dialogOrName: LocalOrRemoteDialog<M, DIALOGARGS, DIALOGRESPONSE> | string, dialogArgs?: DIALOGARGS, dialogResponse?: DIALOGRESPONSE) => | ||
// replaceThisDialog: <DIALOGARGS extends object = {}> (dialogOrName: LocalOrRemoteDialog<M, DIALOGARGS, DIALOGRESPONSE> | string, dialogArgs?: DIALOGARGS, dialogResponse?: DIALOGRESPONSE) => | ||
// toObservable(dialogResponseHandler({ | ||
@@ -755,3 +670,3 @@ // ... m as any, | ||
// ) | ||
// .flatMap<RemoteTryMatchResponse, Route>(response => { | ||
// .flatMap<RemoteTryMatchResponse, Route> (response => { | ||
// if (response.status === 'error') | ||
@@ -761,3 +676,3 @@ // return Observable.throw(`RemoteDialog.tryMatch returned error "${response.error}".`); | ||
// if (response.status === 'matchless') | ||
// return Observable.empty<Route>(); | ||
// return Observable.empty<Route> (); | ||
@@ -774,2 +689,12 @@ // if (response.status !== 'match') | ||
private tryDialogTrigger(dialogOrName: DialogOrName<M>, m: M): Observable<object> { | ||
console.log("tryDialogTrigger()", dialogOrName, m); | ||
const dialog = this.toLocalOrRemoteDialog(dialogOrName); | ||
if (isLocalDialog(dialog)) { | ||
return tryDialogTrigger(dialog.trigger, m); | ||
} else { | ||
// handle remote dialog here | ||
return Observable.empty(); | ||
} | ||
} | ||
@@ -840,3 +765,3 @@ // These methods are used to serve up local dialogs remotely | ||
// // will only be called by replaceThisDialog | ||
// new Promise<void>((resolve) => { | ||
// new Promise<void> ((resolve) => { | ||
// tasks.push({ | ||
@@ -854,3 +779,3 @@ // method: 'beginChildDialog', | ||
// // will only be called by endThisDialog | ||
// new Promise<void>((resolve) => { | ||
// new Promise<void> ((resolve) => { | ||
// tasks.push({ | ||
@@ -952,114 +877,5 @@ // method: 'clearChildDialog' | ||
end: (dialogResponse?: object) => boolean = () => true, | ||
// replace?: (dialogOrName: DialogOrName, args?: object) => Promise<void> | ||
): DialogRouterHelper<M> { | ||
console.log("creating dialogRouterHelper", dialogInstance, m, dialogState, dialogInstances, dialogResponseHandler, end) // , replace); | ||
console.log("creating dialogRouterHelper", dialogInstance, m, dialogState, dialogInstances, dialogResponseHandler, end) | ||
// const routeToChild = (): IRouter<M> => ({ | ||
// getRoute: (m: M) => { | ||
// console.log("dialog.routeToChild.getRoute"); | ||
// return dialogState.child | ||
// ? this.getRouteFromDialogInstance(dialogState.child, m, undefined, | ||
// (dialogResponse) => { | ||
// dialogState.child = undefined; | ||
// if (dialogResponse !== undefined) | ||
// console.warn(`Stacked dialog ${dialogState.child.name} returned a response. It was ignored.`); | ||
// return true; | ||
// }, | ||
// (dialogOrName, args) => | ||
// this.createDialogInstance(dialogOrName, m, args) | ||
// .map(dialogInstance => { | ||
// dialogState.child = dialogInstance; | ||
// }) | ||
// .toPromise() | ||
// ) | ||
// : Observable.empty(); | ||
// } | ||
// }); | ||
const routeToActive = ( | ||
dialogOrName: DialogOrName<M>, | ||
... args, | ||
): Router<M> => ({ | ||
getRoute: (m: M) => { | ||
console.log("dialog.routeToActive().getRoute", dialogOrName); | ||
let tag = "default"; | ||
let i = 0; | ||
if (args.length && typeof args[0] === 'string') { | ||
tag = args[0]; | ||
i = 1; | ||
} | ||
const instanceName = localName(dialogOrName) + '@@@' + tag; | ||
const dialogResponseHandler: DialogResponseHandler<M> = args[i]; | ||
return toFilteredObservable(dialogState.activeDialogs[instanceName]) | ||
.flatMap(dialogInstance => | ||
this.getRouteFromDialogInstance(dialogInstance, m, dialogResponseHandler, (dialogResponse) => { | ||
delete dialogState.activeDialogs[instanceName]; | ||
return true; | ||
}) | ||
); | ||
} | ||
}); | ||
const routeTo = ( | ||
dialogOrName: DialogOrName<M>, | ||
... args | ||
): Router<M> => ({ | ||
getRoute: (m: M) => { | ||
console.log("dialog.routeTo().getRoute", dialogOrName, ...args); | ||
const dialog = this.toLocalOrRemoteDialog(dialogOrName); | ||
if (!dialog) | ||
return Observable.empty(); | ||
let instanceTag = 'default'; | ||
let iArg = 0; | ||
if (typeof args[0] === 'string') { | ||
instanceTag = args[0]; | ||
iArg = 1; | ||
} | ||
const instanceName = dialog.localName + '@@@' + instanceTag; | ||
const dialogInstance = dialogState.activeDialogs[instanceName]; | ||
let predicateOrMatcher: Predicate<M> | Matcher<M>; | ||
if (args.length >= iArg + 1 && typeof args[iArg] === 'function') { | ||
predicateOrMatcher = args[iArg]; | ||
iArg++; | ||
} | ||
let getDialogArgs: (n) => object = () => ({}); | ||
if (args.length >= iArg + 1) { | ||
getDialogArgs = typeof args[iArg] === 'function' | ||
? args[iArg] | ||
: () => args[iArg]; | ||
} | ||
const dialogResponseHandler = args.length === iArg + 2 ? args[iArg + 1] : undefined; | ||
console.log("dialog.routeTo() derived args", instanceName, dialogInstance, predicateOrMatcher, getDialogArgs, dialogResponseHandler); | ||
if (dialogInstance) | ||
// dialog already active - route the message | ||
return this.getRouteFromDialogInstance(dialogInstance, m, dialogResponseHandler, (dialogResponse) => { | ||
delete dialogState.activeDialogs[instanceName]; | ||
return true; | ||
}); | ||
return tryMatch(predicateOrMatcher, m) | ||
.map(n => ({ | ||
action: () => toObservable(getDialogArgs(n)) | ||
.do(dialogArgs => console.log("dialog.routeTo() dialog args", dialogArgs)) | ||
.flatMap(dialogArgs => this.createDialogInstance(dialogOrName, m, dialogArgs, dialogResponseHandler)) | ||
.do(dialogInstance => { | ||
dialogState.activeDialogs[instanceName] = dialogInstance; | ||
}) | ||
} as Route)); | ||
} | ||
}); | ||
return { | ||
@@ -1091,3 +907,2 @@ // core | ||
// return first( | ||
// routeToChild(), | ||
// ... dialogState.activeDialogs | ||
@@ -1114,35 +929,2 @@ // ? Object.keys(dialogState.activeDialogs).map(name => routeToActive(name)) | ||
// // stack | ||
// replace: ( | ||
// dialogOrName: DialogOrName<M>, | ||
// args?: object, | ||
// ): Promise<void> => { | ||
// console.log("dialog.replace", dialogOrName, args) | ||
// return end() && replace | ||
// ? replace(dialogOrName, args) | ||
// : Promise.resolve(); | ||
// }, | ||
// setChild: (... args): Promise<void> => { | ||
// console.log("dialog.setChild", args) | ||
// if (args[0].instanceId !== undefined) { | ||
// dialogState.child = args[0]; | ||
// return Promise.resolve(); | ||
// } | ||
// return this.createDialogInstance(args[0], m, ... args.slice(1)) | ||
// .map(dialogInstance => { | ||
// dialogState.child = dialogInstance; | ||
// }) | ||
// .toPromise(); | ||
// }, | ||
// clearChild: (): void => { | ||
// console.log("dialog.clearChild") | ||
// dialogState.child = undefined; | ||
// }, | ||
// routeToChild, | ||
// activation | ||
@@ -1154,21 +936,37 @@ | ||
): Promise<void> => { | ||
console.log("dialog.activate", dialogOrName, ... args) | ||
console.log("dialog.activate", dialogOrName, ... args); | ||
const dialog = this.toLocalOrRemoteDialog(dialogOrName); | ||
if (!dialog) | ||
return Promise.resolve(); | ||
let i = 0; | ||
let instanceTag = "default"; | ||
if (args.length) { | ||
if (typeof args[0] === 'string') { | ||
instanceTag = args[0]; | ||
i = 1; | ||
} | ||
} | ||
let dialogArgs; | ||
if (args.length >= i + 1) { | ||
if (typeof args[i] !== 'function') { | ||
dialogArgs = args[i]; | ||
i++; | ||
} | ||
} | ||
let dialogResponseHandler: DialogResponseHandler<M>; | ||
let i = 1; | ||
if (args.length === 2) { | ||
dialogArgs = args[0]; | ||
dialogResponseHandler = args[1]; | ||
} else if (args.length === 1) { | ||
if (typeof args[0] === 'function') | ||
dialogResponseHandler = args[0]; | ||
else | ||
dialogArgs = args[0]; | ||
if (args.length >= i + 1) { | ||
dialogResponseHandler = args[i]; | ||
} | ||
const instanceName = dialog.localName + '@@@' + instanceTag; | ||
console.log("dialog.activate() derived args", instanceName, dialogArgs, dialogResponseHandler); | ||
return this.createDialogInstance(dialogOrName, m, dialogArgs, dialogResponseHandler) | ||
.map(dialogInstance => { | ||
dialogState.activeDialogs[dialogInstance.name] = dialogInstance; | ||
dialogState.activeDialogs[instanceName] = dialogInstance; | ||
}) | ||
@@ -1182,3 +980,3 @@ .toPromise(); | ||
console.log("dialog.deactivate", dialogOrName) | ||
delete dialogState.activeDialogs[localName(dialogOrName)]; | ||
delete dialogState.activeDialogs[toLocalName(dialogOrName)]; | ||
}, | ||
@@ -1190,78 +988,103 @@ | ||
console.log("dialog.isActive", dialogOrName) | ||
return dialogState.activeDialogs[localName(dialogOrName)] !== undefined; | ||
return dialogState.activeDialogs[toLocalName(dialogOrName)] !== undefined; | ||
}, | ||
routeToActive, | ||
routeToActive: ( | ||
dialogOrName: DialogOrName<M>, | ||
... args, | ||
): Router<M> => ({ | ||
getRoute: (m: M) => { | ||
console.log("dialog.routeToActive().getRoute", dialogOrName); | ||
const dialog = this.toLocalOrRemoteDialog(dialogOrName); | ||
if (!dialog) | ||
return Observable.empty(); | ||
// "just works" | ||
routeTo | ||
let instanceTag = "default"; | ||
let i = 0; | ||
} as DialogRouterHelper<M> | ||
} | ||
if (args.length && typeof args[0] === 'string') { | ||
instanceTag = args[0]; | ||
i = 1; | ||
} | ||
} | ||
const instanceName = dialog.localName + '@@@' + instanceTag; | ||
const dialogResponseHandler: DialogResponseHandler<M> = args[i]; | ||
console.log("dialog.routeToActive() derived args", instanceName, dialogResponseHandler); | ||
return toFilteredObservable(dialogState.activeDialogs[instanceName]) | ||
.flatMap(dialogInstance => | ||
this.getRouteFromDialogInstance(dialogInstance, m, dialogResponseHandler, (dialogResponse) => { | ||
delete dialogState.activeDialogs[instanceName]; | ||
return true; | ||
}) | ||
); | ||
} | ||
}), | ||
// Child dialogs | ||
// "just works" | ||
routeTo: ( | ||
dialogOrName: DialogOrName<M>, | ||
... args | ||
): Router<M> => ({ | ||
getRoute: (m: M) => { | ||
console.log("dialog.routeTo().getRoute", dialogOrName, ...args); | ||
const dialog = this.toLocalOrRemoteDialog(dialogOrName); | ||
if (!dialog) | ||
return Observable.empty(); | ||
let instanceTag = 'default'; | ||
let iArg = 0; | ||
// runChildIfActive< | ||
// ANYMATCH extends object = M, | ||
// DIALOGRESPONSE extends object = {} | ||
// >( | ||
// dialogOrName?: LocalOrRemoteDialog<M, any, DIALOGRESPONSE> | string, | ||
// dialogResponseHandler: DialogResponseHandler<ANYMATCH, DIALOGRESPONSE> = () => {} | ||
// ): IRouter<ANYMATCH> { | ||
// return { | ||
// getRoute: (message: ANYMATCH & IDialogMatch<ANYMATCH>) => { | ||
if (typeof args[0] === 'string') { | ||
instanceTag = args[0]; | ||
iArg = 1; | ||
} | ||
// konsole.log("runChildIfActive", message); | ||
const instanceName = toLocalName(dialogOrName) + '@@@' + instanceTag; | ||
const dialogInstance = dialogState.activeDialogs[instanceName]; | ||
// let odi: Observable<DialogInstance>; | ||
// if (message.dialogStack) { | ||
// if (!message.dialogState.childDialogInstance) | ||
// return; | ||
// odi = Observable.of(message.dialogState.childDialogInstance); | ||
// } else { | ||
// // This is being run from the "root" (a non-dialog router) | ||
// message = { | ||
// ... message as any, | ||
// dialogStack: [], | ||
// } | ||
// odi = toFilteredObservable(this.rootDialogInstance.get(message)); | ||
// } | ||
let trigger: DialogTrigger; | ||
let dialogArgs: object; | ||
// konsole.log("runChildIfActive (active)", message); | ||
if (args.length >= iArg + 1) { | ||
if (typeof args[iArg] === 'function') | ||
trigger = args[iArg]; | ||
else | ||
dialogArgs = args[iArg]; | ||
} | ||
// return odi | ||
// .flatMap(dialogInstance => { | ||
// const dialog = this.dialogs[dialogInstance.name]; | ||
const dialogResponseHandler = args.length === iArg + 2 ? args[iArg + 1] : undefined; | ||
// if (!dialog) { | ||
// konsole.warn(`The stack references a dialog named "${dialogInstance.name}", which doesn't exist.`); | ||
// return Observable.empty<Route>(); | ||
// } | ||
console.log("dialog.routeTo() derived args", instanceName, dialogInstance, trigger, dialogArgs, dialogResponseHandler); | ||
// // if a dialog is provided, only run that one | ||
// if (dialogOrName && this.dialogize(dialogOrName) !== dialog) | ||
// return Observable.empty<Route>(); | ||
if (dialogInstance) | ||
// dialog already active - route the message | ||
return this.getRouteFromDialogInstance(dialogInstance, m, dialogResponseHandler, (dialogResponse) => { | ||
delete dialogState.activeDialogs[instanceName]; | ||
return true; | ||
}); | ||
// return this.getRoute(dialog, message as any, dialogInstance, dialogResponseHandler as any); | ||
// }); | ||
// } | ||
// } as IRouter<ANYMATCH>; | ||
// } | ||
return ( | ||
dialogArgs | ||
? Observable.of(dialogArgs) | ||
: trigger | ||
? tryDialogTrigger(trigger, m) | ||
: this.tryDialogTrigger(dialog, m) | ||
) | ||
.do(dialogArgs => console.log("dialog.routeTo() dialog args", dialogArgs)) | ||
.map(dialogArgs => ({ | ||
action: () => | ||
this.createDialogInstance(dialogOrName, m, dialogArgs, dialogResponseHandler) | ||
.do(dialogInstance => { | ||
dialogState.activeDialogs[instanceName] = dialogInstance; | ||
}) | ||
} as Route)); | ||
} | ||
}), | ||
// matchRootDialog(message: M): M & IDialogRootMatch<M> { | ||
// return { | ||
// ... message as any, | ||
// beginChildDialog: <DIALOGARGS extends object = {}>(dialog: LocalOrRemoteDialog<M, DIALOGARGS> | string, dialogArgs?: DIALOGARGS) => | ||
// this.activate(dialog, message, dialogArgs) | ||
// .flatMap(dialogInstance => toObservable(this.rootDialogInstance.set(message, dialogInstance))) | ||
// .toPromise(), | ||
// clearChildDialog: () => toObservable(this.rootDialogInstance.set(message)).toPromise() | ||
// } | ||
// } | ||
} as DialogRouterHelper<M> | ||
} | ||
} |
@@ -159,3 +159,3 @@ import { Observable } from 'rxjs'; | ||
public match<M extends ITextMatch>(message: M) { | ||
public match <M extends ITextMatch> (message: M) { | ||
return this.call(message.text) | ||
@@ -175,3 +175,3 @@ .filter(luisResponse => luisResponse.topScoringIntent.score >= this.scoreThreshold) | ||
best<M extends ITextMatch>(luisRouters: LuisRouters<M>) { | ||
best <M extends ITextMatch> (luisRouters: LuisRouters<M>) { | ||
return { | ||
@@ -178,0 +178,0 @@ getRoute: (message: M) => |
@@ -11,3 +11,3 @@ import { konsole } from './Konsole'; | ||
export const matchChoice = (choices: string[]) => | ||
<M extends ITextMatch>(message: M) => { | ||
<M extends ITextMatch> (message: M) => { | ||
const choice = choices.find(choice => choice.toLowerCase() === message.text.toLowerCase()); | ||
@@ -20,3 +20,3 @@ return choice && { | ||
export const promptChoice = <M extends ITextMatch>(choices: string[], routerOrHandler: RouterOrHandler<M & IChatPromptChoiceMatch>) => { | ||
export const promptChoice = <M extends ITextMatch> (choices: string[], routerOrHandler: RouterOrHandler<M & IChatPromptChoiceMatch>) => { | ||
return prependMatcher<M>(matchChoice(choices), routerOrHandler); | ||
@@ -26,3 +26,3 @@ } | ||
export const matchConfirm = () => | ||
<M extends ITextMatch>(message: M) => { | ||
<M extends ITextMatch> (message: M) => { | ||
const m = matchChoice(['Yes', 'No'])(message); | ||
@@ -32,3 +32,3 @@ return m.choice === 'Yes' && m; | ||
export const promptConfirm = <M extends ITextMatch>(routerOrHandler: RouterOrHandler<M>) => { | ||
export const promptConfirm = <M extends ITextMatch> (routerOrHandler: RouterOrHandler<M>) => { | ||
return prependMatcher<M>(matchConfirm(), routerOrHandler); | ||
@@ -50,3 +50,3 @@ } | ||
export const matchTime = () => | ||
<M extends ITextMatch>(m: M) => { | ||
<M extends ITextMatch> (m: M) => { | ||
const time = parseTime(m.text); | ||
@@ -53,0 +53,0 @@ return time && { |
@@ -11,3 +11,3 @@ import { Observable } from 'rxjs'; | ||
export const matchRE = (... intents: RegExp[]) => | ||
<M extends ITextMatch>(message: M) => | ||
<M extends ITextMatch> (message: M) => | ||
Observable.from(intents) | ||
@@ -26,3 +26,3 @@ .do(_ => konsole.log("matchRegExp matching", message)) | ||
// Either call as ifRE(intent, action) or ifRE([intent, intent, ...], action) | ||
export const ifMatchRE = <M extends ITextMatch>(intent: RegExp, routerOrHandler: RouterOrHandler<M & IRegExpMatch>) => | ||
export const ifMatchRE = <M extends ITextMatch> (intent: RegExp, routerOrHandler: RouterOrHandler<M & IRegExpMatch>) => | ||
prependMatcher<M>(matchRE(intent), routerOrHandler); |
import { konsole } from './Konsole'; | ||
import { Observable } from 'rxjs'; | ||
export type Observableable<T> = T | Observable<T> | Promise<T>; | ||
export type Observableable <T> = T | Observable<T> | Promise<T>; | ||
export const toObservable = <T>(t: Observableable<T>) => { | ||
export const toObservable = <T> (t: Observableable<T>) => { | ||
if (t instanceof Observable) | ||
return t; | ||
if (t instanceof Promise) | ||
return Observable.fromPromise<T>(t); | ||
return Observable.fromPromise<T> (t); | ||
return Observable.of(t); | ||
} | ||
export const toFilteredObservable = <T>(t: Observableable<T>) => { | ||
export const toFilteredObservable = <T> (t: Observableable<T>) => { | ||
if (!t) | ||
@@ -20,3 +20,3 @@ return Observable.empty<T>(); | ||
if (t instanceof Promise) | ||
return Observable.fromPromise<T>(t).filter(i => !!i); | ||
return Observable.fromPromise<T> (t).filter(i => !!i); | ||
return Observable.of(t); | ||
@@ -31,21 +31,21 @@ } | ||
export interface Router<M extends object> { | ||
export interface Router <M extends object> { | ||
getRoute(message: M): Observable<Route>; | ||
} | ||
export interface Handler<Z extends object = {}> { | ||
export interface Handler <Z extends object = {}> { | ||
(message: Z): Observableable<any>; | ||
} | ||
export type RouterOrHandler<M extends object = {}> = Router<M> | Handler<M>; | ||
export type RouterOrHandler <M extends object = {}> = Router<M> | Handler<M>; | ||
export function isRouter<M extends object>(routerOrHandler: RouterOrHandler<M>): routerOrHandler is Router<M> { | ||
export function isRouter <M extends object> (routerOrHandler: RouterOrHandler<M>): routerOrHandler is Router<M> { | ||
return ((routerOrHandler as any).getRoute !== undefined); | ||
} | ||
export const toRouter = <M extends object>(routerOrHandler: RouterOrHandler<M>) => { | ||
export const toRouter = <M extends object> (routerOrHandler: RouterOrHandler<M>) => { | ||
return isRouter(routerOrHandler) ? routerOrHandler : simpleRouter(routerOrHandler); | ||
} | ||
export const routeMessage = <M extends object>(router: Router<M>, message: M) => | ||
export const routeMessage = <M extends object> (router: Router<M>, message: M) => | ||
router.getRoute(message) | ||
@@ -57,3 +57,3 @@ .do(route => konsole.log("handle: matched a route", route)) | ||
export const simpleRouter = <M extends object>(handler: Handler<M>): Router<M> => ({ | ||
export const simpleRouter = <M extends object> (handler: Handler<M>): Router<M> => ({ | ||
getRoute: (m) => Observable.of({ | ||
@@ -64,3 +64,3 @@ action: () => handler(m) | ||
const filteredRouter$ = <M extends object>(... routersOrHandlers: (RouterOrHandler<M>)[]) => | ||
const filteredRouter$ = <M extends object> (... routersOrHandlers: RouterOrHandler<M>[]) => | ||
Observable.from(routersOrHandlers) | ||
@@ -70,3 +70,3 @@ .filter(routerOrHandler => !!routerOrHandler) | ||
export const first = <M extends object>(... routersOrHandlers: (RouterOrHandler<M>)[]): Router<M> => ({ | ||
export const first = <M extends object> (... routersOrHandlers: RouterOrHandler<M>[]): Router<M> => ({ | ||
getRoute: (m) => | ||
@@ -89,3 +89,3 @@ filteredRouter$(... routersOrHandlers).flatMap( | ||
export const best = <M extends object>(... routersOrHandlers: (RouterOrHandler<M>)[]): Router<M> => ({ | ||
export const best = <M extends object> (... routersOrHandlers: RouterOrHandler<M>[]): Router<M> => ({ | ||
getRoute: (m) => | ||
@@ -105,3 +105,3 @@ filteredRouter$(... routersOrHandlers).flatMap( | ||
export const run = <M extends object>(handler: Handler<M>): Router<M> => ({ | ||
export const run = <M extends object> (handler: Handler<M>): Router<M> => ({ | ||
getRoute: (m) => | ||
@@ -112,13 +112,13 @@ toObservable(handler(m)) | ||
export interface Matcher<M extends object = {}, Z extends object = {}> { | ||
export interface Matcher <M extends object = {}, Z extends object = {}> { | ||
(message: M): Observableable<Z>; | ||
} | ||
export interface Predicate<M extends object = {}> { | ||
export interface Predicate <M extends object = {}> { | ||
(message: M): Observableable<boolean>; | ||
} | ||
export function tryMatch<M extends object, N extends object>(matcher: Matcher<M, N>, message: M): Observable<N>; | ||
export function tryMatch<M extends object>(predicate: Predicate<M>, message: M): Observable<M>; | ||
export function tryMatch<M extends object>(predicateOrMatcher: Predicate | Matcher, message: M): Observable<any>; | ||
export function tryMatch <M extends object, N extends object> (matcher: Matcher<M, N>, message: M): Observable<N>; | ||
export function tryMatch <M extends object> (predicate: Predicate<M>, message: M): Observable<M>; | ||
export function tryMatch <M extends object> (predicateOrMatcher: Predicate | Matcher, message: M): Observable<any>; | ||
export function tryMatch(matcherOrPredicate: Matcher | Predicate, message): Observable<any> { | ||
@@ -133,26 +133,26 @@ // we want to allow any matcher to be a predicate (return a boolean) | ||
export function matchAll<M extends object, Z extends object>(m1: Matcher<M, Z>): Matcher<M, Z> | ||
export function matchAll<M extends object>(p1: Predicate<M>): Matcher<M, M> | ||
export function matchAll <M extends object, Z extends object> (m1: Matcher<M, Z>): Matcher<M, Z> | ||
export function matchAll <M extends object> (p1: Predicate<M>): Matcher<M, M> | ||
export function matchAll<M extends object, N extends object, Z extends object>(m1: Matcher<M, N>, m2: Matcher<N, Z>): Matcher<M, Z> | ||
export function matchAll<M extends object, Z extends object>(p1: Predicate<M>, m2: Matcher<M, Z>,): Matcher<M, Z> | ||
export function matchAll<M extends object, Z extends object>(m1: Matcher<M, Z>, p2: Predicate<Z>): Matcher<M, Z> | ||
export function matchAll<M extends object>(p1: Predicate<M>, p2: Predicate<M>): Matcher<M, M> | ||
export function matchAll <M extends object, N extends object, Z extends object> (m1: Matcher<M, N>, m2: Matcher<N, Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, Z extends object> (p1: Predicate<M>, m2: Matcher<M, Z>,): Matcher<M, Z> | ||
export function matchAll <M extends object, Z extends object> (m1: Matcher<M, Z>, p2: Predicate<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object> (p1: Predicate<M>, p2: Predicate<M>): Matcher<M, M> | ||
export function matchAll<M extends object, N extends object, O extends object, Z extends object>(m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>): Matcher<M, Z> | ||
export function matchAll<M extends object, N extends object, Z extends object>(p1: Predicate<M>, m2: Matcher<M, N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll<M extends object, N extends object, Z extends object>(m1: Matcher<M, N>, p2: Predicate<N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll<M extends object, N extends object, Z extends object>(m1: Matcher<M, N>, m2: Matcher<N, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll<M extends object, Z extends object>(m1: Matcher<M, Z>, p2: Predicate<Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll<M extends object, Z extends object>(p1: Matcher<M>, m2: Matcher<M, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll<M extends object, Z extends object>(p1: Predicate<M>, p2: Predicate<M>, m3: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll<M extends object>(p1: Predicate<M>, p2: Predicate<M>, p3: Predicate<M>): Matcher<M, M> | ||
export function matchAll <M extends object, N extends object, O extends object, Z extends object> (m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, N extends object, Z extends object> (p1: Predicate<M>, m2: Matcher<M, N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, N extends object, Z extends object> (m1: Matcher<M, N>, p2: Predicate<N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, N extends object, Z extends object> (m1: Matcher<M, N>, m2: Matcher<N, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, Z extends object> (m1: Matcher<M, Z>, p2: Predicate<Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, Z extends object> (p1: Matcher<M>, m2: Matcher<M, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, Z extends object> (p1: Predicate<M>, p2: Predicate<M>, m3: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object> (p1: Predicate<M>, p2: Predicate<M>, p3: Predicate<M>): Matcher<M, M> | ||
export function matchAll<M extends object>(... matchersOrPredicates: (Matcher|Predicate)[]): Matcher<M> | ||
export function matchAll <M extends object> (... matchersOrPredicates: (Matcher|Predicate)[]): Matcher<M> | ||
export function matchAll<M extends object>(... args: Matcher[]): Matcher<M> { | ||
export function matchAll <M extends object> (... args: Matcher[]): Matcher<M> { | ||
return m => { | ||
konsole.log("matchAll", args, m) | ||
return Observable.from(args) | ||
.reduce<Matcher, Observable<any>>( | ||
.reduce<Matcher, Observable<any>> ( | ||
(prevObservable, currentMatcher, i) => | ||
@@ -171,3 +171,3 @@ prevObservable | ||
export function matchAny<M extends object>(... predicatesOrMatchers: (Predicate<M> | Matcher<M>)[]): Matcher<M> { | ||
export function matchAny <M extends object> (... predicatesOrMatchers: (Predicate<M> | Matcher<M>)[]): Matcher<M> { | ||
konsole.log("matchAny", predicatesOrMatchers); | ||
@@ -180,4 +180,4 @@ return m => | ||
export function prependMatcher<L extends object, M extends object>(matcher: Matcher<L, M>, routerOrHandler: RouterOrHandler<M>): Router<L>; | ||
export function prependMatcher<M extends object>(predicate: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M>; | ||
export function prependMatcher <L extends object, M extends object> (matcher: Matcher<L, M>, routerOrHandler: RouterOrHandler<M>): Router<L>; | ||
export function prependMatcher <M extends object> (predicate: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M>; | ||
export function prependMatcher(matcherOrPredicate: Matcher | Predicate, routerOrHandler: RouterOrHandler): Router<any> { | ||
@@ -191,22 +191,22 @@ return { | ||
export function ifMatch<M extends object>(routerOrHandler: RouterOrHandler<M>): Router<M> | ||
export function ifMatch <M extends object> (routerOrHandler: RouterOrHandler<M>): Router<M> | ||
export function ifMatch<M extends object, Z extends object>(m1: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch<M extends object>(p1: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M> | ||
export function ifMatch <M extends object, Z extends object> (m1: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object> (p1: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M> | ||
export function ifMatch<M extends object, N extends object, Z extends object>(m1: Matcher<M, N>, m2: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch<M extends object, Z extends object>(p1: Predicate<M>, m2: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch<M extends object, Z extends object>(m1: Matcher<M, Z>, p2: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch<M extends object>(p1: Predicate<M>, p2: Matcher<M>, routerOrHandler: RouterOrHandler<M>): Router<M> | ||
export function ifMatch <M extends object, N extends object, Z extends object> (m1: Matcher<M, N>, m2: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, Z extends object> (p1: Predicate<M>, m2: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, Z extends object> (m1: Matcher<M, Z>, p2: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object> (p1: Predicate<M>, p2: Matcher<M>, routerOrHandler: RouterOrHandler<M>): Router<M> | ||
export function ifMatch<M extends object, N extends object, O extends object, Z extends object>(m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch<M extends object, Z extends object>(m1: Matcher<M, Z>, p2: Predicate<Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch<M extends object, Z extends object>(p1: Matcher<M>, m2: Matcher<M, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch<M extends object, Z extends object>(p1: Predicate<M>, p2: Predicate<M>, m3: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch<M extends object, N extends object, Z extends object>(p1: Predicate<M>, m2: Matcher<M, N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch<M extends object, N extends object, Z extends object>(m1: Matcher<M, N>, p2: Predicate<N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch<M extends object, N extends object, Z extends object>(m1: Matcher<M, N>, m2: Matcher<N, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch<M extends object>(p1: Predicate<M>, p2: Predicate<M>, p3: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M> | ||
export function ifMatch <M extends object, N extends object, O extends object, Z extends object> (m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, Z extends object> (m1: Matcher<M, Z>, p2: Predicate<Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, Z extends object> (p1: Matcher<M>, m2: Matcher<M, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, Z extends object> (p1: Predicate<M>, p2: Predicate<M>, m3: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, N extends object, Z extends object> (p1: Predicate<M>, m2: Matcher<M, N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, N extends object, Z extends object> (m1: Matcher<M, N>, p2: Predicate<N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, N extends object, Z extends object> (m1: Matcher<M, N>, m2: Matcher<N, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object> (p1: Predicate<M>, p2: Predicate<M>, p3: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M> | ||
export function ifMatch<M extends object>(... args: (Predicate | Matcher | RouterOrHandler)[]): Router<M> { | ||
export function ifMatch <M extends object> (... args: (Predicate | Matcher | RouterOrHandler)[]): Router<M> { | ||
konsole.log("ifMatch", args); | ||
@@ -224,4 +224,4 @@ const router = toRouter(args[args.length - 1] as RouterOrHandler); | ||
export function branchMatch<M extends object>( | ||
predicate: (Predicate<M>), | ||
export function branchMatch <M extends object> ( | ||
predicate: Predicate<M>, | ||
ifRouterOrHandler: RouterOrHandler<M>, | ||
@@ -231,4 +231,4 @@ elseRouterOrHandler: RouterOrHandler<M>, | ||
export function branchMatch<M extends object, N extends object>( | ||
matcher: (Matcher<M, N>), | ||
export function branchMatch <M extends object, N extends object> ( | ||
matcher: Matcher<M, N>, | ||
ifRouterOrHandler: RouterOrHandler<N>, | ||
@@ -238,3 +238,3 @@ elseRouterOrHandler: RouterOrHandler<M>, | ||
export function branchMatch<M extends object>( | ||
export function branchMatch <M extends object> ( | ||
predicateOrMatcher: Predicate | Matcher, | ||
@@ -250,2 +250,6 @@ ifRouterOrHandler: RouterOrHandler, | ||
export const nullRouter = <M extends object = {}> (): Router<M> => ({ | ||
getRoute: (m) => Observable.empty() | ||
}) | ||
const thrownRoute: Route = { | ||
@@ -256,9 +260,9 @@ thrown: true, | ||
export const throwRoute = <M extends object>(): Router<M> => ({ | ||
export const throwRoute = <M extends object> (): Router<M> => ({ | ||
getRoute: (m) => Observable.of(thrownRoute) | ||
}); | ||
export const catchRoute = <M extends object>(router: Router<M>): Router<M> => ({ | ||
export const catchRoute = <M extends object> (router: Router<M>): Router<M> => ({ | ||
getRoute: (m) => router.getRoute(m) | ||
.filter(route => !route.thrown) | ||
}) |
import { Router, RouterOrHandler, Predicate, Matcher, Observableable } from './Router'; | ||
import { Observable } from 'rxjs'; | ||
export interface DialogInstance { | ||
@@ -31,13 +32,11 @@ name: string; | ||
activate<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, args?: DIALOGARGS, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Promise<void>; | ||
activate<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, dialogResponseHandler: DialogResponseHandler<M, DIALOGRESPONSE>): Promise<void>; | ||
activate<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, instanceTag: string, args?: DIALOGARGS, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Promise<void>; | ||
deactivate(dialogOrName: DialogOrName<M>): void; | ||
isActive(dialogOrName: DialogOrName<M>): boolean; | ||
routeToActive<DIALOGRESPONSE extends object = {}>(dialogOrName: DialogOrName<M, any, DIALOGRESPONSE>, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Router<M>; | ||
routeToActive<DIALOGRESPONSE extends object = {}>(dialogOrName: DialogOrName<M, any, DIALOGRESPONSE>, tag: string, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Router<M>; | ||
routeTo<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, N extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, trigger: Matcher<M, N>, args?: DIALOGARGS | ((n: N) => Observableable<DIALOGARGS>), dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Router<M>; | ||
routeTo<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, trigger: Predicate<M>, args?: DIALOGARGS | ((m: M) => Observableable<DIALOGARGS>), dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Router<M>; | ||
routeTo<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, args: DIALOGARGS, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Router<M>; | ||
routeTo<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, N extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, instanceTag: string, trigger: Matcher<M, N>, args?: DIALOGARGS | ((n: N) => Observableable<DIALOGARGS>), dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Router<M>; | ||
routeTo<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, instanceTag: string, trigger: Predicate<M>, args?: DIALOGARGS | ((m: M) => Observableable<DIALOGARGS>), dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Router<M>; | ||
routeTo<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, instanceTag: string, args: DIALOGARGS, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Router<M>; | ||
routeToActive<DIALOGRESPONSE extends object = {}>(dialogOrName: DialogOrName<M, any, DIALOGRESPONSE>, instanceTag: string, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Router<M>; | ||
routeTo<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, N extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, trigger: DialogTrigger<M, DIALOGARGS>, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Router<M>; | ||
routeTo<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, args?: DIALOGARGS, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Router<M>; | ||
routeTo<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, instanceTag: string, trigger: DialogTrigger<M, DIALOGARGS>, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Router<M>; | ||
routeTo<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, instanceTag: string, args?: DIALOGARGS, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Router<M>; | ||
} | ||
@@ -53,5 +52,8 @@ export interface DialogConstructor<M extends object, DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}> { | ||
} | ||
export declare type DialogTrigger<M extends object = {}, DIALOGARGS extends object = {}> = Predicate<M> | Matcher<M, DIALOGARGS>; | ||
export declare function tryDialogTrigger<M extends object = {}, DIALOGARGS extends object = {}>(trigger: DialogTrigger<M, DIALOGARGS>, message: any): Observable<DIALOGARGS>; | ||
export interface IDialog<M extends object, DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}> { | ||
constructor?: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE>; | ||
router?: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE>; | ||
trigger?: DialogTrigger<M, DIALOGARGS>; | ||
} | ||
@@ -63,6 +65,7 @@ export interface LocalDialog<M extends object, DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}> { | ||
router: DialogRouter<M, DIALOGRESPONSE, DIALOGSTATE>; | ||
trigger: DialogTrigger<M, DIALOGARGS>; | ||
} | ||
export interface RemoteDialog<M extends object, DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}> { | ||
localName: string; | ||
remoteUrl: string; | ||
localName: string; | ||
remoteName: string; | ||
@@ -72,3 +75,3 @@ } | ||
export declare type DialogOrName<M extends object = {}, DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}> = LocalOrRemoteDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE> | string; | ||
export declare const localName: (dialogOrName: DialogOrName<{}, {}, {}, {}>) => string; | ||
export declare const toLocalName: (dialogOrName: DialogOrName<{}, {}, {}, {}>) => string; | ||
export interface RootDialogInstance { | ||
@@ -92,12 +95,7 @@ get: (message: object) => Observableable<DialogInstance>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>(localName: string, dialog: IDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>(localName: string, remoteName: string, constructor: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>, routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE>): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>(localName: string, remoteable: boolean, constructor: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>, routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE>): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>(localName: string, constructor: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>, routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE>): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>(localName: string, remoteName: string, routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE>): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>(localName: string, remoteable: boolean, routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE>): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>(localName: string, routerOrHandler: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE>): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>(localName: string, remoteUrl: string): RemoteDialog<M, DIALOGARGS, DIALOGRESPONSE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}>(localName: string, remoteUrl: string, remoteName: string): RemoteDialog<M, DIALOGARGS, DIALOGRESPONSE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>(localName: string, remoteName: string, dialog: IDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
add<DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}>(localName: string, remoteable: boolean, dialog: IDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>): LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
private createDialogInstance(dialogOrName, m, dialogArgs?, dialogResponseHandler?); | ||
private getRouteFromDialogInstance(dialogInstance, m, dialogResponseHandler?, end?); | ||
private tryDialogTrigger(dialogOrName, m); | ||
setRoot<DIALOGARGS extends object = {}>(dialogOrName: DialogOrName<M, DIALOGARGS>, m: M, dialogArgs?: DIALOGARGS): Promise<DialogInstance>; | ||
@@ -104,0 +102,0 @@ routeToRoot<DIALOGARGS extends object = {}>(dialogOrName?: DialogOrName<M, DIALOGARGS>, dialogArgs?: DIALOGARGS): Router<M>; |
@@ -14,6 +14,12 @@ "use strict"; | ||
var Konsole_1 = require("./Konsole"); | ||
function tryDialogTrigger(trigger, message) { | ||
Konsole_1.konsole.log("tryDialogTrigger", trigger, message); | ||
return Router_1.toFilteredObservable(trigger(message)) | ||
.map(function (m) { return typeof m === 'boolean' ? {} : m; }); | ||
} | ||
exports.tryDialogTrigger = tryDialogTrigger; | ||
var isLocalDialog = function (localOrRemoteDialog) { | ||
return localOrRemoteDialog.router !== undefined; | ||
}; | ||
exports.localName = function (dialogOrName) { | ||
exports.toLocalName = function (dialogOrName) { | ||
return typeof dialogOrName === 'string' | ||
@@ -100,2 +106,7 @@ ? dialogOrName | ||
}; | ||
// add <DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}> ( | ||
// localName: string, | ||
// remoteUrl: string, | ||
// remoteName?: string | ||
// ): RemoteDialog<M, DIALOGARGS, DIALOGRESPONSE>; | ||
Dialogs.prototype.add = function (localName) { | ||
@@ -118,5 +129,3 @@ var args = []; | ||
var remoteName = void 0; | ||
var constructor = void 0; | ||
var routerOrHandler_1; | ||
var dialogIndex = 1; | ||
var idialog_1 = args[1]; | ||
if (typeof args[0] === 'string') { | ||
@@ -130,23 +139,10 @@ remoteName = args[0]; | ||
else { | ||
dialogIndex = 0; | ||
idialog_1 = args[0]; | ||
} | ||
if (args.length === dialogIndex + 2) { | ||
// init + router | ||
constructor = args[dialogIndex]; | ||
routerOrHandler_1 = args[dialogIndex + 1]; | ||
} | ||
else if (args[dialogIndex].router) { | ||
// IDialog | ||
constructor = args[dialogIndex].constructor; | ||
routerOrHandler_1 = args[dialogIndex].routerOrHandler; | ||
} | ||
else { | ||
// just router (use default init) | ||
routerOrHandler_1 = args[dialogIndex]; | ||
} | ||
dialog = { | ||
localName: localName, | ||
remoteName: remoteName, | ||
constructor: constructor, | ||
router: function (dialog) { return Router_1.toRouter(routerOrHandler_1(dialog)); } | ||
constructor: idialog_1.constructor || (function (dialog, m) { }), | ||
router: function (dialog) { return idialog_1.router ? Router_1.toRouter(idialog_1.router(dialog)) : Router_1.nullRouter(); }, | ||
trigger: idialog_1.trigger || (function () { return ({}); }) | ||
}; | ||
@@ -163,2 +159,3 @@ } | ||
var _this = this; | ||
if (dialogArgs === void 0) { dialogArgs = {}; } | ||
if (dialogResponseHandler === void 0) { dialogResponseHandler = function () { }; } | ||
@@ -170,7 +167,2 @@ console.log("createDialogInstance", dialogOrName, m, dialogArgs, dialogResponseHandler); | ||
if (isLocalDialog(localOrRemoteDialog)) { | ||
if (!localOrRemoteDialog.constructor) | ||
return Router_1.toObservable(this.localDialogInstances.createInstance(localOrRemoteDialog.localName, { | ||
state: {}, | ||
activeDialogs: {} | ||
})); | ||
var dialogResponse_1; | ||
@@ -290,7 +282,7 @@ var messageToRoute_1; | ||
// ) | ||
// .flatMap<RemoteTryMatchResponse, Route>(response => { | ||
// .flatMap<RemoteTryMatchResponse, Route> (response => { | ||
// if (response.status === 'error') | ||
// return Observable.throw(`RemoteDialog.tryMatch returned error "${response.error}".`); | ||
// if (response.status === 'matchless') | ||
// return Observable.empty<Route>(); | ||
// return Observable.empty<Route> (); | ||
// if (response.status !== 'match') | ||
@@ -306,3 +298,3 @@ // return Observable.throw(`RemoteDialog.tryMatch returned unexpected status "${(response as any).status}".`); | ||
// konsole.log("tryMatch local", m); | ||
// return toObservable(this.localDialogInstances.getDialogData<DIALOGSTATE>(dialogInstance)) | ||
// return toObservable(this.localDialogInstances.getDialogData<DIALOGSTATE> (dialogInstance)) | ||
// .flatMap(dialogState => | ||
@@ -313,11 +305,11 @@ // dialog.router.getRoute({ | ||
// dialogStack: [... m.dialogStack, dialogInstance], | ||
// beginChildDialog: <DIALOGARGS extends object = {}>(dialogOrName: LocalOrRemoteDialog<M, DIALOGARGS> | string, dialogArgs?: DIALOGARGS) => | ||
// beginChildDialog: <DIALOGARGS extends object = {}> (dialogOrName: LocalOrRemoteDialog<M, DIALOGARGS> | string, dialogArgs?: DIALOGARGS) => | ||
// this.activate(dialogOrName, m, dialogArgs) | ||
// .do(dialogInstance => m.dialogState.childDialogInstance = dialogInstance) | ||
// .toPromise(), | ||
// clearChildDialog: () => new Promise<void>((resolve) => { | ||
// clearChildDialog: () => new Promise<void> ((resolve) => { | ||
// dialogState.childDialogInstance = undefined; | ||
// resolve(); | ||
// }), | ||
// replaceThisDialog: <DIALOGARGS extends object = {}>(dialogOrName: LocalOrRemoteDialog<M, DIALOGARGS, DIALOGRESPONSE> | string, dialogArgs?: DIALOGARGS, dialogResponse?: DIALOGRESPONSE) => | ||
// replaceThisDialog: <DIALOGARGS extends object = {}> (dialogOrName: LocalOrRemoteDialog<M, DIALOGARGS, DIALOGRESPONSE> | string, dialogArgs?: DIALOGARGS, dialogResponse?: DIALOGRESPONSE) => | ||
// toObservable(dialogResponseHandler({ | ||
@@ -363,7 +355,7 @@ // ... m as any, | ||
// ) | ||
// .flatMap<RemoteTryMatchResponse, Route>(response => { | ||
// .flatMap<RemoteTryMatchResponse, Route> (response => { | ||
// if (response.status === 'error') | ||
// return Observable.throw(`RemoteDialog.tryMatch returned error "${response.error}".`); | ||
// if (response.status === 'matchless') | ||
// return Observable.empty<Route>(); | ||
// return Observable.empty<Route> (); | ||
// if (response.status !== 'match') | ||
@@ -377,2 +369,13 @@ // return Observable.throw(`RemoteDialog.tryMatch returned unexpected status "${(response as any).status}".`); | ||
// } | ||
Dialogs.prototype.tryDialogTrigger = function (dialogOrName, m) { | ||
console.log("tryDialogTrigger()", dialogOrName, m); | ||
var dialog = this.toLocalOrRemoteDialog(dialogOrName); | ||
if (isLocalDialog(dialog)) { | ||
return tryDialogTrigger(dialog.trigger, m); | ||
} | ||
else { | ||
// handle remote dialog here | ||
return rxjs_1.Observable.empty(); | ||
} | ||
}; | ||
// These methods are used to serve up local dialogs remotely | ||
@@ -435,3 +438,3 @@ // remoteActivate(name: string, remoteMatch: object, dialogArgs: object): Observable<RemoteActivateResponse> { | ||
// // will only be called by replaceThisDialog | ||
// new Promise<void>((resolve) => { | ||
// new Promise<void> ((resolve) => { | ||
// tasks.push({ | ||
@@ -448,3 +451,3 @@ // method: 'beginChildDialog', | ||
// // will only be called by endThisDialog | ||
// new Promise<void>((resolve) => { | ||
// new Promise<void> ((resolve) => { | ||
// tasks.push({ | ||
@@ -529,100 +532,3 @@ // method: 'clearChildDialog' | ||
if (end === void 0) { end = function () { return true; }; } | ||
console.log("creating dialogRouterHelper", dialogInstance, m, dialogState, dialogInstances, dialogResponseHandler, end); // , replace); | ||
// const routeToChild = (): IRouter<M> => ({ | ||
// getRoute: (m: M) => { | ||
// console.log("dialog.routeToChild.getRoute"); | ||
// return dialogState.child | ||
// ? this.getRouteFromDialogInstance(dialogState.child, m, undefined, | ||
// (dialogResponse) => { | ||
// dialogState.child = undefined; | ||
// if (dialogResponse !== undefined) | ||
// console.warn(`Stacked dialog ${dialogState.child.name} returned a response. It was ignored.`); | ||
// return true; | ||
// }, | ||
// (dialogOrName, args) => | ||
// this.createDialogInstance(dialogOrName, m, args) | ||
// .map(dialogInstance => { | ||
// dialogState.child = dialogInstance; | ||
// }) | ||
// .toPromise() | ||
// ) | ||
// : Observable.empty(); | ||
// } | ||
// }); | ||
var routeToActive = function (dialogOrName) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return ({ | ||
getRoute: function (m) { | ||
console.log("dialog.routeToActive().getRoute", dialogOrName); | ||
var tag = "default"; | ||
var i = 0; | ||
if (args.length && typeof args[0] === 'string') { | ||
tag = args[0]; | ||
i = 1; | ||
} | ||
var instanceName = exports.localName(dialogOrName) + '@@@' + tag; | ||
var dialogResponseHandler = args[i]; | ||
return Router_1.toFilteredObservable(dialogState.activeDialogs[instanceName]) | ||
.flatMap(function (dialogInstance) { | ||
return _this.getRouteFromDialogInstance(dialogInstance, m, dialogResponseHandler, function (dialogResponse) { | ||
delete dialogState.activeDialogs[instanceName]; | ||
return true; | ||
}); | ||
}); | ||
} | ||
}); | ||
}; | ||
var routeTo = function (dialogOrName) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return ({ | ||
getRoute: function (m) { | ||
console.log.apply(console, ["dialog.routeTo().getRoute", dialogOrName].concat(args)); | ||
var dialog = _this.toLocalOrRemoteDialog(dialogOrName); | ||
if (!dialog) | ||
return rxjs_1.Observable.empty(); | ||
var instanceTag = 'default'; | ||
var iArg = 0; | ||
if (typeof args[0] === 'string') { | ||
instanceTag = args[0]; | ||
iArg = 1; | ||
} | ||
var instanceName = dialog.localName + '@@@' + instanceTag; | ||
var dialogInstance = dialogState.activeDialogs[instanceName]; | ||
var predicateOrMatcher; | ||
if (args.length >= iArg + 1 && typeof args[iArg] === 'function') { | ||
predicateOrMatcher = args[iArg]; | ||
iArg++; | ||
} | ||
var getDialogArgs = function () { return ({}); }; | ||
if (args.length >= iArg + 1) { | ||
getDialogArgs = typeof args[iArg] === 'function' | ||
? args[iArg] | ||
: function () { return args[iArg]; }; | ||
} | ||
var dialogResponseHandler = args.length === iArg + 2 ? args[iArg + 1] : undefined; | ||
console.log("dialog.routeTo() derived args", instanceName, dialogInstance, predicateOrMatcher, getDialogArgs, dialogResponseHandler); | ||
if (dialogInstance) | ||
// dialog already active - route the message | ||
return _this.getRouteFromDialogInstance(dialogInstance, m, dialogResponseHandler, function (dialogResponse) { | ||
delete dialogState.activeDialogs[instanceName]; | ||
return true; | ||
}); | ||
return Router_1.tryMatch(predicateOrMatcher, m) | ||
.map(function (n) { return ({ | ||
action: function () { return Router_1.toObservable(getDialogArgs(n)) | ||
.do(function (dialogArgs) { return console.log("dialog.routeTo() dialog args", dialogArgs); }) | ||
.flatMap(function (dialogArgs) { return _this.createDialogInstance(dialogOrName, m, dialogArgs, dialogResponseHandler); }) | ||
.do(function (dialogInstance) { | ||
dialogState.activeDialogs[instanceName] = dialogInstance; | ||
}); } | ||
}); }); | ||
} | ||
}); | ||
}; | ||
console.log("creating dialogRouterHelper", dialogInstance, m, dialogState, dialogInstances, dialogResponseHandler, end); | ||
return { | ||
@@ -652,3 +558,2 @@ // core | ||
// return first( | ||
// routeToChild(), | ||
// ... dialogState.activeDialogs | ||
@@ -668,29 +573,2 @@ // ? Object.keys(dialogState.activeDialogs).map(name => routeToActive(name)) | ||
}, | ||
// // stack | ||
// replace: ( | ||
// dialogOrName: DialogOrName<M>, | ||
// args?: object, | ||
// ): Promise<void> => { | ||
// console.log("dialog.replace", dialogOrName, args) | ||
// return end() && replace | ||
// ? replace(dialogOrName, args) | ||
// : Promise.resolve(); | ||
// }, | ||
// setChild: (... args): Promise<void> => { | ||
// console.log("dialog.setChild", args) | ||
// if (args[0].instanceId !== undefined) { | ||
// dialogState.child = args[0]; | ||
// return Promise.resolve(); | ||
// } | ||
// return this.createDialogInstance(args[0], m, ... args.slice(1)) | ||
// .map(dialogInstance => { | ||
// dialogState.child = dialogInstance; | ||
// }) | ||
// .toPromise(); | ||
// }, | ||
// clearChild: (): void => { | ||
// console.log("dialog.clearChild") | ||
// dialogState.child = undefined; | ||
// }, | ||
// routeToChild, | ||
// activation | ||
@@ -703,18 +581,29 @@ activate: function (dialogOrName) { | ||
console.log.apply(console, ["dialog.activate", dialogOrName].concat(args)); | ||
var dialog = _this.toLocalOrRemoteDialog(dialogOrName); | ||
if (!dialog) | ||
return Promise.resolve(); | ||
var i = 0; | ||
var instanceTag = "default"; | ||
if (args.length) { | ||
if (typeof args[0] === 'string') { | ||
instanceTag = args[0]; | ||
i = 1; | ||
} | ||
} | ||
var dialogArgs; | ||
if (args.length >= i + 1) { | ||
if (typeof args[i] !== 'function') { | ||
dialogArgs = args[i]; | ||
i++; | ||
} | ||
} | ||
var dialogResponseHandler; | ||
var i = 1; | ||
if (args.length === 2) { | ||
dialogArgs = args[0]; | ||
dialogResponseHandler = args[1]; | ||
if (args.length >= i + 1) { | ||
dialogResponseHandler = args[i]; | ||
} | ||
else if (args.length === 1) { | ||
if (typeof args[0] === 'function') | ||
dialogResponseHandler = args[0]; | ||
else | ||
dialogArgs = args[0]; | ||
} | ||
var instanceName = dialog.localName + '@@@' + instanceTag; | ||
console.log("dialog.activate() derived args", instanceName, dialogArgs, dialogResponseHandler); | ||
return _this.createDialogInstance(dialogOrName, m, dialogArgs, dialogResponseHandler) | ||
.map(function (dialogInstance) { | ||
dialogState.activeDialogs[dialogInstance.name] = dialogInstance; | ||
dialogState.activeDialogs[instanceName] = dialogInstance; | ||
}) | ||
@@ -725,11 +614,91 @@ .toPromise(); | ||
console.log("dialog.deactivate", dialogOrName); | ||
delete dialogState.activeDialogs[exports.localName(dialogOrName)]; | ||
delete dialogState.activeDialogs[exports.toLocalName(dialogOrName)]; | ||
}, | ||
isActive: function (dialogOrName) { | ||
console.log("dialog.isActive", dialogOrName); | ||
return dialogState.activeDialogs[exports.localName(dialogOrName)] !== undefined; | ||
return dialogState.activeDialogs[exports.toLocalName(dialogOrName)] !== undefined; | ||
}, | ||
routeToActive: routeToActive, | ||
routeToActive: function (dialogOrName) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return ({ | ||
getRoute: function (m) { | ||
console.log("dialog.routeToActive().getRoute", dialogOrName); | ||
var dialog = _this.toLocalOrRemoteDialog(dialogOrName); | ||
if (!dialog) | ||
return rxjs_1.Observable.empty(); | ||
var instanceTag = "default"; | ||
var i = 0; | ||
if (args.length && typeof args[0] === 'string') { | ||
instanceTag = args[0]; | ||
i = 1; | ||
} | ||
var instanceName = dialog.localName + '@@@' + instanceTag; | ||
var dialogResponseHandler = args[i]; | ||
console.log("dialog.routeToActive() derived args", instanceName, dialogResponseHandler); | ||
return Router_1.toFilteredObservable(dialogState.activeDialogs[instanceName]) | ||
.flatMap(function (dialogInstance) { | ||
return _this.getRouteFromDialogInstance(dialogInstance, m, dialogResponseHandler, function (dialogResponse) { | ||
delete dialogState.activeDialogs[instanceName]; | ||
return true; | ||
}); | ||
}); | ||
} | ||
}); | ||
}, | ||
// "just works" | ||
routeTo: routeTo | ||
routeTo: function (dialogOrName) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return ({ | ||
getRoute: function (m) { | ||
console.log.apply(console, ["dialog.routeTo().getRoute", dialogOrName].concat(args)); | ||
var dialog = _this.toLocalOrRemoteDialog(dialogOrName); | ||
if (!dialog) | ||
return rxjs_1.Observable.empty(); | ||
var instanceTag = 'default'; | ||
var iArg = 0; | ||
if (typeof args[0] === 'string') { | ||
instanceTag = args[0]; | ||
iArg = 1; | ||
} | ||
var instanceName = exports.toLocalName(dialogOrName) + '@@@' + instanceTag; | ||
var dialogInstance = dialogState.activeDialogs[instanceName]; | ||
var trigger; | ||
var dialogArgs; | ||
if (args.length >= iArg + 1) { | ||
if (typeof args[iArg] === 'function') | ||
trigger = args[iArg]; | ||
else | ||
dialogArgs = args[iArg]; | ||
} | ||
var dialogResponseHandler = args.length === iArg + 2 ? args[iArg + 1] : undefined; | ||
console.log("dialog.routeTo() derived args", instanceName, dialogInstance, trigger, dialogArgs, dialogResponseHandler); | ||
if (dialogInstance) | ||
// dialog already active - route the message | ||
return _this.getRouteFromDialogInstance(dialogInstance, m, dialogResponseHandler, function (dialogResponse) { | ||
delete dialogState.activeDialogs[instanceName]; | ||
return true; | ||
}); | ||
return (dialogArgs | ||
? rxjs_1.Observable.of(dialogArgs) | ||
: trigger | ||
? tryDialogTrigger(trigger, m) | ||
: _this.tryDialogTrigger(dialog, m)) | ||
.do(function (dialogArgs) { return console.log("dialog.routeTo() dialog args", dialogArgs); }) | ||
.map(function (dialogArgs) { return ({ | ||
action: function () { | ||
return _this.createDialogInstance(dialogOrName, m, dialogArgs, dialogResponseHandler) | ||
.do(function (dialogInstance) { | ||
dialogState.activeDialogs[instanceName] = dialogInstance; | ||
}); | ||
} | ||
}); }); | ||
} | ||
}); | ||
}, | ||
}; | ||
@@ -740,52 +709,2 @@ }; | ||
exports.Dialogs = Dialogs; | ||
// Child dialogs | ||
// runChildIfActive< | ||
// ANYMATCH extends object = M, | ||
// DIALOGRESPONSE extends object = {} | ||
// >( | ||
// dialogOrName?: LocalOrRemoteDialog<M, any, DIALOGRESPONSE> | string, | ||
// dialogResponseHandler: DialogResponseHandler<ANYMATCH, DIALOGRESPONSE> = () => {} | ||
// ): IRouter<ANYMATCH> { | ||
// return { | ||
// getRoute: (message: ANYMATCH & IDialogMatch<ANYMATCH>) => { | ||
// konsole.log("runChildIfActive", message); | ||
// let odi: Observable<DialogInstance>; | ||
// if (message.dialogStack) { | ||
// if (!message.dialogState.childDialogInstance) | ||
// return; | ||
// odi = Observable.of(message.dialogState.childDialogInstance); | ||
// } else { | ||
// // This is being run from the "root" (a non-dialog router) | ||
// message = { | ||
// ... message as any, | ||
// dialogStack: [], | ||
// } | ||
// odi = toFilteredObservable(this.rootDialogInstance.get(message)); | ||
// } | ||
// konsole.log("runChildIfActive (active)", message); | ||
// return odi | ||
// .flatMap(dialogInstance => { | ||
// const dialog = this.dialogs[dialogInstance.name]; | ||
// if (!dialog) { | ||
// konsole.warn(`The stack references a dialog named "${dialogInstance.name}", which doesn't exist.`); | ||
// return Observable.empty<Route>(); | ||
// } | ||
// // if a dialog is provided, only run that one | ||
// if (dialogOrName && this.dialogize(dialogOrName) !== dialog) | ||
// return Observable.empty<Route>(); | ||
// return this.getRoute(dialog, message as any, dialogInstance, dialogResponseHandler as any); | ||
// }); | ||
// } | ||
// } as IRouter<ANYMATCH>; | ||
// } | ||
// matchRootDialog(message: M): M & IDialogRootMatch<M> { | ||
// return { | ||
// ... message as any, | ||
// beginChildDialog: <DIALOGARGS extends object = {}>(dialog: LocalOrRemoteDialog<M, DIALOGARGS> | string, dialogArgs?: DIALOGARGS) => | ||
// this.activate(dialog, message, dialogArgs) | ||
// .flatMap(dialogInstance => toObservable(this.rootDialogInstance.set(message, dialogInstance))) | ||
// .toPromise(), | ||
// clearChildDialog: () => toObservable(this.rootDialogInstance.set(message)).toPromise() | ||
// } | ||
// } | ||
//# sourceMappingURL=Dialogs.js.map |
@@ -66,5 +66,6 @@ import { Observable } from 'rxjs'; | ||
export declare function ifMatch<M extends object>(p1: Predicate<M>, p2: Predicate<M>, p3: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function branchMatch<M extends object>(predicate: (Predicate<M>), ifRouterOrHandler: RouterOrHandler<M>, elseRouterOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function branchMatch<M extends object, N extends object>(matcher: (Matcher<M, N>), ifRouterOrHandler: RouterOrHandler<N>, elseRouterOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function branchMatch<M extends object>(predicate: Predicate<M>, ifRouterOrHandler: RouterOrHandler<M>, elseRouterOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function branchMatch<M extends object, N extends object>(matcher: Matcher<M, N>, ifRouterOrHandler: RouterOrHandler<N>, elseRouterOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare const nullRouter: <M extends object = {}>() => Router<M>; | ||
export declare const throwRoute: <M extends object>() => Router<M>; | ||
export declare const catchRoute: <M extends object>(router: Router<M>) => Router<M>; |
@@ -162,2 +162,5 @@ "use strict"; | ||
exports.branchMatch = branchMatch; | ||
exports.nullRouter = function () { return ({ | ||
getRoute: function (m) { return rxjs_1.Observable.empty(); } | ||
}); }; | ||
var thrownRoute = { | ||
@@ -164,0 +167,0 @@ thrown: true, |
@@ -23,5 +23,5 @@ # Prague 101 | ||
```typescript | ||
import { IRouter } from 'prague'; | ||
import { Router } from 'prague'; | ||
let router: IRouter<Message>; | ||
let router: Router<Message>; | ||
``` | ||
@@ -28,0 +28,0 @@ |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.11.3", | ||
"version": "0.12.0", | ||
"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
231626
3138