Skip to content

Commit

Permalink
Merge pull request #56 from Hagsten/feature/EsPromise
Browse files Browse the repository at this point in the history
Replaced old promise and XMLRequest to native ES versions
  • Loading branch information
Hagsten committed Jul 2, 2021
2 parents d35f1b7 + fd95c17 commit 7276dc4
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 348 deletions.
329 changes: 42 additions & 287 deletions dist/talkify.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/talkify.min.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions main.js
@@ -1,4 +1,3 @@
window.promise = require('./src/promise.js');
var talkify = require('./src/talkify.js');
var talkifyConfig = require('./src/talkify-config.js');
var talkifyUtils = require('./src/talkify-utils.js');
Expand All @@ -19,4 +18,4 @@ var talkifyVoiceCommands = require('./src/talkify-speech-recognition.js');
var talkifyFormReader = require('./src/talkify-formreader.js');
var talkifyTableReader = require('./src/table-reader/talkify-tablereader.js');
var talkifyTestSelectionActivator = require('./src/talkify-text-selection-activator.js');
var talkifyAutoScroll = require('./src/talkify-autoscroll.js');
var talkifyAutoScroll = require('./src/talkify-autoscroll.js');
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "talkify-tts",
"version": "3.8.0",
"version": "3.9.0",
"description": "A JavaScript text to speech (TTS) library. Provides you with high quality TTS voices in many languages and a high quality language. These voices and engines runs on a Talkify hosted server. This lib also supports browser built in voices via the SpeechSynthesis API if you rather not rely on Talkify servers.",
"browser": "dist/talkify.min.js",
"files": ["dist/styles/**/*.css"],
Expand Down
6 changes: 1 addition & 5 deletions src/control-centers/talkify-controlcenter-core.js
Expand Up @@ -619,11 +619,7 @@ talkify.playbar = function (parent, correlationId, controlcenter) {

function getRemoteVoices() {
talkify.http.get(talkify.config.remoteService.speechBaseUrl + "/voices")
.then(function (error, data) {
if (error !== false) {
return;
}

.then(function (data) {
voices = window.talkify.toLowerCaseKeys(data);

if (voiceNameElement.textContent) {
Expand Down
20 changes: 4 additions & 16 deletions src/talkify-ajax.js
@@ -1,24 +1,12 @@
talkify = talkify || {};
talkify.http = (function ajax() {

var get = function(url) {
var call = new promise.promise.Promise();

var get = function (url) {
var keypart = (url.indexOf('?') !== -1 ? "&key=" : "?key=") + talkify.config.remoteService.apiKey;

promise.promise
.get(window.talkify.config.remoteService.host + url + keypart)
.then(function(error, data) {
try {
var jsonObj = JSON.parse(data);
call.done(error, jsonObj);
} catch (e) {
call.done(e, data);
}

return fetch(window.talkify.config.remoteService.host + url + keypart)
.then(function (res) {
return res.json();
});

return call;
};

return {
Expand Down
26 changes: 10 additions & 16 deletions src/talkify-html5-speechsynthesis-player.js
Expand Up @@ -335,30 +335,24 @@ talkify.Html5Player = function () {
return voice;
};

function getVoice() {
var p = new promise.promise.Promise();

function getVoice() {
if (me.forcedVoice) {
p.done(me.forcedVoice);

return p;
return Promise.resolve(me.forcedVoice);
}

if (window.speechSynthesis.getVoices().length) {
var voices = window.speechSynthesis.getVoices();

p.done(selectVoiceToPlay(voices));

return p;
return Promise.resolve(selectVoiceToPlay(voices));
}

window.speechSynthesis.onvoiceschanged = function () {
var voices = window.speechSynthesis.getVoices();

p.done(selectVoiceToPlay(voices));
};

return p;
return new Promise(function(resolve){
window.speechSynthesis.onvoiceschanged = function () {
var voices = window.speechSynthesis.getVoices();

resolve(selectVoiceToPlay(voices));
};
});
};

function stop() {
Expand Down
19 changes: 6 additions & 13 deletions src/talkify-player.js
Expand Up @@ -208,14 +208,7 @@ talkify.TtsPlayer = function (options) {
}

function getPositions(requestId) {
var p = new promise.promise.Promise();

talkify.http.get(talkify.config.remoteService.speechBaseUrl + "/marks?id=" + requestId)
.then(function (error, positions) {
p.done(null, positions);
});

return p;
return talkify.http.get(talkify.config.remoteService.speechBaseUrl + "/marks?id=" + requestId);
};

initialize.apply(this);
Expand Down Expand Up @@ -246,11 +239,11 @@ talkify.TtsPlayer = function (options) {
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status >= 400 && xhr.status <= 599) {
if (xhr.readyState === 4 && xhr.status >= 400 && xhr.status <= 599) {
talkify.messageHub.publish(me.correlationId + ".player.tts.download.error", xhr);
}
if (xhr.readyState === 4 && xhr.status === 200) {

if (xhr.readyState === 4 && xhr.status === 200) {
var link = document.createElement('a');

link.href = URL.createObjectURL(xhr.response);
Expand All @@ -266,7 +259,7 @@ talkify.TtsPlayer = function (options) {
})
);

setTimeout(function(){
setTimeout(function () {
document.body.removeChild(link);
}, 1000);

Expand Down Expand Up @@ -365,7 +358,7 @@ talkify.TtsPlayer = function (options) {
return;
}

getPositions(requestId).then(function (error, positions) {
getPositions(requestId).then(function (positions) {
me.currentContext.positions = positions || [];

talkify.messageHub.publish(me.correlationId + ".player.tts.loaded", me.currentContext.item);
Expand Down
11 changes: 4 additions & 7 deletions src/talkify-playlist.js
Expand Up @@ -559,14 +559,11 @@ talkify.playlist = function () {
var text = playlist.refrenceText.length <= 1000 ? playlist.refrenceText : playlist.refrenceText.substr(0, 1000);

talkify.http.get(talkify.config.remoteService.languageBaseUrl + "/detect?text=" + text)
.then(function (error, data) {
if (error) {
onComplete({ Cultures: [], Language: -1 });

return;
}

.then(function (data) {
onComplete(data);
})
.catch(function(){
onComplete({ Cultures: [], Language: -1 });
});

function onComplete(refLang) {
Expand Down

0 comments on commit 7276dc4

Please sign in to comment.