Skip to content

recognizeMicrophone causes angular (6) change detection to stop working #83

@jackrvaughan

Description

@jackrvaughan

Not 100% sure why this happens, but whenever data returned from STT is used to create a new DOM element or component, change detection on those components don't seem to work.

For example:

this.sttStream.on('data', function (data) {
        here.sttResults = data.results
        here.changeDetectorRef.detectChanges()
})
  • Expected behavior

We'd expect angular change detection to work normally and not have to call it explicitly.

  • Actual behavior

If a UI element is bound to sttResults, it won't update unless changeDetectorRef.detectChanges() is called. It will also cause issues down the line - if you have a click function on an element that was created with STT results data, it won't fire any change detection unless you specifically tell angular there are changes (changeDetectorRef.detectChanges() or appRef.tick()).

For anyone else running into this issue, here is a post on manually triggering change detection: https://stackoverflow.com/questions/34827334/triggering-change-detection-manually-in-angular

  • Steps to reproduce

Maybe we're not using the STT recognizeMicrophone() in the correct way; in that case, maybe some examples for angular usage would be helpful. We call a function to kick off the STT:

  toggleMicrophone() {
    const here = this
    // Start the microphone if it is stopped
    if (this.microphoneState === 'stopped') {
      this.microphoneState = 'started'
      this.sttStream = WatsonSpeech.SpeechToText.recognizeMicrophone({
        token: this.sttToken,
        objectMode: true,
        format: true,
        resultsBySpeaker: true
      })

      this.sttStream.on('data', function (data) {
        here.sttResults = data.results
        // here.changeDetectorRef.detectChanges()
      })

    } else {
      this.sttStream.stop()
      this.microphoneState = 'stopped'
    }
  }

We pass the data to a new component to display (which doesn't update):

<app-transcript [sttData]="sttResults"></app-transcript>
  • Browser version(s)

Chrome Version 68.0.3440.106 (Official Build) (64-bit)

  • SDK version
"watson-speech": "^0.34.2"

Including colleague: @kylebabs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions