Skip to content

Commit 512dee1

Browse files
author
Forbes Lindesay
committedNov 6, 2012
Use the debounce component
tidies up the messy logic arround waiting for the user to stop typing.
1 parent 4bf4196 commit 512dee1

File tree

7 files changed

+105
-22
lines changed

7 files changed

+105
-22
lines changed
 

‎build/build.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

‎build/build.js

+54-14
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function require(p, parent, orig){
2424
if (!mod.exports) {
2525
mod.exports = {};
2626
mod.client = mod.component = true;
27-
mod.call(mod.exports, mod, mod.exports, require.relative(path));
27+
mod.call(this, mod, mod.exports, require.relative(path));
2828
}
2929

3030
return mod.exports;
@@ -68,7 +68,7 @@ require.resolve = function(path){
6868
|| require.modules[index] && index
6969
|| require.modules[indexJSON] && indexJSON
7070
|| require.modules[orig] && orig
71-
|| null;
71+
|| require.aliases[index];
7272
};
7373

7474
/**
@@ -83,8 +83,7 @@ require.resolve = function(path){
8383
require.normalize = function(curr, path) {
8484
var segs = [];
8585

86-
// foo
87-
if ('.' != path[0]) return path;
86+
if ('.' != path.charAt(0)) return path;
8887

8988
curr = curr.split('/');
9089
path = path.split('/');
@@ -137,15 +136,25 @@ require.alias = function(from, to){
137136
require.relative = function(parent) {
138137
var p = require.normalize(parent, '..');
139138

139+
/**
140+
* lastIndexOf helper.
141+
*/
142+
143+
function lastIndexOf(arr, obj){
144+
var i = arr.length;
145+
while (i--) {
146+
if (arr[i] === obj) return i;
147+
}
148+
return -1;
149+
}
150+
140151
/**
141152
* The relative require() itself.
142153
*/
143154

144155
function fn(path){
145156
var orig = path;
146157
path = fn.resolve(path);
147-
var alias = require.aliases[path + '/index.js'];
148-
if (alias) path = alias;
149158
return require(path, parent, orig);
150159
}
151160

@@ -157,9 +166,9 @@ require.relative = function(parent) {
157166
// resolve deps by returning
158167
// the dep in the nearest "deps"
159168
// directory
160-
if ('.' != path[0]) {
169+
if ('.' != path.charAt(0)) {
161170
var segs = parent.split('/');
162-
var i = segs.lastIndexOf('deps') + 1;
171+
var i = lastIndexOf(segs, 'deps') + 1;
163172
if (!i) i = 0;
164173
path = segs.slice(0, i + 1).join('/') + '/deps/' + path;
165174
return path;
@@ -229,15 +238,44 @@ function pathtoRegexp(path, keys, options) {
229238

230239
return new RegExp('^' + path + '$', sensitive ? '' : 'i');
231240
};
241+
});
242+
require.register("MatthewMueller-debounce/index.js", function(module, exports, require){
243+
/**
244+
* Debounce
245+
*
246+
* Returns a function, that, as long as it continues to be invoked, will not
247+
* be triggered. The function will be called after it stops being called for
248+
* N milliseconds. If `immediate` is passed, trigger the function on the
249+
* leading edge, instead of the trailing.
250+
*
251+
* @param {Function} func
252+
* @param {Number} wait
253+
* @param {Boolean} immediate
254+
* @return {Function}
255+
*/
256+
257+
module.exports = function(func, wait, immediate) {
258+
var timeout, result;
259+
return function() {
260+
var context = this, args = arguments;
261+
var later = function() {
262+
timeout = null;
263+
if (!immediate) result = func.apply(context, args);
264+
};
265+
var callNow = immediate && !timeout;
266+
clearTimeout(timeout);
267+
timeout = setTimeout(later, wait);
268+
if (callNow) result = func.apply(context, args);
269+
return result;
270+
};
271+
};
272+
232273
});
233274
require.register("ert/index.js", function(module, exports, require){
234275
$(function () {
235276
var pathRegexp = require('path-to-regexp');
236-
var t;
237-
$('#inputRoute, #inputPath').keyup(function () {
238-
clearTimeout(t);
239-
t = setTimeout(update, 200);
240-
});
277+
var debounce = require('debounce');
278+
$('#inputRoute, #inputPath').keyup(debounce(update, 200));
241279
function update() {
242280
var keys = [];
243281
var regexp = pathRegexp($('#inputRoute').val(), keys);
@@ -269,5 +307,7 @@ $(function () {
269307
});
270308
});
271309
require.alias("component-path-to-regexp/index.js", "ert/deps/path-to-regexp/index.js");
272-
window.ert = require("ert");
310+
311+
require.alias("MatthewMueller-debounce/index.js", "ert/deps/debounce/index.js");
312+
window.a = require("ert");
273313
})();

‎component.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "ert",
33
"dependencies": {
4-
"component/path-to-regexp": "*"
4+
"component/path-to-regexp": "*",
5+
"MatthewMueller/debounce": "*"
56
},
67
"scripts": ["index.js"]
78
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "debounce",
3+
"repo": "matthewmueller/debounce",
4+
"description": "Creates and returns a new debounced version of the passed function that will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked",
5+
"version": "0.0.1",
6+
"keywords": [],
7+
"dependencies": {},
8+
"development": {},
9+
"license": "MIT",
10+
"scripts": [
11+
"index.js"
12+
]
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Debounce
3+
*
4+
* Returns a function, that, as long as it continues to be invoked, will not
5+
* be triggered. The function will be called after it stops being called for
6+
* N milliseconds. If `immediate` is passed, trigger the function on the
7+
* leading edge, instead of the trailing.
8+
*
9+
* @param {Function} func
10+
* @param {Number} wait
11+
* @param {Boolean} immediate
12+
* @return {Function}
13+
*/
14+
15+
module.exports = function(func, wait, immediate) {
16+
var timeout, result;
17+
return function() {
18+
var context = this, args = arguments;
19+
var later = function() {
20+
timeout = null;
21+
if (!immediate) result = func.apply(context, args);
22+
};
23+
var callNow = immediate && !timeout;
24+
clearTimeout(timeout);
25+
timeout = setTimeout(later, wait);
26+
if (callNow) result = func.apply(context, args);
27+
return result;
28+
};
29+
};

‎index.html

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ <h2>Results</h2>
5757
<div class="alert alert-success is-match">
5858
The path matches the route
5959
</div>
60+
<p class="is-match">
61+
And the keys have the following values:
62+
</p>
6063
<div id="keys-results-display" class="is-match">
6164
</div>
6265
</div><!--/span-->

‎index.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
$(function () {
22
var pathRegexp = require('path-to-regexp');
3-
var t;
4-
$('#inputRoute, #inputPath').keyup(function () {
5-
clearTimeout(t);
6-
t = setTimeout(update, 200);
7-
});
3+
var debounce = require('debounce');
4+
$('#inputRoute, #inputPath').keyup(debounce(update, 200));
85
function update() {
96
var keys = [];
107
var regexp = pathRegexp($('#inputRoute').val(), keys);
@@ -18,8 +15,7 @@ $(function () {
1815
}
1916

2017
var path = $('#inputPath').val();
21-
var isMatch = regexp.test(path);
22-
if (isMatch) {
18+
if (regexp.test(path)) {
2319
$('.is-not-match').hide();
2420
$('.is-match').show();
2521
var result = regexp.exec(path);

0 commit comments

Comments
 (0)
Please sign in to comment.