/
is-speaking.jsx
149 lines (124 loc) · 4.65 KB
/
is-speaking.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
This file is part of Talkie -- text-to-speech browser extension button.
<https://joelpurra.com/projects/talkie/>
Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021 Joel Purra <https://joelpurra.com/>
Talkie is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Talkie is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Talkie. If not, see <https://www.gnu.org/licenses/>.
*/
import PropTypes from "prop-types";
import React from "react";
import {
knownEvents,
} from "../../shared/events";
import {
promiseTry,
} from "../../shared/promise";
//import DualLogger from "../frontend/dual-log";
//
//const dualLogger = new DualLogger("status-container.jsx");
export default function isSpeakingHoc(ComponentToWrap) {
return class IsSpeakingHoc extends React.Component {
state = {
isSpeaking: false,
};
constructor(props) {
super(props);
this.componentCleanup = this.componentCleanup.bind(this);
this.isListeningToBroadcasts = false;
this.killSwitches = [];
}
static contextTypes ={
broadcaster: PropTypes.object.isRequired,
}
componentDidMount() {
window.addEventListener("beforeunload", this.componentCleanup);
this.registerBroadcastListeners();
this.isListeningToBroadcasts = true;
}
componentWillUnmount() {
window.removeEventListener("beforeunload", this.componentCleanup);
this.componentCleanup();
}
shouldComponentUpdate(/* eslint-disable no-unused-vars */nextProps/* eslint-enable no-unused-vars */, /* eslint-disable no-unused-vars */nextState/* eslint-enable no-unused-vars */) {
// NOTE: always update.
// TODO: optimize by comparing old and new props/state.
return this.isListeningToBroadcasts;
}
componentCleanup() {
this.isListeningToBroadcasts = false;
this.executeKillSwitches();
}
render() {
const {
isSpeaking,
} = this.state;
return (
<ComponentToWrap
{...this.props}
isSpeaking={isSpeaking}
/>
);
}
updateIsSpeaking(data) {
if (!this.isListeningToBroadcasts) {
return;
}
// NOTE: there seems to be some issues with react trying to re-render the page after the page has (will) unload.
// NOTE: trigger by hitting the Talkie button in quick succession.
// NOTE: This seems to be due to events from the background page having enough state to for the broadcaster to trigger events inside of the page, before (without) killSwitches being executed.
// NOTE: The code inside of react is tricky -- especially in dev mode -- and this try-catch might not do anything.
// https://github.com/facebook/react/pull/10270
// https://github.com/facebook/react/blob/37e4329bc81def4695211d6e3795a654ef4d84f5/packages/react-reconciler/src/ReactFiberScheduler.js#L770
// https://github.com/facebook/react/blob/46b3c3e4ae0d52565f7ed2344036a22016781ca0/packages/shared/invokeGuardedCallback.js#L151-L161
try {
this.setState({
isSpeaking: data,
});
} catch {
// dualLogger.dualLogError("setState", error);
// NOTE: ignoring and swallowing error.
return undefined;
}
}
executeKillSwitches() {
// NOTE: expected to have only synchronous methods for the relevant parts.
const killSwitchesToExecute = this.killSwitches;
this.killSwitches = [];
killSwitchesToExecute.forEach((killSwitch) => {
try {
killSwitch();
} catch {
try {
// dualLogger.dualLogError("executeKillSwitches", error);
} catch {
// NOTE: ignoring error logging errors.
}
}
});
}
registerBroadcastListeners() {
return promiseTry(
() => {
return Promise.all([
/* eslint-disable no-unused-vars */
this.context.broadcaster.registerListeningAction(knownEvents.beforeSpeaking, (actionName, actionData) => this.updateIsSpeaking(true))
.then((killSwitch) => this.killSwitches.push(killSwitch)),
this.context.broadcaster.registerListeningAction(knownEvents.beforeSpeakingPart, (actionName, actionData) => this.updateIsSpeaking(true))
.then((killSwitch) => this.killSwitches.push(killSwitch)),
this.context.broadcaster.registerListeningAction(knownEvents.afterSpeaking, (actionName, actionData) => this.updateIsSpeaking(false))
.then((killSwitch) => this.killSwitches.push(killSwitch)),
/* eslint-enable no-unused-vars */
]);
},
);
}
};
}