Skip to content

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

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();
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions