Skip to content

Commit

Permalink
Merge pull request #2467 from microsoft/v-kydela/ComponentDialog
Browse files Browse the repository at this point in the history
Fix PersistedDialogState
  • Loading branch information
johnataylor authored Sep 4, 2019
2 parents d06cd9e + 0d6585f commit d03a150
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions libraries/Microsoft.Bot.Builder.Dialogs/ComponentDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public ComponentDialog(string dialogId)
}

// Start the inner dialog.
var dialogState = new DialogState();
outerDc.ActiveDialog.State[PersistedDialogState] = dialogState;
var dialogState = InitializeDialogState(outerDc.ActiveDialog);
var innerDc = new DialogContext(_dialogs, outerDc.Context, dialogState);
innerDc.Parent = outerDc;
var turnResult = await OnBeginDialogAsync(innerDc, options, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -128,8 +127,7 @@ public ComponentDialog(string dialogId)
}

// Continue execution of inner dialog.
var dialogState = (DialogState)outerDc.ActiveDialog.State[PersistedDialogState];
var innerDc = new DialogContext(_dialogs, outerDc.Context, dialogState);
var innerDc = new DialogContext(_dialogs, outerDc.Context, GetDialogState(outerDc.ActiveDialog));
innerDc.Parent = outerDc;
var turnResult = await OnContinueDialogAsync(innerDc, cancellationToken).ConfigureAwait(false);

Expand Down Expand Up @@ -191,8 +189,7 @@ public ComponentDialog(string dialogId)
public override async Task RepromptDialogAsync(ITurnContext turnContext, DialogInstance instance, CancellationToken cancellationToken = default(CancellationToken))
{
// Delegate to inner dialog.
var dialogState = (DialogState)instance.State[PersistedDialogState];
var innerDc = new DialogContext(_dialogs, turnContext, dialogState);
var innerDc = new DialogContext(_dialogs, turnContext, GetDialogState(instance));
await innerDc.RepromptDialogAsync(cancellationToken).ConfigureAwait(false);

// Notify component
Expand All @@ -218,8 +215,7 @@ public ComponentDialog(string dialogId)
// Forward cancel to inner dialogs
if (reason == DialogReason.CancelCalled)
{
var dialogState = (DialogState)instance.State[PersistedDialogState];
var innerDc = new DialogContext(_dialogs, turnContext, dialogState);
var innerDc = new DialogContext(_dialogs, turnContext, GetDialogState(instance));
await innerDc.CancelAllDialogsAsync(cancellationToken).ConfigureAwait(false);
}

Expand Down Expand Up @@ -361,5 +357,22 @@ protected virtual Task<DialogTurnResult> EndComponentAsync(DialogContext outerDc
{
return outerDc.EndDialogAsync(result, cancellationToken);
}

private static DialogState GetDialogState(DialogInstance instance)
{
if (!instance.State.TryGetValue(PersistedDialogState, out var dialogState))
{
dialogState = InitializeDialogState(instance);
}

return dialogState as DialogState;
}

private static DialogState InitializeDialogState(DialogInstance instance)
{
var dialogState = new DialogState();
instance.State[PersistedDialogState] = dialogState;
return dialogState;
}
}
}

0 comments on commit d03a150

Please sign in to comment.