Skip to content

Batched Reads/Writes Scheduler #345

@lynchbomb

Description

@lynchbomb

Kicking off the idea/convo of including an API within Backburner for optimally batching work within a rAF scheduler for reads/writes. Addons obviously exist to leverage this, however it's so fundamental and a pattern we should be promoting "out-of-box" with an explicit API for doing both. Thoughts @krisselden @rwjblue @stefanpenner

  _reads: [],
  _writes: [],
  _running: false,
  scheduleRead(callback) {
    this._reads.unshift(callback);
    this._run();
  },
  scheduleWrite(callback) {
    this._writes.unshift(callback);
    this._run();
  },
  _run() {
    if (!this._running) {
      this._running = true;
      rAF(() => {
        join(() => {
         for (let i = 0, rlen = this._reads.length; i < rlen; i++) {
            this._reads.pop()();
          }
          for (let i = 0, wlen = this._writes.length; i < wlen; i++) {
            this._writes.pop()();
          }
          this._running = false;
          if (this._writes.length > 0 || this._reads.length > 0) {
            this._run();
          }
        });
      });
    }
  }

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions