Comparing version 0.10.0 to 0.10.1
@@ -97,4 +97,4 @@ import { IRouter, Handler, routerize, Route, Observableable, toObservable, toFilteredObservable } from './Rules'; | ||
activate<DIALOGARGS extends object = any> ( | ||
dialogOrName: DialogOrName<M, DIALOGARGS>, | ||
activate<DIALOGARGS extends object = any, DIALOGRESPONSE extends object = any> ( | ||
dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, | ||
args?: DIALOGARGS, | ||
@@ -104,2 +104,7 @@ dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE> | ||
activate<DIALOGARGS extends object = any, DIALOGRESPONSE extends object = any> ( | ||
dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, | ||
dialogResponseHandler: DialogResponseHandler<M, DIALOGRESPONSE> | ||
): Promise<void>; | ||
deactivate( | ||
@@ -113,4 +118,4 @@ dialogOrName: DialogOrName<M> | ||
routeTo( | ||
dialogOrName: DialogOrName<M>, | ||
routeTo <DIALOGRESPONSE extends object = any> ( | ||
dialogOrName: DialogOrName<M, DIALOGRESPONSE>, | ||
dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE> | ||
@@ -154,3 +159,3 @@ ): IRouter<M>; | ||
localName: string; | ||
remoteName?: string; // If defined, how it is named to the outside world, otherwise not exposed | ||
remoteName: string; // How it is named to the outside world (might be same as localName) | ||
constructor: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
@@ -177,6 +182,2 @@ router: DialogRouter<M, DIALOGRESPONSE, DIALOGSTATE>; | ||
export interface DialogRegistry<M extends object = any> { | ||
[name: string]: LocalOrRemoteDialog<M>; | ||
} | ||
const isLocalDialog = < | ||
@@ -204,21 +205,21 @@ M extends object = any, | ||
export interface RootDialogInstance { | ||
get: (message: any) => Observableable<DialogInstance>; | ||
set: (message: any, rootDialogInstance?: DialogInstance) => Observableable<void>; | ||
get: (message: object) => Observableable<DialogInstance>; | ||
set: (message: object, rootDialogInstance?: DialogInstance) => Observableable<void>; | ||
} | ||
export interface LocalDialogInstances { | ||
createInstance: (name: string, dialogState?: any) => Observableable<DialogInstance>, | ||
createInstance: (name: string, dialogState?: object) => Observableable<DialogInstance>, | ||
destroyInstance: (dialogInstance: DialogInstance) => Observableable<void>, | ||
getDialogState: (dialogInstance: DialogInstance) => Observableable<any>, | ||
setDialogState: (dialogInstance: DialogInstance, dialogState?: any) => Observableable<void> | ||
setDialogState: (dialogInstance: DialogInstance, dialogState?: object) => Observableable<void> | ||
} | ||
export interface DialogTask { | ||
method: string; | ||
args?: any; | ||
} | ||
// export interface DialogTask { | ||
// method: string; | ||
// args?: object; | ||
// } | ||
// export interface RemoteDialogProxy<M extends object = any> { | ||
// matchLocalToRemote?: (message: M) => Observableable<any>, | ||
// matchRemoteToLocal?: (message: any, tasks: DialogTask[]) => Observableable<M>, | ||
// matchRemoteToLocal?: (message: object, tasks: DialogTask[]) => Observableable<M>, | ||
// executeTask?: (message: M, tasks: DialogTask) => Observableable<any>, | ||
@@ -230,4 +231,4 @@ // } | ||
// name: string; | ||
// message: any; | ||
// args: any; | ||
// message: object; | ||
// args: object; | ||
// } | ||
@@ -238,3 +239,3 @@ | ||
// instance: string; | ||
// tasks: any[]; | ||
// tasks: object[]; | ||
// } | { | ||
@@ -249,3 +250,3 @@ // status: 'error'; | ||
// instance: string; | ||
// message: any; | ||
// message: object; | ||
// } | ||
@@ -269,3 +270,3 @@ | ||
const dialogInstances: { | ||
[name: string]: any[]; | ||
[name: string]: object[]; | ||
} = {}; | ||
@@ -292,4 +293,7 @@ | ||
export class Dialogs<M extends object = any> { | ||
private dialogRegistry: DialogRegistry<M> = {} | ||
private dialogRegistry: { | ||
[name: string]: LocalOrRemoteDialog<M>; | ||
} = {} | ||
constructor( | ||
@@ -380,13 +384,14 @@ private rootDialogInstance: RootDialogInstance, | ||
add() { | ||
const localName: string = arguments[0]; | ||
add( | ||
localName: string, | ||
... args | ||
) { | ||
let dialog: LocalOrRemoteDialog; | ||
if (typeof arguments[1] === 'string' && (arguments.length === 2 || (arguments.length === 3 && typeof arguments[2] === 'string'))) { | ||
if (typeof args[0] === 'string' && (args.length === 1 || (args.length === 2 && typeof args[1] === 'string'))) { | ||
// remote dialog | ||
dialog = { | ||
localName, | ||
remoteUrl: arguments[1], | ||
remoteName: (arguments.length === 3 && arguments[2]) || localName | ||
remoteUrl: args[0], | ||
remoteName: (args.length === 2 && args[1]) || localName | ||
} | ||
@@ -398,24 +403,24 @@ } else { | ||
let router; | ||
let dialogIndex = 2; | ||
let dialogIndex = 1; | ||
if (typeof arguments[1] === 'string') { | ||
remoteName = arguments[1]; | ||
} else if (typeof arguments[1] === 'boolean') { | ||
if (arguments[1] === true) | ||
if (typeof args[0] === 'string') { | ||
remoteName = args[0]; | ||
} else if (typeof args[0] === 'boolean') { | ||
if (args[0] === true) | ||
remoteName = localName; | ||
} else { | ||
dialogIndex = 1; | ||
dialogIndex = 0; | ||
} | ||
if (arguments.length === dialogIndex + 2) { | ||
if (args.length === dialogIndex + 2) { | ||
// init + router | ||
constructor = arguments[dialogIndex]; | ||
router = arguments[dialogIndex + 1]; | ||
} else if (arguments[dialogIndex].router) { | ||
constructor = args[dialogIndex]; | ||
router = args[dialogIndex + 1]; | ||
} else if (args[dialogIndex].router) { | ||
// IDialog | ||
constructor = arguments[dialogIndex].constructor; | ||
router = arguments[dialogIndex].router; | ||
constructor = args[dialogIndex].constructor; | ||
router = args[dialogIndex].router; | ||
} else { | ||
// just router (use default init) | ||
router = arguments[dialogIndex]; | ||
router = args[dialogIndex]; | ||
} | ||
@@ -445,5 +450,6 @@ | ||
dialogResponseHandler: DialogResponseHandler<M> = () => {}, | ||
end: (dialogResponse?: any) => boolean = () => true, | ||
replace?: (dialogOrName: DialogOrName, args?: any) => Promise<void> | ||
end: (dialogResponse?: object) => boolean = () => true, | ||
replace?: (dialogOrName: DialogOrName, args?: object) => Promise<void> | ||
): Observable<Route> { | ||
console.log("getRouteFromDialogInstance", dialogInstance, m, dialogResponseHandler, end, replace); | ||
@@ -489,3 +495,3 @@ // A small optimization - if the provided DialogInstance is null or undefined, no match but also no error | ||
// name: dialog.remoteName, | ||
// instance: dialogInstance.instance, | ||
// instance: dialog.InstanceId, | ||
// message | ||
@@ -566,3 +572,3 @@ // }) | ||
// name: dialog.remoteName, | ||
// instance: dialogInstance.instance, | ||
// instance: dialogInstance.instanceId, | ||
// message | ||
@@ -594,8 +600,10 @@ // }) | ||
m: M, | ||
dialogArgs?: any, | ||
dialogResponseHandler?: DialogResponseHandler<M> | ||
dialogArgs?: object, | ||
dialogResponseHandler: DialogResponseHandler<M> = () => {} | ||
): Observable<DialogInstance> { | ||
console.log("createDialogInstance"); | ||
console.log("createDialogInstance", dialogOrName, m, dialogArgs, dialogResponseHandler); | ||
const localOrRemoteDialog = this.localOrRemoteDialog(dialogOrName); | ||
if (!localOrRemoteDialog) | ||
return Observable.empty(); | ||
@@ -614,4 +622,5 @@ if (isLocalDialog(localOrRemoteDialog)) { | ||
.flatMap(_ => { | ||
console.log("dialog ended", ended); | ||
if (ended) | ||
return Observable.of(undefined); | ||
return Observable.empty(); | ||
return toObservable(this.localDialogInstances.createInstance(localOrRemoteDialog.localName, { | ||
@@ -658,3 +667,3 @@ state: dialogConstructorHelper.state, | ||
// remoteActivate(name: string, remoteMatch: any, dialogArgs: any): Observable<RemoteActivateResponse> { | ||
// remoteActivate(name: string, remoteMatch: object, dialogArgs: object): Observable<RemoteActivateResponse> { | ||
// const tasks: DialogTask[] = []; | ||
@@ -678,3 +687,3 @@ | ||
// private matchLocalToRemote(message: M & IDialogMatch): any { | ||
// private matchLocalToRemote(message: M & IDialogMatch): object { | ||
// return { | ||
@@ -686,3 +695,3 @@ // ... this.remoteDialogProxy.matchLocalToRemote(message), | ||
// private matchRemoteToLocal(message: any, tasks: DialogTask[]) { | ||
// private matchRemoteToLocal(message: object, tasks: DialogTask[]) { | ||
// return { | ||
@@ -716,3 +725,3 @@ // ... this.remoteDialogProxy.matchRemoteToLocal(message, tasks) as any, | ||
// remoteTryMatch(name: string, instance: string, remoteMatch: any): Observable<RemoteTryMatchResponse> { | ||
// remoteTryMatch(name: string, instance: string, remoteMatch: object): Observable<RemoteTryMatchResponse> { | ||
// const tasks: DialogTask[] = []; | ||
@@ -722,3 +731,3 @@ | ||
// ... this.matchRemoteToLocal(remoteMatch, tasks) as any, | ||
// beginChildDialog: (dialogOrName: DialogOrName<M>, dialogArgs?: any) => | ||
// beginChildDialog: (dialogOrName: DialogOrName<M>, dialogArgs?: object) => | ||
// // will only be called by replaceThisDialog | ||
@@ -785,2 +794,3 @@ // new Promise<void>((resolve) => { | ||
) { | ||
console.log("createRoot", dialogOrName, m); | ||
return toObservable(this.createDialogInstance(dialogOrName, m, args)) | ||
@@ -798,2 +808,3 @@ .flatMap(dialogInstance => | ||
): IRouter<M> { | ||
console.log("routeToRoot") | ||
return { | ||
@@ -803,15 +814,14 @@ getRoute: (m: M) => | ||
.flatMap(dialogInstance => { | ||
if (!dialogInstance) { | ||
if (!dialogOrName) { | ||
console.warn("You attempted to route to a root dialog, but bo root dialog has been created. You need to call dialogs.createRoot or name a dialog to create."); | ||
return Observable.empty<DialogInstance>(); | ||
} | ||
if (dialogInstance) | ||
return Observable.of(dialogInstance); | ||
return this.createRoot(dialogOrName, m, args); | ||
} | ||
if (!dialogOrName) { | ||
console.warn("You attempted to route to a root dialog, but bo root dialog has been created. You need to call dialogs.createRoot or name a dialog to create."); | ||
return Observable.empty<DialogInstance>(); | ||
} | ||
return Observable.of(dialogInstance); | ||
return this.createRoot(dialogOrName, m, args); | ||
}) | ||
.flatMap(dialogInstance => { | ||
console.log("routeToRoot", dialogInstance); | ||
console.log("routeToRoot dialogInstance", dialogInstance); | ||
return this.getRouteFromDialogInstance(dialogInstance, m, undefined, () => { | ||
@@ -825,3 +835,3 @@ console.warn("An attempt was made to end or replace the root dialog. The root dialog cannot be ended or replaced."); | ||
createDialogConstructorHelper ( | ||
private createDialogConstructorHelper ( | ||
m: M, | ||
@@ -833,9 +843,14 @@ dialogResponseHandler: DialogResponseHandler<M>, | ||
end: (dialogResponse?: any): Promise<void> => | ||
toObservable(dialogResponseHandler(dialogResponse)) | ||
.toPromise(), | ||
end: (dialogResponse: object = {}): Promise<void> => { | ||
console.log("ending dialog with response", dialogResponse, dialogResponseHandler); | ||
return toObservable(dialogResponseHandler({ | ||
... m as any, | ||
dialogResponse | ||
})) | ||
.toPromise(); | ||
} | ||
} | ||
} | ||
createDialogRouterHelper ( | ||
private createDialogRouterHelper ( | ||
dialogInstance: DialogInstance, | ||
@@ -845,4 +860,4 @@ m: M, | ||
dialogResponseHandler: DialogResponseHandler<M>, | ||
end: (dialogResponse?: any) => boolean = () => true, | ||
replace?: (dialogOrName: DialogOrName, args?: any) => Promise<void> | ||
end: (dialogResponse?: object) => boolean = () => true, | ||
replace?: (dialogOrName: DialogOrName, args?: object) => Promise<void> | ||
): DialogRouterHelper { | ||
@@ -859,3 +874,3 @@ console.log("creating dialogRouterHelper", dialogInstance, dialogState, dialogInstances); | ||
dialogOrName: DialogOrName, | ||
dialogArgs?: any, | ||
dialogArgs?: object, | ||
dialogResponseHandler?: DialogResponseHandler | ||
@@ -865,24 +880,26 @@ ): Promise<DialogInstance> => { | ||
return this.createDialogInstance(dialogOrName, m, dialogArgs, dialogResponseHandler) | ||
.do(di => console.log("createInstance returning", di)) | ||
.toPromise(); | ||
}, | ||
routeToInstance: (... args): IRouter<M> => { | ||
console.log("dialog.routeToInstance", args) | ||
const i = args[0].instance !== undefined ? 0 : 1; | ||
return { | ||
getRoute: (m: M) => | ||
this.getRouteFromDialogInstance(args[i], m, args[i + 1]) | ||
routeToInstance: (... args): IRouter<M> => ({ | ||
getRoute: (m: M) => { | ||
console.log("dialog.routeToInstance.getRoute", ... args) | ||
const i = args[0].instanceId !== undefined ? 0 : 1; | ||
return this.getRouteFromDialogInstance(args[i], m, args[i + 1]); | ||
} | ||
}, | ||
}), | ||
end: ( | ||
dialogResponse?: any | ||
dialogResponse: object = {} | ||
): Promise<void> => { | ||
console.log("dialog.end") | ||
return (end(dialogResponse) | ||
? toObservable(dialogResponseHandler(dialogResponse)) | ||
return end(dialogResponse) | ||
? toObservable(dialogResponseHandler({ | ||
... m as any, | ||
dialogResponse | ||
})) | ||
// .flatMap(_ => toObservable(this.destroyDialogInstance(dialogInstance))) | ||
: Observable.of<void>() | ||
) | ||
.toPromise() | ||
.toPromise() | ||
: Promise.resolve(); | ||
}, | ||
@@ -894,8 +911,8 @@ | ||
dialogOrName: DialogOrName<M>, | ||
args?: any, | ||
args?: object, | ||
): Promise<void> => { | ||
console.log("dialog.replace", args) | ||
console.log("dialog.replace", dialogOrName, args) | ||
return end() && replace | ||
? replace(dialogOrName, args) | ||
: new Promise(resolve => resolve()) | ||
: Promise.resolve(); | ||
}, | ||
@@ -905,7 +922,6 @@ | ||
console.log("dialog.setChild", args) | ||
if (args.length === 1 && args[0].instance !== undefined) | ||
return new Promise((resolve) => { | ||
dialogState.child = args[0]; | ||
resolve(); | ||
}); | ||
if (args[0].instanceId !== undefined) { | ||
dialogState.child = args[0]; | ||
return Promise.resolve(); | ||
} | ||
@@ -924,6 +940,6 @@ return this.createDialogInstance(args[0], m, ... args.slice(1)) | ||
routeToChild: (): IRouter<M> => { | ||
console.log("dialog.routeToChild") | ||
return { | ||
getRoute: (m: M) => dialogState.child | ||
routeToChild: (): IRouter<M> => ({ | ||
getRoute: (m: M) => { | ||
console.log("dialog.routeToChild.getRoute"); | ||
return dialogState.child | ||
? this.getRouteFromDialogInstance(dialogState.child, m, undefined, | ||
@@ -943,5 +959,5 @@ (dialogResponse) => { | ||
) | ||
: Observable.empty() | ||
: Observable.empty(); | ||
} | ||
}, | ||
}), | ||
@@ -952,6 +968,20 @@ // activation | ||
dialogOrName: DialogOrName<M>, | ||
dialogArgs?: any, | ||
dialogResponseHandler?: DialogResponseHandler<M> | ||
... args | ||
): Promise<void> => { | ||
console.log("dialog.activate", dialogOrName) | ||
console.log("dialog.activate", dialogOrName, ... args) | ||
let dialogArgs; | ||
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]; | ||
} | ||
return this.createDialogInstance(dialogOrName, m, dialogArgs, dialogResponseHandler) | ||
@@ -978,3 +1008,3 @@ .map(dialogInstance => { | ||
console.log("dialog.isActive", dialogOrName) | ||
return dialogState.activeDialogs && dialogState.activeDialogs[nameize(dialogOrName)] !== undefined; | ||
return dialogState.activeDialogs !== undefined && dialogState.activeDialogs[nameize(dialogOrName)] !== undefined; | ||
}, | ||
@@ -985,8 +1015,8 @@ | ||
dialogResponseHandler?: DialogResponseHandler<M> | ||
): IRouter<M> => { | ||
console.log("dialog.routeTo", dialogOrName) | ||
const name = nameize(dialogOrName); | ||
return { | ||
getRoute: (m: M) => dialogState.activeDialogs && dialogState.activeDialogs[name] | ||
? this.getRouteFromDialogInstance(dialogState.activeDialogs[name], m, undefined, (dialogResponse) => { | ||
): IRouter<M> => ({ | ||
getRoute: (m: M) => { | ||
console.log("dialog.routeTo().getRoute", dialogOrName); | ||
const name = nameize(dialogOrName); | ||
return dialogState.activeDialogs && dialogState.activeDialogs[name] | ||
? this.getRouteFromDialogInstance(dialogState.activeDialogs[name], m, dialogResponseHandler, (dialogResponse) => { | ||
if (dialogState.activeDialogs) | ||
@@ -996,5 +1026,5 @@ dialogState.activeDialogs[name] = undefined; | ||
}) | ||
: Observable.empty() | ||
: Observable.empty(); | ||
} | ||
} | ||
}) | ||
@@ -1068,113 +1098,2 @@ } | ||
// import { first, ifMatch } from './Rules'; | ||
// import { ifMatchRE, matchRE } from './RegExp'; | ||
// interface GameBot { | ||
// text: string; | ||
// reply (text: string): void; | ||
// } | ||
// interface GameArgs { | ||
// foo: string; | ||
// } | ||
// interface GameState { | ||
// score: number; | ||
// } | ||
// interface GameResponse { | ||
// score: number | ||
// } | ||
// const dialogs = new Dialogs<GameBot>({}, {}); | ||
// const not = (promise: Promise<boolean>): Promise<boolean> => promise.then(f => !f); | ||
// let di: DialogInstance; | ||
// const aDialog = dialogs.add<{ cat: string }>( | ||
// 'adialog', | ||
// (dialog, m, args) => ({ foo: args.cat }), | ||
// (dialog) => m => m.reply("hi there") | ||
// ) | ||
// const stockPrompt = dialogs.add ( | ||
// 'stock', | ||
// (dialog, m, args) => { | ||
// m.reply("What stock would you like to check out?"); | ||
// }, | ||
// (dialog) => first( | ||
// m => m.reply(`The stock price for ${m.text} is $97.60`), | ||
// m => dialog.createInstance(aDialog, { canaery: 5 }), | ||
// ) | ||
// ) | ||
// const stockReply = m => m.reply(`The stock price for ${m.dialogResponse} is $97.60`); | ||
// const gameDialog = dialogs.add <GameArgs, GameResponse, GameState> ( | ||
// 'game', | ||
// (dialog, message, args) => { | ||
// return dialog.endThisDialog(); | ||
// // return { dialogState: { score: 0 } }; | ||
// }, | ||
// (dialog) => first( | ||
// dialog.routeToChild(), | ||
// dialog.routeTo(stockPrompt, stockReply), | ||
// ifMatchRE(/stock/, m => dialog.activate(stockPrompt, {}, stockReply)) | ||
// ) | ||
// ) | ||
// const foo: IRouter<GameBot> = first( | ||
// dialogs.routeToRoot(gameDialog, { bar: "cat" }), | ||
// ) | ||
// // BIG PROBLEM | ||
// // If stacked dialogs don't return values - and they can't -- then how do we do prompts? | ||
// // Like, how do prompts "return" a value to the dialog without setting state? Or is that the answer? | ||
// // A QUESTION | ||
// // What do we pass to init()? It seems like we want it to have "dialog.end" but is there anything else it could reasonable need? | ||
// interface ICascade { | ||
// next: () => void; | ||
// } | ||
// const cascade = ( | ||
// init: DialogConstructor, | ||
// functions: (dialog: DialogRouterHelper) => (cascade: ICascade) => any[] | ||
// ) => { | ||
// } | ||
// dialogs.add(cascade( | ||
// (dialog, m, args) => {}, | ||
// (dialog) => (cascade) => [ | ||
// m => m.reply("How ya doing?"), | ||
// m => {}, | ||
// m => {} | ||
// ] | ||
// )) | ||
// // MY CURRENT IDEA | ||
// // The dialog response is structured: | ||
// interface DialogResponse<DIALOGRESPONSE> { | ||
// replace: { | ||
// dialogOrName: DialogOrName; | ||
// args: any | ||
// }; | ||
// response: DIALOGRESPONSE; | ||
// } | ||
// // How does the core "routeToInstance" function stop? | ||
// // It has to provide a handler, otherwise it doesn't stop` |
@@ -34,6 +34,7 @@ import { IRouter, Observableable } from './Rules'; | ||
routeToChild(): IRouter<M>; | ||
activate<DIALOGARGS extends object = any>(dialogOrName: DialogOrName<M, DIALOGARGS>, args?: DIALOGARGS, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Promise<void>; | ||
activate<DIALOGARGS extends object = any, DIALOGRESPONSE extends object = any>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, args?: DIALOGARGS, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): Promise<void>; | ||
activate<DIALOGARGS extends object = any, DIALOGRESPONSE extends object = any>(dialogOrName: DialogOrName<M, DIALOGARGS, DIALOGRESPONSE>, dialogResponseHandler: DialogResponseHandler<M, DIALOGRESPONSE>): Promise<void>; | ||
deactivate(dialogOrName: DialogOrName<M>): void; | ||
isActive(dialogOrName: DialogOrName<M>): boolean; | ||
routeTo(dialogOrName: DialogOrName<M>, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): IRouter<M>; | ||
routeTo<DIALOGRESPONSE extends object = any>(dialogOrName: DialogOrName<M, DIALOGRESPONSE>, dialogResponseHandler?: DialogResponseHandler<M, DIALOGRESPONSE>): IRouter<M>; | ||
} | ||
@@ -52,3 +53,3 @@ export interface DialogConstructor<M extends object = any, DIALOGARGS extends object = any, DIALOGRESPONSE extends object = any, DIALOGSTATE extends object = any> { | ||
localName: string; | ||
remoteName?: string; | ||
remoteName: string; | ||
constructor: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
@@ -63,21 +64,14 @@ router: DialogRouter<M, DIALOGRESPONSE, DIALOGSTATE>; | ||
export declare type LocalOrRemoteDialog<M extends object = any, DIALOGARGS extends object = any, DIALOGRESPONSE extends object = any, DIALOGSTATE extends object = any> = LocalDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE> | RemoteDialog<M, DIALOGARGS, DIALOGRESPONSE>; | ||
export interface DialogRegistry<M extends object = any> { | ||
[name: string]: LocalOrRemoteDialog<M>; | ||
} | ||
export declare type DialogOrName<M extends object = any, DIALOGARGS extends object = any, DIALOGRESPONSE extends object = any, DIALOGSTATE extends object = any> = LocalOrRemoteDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE> | string; | ||
export declare const nameize: (dialogOrName: DialogOrName<any, any, any, any>) => string; | ||
export interface RootDialogInstance { | ||
get: (message: any) => Observableable<DialogInstance>; | ||
set: (message: any, rootDialogInstance?: DialogInstance) => Observableable<void>; | ||
get: (message: object) => Observableable<DialogInstance>; | ||
set: (message: object, rootDialogInstance?: DialogInstance) => Observableable<void>; | ||
} | ||
export interface LocalDialogInstances { | ||
createInstance: (name: string, dialogState?: any) => Observableable<DialogInstance>; | ||
createInstance: (name: string, dialogState?: object) => Observableable<DialogInstance>; | ||
destroyInstance: (dialogInstance: DialogInstance) => Observableable<void>; | ||
getDialogState: (dialogInstance: DialogInstance) => Observableable<any>; | ||
setDialogState: (dialogInstance: DialogInstance, dialogState?: any) => Observableable<void>; | ||
setDialogState: (dialogInstance: DialogInstance, dialogState?: object) => Observableable<void>; | ||
} | ||
export interface DialogTask { | ||
method: string; | ||
args?: any; | ||
} | ||
export declare const inMemoryDialogInstances: LocalDialogInstances; | ||
@@ -103,4 +97,4 @@ export declare class Dialogs<M extends object = any> { | ||
routeToRoot<DIALOGARGS extends object = any>(dialogOrName?: DialogOrName<M, DIALOGARGS>, args?: DIALOGARGS): IRouter<M>; | ||
createDialogConstructorHelper(m: M, dialogResponseHandler: DialogResponseHandler<M>): DialogConstructorHelper; | ||
createDialogRouterHelper(dialogInstance: DialogInstance, m: M, dialogState: DialogState, dialogResponseHandler: DialogResponseHandler<M>, end?: (dialogResponse?: any) => boolean, replace?: (dialogOrName: DialogOrName, args?: any) => Promise<void>): DialogRouterHelper; | ||
private createDialogConstructorHelper(m, dialogResponseHandler); | ||
private createDialogRouterHelper(dialogInstance, m, dialogState, dialogResponseHandler, end?, replace?); | ||
} |
@@ -22,5 +22,9 @@ "use strict"; | ||
}; | ||
// export interface DialogTask { | ||
// method: string; | ||
// args?: object; | ||
// } | ||
// export interface RemoteDialogProxy<M extends object = any> { | ||
// matchLocalToRemote?: (message: M) => Observableable<any>, | ||
// matchRemoteToLocal?: (message: any, tasks: DialogTask[]) => Observableable<M>, | ||
// matchRemoteToLocal?: (message: object, tasks: DialogTask[]) => Observableable<M>, | ||
// executeTask?: (message: M, tasks: DialogTask) => Observableable<any>, | ||
@@ -31,4 +35,4 @@ // } | ||
// name: string; | ||
// message: any; | ||
// args: any; | ||
// message: object; | ||
// args: object; | ||
// } | ||
@@ -38,3 +42,3 @@ // export type RemoteActivateResponse = { | ||
// instance: string; | ||
// tasks: any[]; | ||
// tasks: object[]; | ||
// } | { | ||
@@ -48,3 +52,3 @@ // status: 'error'; | ||
// instance: string; | ||
// message: any; | ||
// message: object; | ||
// } | ||
@@ -97,11 +101,14 @@ // export type RemoteTryMatchResponse = { | ||
}; | ||
Dialogs.prototype.add = function () { | ||
var localName = arguments[0]; | ||
Dialogs.prototype.add = function (localName) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
var dialog; | ||
if (typeof arguments[1] === 'string' && (arguments.length === 2 || (arguments.length === 3 && typeof arguments[2] === 'string'))) { | ||
if (typeof args[0] === 'string' && (args.length === 1 || (args.length === 2 && typeof args[1] === 'string'))) { | ||
// remote dialog | ||
dialog = { | ||
localName: localName, | ||
remoteUrl: arguments[1], | ||
remoteName: (arguments.length === 3 && arguments[2]) || localName | ||
remoteUrl: args[0], | ||
remoteName: (args.length === 2 && args[1]) || localName | ||
}; | ||
@@ -114,26 +121,26 @@ } | ||
var router = void 0; | ||
var dialogIndex = 2; | ||
if (typeof arguments[1] === 'string') { | ||
remoteName = arguments[1]; | ||
var dialogIndex = 1; | ||
if (typeof args[0] === 'string') { | ||
remoteName = args[0]; | ||
} | ||
else if (typeof arguments[1] === 'boolean') { | ||
if (arguments[1] === true) | ||
else if (typeof args[0] === 'boolean') { | ||
if (args[0] === true) | ||
remoteName = localName; | ||
} | ||
else { | ||
dialogIndex = 1; | ||
dialogIndex = 0; | ||
} | ||
if (arguments.length === dialogIndex + 2) { | ||
if (args.length === dialogIndex + 2) { | ||
// init + router | ||
constructor = arguments[dialogIndex]; | ||
router = arguments[dialogIndex + 1]; | ||
constructor = args[dialogIndex]; | ||
router = args[dialogIndex + 1]; | ||
} | ||
else if (arguments[dialogIndex].router) { | ||
else if (args[dialogIndex].router) { | ||
// IDialog | ||
constructor = arguments[dialogIndex].constructor; | ||
router = arguments[dialogIndex].router; | ||
constructor = args[dialogIndex].constructor; | ||
router = args[dialogIndex].router; | ||
} | ||
else { | ||
// just router (use default init) | ||
router = arguments[dialogIndex]; | ||
router = args[dialogIndex]; | ||
} | ||
@@ -158,2 +165,3 @@ dialog = { | ||
if (end === void 0) { end = function () { return true; }; } | ||
console.log("getRouteFromDialogInstance", dialogInstance, m, dialogResponseHandler, end, replace); | ||
// A small optimization - if the provided DialogInstance is null or undefined, no match but also no error | ||
@@ -193,3 +201,3 @@ // This allows the developer to use a variable initialized to undefined | ||
// name: dialog.remoteName, | ||
// instance: dialogInstance.instance, | ||
// instance: dialog.InstanceId, | ||
// message | ||
@@ -264,3 +272,3 @@ // }) | ||
// name: dialog.remoteName, | ||
// instance: dialogInstance.instance, | ||
// instance: dialogInstance.instanceId, | ||
// message | ||
@@ -287,4 +295,7 @@ // }) | ||
var _this = this; | ||
console.log("createDialogInstance"); | ||
if (dialogResponseHandler === void 0) { dialogResponseHandler = function () { }; } | ||
console.log("createDialogInstance", dialogOrName, m, dialogArgs, dialogResponseHandler); | ||
var localOrRemoteDialog = this.localOrRemoteDialog(dialogOrName); | ||
if (!localOrRemoteDialog) | ||
return rxjs_1.Observable.empty(); | ||
if (isLocalDialog(localOrRemoteDialog)) { | ||
@@ -301,4 +312,5 @@ if (!localOrRemoteDialog.constructor) | ||
.flatMap(function (_) { | ||
console.log("dialog ended", ended_1); | ||
if (ended_1) | ||
return rxjs_1.Observable.of(undefined); | ||
return rxjs_1.Observable.empty(); | ||
return Rules_1.toObservable(_this.localDialogInstances.createInstance(localOrRemoteDialog.localName, { | ||
@@ -341,3 +353,3 @@ state: dialogConstructorHelper_1.state, | ||
// These methods are used to serve up local dialogs remotely | ||
// remoteActivate(name: string, remoteMatch: any, dialogArgs: any): Observable<RemoteActivateResponse> { | ||
// remoteActivate(name: string, remoteMatch: object, dialogArgs: object): Observable<RemoteActivateResponse> { | ||
// const tasks: DialogTask[] = []; | ||
@@ -359,3 +371,3 @@ // return this.createDialogInstance( | ||
// } | ||
// private matchLocalToRemote(message: M & IDialogMatch): any { | ||
// private matchLocalToRemote(message: M & IDialogMatch): object { | ||
// return { | ||
@@ -366,3 +378,3 @@ // ... this.remoteDialogProxy.matchLocalToRemote(message), | ||
// } | ||
// private matchRemoteToLocal(message: any, tasks: DialogTask[]) { | ||
// private matchRemoteToLocal(message: object, tasks: DialogTask[]) { | ||
// return { | ||
@@ -394,7 +406,7 @@ // ... this.remoteDialogProxy.matchRemoteToLocal(message, tasks) as any, | ||
// } | ||
// remoteTryMatch(name: string, instance: string, remoteMatch: any): Observable<RemoteTryMatchResponse> { | ||
// remoteTryMatch(name: string, instance: string, remoteMatch: object): Observable<RemoteTryMatchResponse> { | ||
// const tasks: DialogTask[] = []; | ||
// const message: M & IDialogMatch = { | ||
// ... this.matchRemoteToLocal(remoteMatch, tasks) as any, | ||
// beginChildDialog: (dialogOrName: DialogOrName<M>, dialogArgs?: any) => | ||
// beginChildDialog: (dialogOrName: DialogOrName<M>, dialogArgs?: object) => | ||
// // will only be called by replaceThisDialog | ||
@@ -453,2 +465,3 @@ // new Promise<void>((resolve) => { | ||
var _this = this; | ||
console.log("createRoot", dialogOrName, m); | ||
return Rules_1.toObservable(this.createDialogInstance(dialogOrName, m, args)) | ||
@@ -463,2 +476,3 @@ .flatMap(function (dialogInstance) { | ||
var _this = this; | ||
console.log("routeToRoot"); | ||
return { | ||
@@ -468,13 +482,12 @@ getRoute: function (m) { | ||
.flatMap(function (dialogInstance) { | ||
if (!dialogInstance) { | ||
if (!dialogOrName) { | ||
console.warn("You attempted to route to a root dialog, but bo root dialog has been created. You need to call dialogs.createRoot or name a dialog to create."); | ||
return rxjs_1.Observable.empty(); | ||
} | ||
return _this.createRoot(dialogOrName, m, args); | ||
if (dialogInstance) | ||
return rxjs_1.Observable.of(dialogInstance); | ||
if (!dialogOrName) { | ||
console.warn("You attempted to route to a root dialog, but bo root dialog has been created. You need to call dialogs.createRoot or name a dialog to create."); | ||
return rxjs_1.Observable.empty(); | ||
} | ||
return rxjs_1.Observable.of(dialogInstance); | ||
return _this.createRoot(dialogOrName, m, args); | ||
}) | ||
.flatMap(function (dialogInstance) { | ||
console.log("routeToRoot", dialogInstance); | ||
console.log("routeToRoot dialogInstance", dialogInstance); | ||
return _this.getRouteFromDialogInstance(dialogInstance, m, undefined, function () { | ||
@@ -492,5 +505,7 @@ console.warn("An attempt was made to end or replace the root dialog. The root dialog cannot be ended or replaced."); | ||
end: function (dialogResponse) { | ||
return Rules_1.toObservable(dialogResponseHandler(dialogResponse)) | ||
if (dialogResponse === void 0) { dialogResponse = {}; } | ||
console.log("ending dialog with response", dialogResponse, dialogResponseHandler); | ||
return Rules_1.toObservable(dialogResponseHandler(__assign({}, m, { dialogResponse: dialogResponse }))) | ||
.toPromise(); | ||
}, | ||
} | ||
}; | ||
@@ -508,2 +523,3 @@ }; | ||
return _this.createDialogInstance(dialogOrName, m, dialogArgs, dialogResponseHandler) | ||
.do(function (di) { return console.log("createInstance returning", di); }) | ||
.toPromise(); | ||
@@ -516,23 +532,24 @@ }, | ||
} | ||
console.log("dialog.routeToInstance", args); | ||
var i = args[0].instance !== undefined ? 0 : 1; | ||
return { | ||
return ({ | ||
getRoute: function (m) { | ||
console.log.apply(console, ["dialog.routeToInstance.getRoute"].concat(args)); | ||
var i = args[0].instanceId !== undefined ? 0 : 1; | ||
return _this.getRouteFromDialogInstance(args[i], m, args[i + 1]); | ||
} | ||
}; | ||
}); | ||
}, | ||
end: function (dialogResponse) { | ||
if (dialogResponse === void 0) { dialogResponse = {}; } | ||
console.log("dialog.end"); | ||
return (end(dialogResponse) | ||
? Rules_1.toObservable(dialogResponseHandler(dialogResponse)) | ||
: rxjs_1.Observable.of()) | ||
.toPromise(); | ||
return end(dialogResponse) | ||
? Rules_1.toObservable(dialogResponseHandler(__assign({}, m, { dialogResponse: dialogResponse }))) | ||
.toPromise() | ||
: Promise.resolve(); | ||
}, | ||
// stack | ||
replace: function (dialogOrName, args) { | ||
console.log("dialog.replace", args); | ||
console.log("dialog.replace", dialogOrName, args); | ||
return end() && replace | ||
? replace(dialogOrName, args) | ||
: new Promise(function (resolve) { return resolve(); }); | ||
: Promise.resolve(); | ||
}, | ||
@@ -545,7 +562,6 @@ setChild: function () { | ||
console.log("dialog.setChild", args); | ||
if (args.length === 1 && args[0].instance !== undefined) | ||
return new Promise(function (resolve) { | ||
dialogState.child = args[0]; | ||
resolve(); | ||
}); | ||
if (args[0].instanceId !== undefined) { | ||
dialogState.child = args[0]; | ||
return Promise.resolve(); | ||
} | ||
return _this.createDialogInstance.apply(_this, [args[0], m].concat(args.slice(1))).map(function (dialogInstance) { | ||
@@ -560,6 +576,6 @@ dialogState.child = dialogInstance; | ||
}, | ||
routeToChild: function () { | ||
console.log("dialog.routeToChild"); | ||
return { | ||
getRoute: function (m) { return dialogState.child | ||
routeToChild: function () { return ({ | ||
getRoute: function (m) { | ||
console.log("dialog.routeToChild.getRoute"); | ||
return dialogState.child | ||
? _this.getRouteFromDialogInstance(dialogState.child, m, undefined, function (dialogResponse) { | ||
@@ -577,8 +593,25 @@ dialogState.child = undefined; | ||
}) | ||
: rxjs_1.Observable.empty(); } | ||
}; | ||
}, | ||
: rxjs_1.Observable.empty(); | ||
} | ||
}); }, | ||
// activation | ||
activate: function (dialogOrName, dialogArgs, dialogResponseHandler) { | ||
console.log("dialog.activate", dialogOrName); | ||
activate: function (dialogOrName) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
console.log.apply(console, ["dialog.activate", dialogOrName].concat(args)); | ||
var dialogArgs; | ||
var dialogResponseHandler; | ||
var 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]; | ||
} | ||
return _this.createDialogInstance(dialogOrName, m, dialogArgs, dialogResponseHandler) | ||
@@ -599,10 +632,10 @@ .map(function (dialogInstance) { | ||
console.log("dialog.isActive", dialogOrName); | ||
return dialogState.activeDialogs && dialogState.activeDialogs[exports.nameize(dialogOrName)] !== undefined; | ||
return dialogState.activeDialogs !== undefined && dialogState.activeDialogs[exports.nameize(dialogOrName)] !== undefined; | ||
}, | ||
routeTo: function (dialogOrName, dialogResponseHandler) { | ||
console.log("dialog.routeTo", dialogOrName); | ||
var name = exports.nameize(dialogOrName); | ||
return { | ||
getRoute: function (m) { return dialogState.activeDialogs && dialogState.activeDialogs[name] | ||
? _this.getRouteFromDialogInstance(dialogState.activeDialogs[name], m, undefined, function (dialogResponse) { | ||
routeTo: function (dialogOrName, dialogResponseHandler) { return ({ | ||
getRoute: function (m) { | ||
console.log("dialog.routeTo().getRoute", dialogOrName); | ||
var name = exports.nameize(dialogOrName); | ||
return dialogState.activeDialogs && dialogState.activeDialogs[name] | ||
? _this.getRouteFromDialogInstance(dialogState.activeDialogs[name], m, dialogResponseHandler, function (dialogResponse) { | ||
if (dialogState.activeDialogs) | ||
@@ -612,5 +645,5 @@ dialogState.activeDialogs[name] = undefined; | ||
}) | ||
: rxjs_1.Observable.empty(); } | ||
}; | ||
} | ||
: rxjs_1.Observable.empty(); | ||
} | ||
}); } | ||
}; | ||
@@ -671,83 +704,2 @@ }; | ||
// } | ||
// import { first, ifMatch } from './Rules'; | ||
// import { ifMatchRE, matchRE } from './RegExp'; | ||
// interface GameBot { | ||
// text: string; | ||
// reply (text: string): void; | ||
// } | ||
// interface GameArgs { | ||
// foo: string; | ||
// } | ||
// interface GameState { | ||
// score: number; | ||
// } | ||
// interface GameResponse { | ||
// score: number | ||
// } | ||
// const dialogs = new Dialogs<GameBot>({}, {}); | ||
// const not = (promise: Promise<boolean>): Promise<boolean> => promise.then(f => !f); | ||
// let di: DialogInstance; | ||
// const aDialog = dialogs.add<{ cat: string }>( | ||
// 'adialog', | ||
// (dialog, m, args) => ({ foo: args.cat }), | ||
// (dialog) => m => m.reply("hi there") | ||
// ) | ||
// const stockPrompt = dialogs.add ( | ||
// 'stock', | ||
// (dialog, m, args) => { | ||
// m.reply("What stock would you like to check out?"); | ||
// }, | ||
// (dialog) => first( | ||
// m => m.reply(`The stock price for ${m.text} is $97.60`), | ||
// m => dialog.createInstance(aDialog, { canaery: 5 }), | ||
// ) | ||
// ) | ||
// const stockReply = m => m.reply(`The stock price for ${m.dialogResponse} is $97.60`); | ||
// const gameDialog = dialogs.add <GameArgs, GameResponse, GameState> ( | ||
// 'game', | ||
// (dialog, message, args) => { | ||
// return dialog.endThisDialog(); | ||
// // return { dialogState: { score: 0 } }; | ||
// }, | ||
// (dialog) => first( | ||
// dialog.routeToChild(), | ||
// dialog.routeTo(stockPrompt, stockReply), | ||
// ifMatchRE(/stock/, m => dialog.activate(stockPrompt, {}, stockReply)) | ||
// ) | ||
// ) | ||
// const foo: IRouter<GameBot> = first( | ||
// dialogs.routeToRoot(gameDialog, { bar: "cat" }), | ||
// ) | ||
// // BIG PROBLEM | ||
// // If stacked dialogs don't return values - and they can't -- then how do we do prompts? | ||
// // Like, how do prompts "return" a value to the dialog without setting state? Or is that the answer? | ||
// // A QUESTION | ||
// // What do we pass to init()? It seems like we want it to have "dialog.end" but is there anything else it could reasonable need? | ||
// interface ICascade { | ||
// next: () => void; | ||
// } | ||
// const cascade = ( | ||
// init: DialogConstructor, | ||
// functions: (dialog: DialogRouterHelper) => (cascade: ICascade) => any[] | ||
// ) => { | ||
// } | ||
// dialogs.add(cascade( | ||
// (dialog, m, args) => {}, | ||
// (dialog) => (cascade) => [ | ||
// m => m.reply("How ya doing?"), | ||
// m => {}, | ||
// m => {} | ||
// ] | ||
// )) | ||
// // MY CURRENT IDEA | ||
// // The dialog response is structured: | ||
// interface DialogResponse<DIALOGRESPONSE> { | ||
// replace: { | ||
// dialogOrName: DialogOrName; | ||
// args: any | ||
// }; | ||
// response: DIALOGRESPONSE; | ||
// } | ||
// // How does the core "routeToInstance" function stop? | ||
// // It has to provide a handler, otherwise it doesn't stop` | ||
//# sourceMappingURL=Dialogs.js.map |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.10.0", | ||
"version": "0.10.1", | ||
"description": "rules-based app engine", | ||
@@ -10,0 +10,0 @@ "main": "dist/prague.js", |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
49
1
191445
2667