From a724b26d5435db7647941f4f62e6ca3a12a63b06 Mon Sep 17 00:00:00 2001 From: Florrie Date: Tue, 5 Sep 2017 21:20:02 -0300 Subject: Cache flattened playlist --- src/pickers2.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'src/pickers2.js') diff --git a/src/pickers2.js b/src/pickers2.js index f1b53eb..f07cd91 100644 --- a/src/pickers2.js +++ b/src/pickers2.js @@ -187,6 +187,8 @@ function sortFlattenGrouplike(grouplike, sort, seed) { } } +const flattenedCache = Symbol('Cache of flattened') + function generalPicker(playlist, lastTrack, options) { const { sort, loop } = options @@ -204,11 +206,22 @@ function generalPicker(playlist, lastTrack, options) { throw new Error(`Invalid loop mode: ${loop}`) } - const flattened = sortFlattenGrouplike(playlist, sort, options.seed) - if (typeof options.seed === 'undefined') { - options.seed = flattened.newSeed + // Regenerating the flattened list is really time-expensive, so we make sure + // to cache the result of the operation (in the 'options' property, which is + // used to store "state"-specific data for the picker). + let flattened + if (options.hasOwnProperty(flattenedCache)) { + flattened = options[flattenedCache] + } else { + flattened = sortFlattenGrouplike(playlist, sort, options.seed) + + if (typeof options.seed === 'undefined') { + options.seed = flattened.newSeed + } + delete flattened.newSeed + + options[flattenedCache] = flattened } - delete flattened.newSeed const index = flattened.items.indexOf(lastTrack) @@ -225,9 +238,11 @@ function generalPicker(playlist, lastTrack, options) { // Deletes the random number generation seed then starts over. Assigning // a new RNG seed makes it so we get a new shuffle the next time, and // clearing the lastTrack value makes generalPicker thinks we're - // starting over. + // starting over. We also need to destroy the flattenedCache, or else it + // won't actually recalculate the list. const newSeed = seedRandom(options.seed)() options.seed = newSeed + delete options[flattenedCache] return generalPicker(playlist, null, options) } -- cgit 1.3.0-6-gf8a5