Coder Social home page Coder Social logo

Comments (5)

dgeb avatar dgeb commented on August 28, 2024

@ef4 Each of these queues is an Orbit TaskQueue. When the queues are empty they emit a complete event, which you can listen for:

currentSource.requestQueue.on('complete', () => { 
  // optionally return a promise here if you want to delay future actions of the queue until the promise resolves or errors
});

Remember to cleanup with off.

from ember-orbit.

ef4 avatar ef4 commented on August 28, 2024

Thanks. Is there a corresponding event for going from empty to active?

from ember-orbit.

dgeb avatar dgeb commented on August 28, 2024

beforeTask is emitted before each task in the queue is processed. I think this is most like an "active" indicator.

Another event to be aware of is change which is emitted whenever the contents of the queue change, but this may happen when the queue is complete or processing has paused and its contents are changed.

from ember-orbit.

ef4 avatar ef4 commented on August 28, 2024

Thanks, that set me on the right track. I ended up going with beforeTask for start and task or fail for end. Here is a utility that may help others (and could potentially go into ember-orbit):

import { buildWaiter } from '@ember/test-waiters';

export function buildOrbitWaiter(label) {
  let waiter = buildWaiter(label);

  const taskTokens = new WeakMap();

  function taskStarted(task) {
    let token = waiter.beginAsync();
    taskTokens.set(task, token);
  }
  function taskEnded(task) {
    let token = taskTokens.get(task);
    if (token) {
      waiter.endAsync(token);
    }
  }

  function waitForSource(source) {
    source.requestQueue.on('beforeTask', taskStarted);
    source.requestQueue.on('task', taskEnded);
    source.requestQueue.on('fail', taskEnded);
    source.syncQueue.on('beforeTask', taskStarted);
    source.syncQueue.on('task', taskEnded);
    source.syncQueue.on('fail', taskEnded);
    return source;
  }

  return { waitForSource };
}

You use it like:

import JSONAPISource from '@orbit/jsonapi';
import { buildOrbitWaiter } from '../lib/orbit-waiter';

let { waitForSource } = buildOrbitWaiter('data-source:rails');

export default {
  create(injections = {}) {
    return waitForSource(new JSONAPISource(injections));
  }
}

from ember-orbit.

dgeb avatar dgeb commented on August 28, 2024

Looks good @ef4 - thanks for sharing! I've opened tracking issue #341 to ensure that we add your work, or something similar, to ember-orbit soon.

from ember-orbit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.