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

Add FPS to utils? #658

Closed
NullVoxPopuli opened this issue Oct 20, 2022 · 0 comments · May be fixed by MichalBryxi/ember-resources#1
Closed

Add FPS to utils? #658

NullVoxPopuli opened this issue Oct 20, 2022 · 0 comments · May be fixed by MichalBryxi/ember-resources#1

Comments

@NullVoxPopuli
Copy link
Owner

NullVoxPopuli commented Oct 20, 2022

demo

export default class FrameIncrementer extends Component {
  @use fps = FPS.of(() => /* something that changes quickly */);

  <template>
    Frames: {{this.frameCount}}<br>
    FPS: {{this.fps}}
  </template>
}

const FPS = {
  of: resourceFactory((ofWhat) => {
    let updateInterval = 500; // ms
    let multiplier = 1000 / updateInterval;
    let framesSinceUpdate = 0;

    return resource(({ on }) => {
      let value = cell(0);
      let interval = setInterval(() => {
        value.current = framesSinceUpdate * multiplier;
        framesSinceUpdate = 0;
      }, updateInterval);

      on.cleanup(() => clearInterval(interval));

      return () => {
        ofWhat();
        framesSinceUpdate++;

        return value.current;
      }
    });
  })
}

demo of page FPS

const FPS = {
  ofPage: resource(({ on }) => {
    let updateInterval = 500; // ms
    let multiplier = 1000 / updateInterval;
    let framesSinceUpdate = 0;
    let frame;

    return resource(({ on }) => {
      let value = cell(0);
      let interval = setInterval(() => {
        cancelAnimationFrame(frame);
        value.current = framesSinceUpdate * multiplier;
        framesSinceUpdate = 0;
      }, updateInterval);

      on.cleanup(() => clearInterval(interval));
      on.cleanup(() => cancelAnimationFrame(frame))

      return () => {
        frame = requestAnimationFrame(() => {
          framesSinceUpdate++;
          value.current = value.current;
        });

        return value.current;
      }
    });
  }),
}

Why not "just do it"?

Unanswered questions:

  • can this type of resource be used in a template-only component?
  • is it worth disambiguating property update frequency from actual FPS?
NullVoxPopuli added a commit that referenced this issue Mar 6, 2023
NullVoxPopuli added a commit that referenced this issue Mar 7, 2023
NullVoxPopuli added a commit that referenced this issue Mar 7, 2023
NullVoxPopuli added a commit that referenced this issue Mar 7, 2023
NullVoxPopuli added a commit that referenced this issue Mar 8, 2023
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

Successfully merging a pull request may close this issue.

1 participant