@@ -9,7 +9,9 @@ const getClaps = (api, url) =>
9
9
headers : {
10
10
"Content-Type" : "text/plain"
11
11
}
12
- } ) . then ( response => response . text ( ) ) ;
12
+ } )
13
+ . then ( response => response . text ( ) )
14
+ . then ( res => Number ( res ) ) ;
13
15
14
16
const updateClaps = ( api , claps , url ) =>
15
17
// TODO: polyfill for IE (not edge)
@@ -19,7 +21,9 @@ const updateClaps = (api, claps, url) =>
19
21
"Content-Type" : "text/plain"
20
22
} ,
21
23
body : JSON . stringify ( `${ claps } ,${ VERSION } ` )
22
- } ) . then ( response => response . text ( ) ) ;
24
+ } )
25
+ . then ( response => response . text ( ) )
26
+ . then ( res => Number ( res ) ) ;
23
27
24
28
const arrayOfSize = size => new Array ( size ) . fill ( undefined ) ;
25
29
@@ -98,11 +102,17 @@ class ApplauseButton extends HTMLCustomElement {
98
102
// by the MAX_MULTI_CLAP property, and whether multiclap is enabled
99
103
this . _totalClaps = 0 ;
100
104
105
+ // return the initial clap count as a promise
101
106
let initialClapCountResolve ;
102
107
this . _initialClapCount = new Promise (
103
108
resolve => ( initialClapCountResolve = resolve )
104
109
) ;
105
110
111
+ // cache the most recent clap count returned from the server. If, after an update, the clap count
112
+ // is unchanged, either the maximum clap count has been exceeded. Or, the server-side imposed
113
+ // clap limit for this IP address has been exceeded.
114
+ this . _cachedClapCount = 0 ;
115
+
106
116
// buffer claps within a 2 second window
107
117
this . _bufferedClaps = 0 ;
108
118
this . _updateClaps = debounce ( ( ) => {
@@ -111,9 +121,20 @@ class ApplauseButton extends HTMLCustomElement {
111
121
this . _bufferedClaps ,
112
122
MAX_MULTI_CLAP - this . _totalClaps
113
123
) ;
114
- updateClaps ( this . api , increment , this . url ) ;
115
- this . _totalClaps += increment ;
116
- this . _bufferedClaps = 0 ;
124
+ // send the updated clap count - checking the response to see if the server-held
125
+ // clap count has actually incremented
126
+ updateClaps ( this . api , increment , this . url ) . then ( updatedClapCount => {
127
+ if ( updatedClapCount === this . _cachedClapCount ) {
128
+ // if the clap number as not incremented, disable further updates
129
+ this . classList . add ( "clap-limit-exceeded" ) ;
130
+ // and reset the counter
131
+ this . _countElement . innerHTML = formatClaps ( updatedClapCount ) ;
132
+ }
133
+ this . _cachedClapCount = updatedClapCount ;
134
+
135
+ this . _totalClaps += increment ;
136
+ this . _bufferedClaps = 0 ;
137
+ } ) ;
117
138
}
118
139
} , 2000 ) ;
119
140
@@ -161,9 +182,9 @@ class ApplauseButton extends HTMLCustomElement {
161
182
}
162
183
} ) ;
163
184
164
- getClaps ( this . api , this . url ) . then ( claps => {
185
+ getClaps ( this . api , this . url ) . then ( clapCount => {
165
186
this . classList . remove ( "loading" ) ;
166
- const clapCount = Number ( claps ) ;
187
+ this . _cachedClapCount = clapCount ;
167
188
initialClapCountResolve ( clapCount ) ;
168
189
if ( clapCount > 0 ) {
169
190
this . _countElement . innerHTML = formatClaps ( clapCount ) ;
0 commit comments