Skip to content

Commit c9a41a7

Browse files
committed
Update demo to show framerate
1 parent 7b10db0 commit c9a41a7

File tree

6 files changed

+86
-46
lines changed

6 files changed

+86
-46
lines changed

docs/all.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/all.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44
<div id="main">
55
<p>
66
This is an demo for the
7-
<a href="https://github.com/bryanjos/processes"
7+
<a href="https://github.com/elixirscript/processes"
88
>erlang processes javascript library</a
99
>
1010
which will hopefully be part of elixirscript. Below is a table with
11-
10,000 cells. For each cell there is a process responsible for updating
12-
its contents. Each process updates its cell's color and updates it to a
13-
random number. This demo is inspired by the
14-
<a href="http://swannodette.github.io/2013/08/02/100000-processes/"
11+
10,000 cells. There are 10,000 independent processes. Each process
12+
updates a random table cell's color and updates it to a random number.
13+
This demo is inspired by the
14+
<a href="https://swannodette.github.io/2013/08/02/100000-processes"
1515
>10,000 Processes</a
1616
>
1717
demo on David Nolen's blog.
1818
</p>
19+
<p><span id="fps"></span> FPS</p>
1920
</div>
2021
<script type="text/javascript" src="all.js"></script></body>
2122
</html>

lib/processes.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class ProcessQueue {
193193

194194
}
195195

196-
class Scheduler {
196+
class DefaultScheduler {
197197
constructor(throttle = 0, reductions_per_process = 8) {
198198
this.isRunning = false;
199199

@@ -222,11 +222,15 @@ class Scheduler {
222222
this.isRunning = false;
223223
}
224224

225+
_run(run) {
226+
this.invokeLater(() => {
227+
run();
228+
});
229+
}
230+
225231
run() {
226232
if (this.isRunning) {
227-
this.invokeLater(() => {
228-
this.run();
229-
});
233+
this._run(this.run.bind(this));
230234
} else {
231235
for (let [pid, queue] of this.queues) {
232236
let reductions = 0;
@@ -253,9 +257,7 @@ class Scheduler {
253257
}
254258
}
255259

256-
this.invokeLater(() => {
257-
this.run();
258-
});
260+
this._run(this.run.bind(this));
259261
}
260262
}
261263

@@ -288,16 +290,14 @@ class Scheduler {
288290
/* @flow */
289291

290292
class ProcessSystem {
291-
constructor() {
293+
constructor(scheduler = new DefaultScheduler(5)) {
292294
this.pids = new Map();
293295
this.mailboxes = new Map();
294296
this.names = new Map();
295297
this.links = new Map();
296298
this.monitors = new Map();
297-
const throttle = 5; //ms between scheduled tasks
298-
299299
this.current_process = null;
300-
this.scheduler = new Scheduler(throttle);
300+
this.scheduler = scheduler;
301301
this.suspended = new Map();
302302
let process_system_scope = this;
303303
this.main_process_pid = this.spawn(function* () {
@@ -622,8 +622,21 @@ class ProcessSystem {
622622

623623
}
624624

625+
class RequestAnimationScheduler extends DefaultScheduler {
626+
constructor(throttle = 0, reductions_per_process = 8) {
627+
super(throttle, reductions_per_process);
628+
}
629+
630+
_run(run) {
631+
window.requestAnimationFrame(run);
632+
}
633+
634+
}
635+
625636
var index = {
626-
ProcessSystem
637+
ProcessSystem,
638+
RequestAnimationScheduler,
639+
DefaultScheduler
627640
};
628641

629642
module.exports = index;

src/docs/html/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
>erlang processes javascript library</a
99
>
1010
which will hopefully be part of elixirscript. Below is a table with
11-
10,000 cells. For each cell there is a process responsible for updating
12-
its contents. Each process updates its cell's color and updates it to a
13-
random number. This demo is inspired by the
11+
10,000 cells. There are 10,000 independent processes. Each process
12+
updates a random table cell's color and updates it to a random number.
13+
This demo is inspired by the
1414
<a href="https://swannodette.github.io/2013/08/02/100000-processes"
1515
>10,000 Processes</a
1616
>
1717
demo on David Nolen's blog.
1818
</p>
19+
<p><span id="fps"></span> FPS</p>
1920
</div>
2021
</body>
2122
</html>

src/docs/js/index.js

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,6 @@ import '../css/index.css'
22

33
import Processes from '../../index.js'
44

5-
const system = new Processes.ProcessSystem(
6-
new Processes.RequestAnimationScheduler()
7-
)
8-
9-
const rows = 200
10-
const columns = 50
11-
12-
const maxProcesses = rows * columns
13-
14-
const main = document.getElementById('main')
15-
16-
tableCreate(rows, columns)
17-
18-
for (let i = 0; i < maxProcesses; i++) {
19-
const _pid = system.spawn(function*() {
20-
while (true) {
21-
yield* render(
22-
Math.floor(Math.random() * maxProcesses),
23-
Math.floor(Math.random() * 10)
24-
)
25-
}
26-
})
27-
}
28-
295
function* render(cell, number) {
306
let el = document.getElementById(`td-${cell}`)
317
const style = 'group' + (number % 6)
@@ -35,6 +11,7 @@ function* render(cell, number) {
3511
}
3612

3713
function tableCreate(numberOfRows, numberOfColumns) {
14+
const main = document.getElementById('main')
3815
let tbl = document.createElement('table')
3916
tbl.cellPadding = 0
4017
tbl.cellSpacing = 0
@@ -59,3 +36,51 @@ function tableCreate(numberOfRows, numberOfColumns) {
5936
tbl.appendChild(tbdy)
6037
main.appendChild(tbl)
6138
}
39+
40+
// from https://www.growingwiththeweb.com/2017/12/fast-simple-js-fps-counter.html
41+
const times = []
42+
let fps
43+
let fpsEl
44+
45+
function refreshLoop() {
46+
window.requestAnimationFrame(() => {
47+
const now = performance.now()
48+
while (times.length > 0 && times[0] <= now - 1000) {
49+
times.shift()
50+
}
51+
times.push(now)
52+
fps = times.length
53+
if (!fpsEl) {
54+
fpsEl = document.getElementById('fps')
55+
}
56+
57+
fpsEl.textContent = fps.toString()
58+
refreshLoop()
59+
})
60+
}
61+
62+
document.addEventListener('DOMContentLoaded', function(event) {
63+
const system = new Processes.ProcessSystem(
64+
new Processes.RequestAnimationScheduler()
65+
)
66+
67+
const rows = 200
68+
const columns = 50
69+
70+
const maxProcesses = rows * columns
71+
72+
refreshLoop()
73+
74+
tableCreate(rows, columns)
75+
76+
for (let i = 0; i < maxProcesses; i++) {
77+
const _pid = system.spawn(function*() {
78+
while (true) {
79+
yield* render(
80+
Math.floor(Math.random() * maxProcesses),
81+
Math.floor(Math.random() * 10)
82+
)
83+
}
84+
})
85+
}
86+
})

0 commit comments

Comments
 (0)