Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

cleanup and simplify

Jason Kent1e684acb29df
diff --git a/web/js/components/animation-widget/play-queue.js b/web/js/components/animation-widget/play-queue.js
index e151f19b8..2003c48f6 100644
--- a/web/js/components/animation-widget/play-queue.js
+++ b/web/js/components/animation-widget/play-queue.js
@@ -5,96 +5,95 @@ import PQueue from 'p-queue/dist';
import PreloadSpinner from './preload-spinner';
import util from '../../util/util';
-/*
- * Preload and play animation
- *
- * @class PlayAnimation
- * @extends React.Component
- */
-class PlayAnimation extends React.Component {
+const fetchTimes = [0];
+const toString = (date) => util.toISOStringSeconds(date);
+const toDate = (dateString) => util.parseDateUTC(dateString);
+
+class PlayQueue extends React.Component {
constructor(props) {
super(props);
this.state = {
isPlaying: false,
};
this.queue = new PQueue({ concurrency: 2 });
- this.preloadObject = {};
this.inQueueObject = {};
+ this.preloadObject = {};
this.preloadedArray = [];
this.pastDates = {};
- this.interval = 0;
- this.currentPlayingDate = this.getStartDate();
- this.checkQueue();
- this.checkShouldPlay();
+ this.playInterval = 0;
}
componentDidMount() {
this.mounted = true;
+
+ this.queue.on('completed', (dateStr) => {
+ // console.log(dateStr);
+ });
+
+ this.queue.on('active', () => {
+ // console.log('qSize', this.queue.size);
+ });
+
+ this.playingDate = this.getStartDate();
+ this.checkQueue();
+ this.checkShouldPlay();
}
componentWillUnmount() {
this.mounted = false;
- this.queue.queue = [];
- if (this.interval) {
- clearInterval(this.interval);
+ this.clearCache();
+ this.queue.clear();
+ if (this.playInterval) {
+ clearInterval(this.playInterval);
}
}
/**
- * Determines whether to start at
- * current date or to start at the
- * selected start date
- *
- * @returns {string} ISO string Date
+ * Determines whether to start at current date or the selected start date
*/
getStartDate() {
const { endDate, startDate, currentDate } = this.props;
const nextDate = this.nextDate(currentDate);
if (currentDate > startDate && nextDate < endDate) {
- return util.toISOStringSeconds(nextDate);
+ return toString(nextDate);
}
- return util.toISOStringSeconds(startDate);
+ return toString(startDate);
}
/**
- * Gets the last date that should be added
- * to the queuer
- *
- * @param currentDate {object} JS date
- * @param startDate {object} JS date
- * @param endDate {object} JS date
- *
- * @returns {string} Date string
+ * Gets the last date that should be added to the queue
*/
- getLastBufferDateStr = function(currentDate, startDate, endDate) {
- const { queueLength, isLoopActive } = this.props;
- let day = currentDate;
+ getLastBufferDateStr = function() {
+ const {
+ queueLength, isLoopActive, startDate, endDate,
+ } = this.props;
+ let currentDate = toDate(this.playingDate);
let i = 1;
while (i < queueLength) {
- if (this.nextDate(day) > endDate) {
+ if (this.nextDate(currentDate) > endDate) {
if (!isLoopActive) {
- return util.toISOStringSeconds(day);
+ return toString(currentDate);
}
- day = startDate;
+ currentDate = startDate;
} else {
- day = this.nextDate(day);
+ currentDate = this.nextDate(currentDate);
}
i += 1;
}
- return util.toISOStringSeconds(day);
+ return toString(currentDate);
};
/**
* adds dates to precache queuer
*
* @param currentDate {object} JS date
- * @param startDate {object} JS date
- * @param endDate {object} JS date
* @param lastToQueue {string} date String
*/
- initialPreload(currentDate, startDate, endDate, lastToQueue) {
- const { queueLength, selectDate, togglePlaying } = this.props;
+ initialPreload(currentDate, lastToQueue) {
+ const {
+ queueLength, selectDate, togglePlaying, startDate,
+ } = this.props;
let day = currentDate;
if (queueLength <= 1) {
// if only one frame will play just move to that date
@@ -104,8 +103,8 @@ class PlayAnimation extends React.Component {
}
for (let i = 0; i < queueLength; i += 1) {
this.addDate(day);
- day = this.getNextBufferDate(day, startDate, endDate);
- if (util.toISOStringSeconds(day) === lastToQueue) {
+ day = this.getNextBufferDate();
+ if (toString(day) === lastToQueue) {
this.addDate(day);
}
}
@@ -113,30 +112,26 @@ class PlayAnimation extends React.Component {
/**
* Determine if we should play
- *
- * @param isLoopStart {Boolean}
*/
- checkShouldPlay = function(isLoopStart) {
- const { startDate, endDate, hasCustomPalettes } = this.props;
+ checkShouldPlay = function(loopStart) {
+ const {
+ startDate, hasCustomPalettes,
+ } = this.props;
const { isPlaying } = this.state;
- const currentDate = util.parseDateUTC(this.currentPlayingDate);
- const lastToQueue = this.getLastBufferDateStr(currentDate, startDate, endDate);
+ const currentDate = toDate(this.playingDate);
+ const lastToQueue = this.getLastBufferDateStr();
+ const restartLoop = loopStart && currentDate.getTime() === startDate.getTime();
- if (isPlaying && !isLoopStart) {
+ if (isPlaying && !loopStart) {
return false;
}
- if (this.preloadObject[lastToQueue]) {
- return this.play(this.currentPlayingDate);
- }
- if (
- hasCustomPalettes
- && this.preloadObject[this.currentPlayingDate]
- && lodashIsEmpty(this.inQueueObject)
- ) {
- return this.play(this.currentPlayingDate);
+
+ // TODO instead of checking lastToQueue check preload time?
+ if (this.preloadObject[lastToQueue] || restartLoop) {
+ return this.play(this.playingDate);
}
- if (isLoopStart && currentDate.getTime() === startDate.getTime()) {
- return this.play(this.currentPlayingDate);
+ if (hasCustomPalettes && this.preloadObject[this.playingDate] && lodashIsEmpty(this.inQueueObject)) {
+ return this.play(this.playingDate);
}
this.shiftCache();
};
@@ -148,7 +143,7 @@ class PlayAnimation extends React.Component {
const { isLoopActive, startDate, togglePlaying } = this.props;
if (isLoopActive) {
this.shiftCache();
- this.currentPlayingDate = util.toISOStringSeconds(startDate);
+ this.playingDate = toString(startDate);
setTimeout(() => {
this.checkShouldPlay(true);
this.checkQueue();
@@ -160,7 +155,6 @@ class PlayAnimation extends React.Component {
/**
* Determines what dates should be queued
- *
*/
checkQueue() {
const {
@@ -171,15 +165,12 @@ class PlayAnimation extends React.Component {
queueLength,
maxQueueLength,
} = this.props;
- const currentDate = util.parseDateUTC(this.currentPlayingDate);
- const lastToQueue = this.getLastBufferDateStr(currentDate, startDate, endDate);
+ const currentDate = toDate(this.playingDate);
+ const lastToQueue = this.getLastBufferDateStr();
const nextDate = this.nextDate(currentDate);
- if (
- !this.preloadedArray[0]
- && !this.inQueueObject[this.currentPlayingDate]
- ) {
- this.initialPreload(currentDate, startDate, endDate, lastToQueue);
+ if (!this.preloadedArray[0] && !this.inQueueObject[this.playingDate]) {
+ this.initialPreload(currentDate, lastToQueue);
} else if (
// TODO Can't lookup this array entry with date string as key. Is this causing bugs?
!this.preloadedArray[lastToQueue]
@@ -188,7 +179,7 @@ class PlayAnimation extends React.Component {
&& !canPreloadAll
) {
// if last preload date doesn't exist
- this.addItemToQueue(currentDate, startDate, endDate);
+ this.addItemToQueue(startDate, endDate);
} else if (
hasCustomPalettes
&& this.preloadedArray[0]
@@ -199,34 +190,19 @@ class PlayAnimation extends React.Component {
}
}
- /**
- * Clears precache
- */
- clearCache() {
+ clearCache = () => {
this.preloadObject = {};
this.preloadedArray = [];
this.inQueueObject = {};
}
- /**
- * Gets next date based on current
- * increments
- * @param date {object} JS date obj
- *
- * @returns {object} JS Date
- */
nextDate(date) {
const { interval, delta } = this.props;
return util.dateAdd(date, interval, delta);
}
/*
- * Custom date queuer created for
- * custom colormaps
- *
- * @param currentDate {object} JS date
- * @param startDate {object} JS date
- * @param endDate {object} JS date
+ * Custom date queuer created for custom colormaps
*/
customQueuer(currentDate, startDate, endDate) {
const { isPlaying } = this.state;
@@ -234,55 +210,35 @@ class PlayAnimation extends React.Component {
if (nextDate > endDate) {
nextDate = startDate;
}
- const nextDateStr = util.toISOStringSeconds(nextDate);
- if (
- !this.preloadObject[nextDateStr]
- && !this.inQueueObject[nextDateStr]
- && !isPlaying
- ) {
+ const nextDateStr = toString(nextDate);
+ if (!this.preloadObject[nextDateStr] && !this.inQueueObject[nextDateStr] && !isPlaying) {
this.clearCache();
this.checkQueue();
}
}
- /*
- * Add date to loading Queue obj
- *
- * @param date {object} JS date obj
- */
- addToInQueue(date) {
- const strDate = util.toISOStringSeconds(date);
- this.inQueueObject[strDate] = date;
- this.preloadedArray.push(strDate);
- }
-
/**
- * removes item from cache after it has
- * been played and quelimit reached
+ * Removes item from cache after it has been played and quelimit reached
*/
shiftCache() {
const { queueLength, canPreloadAll } = this.props;
- if (
- this.preloadObject[this.preloadedArray[0]]
- && util.objectLength(this.preloadObject) > queueLength
+ const hasPlayed = !this.isInToPlayGroup(this.preloadedArray[0])
&& this.pastDates[this.preloadedArray[0]]
- && !this.isInToPlayGroup(this.preloadedArray[0])
- && !canPreloadAll
- ) {
+ && this.preloadObject[this.preloadedArray[0]];
+ const preloadExceedsQueue = util.objectLength(this.preloadObject) > queueLength;
+
+ if (hasPlayed && preloadExceedsQueue && !canPreloadAll) {
+ // TODO does this cause stuttering? Why do we need to remove previously preloaded dates?
const key = this.preloadedArray.shift();
delete this.preloadObject[key];
delete this.pastDates[key];
}
}
- /**
- * @param currentDate {object} JS date
- * @param startDate {object} JS date
- * @param endDate {object} JS date
- */
- getNextBufferDate(currentDate, startDate, endDate) {
+ getNextBufferDate() {
+ const { startDate, endDate } = this.props;
const strDate = this.preloadedArray[this.preloadedArray.length - 1];
- const lastInBuffer = util.parseDateUTC(strDate);
+ const lastInBuffer = toDate(strDate);
const nextDate = this.nextDate(lastInBuffer);
if (lastInBuffer >= endDate || nextDate > endDate) {
return startDate;
@@ -296,17 +252,14 @@ class PlayAnimation extends React.Component {
* @param startDate {object} JS date
* @param endDate {object} JS date
*/
- addItemToQueue(currentDate, startDate, endDate) {
+ addItemToQueue(startDate, endDate) {
const { speed } = this.props;
- const nextDate = this.getNextBufferDate(currentDate, startDate, endDate);
- const nextDateStr = util.toISOStringSeconds(nextDate);
-
- if (
- !this.inQueueObject[nextDateStr]
- && !this.preloadObject[nextDateStr]
- && nextDate <= endDate
- && nextDate >= startDate
- ) {
+ const nextDate = this.getNextBufferDate();
+ const nextDateStr = toString(nextDate);
+ const dateInRange = nextDate <= endDate && nextDate >= startDate;
+ const shouldQueue = !this.inQueueObject[nextDateStr] && !this.preloadObject[nextDateStr];
+
+ if (shouldQueue && dateInRange) {
setTimeout(() => {
this.addDate(nextDate);
this.checkQueue();
@@ -315,8 +268,7 @@ class PlayAnimation extends React.Component {
}
/**
- * checks if this date is in array of dates that need
- * to play in future within buffer length
+ * checks if this date is in array of dates that need to play in future within buffer length
*
* @param testDate {string} JS date string
*/
@@ -325,18 +277,19 @@ class PlayAnimation extends React.Component {
startDate, endDate, isLoopActive, queueLength,
} = this.props;
let i = 0;
- let day = util.parseDateUTC(this.currentPlayingDate);
- const jsTestDate = util.parseDateUTC(testDate);
+ let currentDate = toDate(this.playingDate);
+ const jsTestDate = toDate(testDate);
+
while (i < queueLength) {
- if (this.nextDate(day) > endDate) {
+ if (this.nextDate(currentDate) > endDate) {
if (!isLoopActive) {
return false;
}
- day = startDate;
+ currentDate = startDate;
} else {
- day = this.nextDate(day);
+ currentDate = this.nextDate(currentDate);
}
- if (day.valueOf() === jsTestDate.valueOf()) {
+ if (currentDate.valueOf() === jsTestDate.valueOf()) {
return true;
}
i += 1;
@@ -346,88 +299,80 @@ class PlayAnimation extends React.Component {
/**
* Gets next date based on current increments
- * @param date {object} JS date obj
- *
- * @returns {object} JS Date
*/
- addDate(date) {
+ async addDate(date) {
const { promiseImageryForTime } = this.props;
- const strDate = util.toISOStringSeconds(date);
-
+ const strDate = toString(date);
if (this.inQueueObject[strDate] || this.preloadObject[strDate]) {
return;
}
+ this.inQueueObject[strDate] = date;
+ this.preloadedArray.push(strDate);
- this.addToInQueue(date);
- this.queue
- .add(() => promiseImageryForTime(date))
- .then((addedDate) => {
- if (this.mounted) {
- this.preloadObject[strDate] = addedDate;
- delete this.inQueueObject[strDate];
- this.shiftCache();
- this.checkQueue();
- this.checkShouldPlay();
- }
- });
+ await this.queue.add(async () => {
+ const startTime = Date.now();
+ await promiseImageryForTime(date);
+ fetchTimes.push(Date.now() - startTime);
+ return strDate;
+ });
+
+ if (!this.mounted) return;
+ this.preloadObject[strDate] = strDate;
+ delete this.inQueueObject[strDate];
+ this.shiftCache();
+ this.checkQueue();
+ this.checkShouldPlay();
}
- /**
- * removes loader and starts the animation
- *
- * @param index {string} date string
- */
- play(dateStr) {
+ play() {
const { togglePlaying } = this.props;
const { isPlaying } = this.state;
if (!isPlaying) this.setState({ isPlaying: true });
-
- this.animate(dateStr);
+ this.animate();
if (document.hidden) {
togglePlaying();
}
}
stopPlaying() {
- clearInterval(this.interval);
+ clearInterval(this.playInterval);
this.setState({ isPlaying: false });
}
/**
* loops through frame at a specified time interval
- *
- * @param index {string} Date string
*/
- animate(index) {
+ animate() {
const {
selectDate, endDate, speed, isPlaying,
} = this.props;
- let currentDateStr = index;
+ let currentDateStr = this.playingDate;
+ let nextDate;
let nextDateStr;
- let nextDateParsed;
+
const player = () => {
if (!this.mounted) {
- return clearInterval(this.interval);
+ return clearInterval(this.playInterval);
}
- const currentDateParsed = util.parseDateUTC(currentDateStr);
- nextDateParsed = this.nextDate(currentDateParsed);
- nextDateStr = util.toISOStringSeconds(nextDateParsed);
+ const currentDate = toDate(currentDateStr);
+ nextDate = this.nextDate(currentDate);
+ nextDateStr = toString(nextDate);
this.shiftCache();
this.checkQueue();
if (isPlaying) {
- selectDate(currentDateParsed);
+ selectDate(currentDate);
console.log(currentDateStr);
}
- this.pastDates[currentDateStr] = currentDateParsed;
- this.currentPlayingDate = currentDateStr;
+ this.pastDates[currentDateStr] = currentDate;
+ this.playingDate = currentDateStr;
// Advance to next
currentDateStr = nextDateStr;
// End of animation range
- if (nextDateParsed > endDate) {
- clearInterval(this.interval);
+ if (nextDate > endDate) {
+ clearInterval(this.playInterval);
this.checkShouldLoop();
return;
}
@@ -443,9 +388,9 @@ class PlayAnimation extends React.Component {
this.stopPlaying();
}
this.checkQueue();
- this.interval = setTimeout(player, 1000 / speed);
+ this.playInterval = setTimeout(player, 1000 / speed);
};
- this.interval = setTimeout(player, speed);
+ this.playInterval = setTimeout(player, speed);
}
render() {
@@ -464,7 +409,7 @@ class PlayAnimation extends React.Component {
}
}
-PlayAnimation.propTypes = {
+PlayQueue.propTypes = {
endDate: PropTypes.object.isRequired,
isPlaying: PropTypes.bool.isRequired,
promiseImageryForTime: PropTypes.func.isRequired,
@@ -483,4 +428,4 @@ PlayAnimation.propTypes = {
onClose: PropTypes.func,
};
-export default PlayAnimation;
+export default PlayQueue;