@@ -4,6 +4,9 @@ var executeApiFetch = require('./apifetch');
4
4
var sendStats = require ( './stats' ) ;
5
5
var Settings = require ( './settings' ) ;
6
6
var util = require ( './util' ) ;
7
+ var throttle = require ( './throttle' ) ;
8
+
9
+ var API_THROTTLE = 200 ;
7
10
8
11
var client = function ( sitekey ) {
9
12
this . sitekey = sitekey ;
@@ -44,7 +47,11 @@ var client = function(sitekey) {
44
47
45
48
this . settings . setCallback ( callback ) ;
46
49
this . settings . setKeyword ( keyword ) ;
47
- executeApiFetch ( this . sitekey , 'search' , this . settings . getSettings ( ) , callback ) ;
50
+
51
+ if ( ! this . throttledSearchFetch ) {
52
+ this . throttledSearchFetch = throttle ( API_THROTTLE , executeApiFetch ) ;
53
+ }
54
+ this . throttledSearchFetch ( this . sitekey , 'search' , this . settings . getSettings ( ) , callback ) ;
48
55
}
49
56
50
57
@@ -58,7 +65,11 @@ var client = function(sitekey) {
58
65
throw "Illegal suggestions parameters. Should be (prefix, callbackFunction)" ;
59
66
}
60
67
this . settings . setSuggestionsPrefix ( prefix ) ;
61
- executeApiFetch ( this . sitekey , 'suggest' , this . settings . getSettings ( ) , callback ) ;
68
+
69
+ if ( ! this . throttledSuggestionsFetch ) {
70
+ this . throttledSuggestionsFetch = throttle ( API_THROTTLE , executeApiFetch ) ;
71
+ }
72
+ this . throttledSuggestionsFetch ( this . sitekey , 'suggest' , this . settings . getSettings ( ) , callback ) ;
62
73
}
63
74
64
75
@@ -72,7 +83,11 @@ var client = function(sitekey) {
72
83
throw "Illegal autocomplete parameters. Should be (field, prefix, callbackFunction)" ;
73
84
}
74
85
this . settings . setAutocompleteParams ( field , prefix ) ;
75
- executeApiFetch ( this . sitekey , 'autocomplete' , this . settings . getSettings ( ) , callback ) ;
86
+
87
+ if ( ! this . throttledAutocompleteFetch ) {
88
+ this . throttledAutocompleteFetch = throttle ( API_THROTTLE , executeApiFetch ) ;
89
+ }
90
+ this . throttledAutocompleteFetch ( this . sitekey , 'autocomplete' , this . settings . getSettings ( ) , callback ) ;
76
91
}
77
92
78
93
@@ -101,6 +116,7 @@ var client = function(sitekey) {
101
116
this . setShuffleAndLimitTo = function ( shuffleAndLimitTo ) { this . settings . setShuffleAndLimitTo ( shuffleAndLimitTo ) ; }
102
117
this . setFuzzyMatch = function ( fuzzy ) { this . settings . setFuzzyMatch ( fuzzy ) ; }
103
118
this . setCollectAnalytics = function ( collectAnalytics ) { this . settings . setCollectAnalytics ( collectAnalytics ) ; }
119
+ this . setThrottleDelay = function ( delay ) { API_THROTTLE = delay ; }
104
120
this . setStatsSessionId = function ( id ) { this . sessionId = id ; }
105
121
this . getStatsSessionId = function ( ) { return this . sessionId ; }
106
122
0 commit comments