Skip to content

Commit

Permalink
remove announcingEmitter, #41
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Jan 11, 2022
1 parent 42aa1c6 commit dd290dd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 35 deletions.
1 change: 0 additions & 1 deletion js/Announcer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class Announcer {
// @public {Emitter} - Emits an event when this Announcer is finished with an Utterance. It is up
// to the Announcer subclass to emit this because different speech technologies may have different APIs
// to determine when speaking is finished.
// TODO: This should deprecate AriaLiveAnnouncer.announcingEmitter, see https://github.com/phetsims/utterance-queue/issues/41
this.announcementCompleteEmitter = new Emitter( {
parameters: [ { valueType: Utterance } ]
} );
Expand Down
51 changes: 20 additions & 31 deletions js/AriaLiveAnnouncer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@
* @author John Blanco
*/

import Emitter from '../../axon/js/Emitter.js';
import stepTimer from '../../axon/js/stepTimer.js';
import EnumerationDeprecated from '../../phet-core/js/EnumerationDeprecated.js';
import merge from '../../phet-core/js/merge.js';
import platform from '../../phet-core/js/platform.js';
import { PDOMUtils } from '../../scenery/js/imports.js';
import Announcer from './Announcer.js';
import Utterance from './Utterance.js';
import utteranceQueueNamespace from './utteranceQueueNamespace.js';

// constants
Expand Down Expand Up @@ -79,11 +77,6 @@ class AriaLiveAnnouncer extends Announcer {
this.politeElementIndex = 0;
this.assertiveElementIndex = 0;

// @public {null|Emitter} - Emit whenever we announce.
this.announcingEmitter = new Emitter( {
parameters: [ { valueType: 'string' }, { valueType: AriaLiveAnnouncer.AriaLive }, { valueType: Utterance } ]
} );

// @public (read-only)
this.ariaLiveContainer = document.createElement( 'div' ); //container div
this.ariaLiveContainer.setAttribute( 'id', `aria-live-elements-${ariaLiveAnnouncerIndex}` );
Expand All @@ -102,27 +95,6 @@ class AriaLiveAnnouncer extends Announcer {
this.politeElements = Array.from( this.politeElements.children );
this.assertiveElements = Array.from( this.assertiveElements.children );

// no need to be removed, exists for the lifetime of the simulation.
this.announcingEmitter.addListener( ( textContent, priority, utterance ) => {

if ( priority === AriaLive.POLITE ) {
const element = this.politeElements[ this.politeElementIndex ];
this.updateLiveElement( element, textContent, utterance );

// update index for next time
this.politeElementIndex = ( this.politeElementIndex + 1 ) % this.politeElements.length;
}
else if ( priority === AriaLive.ASSERTIVE ) {
const element = this.assertiveElements[ this.assertiveElementIndex ];
this.updateLiveElement( element, textContent, utterance );
// update index for next time
this.assertiveElementIndex = ( this.assertiveElementIndex + 1 ) % this.assertiveElements.length;
}
else {
assert && assert( false, 'unsupported aria live prioirity' );
}
} );

// increment index so the next AriaLiveAnnouncer instance has different ids for its elements.
ariaLiveAnnouncerIndex++;
}
Expand All @@ -144,8 +116,25 @@ class AriaLiveAnnouncer extends Announcer {
}, options );

// Note that getTextToAlert will have side effects on the Utterance as the Utterance
// may have have logic that changes its alert content each time it is used
this.announcingEmitter.emit( utterance.getTextToAlert( this.respectResponseCollectorProperties ), options.ariaLivePriority, utterance );
// may have logic that changes its alert content each time it is used
const textContent = utterance.getTextToAlert( this.respectResponseCollectorProperties );

if ( options.ariaLivePriority === AriaLive.POLITE ) {
const element = this.politeElements[ this.politeElementIndex ];
this.updateLiveElement( element, textContent, utterance );

// update index for next time
this.politeElementIndex = ( this.politeElementIndex + 1 ) % this.politeElements.length;
}
else if ( options.ariaLivePriority === AriaLive.ASSERTIVE ) {
const element = this.assertiveElements[ this.assertiveElementIndex ];
this.updateLiveElement( element, textContent, utterance );
// update index for next time
this.assertiveElementIndex = ( this.assertiveElementIndex + 1 ) % this.assertiveElements.length;
}
else {
assert && assert( false, 'unsupported aria live prioirity' );
}

// With aria-live we don't have information about when the screen reader is done speaking
// the content, so we have to emit this right away
Expand Down Expand Up @@ -199,7 +188,7 @@ class AriaLiveAnnouncer extends Announcer {
}

// @public - Possible values for the `aria-live` attribute (priority) that can be alerted (like "polite" and
// "assertive"), see AriaLiveAnnouncer.announcingEmitter for details.
// "assertive"), see AriaLiveAnnouncer.announce options for details.
AriaLiveAnnouncer.AriaLive = AriaLive;

utteranceQueueNamespace.register( 'AriaLiveAnnouncer', AriaLiveAnnouncer );
Expand Down
6 changes: 6 additions & 0 deletions js/Utterance.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ class Utterance {
// @public - observable for the priority, can be set to change the priority of this Utterance
// while it is still in the UtteranceQueue. See options documentation for behavior of priority.
this.priorityProperty = new NumberProperty( options.priority );

// @public (read-only) {string|null}l - the previous value of "getAlertText", which formulates the alert into a
// string, depending on criteria.
this.previousAlertText = null;
}

/**
Expand Down Expand Up @@ -171,6 +175,7 @@ class Utterance {
alert = this.getAlertStringFromResponsePacket( alert, respectResponseCollectorProperties );
}

this.previousAlertText = alert;
return alert;
}

Expand Down Expand Up @@ -237,6 +242,7 @@ class Utterance {
*/
reset() {
this.numberOfTimesAlerted = 0;
this.previousAlertText = null;
}
}

Expand Down
9 changes: 6 additions & 3 deletions js/UtteranceTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ QUnit.module( 'Utterance', {
}, timerInterval * 1000 );

// whenever announcing, get a callback and populate the alerts array
ariaLiveAnnouncer.announcingEmitter.addListener( text => {
alerts.unshift( text );
ariaLiveAnnouncer.announcementCompleteEmitter.addListener( utterance => {
alerts.unshift( utterance.previousAlertText );
} );

// slightly slower than the interval that the utteranceQueue will wait so we don't have a race condition
Expand Down Expand Up @@ -75,6 +75,10 @@ QUnit.test( 'Basic Utterance testing', async assert => {
utteranceQueue.addToBack( utterance );
await timeout( sleepTiming );
assert.ok( alerts[ 0 ] === otherAlert, 'second alert made it to ariaLiveAnnouncer' );

utterance.reset();
assert.ok( utterance.numberOfTimesAlerted === 0, 'numberOfTimesAlerted reset' );
assert.ok( utterance.previousAlertText === null, 'previousAlertText reset' );
} );

QUnit.test( 'Utterance options', async assert => {
Expand Down Expand Up @@ -213,7 +217,6 @@ QUnit.test( 'alertMaximumDelay tests', async assert => {
await timeout( 150 );
assert.ok( utteranceQueue.queue.length === 0, 'not stable, but past max' );
assert.ok( alerts[ 0 ] === rapidlyChanging, 'it was announced' );

} );

QUnit.test( 'announceImmediately', async assert => {
Expand Down

0 comments on commit dd290dd

Please sign in to comment.