aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/clipboard-monitor.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-03-07 21:41:45 -0500
committerGitHub <noreply@github.com>2020-03-07 21:41:45 -0500
commitb8eb5e6016834cc751c973239e1e4604fe9799ee (patch)
tree34001835d0efa086a32e50fc8fb70fee4b7f4795 /ext/bg/js/clipboard-monitor.js
parent4b0dfa92aaa9e088c2f59edb3adcb89f0b3c1053 (diff)
parentba64f34df19d446cbe5b8ec2e367d4f6a4d1061f (diff)
Merge pull request #397 from toasted-nutbread/clipboard-monitor-refactor2
Clipboard monitor refactor
Diffstat (limited to 'ext/bg/js/clipboard-monitor.js')
-rw-r--r--ext/bg/js/clipboard-monitor.js48
1 files changed, 23 insertions, 25 deletions
diff --git a/ext/bg/js/clipboard-monitor.js b/ext/bg/js/clipboard-monitor.js
index c2f41385..a6d73c79 100644
--- a/ext/bg/js/clipboard-monitor.js
+++ b/ext/bg/js/clipboard-monitor.js
@@ -16,66 +16,64 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-/*global apiClipboardGet, jpIsStringPartiallyJapanese*/
+/*global jpIsStringPartiallyJapanese*/
-class ClipboardMonitor {
- constructor() {
- this.timerId = null;
- this.timerToken = null;
- this.interval = 250;
- this.previousText = null;
- }
-
- onClipboardText(_text) {
- throw new Error('Override me');
+class ClipboardMonitor extends EventDispatcher {
+ constructor({getClipboard}) {
+ super();
+ this._timerId = null;
+ this._timerToken = null;
+ this._interval = 250;
+ this._previousText = null;
+ this._getClipboard = getClipboard;
}
start() {
this.stop();
// The token below is used as a unique identifier to ensure that a new clipboard monitor
- // hasn't been started during the await call. The check below the await apiClipboardGet()
+ // hasn't been started during the await call. The check below the await this._getClipboard()
// call will exit early if the reference has changed.
const token = {};
const intervalCallback = async () => {
- this.timerId = null;
+ this._timerId = null;
let text = null;
try {
- text = await apiClipboardGet();
+ text = await this._getClipboard();
} catch (e) {
// NOP
}
- if (this.timerToken !== token) { return; }
+ if (this._timerToken !== token) { return; }
if (
typeof text === 'string' &&
(text = text.trim()).length > 0 &&
- text !== this.previousText
+ text !== this._previousText
) {
- this.previousText = text;
+ this._previousText = text;
if (jpIsStringPartiallyJapanese(text)) {
- this.onClipboardText(text);
+ this.trigger('change', {text});
}
}
- this.timerId = setTimeout(intervalCallback, this.interval);
+ this._timerId = setTimeout(intervalCallback, this._interval);
};
- this.timerToken = token;
+ this._timerToken = token;
intervalCallback();
}
stop() {
- this.timerToken = null;
- if (this.timerId !== null) {
- clearTimeout(this.timerId);
- this.timerId = null;
+ this._timerToken = null;
+ if (this._timerId !== null) {
+ clearTimeout(this._timerId);
+ this._timerId = null;
}
}
setPreviousText(text) {
- this.previousText = text;
+ this._previousText = text;
}
}