Skip to content

Commit 25221dc

Browse files
committed
[ Add ] Debounce & Throttle utility methods
[ Fix ] Several detail bugs
1 parent 5920000 commit 25221dc

File tree

16 files changed

+151
-7565
lines changed

16 files changed

+151
-7565
lines changed

.esdoc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"test": {
1414
"source": "./test",
1515
"interfaces": ["describe", "it"],
16-
"includes": ["\\.js$"]
16+
"includes": ["\\.spec\\.js$"]
1717
},
1818
"manual": {
1919
"files": ["./manual/Template.md"]

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Node.JS
22
node_modules/
3+
package-lock.json
34

45
# Building
56
dist/

package-lock.json

Lines changed: 0 additions & 7494 deletions
This file was deleted.

package.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dom-renderer",
3-
"version": "0.8.1",
3+
"version": "0.9.0",
44
"description": "Template engine based on HTML 5, ECMAScript 6 & MVVM",
55
"keywords": [
66
"template",
@@ -32,40 +32,40 @@
3232
"pack": "cross-env NODE_ENV=pack amd-bundle source/index dist/dom-renderer -m",
3333
"patch": "babel source/DOM/polyfill.js -o dist/polyfill.js",
3434
"build": "npm run format && npm run lint && npm run pack && npm run patch",
35-
"debug": "npm run pack && mocha --inspect-brk",
35+
"debug": "npm run pack && mocha --inspect-brk --no-timeouts",
3636
"test": "npm run build && mocha --exit && esdoc",
3737
"prepublishOnly": "npm test",
38-
"help": "esdoc && web-server docs/ -o"
38+
"help": "esdoc && opn docs/index.html"
3939
},
4040
"husky": {
4141
"hooks": {
4242
"pre-commit": "npm test && git add ."
4343
}
4444
},
4545
"peerDependencies": {
46-
"@babel/polyfill": "^7.2.5"
46+
"@babel/polyfill": "^7.4.4"
4747
},
4848
"devDependencies": {
49-
"@babel/cli": "^7.2.3",
50-
"@babel/polyfill": "^7.2.5",
51-
"@babel/preset-env": "^7.3.4",
52-
"@babel/register": "^7.0.0",
53-
"amd-bundle": "^1.7.7",
49+
"@babel/cli": "^7.4.4",
50+
"@babel/polyfill": "^7.4.4",
51+
"@babel/preset-env": "^7.4.4",
52+
"@babel/register": "^7.4.4",
53+
"amd-bundle": "^1.7.8",
5454
"babel-plugin-inline-import": "^3.0.0",
5555
"cross-env": "^5.2.0",
5656
"esdoc": "^1.1.0",
5757
"esdoc-ecmascript-proposal-plugin": "^1.0.0",
5858
"esdoc-external-webapi-plugin": "^1.0.0",
5959
"esdoc-standard-plugin": "^1.0.0",
60-
"eslint": "^5.15.1",
61-
"husky": "^1.3.1",
62-
"jsdom": "^14.0.0",
63-
"koapache": "^1.0.6",
64-
"mocha": "^6.0.2",
65-
"prettier": "^1.16.4",
60+
"eslint": "^5.16.0",
61+
"husky": "^2.1.0",
62+
"jsdom": "^15.0.0",
63+
"mocha": "^6.1.4",
64+
"opn-cli": "^4.1.0",
65+
"prettier": "^1.17.0",
6666
"should": "^13.2.3",
6767
"should-sinon": "0.0.6",
68-
"sinon": "^7.2.7"
68+
"sinon": "^7.3.2"
6969
},
7070
"prettier": {
7171
"tabWidth": 4,
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,6 @@ export function scanDOM(root, expression, { attribute, text, ...element }) {
129129
});
130130
}
131131

132-
/**
133-
* @return {Promise<Number>} https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp
134-
*/
135-
export function nextTick() {
136-
return new Promise(resolve => self.requestAnimationFrame(resolve));
137-
}
138-
139132
/**
140133
* @param {HTMLElement} input
141134
*

source/DOM/polyfill.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,18 @@ const { window } = new JSDOM('', {
3030
*
3131
* @return {Promise}
3232
*/
33-
export function typeIn(input, raw) {
34-
return Promise.all(
35-
Array.from(
36-
raw,
37-
data =>
38-
new Promise(resolve =>
39-
setTimeout(() => {
40-
input.value += data;
33+
export async function typeIn(input, raw) {
34+
for (let data of raw) {
35+
input.value += data;
4136

42-
input.dispatchEvent(
43-
new InputEvent('input', {
44-
bubbles: true,
45-
composed: true,
46-
data
47-
})
48-
);
37+
input.dispatchEvent(
38+
new InputEvent('input', {
39+
bubbles: true,
40+
composed: true,
41+
data
42+
})
43+
);
4944

50-
resolve();
51-
})
52-
)
53-
)
54-
);
45+
await new Promise(resolve => setTimeout(resolve));
46+
}
5547
}

source/DOM/timer.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @return {Promise<Number>} https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp
3+
*/
4+
export function nextTick() {
5+
return new Promise(resolve => self.requestAnimationFrame(resolve));
6+
}
7+
8+
/**
9+
* @param {Number} [seconds=0.25]
10+
*
11+
* @return {Promise} Wait seconds in Macro tasks
12+
*/
13+
export function delay(seconds = 0.25) {
14+
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
15+
}
16+
17+
/**
18+
* @param {Function} origin
19+
* @param {Number} [interval=0.25] - Seconds
20+
*
21+
* @return {Function}
22+
*/
23+
export function debounce(origin, interval = 0.25) {
24+
var timer;
25+
26+
return function() {
27+
clearTimeout(timer);
28+
29+
timer = setTimeout(
30+
origin.bind.apply(origin, [].concat.apply([this], arguments)),
31+
interval * 1000
32+
);
33+
};
34+
}
35+
36+
/**
37+
* @param {Function} origin
38+
* @param {Number} [interval=0.25] - Seconds
39+
*
40+
* @return {Function} Wrapped function with Result cache
41+
*/
42+
export function throttle(origin, interval = 0.25) {
43+
var lastTime, lastValue;
44+
45+
return function() {
46+
const now = Date.now();
47+
48+
if (lastTime && lastTime + interval * 1000 > now) return lastValue;
49+
50+
lastTime = now;
51+
52+
return (lastValue = origin.apply(this, arguments));
53+
};
54+
}

source/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
export * from './DOM/utility';
1+
export * from './DOM/parser';
22

33
export * from './DOM/CustomInputEvent';
44

5+
export * from './DOM/timer';
6+
57
export { default as Template } from './view/Template';
68

79
export { default as Model } from './view/Model';

source/view/Model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { nextTick } from '../DOM/utility';
1+
import { nextTick } from '../DOM/timer';
22

33
const view_data = new WeakMap(),
44
cache_data = Symbol('Cache data'),

source/view/Template.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export default class Template extends Array {
1313
Object.assign(super(), {
1414
[template_raw]: raw,
1515
[template_scope]: scope || [],
16-
[template_value]: null,
1716
onChange: onChange instanceof Function && onChange
1817
});
1918

@@ -27,7 +26,7 @@ export default class Template extends Array {
2726
}
2827

2928
toString() {
30-
return this[template_value] + '';
29+
return this[template_value] != null ? this[template_value] + '' : '';
3130
}
3231

3332
/**
@@ -103,7 +102,7 @@ export default class Template extends Array {
103102
return value;
104103
}
105104
} catch (error) {
106-
console.warn(error);
105+
if (template_value in this) console.warn(error);
107106
}
108107

109108
return '';

0 commit comments

Comments
 (0)