Comparing version 0.12.2 to 0.12.3
@@ -25,3 +25,3 @@ import { Router, RouterOrHandler, Predicate, Matcher, tryMatch, toRouter, Route, Observableable, toObservable, toFilteredObservable, first, nullRouter } from './Router'; | ||
export interface DialogConstructorHelper < | ||
export interface CreateDialogHelper < | ||
M extends object = {}, | ||
@@ -140,3 +140,3 @@ DIALOGARGS extends object = {}, | ||
export interface DialogConstructor < | ||
export interface CreateDialog < | ||
M extends object, | ||
@@ -147,3 +147,3 @@ DIALOGARGS extends object = {}, | ||
> { | ||
(dialog: DialogConstructorHelper<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>, message: M): Observableable<void>; | ||
(dialog: CreateDialogHelper<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>, message: M): Observableable<void>; | ||
} | ||
@@ -187,3 +187,3 @@ | ||
> { | ||
create?: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>, | ||
create?: CreateDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>, | ||
router?: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE>, | ||
@@ -201,3 +201,3 @@ trigger?: DialogTrigger<M, DIALOGARGS>, | ||
remoteName: string; // How it is named to the outside world (might be same as localName) | ||
create: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
create: CreateDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
router: DialogRouter<M, DIALOGRESPONSE, DIALOGSTATE>; | ||
@@ -441,3 +441,3 @@ trigger: DialogTrigger<M, DIALOGARGS>; | ||
const dialogConstructorHelper: DialogConstructorHelper = { | ||
const CreateDialogHelper: CreateDialogHelper = { | ||
args: dialogArgs, | ||
@@ -453,3 +453,3 @@ state: {}, | ||
return toObservable(localOrRemoteDialog.create(dialogConstructorHelper, m)) | ||
return toObservable(localOrRemoteDialog.create(CreateDialogHelper, m)) | ||
.flatMap(_ => { | ||
@@ -464,3 +464,3 @@ console.log("createDialogInstance create response", dialogResponse, messageToRoute); | ||
return toObservable(this.localDialogInstances.createInstance(localOrRemoteDialog.localName, { | ||
state: dialogConstructorHelper.state, | ||
state: CreateDialogHelper.state, | ||
activeDialogs: {} | ||
@@ -467,0 +467,0 @@ } as DialogState)) |
@@ -19,3 +19,3 @@ import { Router, RouterOrHandler, Predicate, Matcher, Observableable } from './Router'; | ||
} | ||
export interface DialogConstructorHelper<M extends object = {}, DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}> { | ||
export interface CreateDialogHelper<M extends object = {}, DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}> { | ||
readonly args: DIALOGARGS; | ||
@@ -43,4 +43,4 @@ state: DIALOGSTATE; | ||
} | ||
export interface DialogConstructor<M extends object, DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}> { | ||
(dialog: DialogConstructorHelper<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>, message: M): Observableable<void>; | ||
export interface CreateDialog<M extends object, DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}> { | ||
(dialog: CreateDialogHelper<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>, message: M): Observableable<void>; | ||
} | ||
@@ -56,3 +56,3 @@ export interface DialogRouter<M extends object, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}> { | ||
export interface IDialog<M extends object, DIALOGARGS extends object = {}, DIALOGRESPONSE extends object = {}, DIALOGSTATE extends object = {}> { | ||
create?: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
create?: CreateDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
router?: DialogRouterOrHandler<M, DIALOGRESPONSE, DIALOGSTATE>; | ||
@@ -64,3 +64,3 @@ trigger?: DialogTrigger<M, DIALOGARGS>; | ||
remoteName: string; | ||
create: DialogConstructor<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
create: CreateDialog<M, DIALOGARGS, DIALOGRESPONSE, DIALOGSTATE>; | ||
router: DialogRouter<M, DIALOGRESPONSE, DIALOGSTATE>; | ||
@@ -67,0 +67,0 @@ trigger: DialogTrigger<M, DIALOGARGS>; |
@@ -164,3 +164,3 @@ "use strict"; | ||
var messageToRoute_1; | ||
var dialogConstructorHelper_1 = { | ||
var CreateDialogHelper_1 = { | ||
args: dialogArgs, | ||
@@ -175,3 +175,3 @@ state: {}, | ||
}; | ||
return Router_1.toObservable(localOrRemoteDialog.create(dialogConstructorHelper_1, m)) | ||
return Router_1.toObservable(localOrRemoteDialog.create(CreateDialogHelper_1, m)) | ||
.flatMap(function (_) { | ||
@@ -185,3 +185,3 @@ console.log("createDialogInstance create response", dialogResponse_1, messageToRoute_1); | ||
return Router_1.toObservable(_this.localDialogInstances.createInstance(localOrRemoteDialog.localName, { | ||
state: dialogConstructorHelper_1.state, | ||
state: CreateDialogHelper_1.state, | ||
activeDialogs: {} | ||
@@ -188,0 +188,0 @@ })) |
@@ -5,6 +5,6 @@ # Dialogs | ||
First we'll create the constructor and its types : | ||
First we'll create the constructor (which we call `create`) and its types : | ||
```typescript | ||
import { DialogConstructor } from 'prague'; | ||
import { CreateDialog } from 'prague'; | ||
@@ -21,14 +21,17 @@ interface GameState { | ||
gameConstructor: DialogConstructor<M, GameArgs, {}, GameState> = | ||
gameCreate: CreateDialog<M, GameArgs, {}, GameState> = | ||
(dialog, m) => { | ||
m.reply(`Guess a number between 1 and ${dialog.args.upperLimit}. You have ${dialog.args.numGuesses} guesses.`); | ||
const upperLimit = dialog.args.upperLimit || 50; | ||
const numGuesses = dialog.args.numGuesses || 10; | ||
m.reply(`Guess a number between 1 and ${upperLimit}. You have ${numGuesses} guesses.`); | ||
dialog.state = { | ||
num: Math.floor(Math.random() * (dialog.args.upperLimit || 50)), | ||
guesses: (dialog.args.numGuesses || 10) | ||
num: Math.floor(Math.random() * upperLimit), | ||
guesses: numGuesses | ||
} | ||
} | ||
``` | ||
The main change from our original code is we no longer look for a starting message. That's the responsibility of the calling router. We also now take arguments for the constructor (the code itself embeds default values for those arguments). | ||
The main change from our original code is we no longer look for a starting message. That's the responsibility of the calling router. We also now take arguments for `create` (the code itself embeds default values for those arguments). | ||
Now let's create the router: | ||
Now let's write the router: | ||
@@ -72,3 +75,3 @@ ```typescript | ||
dialogs.add('game', { | ||
constructor: gameConstructor, | ||
create: gameCreate, | ||
router: gameRouter | ||
@@ -93,3 +96,3 @@ }); | ||
This dialog doesn't set any initial state so it doesn't require a constructor. | ||
This dialog doesn't set any initial state so it doesn't require a `create` method. | ||
@@ -101,3 +104,3 @@ If you squint you can see most of the rest of our original game logic: | ||
`dialog.routeTo` checks to see if the named dialog is currently activated. If not, it checks the supplied condition. If that succeeds, the dialog is activated (calling its constructor). On the dialog was already activated, it routes the message to that dialog's router. If that dialog's router calls `dialog.end()` then it is deactivated, returning everything to its original state. | ||
`dialog.routeTo` checks to see if the named dialog is currently activated. If not, it checks the supplied condition. If that succeeds, the dialog is activated (calling its `create` method). On the dialog was already activated, it routes the message to that dialog's router. If that dialog's router calls `dialog.end()` then it is deactivated, returning everything to its original state. | ||
@@ -104,0 +107,0 @@ Finally we route to this dialog from our application router: |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.12.2", | ||
"version": "0.12.3", | ||
"description": "rules-based app engine", | ||
@@ -10,0 +10,0 @@ "main": "dist/prague.js", |
Sorry, the diff of this file is not supported yet
231070