src/client/label_matcher.js
function createLabelMatcher(speechEmitter) {
  if (speechEmitter == null) {
    throw TypeError('speech emitter must be provided');
  }

  if (!(speechEmitter instanceof SpeechEmitter)) {
    throw TypeError('first argument must be a speech emitter');
  }

  // This will be used to register events for fetched labels
  const labelEmitter = new SpeechEmitter();
  const sentencePattern = /.*/;

  // This will fetch labels and emit them whenever there is an incoming sentence
  const fetchHandler = (sentence) => {
    // e.g. ' ' (space) will be replaced with '%20'
    const encodedSentence = encodeURIComponent(sentence);
    const labelQueryUrl = `${settings.apiUrl}/label?sentence=${encodedSentence}`;
    const request = new Request(labelQueryUrl);

    fetch(request).then(response => response.json()).then(({ label }) => {
      labelEmitter.emit(label, sentence);

      // Re-register event listener after it (could have been) zeroed by the speech node.
      // Here we wait run the registration in the next event loop to ensure all promises
      // have been resolved
      setTimeout(() => {
        speechEmitter.once(sentencePattern, fetchHandler);
      });
    })
    .catch((error) => {
      console.error(error);
    });
  };

  // Here we use the once() method and not the on() method we re-register the event
  // listener once a fetch has been done
  speechEmitter.once(sentencePattern, fetchHandler);

  // A factory function which will generate an event handler for matching sentences
  // against fetched labels. Be sure to call the dispose() method once you don't need
  // the labels logic anymore! Otherwise requests will keep being made to the server
  // in the background
  const matchLabel = (label) => {
    if (label == null) {
      throw TypeError('label must be provided');
    }

    if (typeof label != 'string') {
      throw TypeError('label must be a string');
    }

    // An async event handler which returns a promise
    return () => new Promise((resolve) => {
      // The promise will resolve itself whenever an emitted label matches the
      // expected label
      const labelTest = (actualLabel, sentence) => {
        // This makes it a one-time test
        labelEmitter.off(labelTest);

        if (actualLabel == label) {
          // These are the arguments with whom the event handler will be invoked with
          return [sentence, label];
        }
      };

      labelEmitter.on(labelTest, resolve);
    });
  };

  // The disposal methods disposes all the registered label events and it stops the
  // auto-fetching to the server whenever there is an incoming sentence
  matchLabel.dispose = () => {
    speechEmitter.off(sentencePattern, fetchHandler);
    labelEmitter.off();
  };

  return matchLabel;
}
function createLabelMatcher(speechEmitter) {
  if (speechEmitter == null) {
    throw TypeError('speech emitter must be provided');
  }

  if (!(speechEmitter instanceof SpeechEmitter)) {
    throw TypeError('first argument must be a speech emitter');
  }

  // This will be used to register events for fetched labels
  const labelEmitter = new SpeechEmitter();
  const sentencePattern = /.*/;

  // This will fetch labels and emit them whenever there is an incoming sentence
  const fetchHandler = (sentence) => {
    // e.g. ' ' (space) will be replaced with '%20'
    const encodedSentence = encodeURIComponent(sentence);
    const labelQueryUrl = `${settings.apiUrl}/label?sentence=${encodedSentence}`;
    const request = new Request(labelQueryUrl);

    fetch(request).then(response => response.json()).then(({ label }) => {
      labelEmitter.emit(label, sentence);

      // Re-register event listener after it (could have been) zeroed by the speech node.
      // Here we wait run the registration in the next event loop to ensure all promises
      // have been resolved
      setTimeout(() => {
        speechEmitter.once(sentencePattern, fetchHandler);
      });
    })
    .catch((error) => {
      console.error(error);
    });
  };

  // Here we use the once() method and not the on() method we re-register the event
  // listener once a fetch has been done
  speechEmitter.once(sentencePattern, fetchHandler);

  // A factory function which will generate an event handler for matching sentences
  // against fetched labels. Be sure to call the dispose() method once you don't need
  // the labels logic anymore! Otherwise requests will keep being made to the server
  // in the background
  const matchLabel = (label) => {
    if (label == null) {
      throw TypeError('label must be provided');
    }

    if (typeof label != 'string') {
      throw TypeError('label must be a string');
    }

    // An async event handler which returns a promise
    return () => new Promise((resolve) => {
      // The promise will resolve itself whenever an emitted label matches the
      // expected label
      const labelTest = (actualLabel, sentence) => {
        // This makes it a one-time test
        labelEmitter.off(labelTest);

        if (actualLabel == label) {
          // These are the arguments with whom the event handler will be invoked with
          return [sentence, label];
        }
      };

      labelEmitter.on(labelTest, resolve);
    });
  };

  // The disposal methods disposes all the registered label events and it stops the
  // auto-fetching to the server whenever there is an incoming sentence
  matchLabel.dispose = () => {
    speechEmitter.off(sentencePattern, fetchHandler);
    labelEmitter.off();
  };

  return matchLabel;
}
function createLabelMatcher ( speechEmitter ) { if ( speechEmitter == null ) { throw TypeError ( 'speech emitter must be provided' ) ; } if ( ! ( speechEmitter instanceof SpeechEmitter ) ) { throw TypeError ( 'first argument must be a speech emitter' ) ; } // This will be used to register events for fetched labels const labelEmitter = new SpeechEmitter ( ) ; const sentencePattern = / .* / ; // This will fetch labels and emit them whenever there is an incoming sentence const fetchHandler = ( sentence ) => { // e.g. ' ' (space) will be replaced with '%20' const encodedSentence = encodeURIComponent ( sentence ) ; const labelQueryUrl = ` ${ settings . apiUrl } ${ encodedSentence } ` ; const request = new Request ( labelQueryUrl ) ; fetch ( request ) . then ( response => response . json ( ) ) . then ( ( { label } ) => { labelEmitter . emit ( label , sentence ) ; // Re-register event listener after it (could have been) zeroed by the speech node. // Here we wait run the registration in the next event loop to ensure all promises // have been resolved setTimeout ( ( ) => { speechEmitter . once ( sentencePattern , fetchHandler ) ; } ) ; } ) . catch ( ( error ) => { console . error ( error ) ; } ) ; } ; // Here we use the once() method and not the on() method we re-register the event // listener once a fetch has been done speechEmitter . once ( sentencePattern , fetchHandler ) ; // A factory function which will generate an event handler for matching sentences // against fetched labels. Be sure to call the dispose() method once you don't need // the labels logic anymore! Otherwise requests will keep being made to the server // in the background const matchLabel = ( label ) => { if ( label == null ) { throw TypeError ( 'label must be provided' ) ; } if ( typeof label != 'string' ) { throw TypeError ( 'label must be a string' ) ; } // An async event handler which returns a promise return ( ) => new Promise ( ( resolve ) => { // The promise will resolve itself whenever an emitted label matches the // expected label const labelTest = ( actualLabel , sentence ) => { // This makes it a one-time test labelEmitter . off ( labelTest ) ; if ( actualLabel == label ) { // These are the arguments with whom the event handler will be invoked with return [ sentence , label ] ; } } ; labelEmitter . on ( labelTest , resolve ) ; } ) ; } ; // The disposal methods disposes all the registered label events and it stops the // auto-fetching to the server whenever there is an incoming sentence matchLabel . dispose = ( ) => { speechEmitter . off ( sentencePattern , fetchHandler ) ; labelEmitter . off ( ) ; } ; return matchLabel ; }
This will start listening for incoming sentences and will fetch labels from the server each time a speech was recognized. It will return a factory function which will generate an event handler for testing sentences against fetched labels
This will start listening for incoming sentences and will fetch labels from the server each time a speech was recognized . It will return a factory function which will generate an event handler for testing sentences against fetched labels