Skip to content

Commit eed48b8

Browse files
feat: disable further claps if the server hasn't incremented cla count
1 parent 7d346ef commit eed48b8

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/applause-button.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ const getClaps = (api, url) =>
99
headers: {
1010
"Content-Type": "text/plain"
1111
}
12-
}).then(response => response.text());
12+
})
13+
.then(response => response.text())
14+
.then(res => Number(res));
1315

1416
const updateClaps = (api, claps, url) =>
1517
// TODO: polyfill for IE (not edge)
@@ -19,7 +21,9 @@ const updateClaps = (api, claps, url) =>
1921
"Content-Type": "text/plain"
2022
},
2123
body: JSON.stringify(`${claps},${VERSION}`)
22-
}).then(response => response.text());
24+
})
25+
.then(response => response.text())
26+
.then(res => Number(res));
2327

2428
const arrayOfSize = size => new Array(size).fill(undefined);
2529

@@ -98,11 +102,17 @@ class ApplauseButton extends HTMLCustomElement {
98102
// by the MAX_MULTI_CLAP property, and whether multiclap is enabled
99103
this._totalClaps = 0;
100104

105+
// return the initial clap count as a promise
101106
let initialClapCountResolve;
102107
this._initialClapCount = new Promise(
103108
resolve => (initialClapCountResolve = resolve)
104109
);
105110

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+
106116
// buffer claps within a 2 second window
107117
this._bufferedClaps = 0;
108118
this._updateClaps = debounce(() => {
@@ -111,9 +121,20 @@ class ApplauseButton extends HTMLCustomElement {
111121
this._bufferedClaps,
112122
MAX_MULTI_CLAP - this._totalClaps
113123
);
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+
});
117138
}
118139
}, 2000);
119140

@@ -161,9 +182,9 @@ class ApplauseButton extends HTMLCustomElement {
161182
}
162183
});
163184

164-
getClaps(this.api, this.url).then(claps => {
185+
getClaps(this.api, this.url).then(clapCount => {
165186
this.classList.remove("loading");
166-
const clapCount = Number(claps);
187+
this._cachedClapCount = clapCount;
167188
initialClapCountResolve(clapCount);
168189
if (clapCount > 0) {
169190
this._countElement.innerHTML = formatClaps(clapCount);

0 commit comments

Comments
 (0)