Skip to content

Commit b42d0ca

Browse files
Merge pull request #443 from lightning-js/feature/announcer-debug-logs
Added debug logs to announcer. Fixed (?) issues with isProcessing. Op…
2 parents 970e7cc + 479a850 commit b42d0ca

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

docs/plugins/text-to-speech-announcer.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ In some cases you may not want to clear the entire queue, but instead cancel out
7979

8080
Imagine an App with a row of tiles, it's possible that before the title of the role is being spoken out, the user already navigates through the tiles within the row. Traditionally you'd use the focus event to speak out info about each tile (i.e. adding tot the queue). You don't want all previously focused tiles to still be announced, but would still want the category of the row to be announced, making clearing the queue not required.
8181

82-
The `speak()`-method return a Promise that also contains a `cancel()` function. When called, it will cancel that specific message and remove it from the queue before it can be spoken out.
82+
The `speak()`-method return a Promise that also contains a `remove()` function. When called, it will remove it from the queue before it can be spoken out.
8383

8484
Additionally if you want to _interrupt_ a specific messages as it's being spoken out as well and go straight to the next message in the queue (i.e. the newly focused item, for example). You can use the `stop()` message that is returned on the Promise returned by the `speak()`-method.
8585

@@ -100,8 +100,8 @@ Blits.Component('MyTile', {
100100
unfocus() {
101101
// when unfocused interrupt the message if it's already being spoken out
102102
this.message.stop()
103-
// and cancel the message to remove it from the queue
104-
this.message.cancel()
103+
// and remove the message from the queue
104+
this.message.remove()
105105
}
106106
}
107107
})
@@ -117,4 +117,4 @@ Alternatively the announcer can be enabled or disabled run time by using one of
117117

118118
- `this.$announcer.enable()` - activates the announcer
119119
- `this.$announcer.disable()` - deactivates the announcer
120-
- `this.$announcer.disable(true/false)` - turns the announcer or on off
120+
- `this.$announcer.disable(true/false)` - turns the announcer on or off

src/announcer/announcer.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* SPDX-License-Identifier: Apache-2.0
1616
*/
1717

18+
import { Log } from '../lib/log.js'
1819
import speechSynthesis from './speechSynthesis.js'
1920

2021
let active = false
@@ -28,6 +29,7 @@ const noopAnnouncement = {
2829
then() {},
2930
done() {},
3031
cancel() {},
32+
remove() {},
3133
stop() {},
3234
}
3335

@@ -66,11 +68,11 @@ const addToQueue = (message, politeness, delay = false) => {
6668
resolveFn = resolve
6769
})
6870

69-
// augment the promise with a cancel function
70-
done.cancel = () => {
71+
// augment the promise with a cancel / remove function
72+
done.remove = done.cancel = () => {
7173
const index = queue.findIndex((item) => item.id === id)
7274
if (index !== -1) queue.splice(index, 1)
73-
isProcessing = false
75+
Log.debug(`Announcer - removed from queue: "${message}" (id: ${id})`)
7476
resolveFn('canceled')
7577
}
7678

@@ -92,6 +94,8 @@ const addToQueue = (message, politeness, delay = false) => {
9294
queue.push({ delay, resolveFn, id })
9395
}
9496

97+
Log.debug(`Announcer - added to queue: "${message}" (id: ${id})`)
98+
9599
setTimeout(() => {
96100
processQueue()
97101
}, 100)
@@ -118,17 +122,22 @@ const processQueue = async () => {
118122
if (debounce !== null) clearTimeout(debounce)
119123
// add some easing when speaking the messages to reduce stuttering
120124
debounce = setTimeout(() => {
125+
Log.debug(`Announcer - speaking: "${message}" (id: ${id})`)
126+
121127
speechSynthesis
122-
.speak({ message })
128+
.speak({ message, id })
123129
.then(() => {
124-
isProcessing = false
130+
Log.debug(`Announcer - finished speaking: "${message}" (id: ${id})`)
131+
125132
currentId = null
133+
isProcessing = false
126134
resolveFn('finished')
127135
processQueue()
128136
})
129137
.catch((e) => {
130-
isProcessing = false
131138
currentId = null
139+
isProcessing = false
140+
Log.debug(`Announcer - error ("${e.error}") while speaking: "${message}" (id: ${id})`)
132141
resolveFn(e.error)
133142
processQueue()
134143
})

src/announcer/speechSynthesis.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const initialize = () => {
6464

6565
const speak = (options) => {
6666
const utterance = new SpeechSynthesisUtterance(options.message)
67-
const id = Date.now() + Math.random() // Unique ID for tracking
67+
const id = options.id
6868
utterance.lang = options.lang || defaultUtteranceProps.lang
6969
utterance.pitch = options.pitch || defaultUtteranceProps.pitch
7070
utterance.rate = options.rate || defaultUtteranceProps.rate

0 commit comments

Comments
 (0)