Skip to content

explore building an API for writing dynamically scaling realtime js code "as if" it were synchronous #43

@hellochar

Description

@hellochar

Lets say our code simulates physics of 100k particles and looks like this:

function simulateOneStep() {
  for(const particle in this.particles) {
    // do math and mutate particle
    particle.x += ...
    particle.y += ...
  }
}

Explore the easiest way to make this async such that it adapts to perf:

function* asyncSimulator() {
  while(true) {
    for(const particle in this.particles) {
      ...
      yield;
    }
  }
}

const simulator = asyncSimulator();

function animate() {
  const curTime = Date.now();
  while(Date.now() - curTime < 16) { // do 16ms work
    simulator.next();
  }
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions