Skip to content

Commit 0fd18b2

Browse files
committed
tidy-up counter-reset-keyboard example subscrition considerably! #57
1 parent 39d270b commit 0fd18b2

File tree

4 files changed

+25
-40
lines changed

4 files changed

+25
-40
lines changed

Diff for: examples/counter-reset-keyboard/counter.js

+11-18
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function update (action, model) { // Update function takes the current state
1515
}
1616

1717
function view(model, signal) {
18-
return div([], [
18+
return div(['id=wrap'], [
1919
button(["class=inc", "id=inc", signal('inc')], [text('+')]), // increment
2020
div(["class=count", "id=count"], [text(model.toString())]), // count
2121
button(["class=dec", "id=dec", signal('dec')], [text('-')]), // decrement
@@ -26,24 +26,17 @@ function view(model, signal) {
2626
function subscriptions (signal, root) {
2727
var UP_KEY = 38; // increment the counter when [↑] (up) key is pressed
2828
var DOWN_KEY = 40; // decrement the counter when [↓] (down) key is pressed
29-
var called = false; // ensure event hanlder is only called once! #HelpWanted!
3029

31-
function handler (e) {
32-
if (!called) { // had issues with event handler being called multiple!
33-
called = true;
34-
switch (e.keyCode) {
35-
case UP_KEY:
36-
signal('inc')(); // invoke the signal.callback function directly
37-
break;
38-
case DOWN_KEY:
39-
signal('dec')();
40-
break;
41-
}
42-
} // only call event handler once!
43-
}
44-
// event listners were being fired multiple times so, remove before addding:
45-
root.removeEventListener('keypress', handler, false); // remove previous
46-
root.addEventListener('keypress', handler, false); // add fresh listeners
30+
document.addEventListener('keyup', function handler (e) {
31+
switch (e.keyCode) {
32+
case UP_KEY:
33+
signal('inc')(); // invoke the signal > callback function directly
34+
break;
35+
case DOWN_KEY:
36+
signal('dec')();
37+
break;
38+
}
39+
});
4740
}
4841

4942
/* The code block below ONLY Applies to tests run using Node.js */

Diff for: examples/counter-reset-keyboard/index.html

+4-12
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,14 @@
1313
</head>
1414
<body>
1515
<div id="app"></div>
16+
1617
<script src="counter.js" data-cover></script> <!-- load counter -->
1718
<script>
1819
// Initialise the app by "mounting" it passing in MUV Object & "root" DOM node
19-
mount(0, update, view, 'app');
20+
mount(0, update, view, 'app', subscriptions);
21+
console.log('subs:', subscriptions.toString());
2022
</script>
2123

22-
<!-- Below this point is all related to the Tests for the App -->
23-
<div id="test-app"></div> <!-- Create a test-app div to mount the app -->
24-
<div id="qunit"></div> <!-- test results are displayed here -->
25-
<!-- Load the QUnit CSS file from CDN - require to display our tests -->
26-
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-1.18.0.css">
27-
<!-- Load the QUnit Testing Framework from CDN - to run the tests -->
28-
<script src="https://code.jquery.com/qunit/qunit-1.18.0.js"></script>
29-
<!-- Load Blanket.js from CDN - for test coverage stats -->
30-
<script src="https://cdnjs.cloudflare.com/ajax/libs/blanket.js/1.1.4/blanket.js">
31-
</script>
32-
<script src="test.js"></script> <!-- always load test.js last -->
24+
3325
</body>
3426
</html>

Diff for: examples/todo-list/elmish.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@ function empty (node) {
1919
* @param {Function} update how the application state is updated ("controller")
2020
* @param {Function} view function that renders HTML/DOM elements with model.
2121
* @param {String} root_element_id root DOM element in which the app is mounted
22-
* @param {Function} subscriptions any event listeners the application needs
22+
* @param {Function} subs any event listeners the application needs
2323
*/
24-
function mount (model, update, view, root_element_id, subscriptions) {
24+
function mount (model, update, view, root_element_id, subs) {
2525
var ROOT = document.getElementById(root_element_id); // root DOM element
2626
var store_name = 'elmish_' + root_element_id; // test-app !== app
27-
console.log('store_name:', store_name);
28-
function render (mod, sig, root, subs) { // DRY rendering code (invoked twice)
27+
28+
function render (mod, sig, root) { // DRY rendering code (invoked twice)
2929
localStorage.setItem(store_name, JSON.stringify(mod)); // save the model!
3030
empty(root); // clear root element (container) before (re)rendering
3131
root.appendChild(view(mod, sig)) // render view based on model & signal
32-
if (subs && typeof subs === 'function') { subs(sig, root); } // subscription
3332
}
3433

3534
function signal(action) { // signal function takes action
3635
return function callback() { // and returns callback
3736
model = JSON.parse(localStorage.getItem(store_name)) //|| model;
3837
var updatedModel = update(action, model); // update model for the action
39-
render(updatedModel, signal, ROOT, subscriptions);
38+
render(updatedModel, signal, ROOT);
4039
};
4140
};
4241

4342
model = JSON.parse(localStorage.getItem(store_name)) || model;
44-
render(model, signal, ROOT, subscriptions);
43+
render(model, signal, ROOT);
44+
if (subs && typeof subs === 'function') { subs(signal, ROOT); }
4545
}
4646

4747
/**

Diff for: test/elmish.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -467,22 +467,22 @@ test('subscriptions test using counter-reset-keyaboard ⌨️', function (t) {
467467
"elmish_store is 0 (as expected). initial state saved to localStorage.");
468468

469469
// trigger the [↑] (up) keyboard key to increment the counter:
470-
root.dispatchEvent(new KeyboardEvent('keypress', {'keyCode': 38})); //
470+
document.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 38})); // up
471471
t.equal(parseInt(document.getElementById('count')
472472
.textContent, 10), 1, "Up key press increment 0 -> 1");
473473
t.equal(JSON.parse(localStorage.getItem('elmish_' + id)), 1,
474474
"elmish_store 1 (as expected). incremented state saved to localStorage.");
475475

476476
// trigger the [↓] (down) keyboard key to increment the counter:
477-
root.dispatchEvent(new KeyboardEvent('keypress', {'keyCode': 40})); //
477+
document.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 40})); // down
478478
t.equal(parseInt(document.getElementById('count')
479479
.textContent, 10), 0, "Up key press dencrement 1 -> 0");
480480
t.equal(JSON.parse(localStorage.getItem('elmish_' + id)), 0,
481481
"elmish_store 0. keyboard down key decrement state saved to localStorage.");
482482

483483
// subscription keyCode trigger "branch" test (should NOT fire the signal):
484484
const clone = document.getElementById(id).cloneNode(true);
485-
document.dispatchEvent(new KeyboardEvent('keypress', {'keyCode': 42})); //
485+
document.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 42})); //
486486
t.deepEqual(document.getElementById(id), clone, "#" + id + " no change");
487487

488488
// default branch execution:

0 commit comments

Comments
 (0)