Added the Porter Stemmer module to improve searches. This doesn't deal with some...
[yaffs-website] / node_modules / video.js / es5 / utils / fn.js
1 'use strict';
2
3 exports.__esModule = true;
4 exports.throttle = exports.bind = undefined;
5
6 var _guid = require('./guid.js');
7
8 /**
9  * Bind (a.k.a proxy or Context). A simple method for changing the context of a function
10  * It also stores a unique id on the function so it can be easily removed from events.
11  *
12  * @param {Mixed} context
13  *        The object to bind as scope.
14  *
15  * @param {Function} fn
16  *        The function to be bound to a scope.
17  *
18  * @param {number} [uid]
19  *        An optional unique ID for the function to be set
20  *
21  * @return {Function}
22  *         The new function that will be bound into the context given
23  */
24 var bind = exports.bind = function bind(context, fn, uid) {
25   // Make sure the function has a unique ID
26   if (!fn.guid) {
27     fn.guid = (0, _guid.newGUID)();
28   }
29
30   // Create the new function that changes the context
31   var bound = function bound() {
32     return fn.apply(context, arguments);
33   };
34
35   // Allow for the ability to individualize this function
36   // Needed in the case where multiple objects might share the same prototype
37   // IF both items add an event listener with the same function, then you try to remove just one
38   // it will remove both because they both have the same guid.
39   // when using this, you need to use the bind method when you remove the listener as well.
40   // currently used in text tracks
41   bound.guid = uid ? uid + '_' + fn.guid : fn.guid;
42
43   return bound;
44 };
45
46 /**
47  * Wraps the given function, `fn`, with a new function that only invokes `fn`
48  * at most once per every `wait` milliseconds.
49  *
50  * @param  {Function} fn
51  *         The function to be throttled.
52  *
53  * @param  {Number}   wait
54  *         The number of milliseconds by which to throttle.
55  *
56  * @return {Function}
57  */
58 /**
59  * @file fn.js
60  * @module fn
61  */
62 var throttle = exports.throttle = function throttle(fn, wait) {
63   var last = Date.now();
64
65   var throttled = function throttled() {
66     var now = Date.now();
67
68     if (now - last >= wait) {
69       fn.apply(undefined, arguments);
70       last = now;
71     }
72   };
73
74   return throttled;
75 };