Clear the garbage collection of speech utterance objects stored within an array

The clearGarbageCollection methods cleans the garbage collection of all the SpeechSynthesisUtterance objects in the artyom library (using the artyom.say function).

Due to a bug till the date (14.01.2017) in the speechSynthesis API, the garbage collector of javascript removes after a couple of seconds many SpeechSynthesisUtterance objects that still without being spoken. Read more about the bug here in the official Chrome Bug Tracker. That means that when artyom try to speak other SynthesisUtteranceObject in the queue (and that doesn't exist now due to the garbage collection), the API will stop working and you need to execute the artyom.shutUp method or speechSynthesis.cancel as well. To solve this problem, all the speechSynthesis objects are stored in an internal variable (an array) of the library.

Cleaning the garbage collection

To clean the garbage collection, be sure that not any SpeechUtterance is pending (execute it on the last execution of artyom.say):

let artyom = new Artyom();

// The clear command needs to be executed 
// always in the last artyom.say function
artyom.say("Hello , this is a long text 1 .");
artyom.say("Hello , this is a long text 2.");
artyom.say("Hello , this is a long text 3.");
artyom.say("Hello , this is a long text 4.");
artyom.say("Hello , this is a long text 5.");
artyom.say("Hello , this is a long text 6.");
artyom.say("Hello , this is a long text 7.");
artyom.say("Hello , this is a long text. Now i'll clean the garbage collection.",{
    onEnd: function(){
        var totalObjectsInCollection = artyom.getGarbageCollection().length;
        // Clear now that there are no more text to say.
        artyom.clearGarbageCollection();
        alert("The garbage collection has been cleaned. "+totalObjectsInCollection+" Items found. Now there are " + artyom.getGarbageCollection().length);
    }
});

Why you need to clean the garbage collection

If you're using artyom for simple things, then you won't need to do this. However, if you synthesize a lot of text (50K Characters), then the text will be processed by the artyom.say function and it will end up with a huge array of more than 20K (approximately as this can vary) that you may want to clean because after the text is spoken, it isn't important anymore.

To understand with code, see the following snippet:

// Remember that SpeechSynthesis is an experimental API
// Therefore, there are many unsolved bugs originally

// @INSTRUCTION 1
// This will add 1 item to the garbage collection
artyom.say("This is a semi long text that will be spoken",{
    onEnd: function(){
        // If it is executed here , the intruction 2 will be never executed
        // Because you just cleaned all the existent SpeechSynthesisObjects
        // This is safe only if is executed in the onEnd callback and you're
        // sure that no more artyom.say functions will be executed after this instruction
        artyom.clearGarbageCollection();
    }
});

artyom.say("What's up",{
    onEnd: function(){
        console.log("You may see this text in the console.");
    }
});

artyom.say("I'm trying to do something here. Please shut up your mouth.",{
    onEnd: function(){
        console.log("Probably this not ...");
    }
});

// More instructions ...
artyom.say("Hello, this other text that will be spoken",{
    onEnd: function(){
        console.log("You may see this text in the console but sometimes don't");
    }
});

Remember that you can clean the garbage collection only in the onEnd callback after the last artyom.say execution.