Au 2- TaskQueue - How to use TaskQueue in a decorator

We need to intialize the TaskQueue instance in a custom decorator method in Au 2

    export function delayed(): any {
    
    return function (target, key, descriptor) {
       //initialise the task queue here
       // taskQueue.queueTask(() => { });
    };
    
}


export class Foo{
@delayed()
    afterAttach() {
       
    }

}
1 Like

The API isn’t fully fleshed out yet but in a normal component you could do something like this:

export function delayed() {
  return function (target, key, descriptor) {
    const method = descriptor.value;
    return {
      ...descriptor,
      value: function () {
        const container = Controller.getCached(this).context;
        const scheduler = container.get(IScheduler);
        scheduler.queueMacroTask(method.bind(this));
      }
    };
  };
}

I should point out though, that au2 is specifically designed so that you normally don’t need to delay stuff inside your lifecycle hooks.

The documentation that comes with the alpha release will explain more about this and which hooks to use in which situations, just giving you a heads-up that you probably won’t be needing this decorator when Au2 is finished.

2 Likes

Thanks for your response. I have tried the code in decorator , but the container is undefined.

import {Controller } from '@aurelia/runtime';

 const container = Controller.getCached(this).context;
1 Like

Is Foo a customAttribute?

2 Likes

The Foo is a custom attribute. We are using thisdecorator in a custom attribute.

1 Like

I see. Then there’s currently no straightforward manner to accomplish this without putting some code in your custom attribute itself (such as injecting the scheduler so the decorator can access it). I’ve made a note and will make sure it’s addressed in alpha.

3 Likes