-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.js
63 lines (56 loc) · 1.68 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import looper from './src/index.js';
import { $, $$ } from './src/dom/query.js';
import { on } from './src/dom/event.js';
const originalCountValue = Number($('count').value);
const initialState = { isReset: true, isStarted: false, isFinished: false };
var state = initialState;
const setState = (newState) => {
state = {
...state,
...newState,
};
render();
};
const render = () => {
const { isStarted, isFinished } = state;
const isDisabled = isStarted && !isFinished;
$('start-loop-button').disabled = isDisabled;
$('looper-loop-button').disabled = isDisabled;
$('log').innerHTML = isFinished ? "<span style='color:red;'>Done</span>" : '';
};
const reset = () => {
for (let element of $$('.modal')) {
element.parentNode.removeChild(element);
}
setState(initialState);
$('count').value = originalCountValue;
window.modals.length = 0;
};
// example looper setup
on($('start-loop-button'), 'click', async () => {
const setup = async () => {
await looper.clickSelector('#show-modal-button')();
return new Promise(requestAnimationFrame);
};
const teardown = async () => {
await looper.clickSelector('#hide-modal-button')();
return new Promise(requestAnimationFrame);
};
const runs = Number($('count').value);
const loop = looper([setup, teardown], runs);
setState({ isStarted: true, isFinished: false, loop: loop() });
await state.loop;
setState({ isFinished: true });
});
on($('reset-button'), 'click', reset);
// loop the looper!
on($('looper-loop-button'), 'click', async () => {
await looper(
[
looper.clickSelector('#reset-button'),
looper.clickSelector('#start-loop-button'),
() => state.loop,
],
3
)();
});