Skip to content

Unity: Accept an optional CancellationToken on all methods that return a YarnTask #129

@jasonpanosso

Description

@jasonpanosso

Is your feature request related to a problem? Please describe.

currently, async methods that return a YarnTask do not allow a caller to provide an optional CancellationToken, e.g.

        // DialogueRunner.cs
        public async YarnTask StartDialogue(string nodeName)
        {
            // ... removed code for brevity
            dialogueCancellationSource?.Dispose();

            dialogueCompletionSource = new YarnTaskCompletionSource();

            dialogueCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(this.destroyCancellationToken);
            // ... removed code for brevity

because of this, managing cancellation is completely up to the caller, and figuring out how to go about cancellation beyond destroying the respective GameObject is also up to the caller, e.g.

            await using CancellationTokenRegistration registration = cancellationToken.Register(
                () =>
                {
                    if (_runner.IsDialogueRunning)
                        _runner.Stop();
                }
            );
            await _runner.StartDialogue(_encounter.DialogueConfig.StartNode);
            await _runner.DialogueTask;

Describe the solution you'd like

accept an optional CancellationToken parameter, e.g.

        // DialogueRunner.cs
        public async YarnTask StartDialogue(string nodeName, CancellationToken cancellationToken = default)
        {
            // ... removed code for brevity
            dialogueCancellationSource?.Dispose();

            dialogueCompletionSource = new YarnTaskCompletionSource();

            dialogueCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(this.destroyCancellationToken, cancellationToken);
            // ... removed code for brevity

Add tags

  • Unity

Metadata

Metadata

Assignees

No one assigned

    Labels

    ProposalAn idea for a new feature or changed behaviour you want

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions