Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PriorityGovernor #11

Open
bakkot opened this issue Nov 8, 2024 · 0 comments
Open

PriorityGovernor #11

bakkot opened this issue Nov 8, 2024 · 0 comments

Comments

@bakkot
Copy link

bakkot commented Nov 8, 2024

Sometimes people want the ability to acquire with higher or lower priority.

You can build that on top of the existing interface easily enough:

class PriorityGovernor {
  #queue = new MinHeap; // pretend we have a MinHeap
  #underlying;

  constructor(underlying) {
    this.#underlying = underlying;
  }

  with(fn, priority) {
    let cap = Promise.withResolvers();
    this.#queue.add({ cap, fn, priority });
    this.#advance();
    return cap.promise;
  }

  // other methods omitted

  #advance() {
    this.#underlying.with(async () => {
      let { fn, cap } = this.#queue.shift();
      try {
        cap.resolve(fn());
      } catch (e) {
        cap.reject(e);
      }
    });
  }
}

but maybe it's worth including?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant