-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
832adb0
commit 8989bbb
Showing
7 changed files
with
220 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
"ember-resources": minor | ||
--- | ||
|
||
New Utils: UpdateFrequency and FrameRate | ||
|
||
<details><summary>FrameRate</summary> | ||
|
||
Utility that uses requestAnimationFrame to report | ||
how many frames per second the current monitor is | ||
rendering at. | ||
|
||
The result is rounded to two decimal places. | ||
|
||
```js | ||
import { FramRate } from 'ember-resources/util/fps'; | ||
|
||
<template> | ||
{{FrameRate}} | ||
</template> | ||
``` | ||
|
||
</details> | ||
|
||
|
||
<details><summary>FrameRate</summary> | ||
|
||
|
||
Utility that will report the frequency of updates to tracked data. | ||
|
||
```js | ||
import { UpdateFrequency } from 'ember-resources/util/fps'; | ||
export default class Demo extends Component { | ||
@tracked someProp; | ||
@use updateFrequency = UpdateFrequency(() => this.someProp); | ||
<template> | ||
{{this.updateFrequency}} | ||
</template> | ||
} | ||
``` | ||
|
||
NOTE: the function passed to UpdateFrequency may not set tracked data. | ||
|
||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { cell, resource, resourceFactory } from '../index'; | ||
|
||
/** | ||
* Utility that uses requestAnimationFrame to report | ||
* how many frames per second the current monitor is | ||
* rendering at. | ||
* | ||
* The result is rounded to two decimal places. | ||
* | ||
* ```js | ||
* import { FramRate } from 'ember-resources/util/fps'; | ||
* | ||
* <template> | ||
* {{FrameRate}} | ||
* </template> | ||
* ``` | ||
*/ | ||
export const FrameRate = resource(({ on }) => { | ||
let value = cell(0); | ||
let startTime = new Date().getTime(); | ||
let frame: number; | ||
|
||
let update = () => { | ||
// simulate receiving data as fast as possible | ||
frame = requestAnimationFrame(() => { | ||
value.current++; | ||
update(); | ||
}); | ||
}; | ||
|
||
on.cleanup(() => cancelAnimationFrame(frame)); | ||
|
||
// Start the infinite requestAnimationFrame chain | ||
update(); | ||
|
||
return () => { | ||
let elapsed = (new Date().getTime() - startTime) * 0.001; | ||
let fps = value.current * Math.pow(elapsed, -1); | ||
let rounded = Math.round(fps * 100) * 0.01; | ||
// account for https://stackoverflow.com/a/588014/356849 | ||
let formatted = `${rounded}`.substring(0, 5); | ||
|
||
return formatted; | ||
}; | ||
}); | ||
|
||
/** | ||
* Utility that will report the frequency of updates to tracked data. | ||
* | ||
* ```js | ||
* import { UpdateFrequency } from 'ember-resources/util/fps'; | ||
* | ||
* export default class Demo extends Component { | ||
* @tracked someProp; | ||
* | ||
* @use updateFrequency = UpdateFrequency(() => this.someProp); | ||
* | ||
* <template> | ||
* {{this.updateFrequency}} | ||
* </template> | ||
* } | ||
* ``` | ||
* | ||
* NOTE: the function passed to UpdateFrequency may not set tracked data. | ||
*/ | ||
export const UpdateFrequency = resourceFactory((ofWhat: () => unknown, updateInterval = 500) => { | ||
updateInterval ||= 500; | ||
|
||
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; | ||
}; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import Component from '@glimmer/component'; | ||
import { tracked } from '@glimmer/tracking'; | ||
// @ts-ignore | ||
import { on } from '@ember/modifier'; | ||
import { click, find, render } from '@ember/test-helpers'; | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
|
||
import { use } from 'ember-resources'; | ||
import { FrameRate, UpdateFrequency } from 'ember-resources/util/fps'; | ||
|
||
module('Utils | FPS | rendering', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
module('FrameRate', function() { | ||
test('it works', async function (assert) { | ||
await render(<template> | ||
<out>{{FrameRate}}</out> | ||
</template>); | ||
|
||
|
||
let text = find('out')?.innerHTML?.trim() || '' | ||
|
||
assert.notStrictEqual(text, '', 'Content is rendered'); | ||
}); | ||
|
||
|
||
}); | ||
|
||
module('UpdateFrequency', function() { | ||
test('it works', async function (assert) { | ||
class Demo extends Component { | ||
@tracked someProp = 0; | ||
|
||
@use updateFrequency = UpdateFrequency(() => this.someProp); | ||
|
||
inc = () => this.someProp++; | ||
|
||
<template> | ||
<button type="button" {{on "click" this.inc}}>Inc</button> | ||
<out>{{this.updateFrequency}}</out> | ||
</template> | ||
} | ||
|
||
await render( | ||
<template> | ||
<Demo /> | ||
</template> | ||
); | ||
|
||
assert.dom('out').hasText('0', 'Initial value is 0'); | ||
|
||
for (let i = 0; i < 100; i++) { | ||
await click('button'); | ||
} | ||
|
||
let text = find('out')?.innerHTML?.trim() || '' | ||
|
||
assert.notStrictEqual(text, '', 'Content is rendered'); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters