aurelia / task-queue

A simple task queue for the browser that enables the queuing of both standard tasks and micro tasks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unexpected behavior with microTask

VagyokC4 opened this issue · comments

I'm submitting a bug report

  • Library Version:
    1.1.0

Please tell us about your environment:

  • Operating System:
    Windows [10]

  • Node Version:
    5.1.0

  • NPM Version:
    3.10.8

  • JSPM OR Webpack AND Version
    CLI

  • Browser:
    Chrome 53

  • Language:
    ESNext

Current behavior:
My microTask fires, but apparently still too early in the lifecycle and I'm not getting my desired results, however, if I wrap my code into a setTimeout(()=>{},1) then I get my desired results.

This code works...

    bind() {
        if (this.appState.scrollPosition)
            this.taskQueue.queueMicroTask(() => {
                setTimeout(() => {
                    $('body').scrollTop(this.appState.scrollPosition);
                },1);
            });
    }

    canDeactivate() {
        this.appState.scrollPosition = $("body").scrollTop();
        return true;
    }

Expected/desired behavior:
This code should work, but does not...

    bind() {
        if (this.appState.scrollPosition)
            this.taskQueue.queueMicroTask(() => {
                $('body').scrollTop(this.appState.scrollPosition);
            });
    }

    canDeactivate() {
        this.appState.scrollPosition = $("body").scrollTop();
        return true;
    }
  • What is the expected behavior?

I am expecting this code to work, without the need for the setTimeout()

  • What is the motivation / use case for changing the behavior?

I thought this used to work without the setTimeout... however currently I need the setTimeout to get my desired results.

commented

Is there a reason for why you're not scrolling in the attached life cycle hook? The DOM element is not yet attached when bind is invoked. In the attached hook you probably don't even need to queue a task.

You could also try with queueTask instead of a micro task.

@stsje Beautiful... thank-you :)