Skip to content

Commit

Permalink
add load session function that does not dispatch (microsoft#3396)
Browse files Browse the repository at this point in the history
  • Loading branch information
microsoftly authored and Stevenic committed Sep 21, 2017
1 parent 24c3ef1 commit 7eecd33
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions Node/core/src/bots/UniversalBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,16 @@ export class UniversalBot extends Library {

/** Loads a session object for an arbitrary address. */
public loadSession(address: IAddress, done: (err: Error, session: Session) => void): void {
this.loadSessionWithOptionalDispatch(address, false, done);
}

public loadSessionWithoutDispatching(address: IAddress, done: (err: Error, session: Session) => void): void {
this.loadSessionWithOptionalDispatch(address, false, done);
}

private loadSessionWithOptionalDispatch(address: IAddress, shouldDispatch: boolean, done: (err: Error, session: Session) => void): void {
const newStack = false;

this.lookupUser(address, (user) => {
var msg = <IMessage>{
type: consts.messageType,
Expand All @@ -355,11 +365,10 @@ export class UniversalBot extends Library {
persistUserData: this.settings.persistUserData,
persistConversationData: this.settings.persistConversationData
};
this.createSession(storageCtx, msg, this.settings.defaultDialogId || '/', this.settings.defaultDialogArgs, done);
this.createSession(storageCtx, msg, this.settings.defaultDialogId || '/', this.settings.defaultDialogArgs, done, newStack, shouldDispatch);
}, this.errorLogger(<any>done));
}, this.errorLogger(<any>done));
}

//-------------------------------------------------------------------------
// Helpers
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -407,7 +416,7 @@ export class UniversalBot extends Library {
}, newStack);
}

private createSession(storageCtx: IBotStorageContext, message: IMessage, dialogId: string, dialogArgs: any, done: (err: Error, session: Session) => void, newStack = false): void {
private createSession(storageCtx: IBotStorageContext, message: IMessage, dialogId: string, dialogArgs: any, done: (err: Error, session: Session) => void, newStack = false, shouldDispatch = true): void {
var loadedData: IBotStorageData;
this.getStorageData(storageCtx, (data) => {
// Create localizer on first access
Expand Down Expand Up @@ -464,8 +473,11 @@ export class UniversalBot extends Library {
}
loadedData = data; // We'll clone it when saving data later

// Dispatch message
session.dispatch(sessionState, message, () => done(null, session));
if (shouldDispatch) {
session.dispatch(sessionState, message, () => done(null, session));
} else {
done(null, session);
}
}, <any>done);
}

Expand Down

0 comments on commit 7eecd33

Please sign in to comment.