Skip to content

Commit

Permalink
Retrieve list of currently stored group ids. Issue #22
Browse files Browse the repository at this point in the history
Change-Id: Ifeaa4085ec289679ccd9cb1fc22bd400c9cd8198
  • Loading branch information
natalieharris authored and Gerrit Code Review committed Apr 7, 2015
1 parent ae8b3fb commit a0aa5e5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 16 deletions.
56 changes: 40 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,28 +136,47 @@ app.init = function() {
shaka.log.setLevel(shaka.log.Level.V1);
}

// Retrieve list of offline streams
var groups = app.getOfflineGroups_();
for (var key in groups) {
var value = groups[key];
app.addOfflineStream_(key, value);
}
// Retrieve and verify list of offline streams
var storage = new shaka.util.ContentDatabase(null);
storage.setUpDatabase().then(
function() {
return storage.retrieveGroupIds();
}
).then(
function(groupIds) {
var groups = app.getOfflineGroups_();
for (var i in groupIds) {
var id = groupIds[i];
var value = groups[id];
if (!value) {
value = 'Unknown Stream ID ' + id;
}
app.addOfflineStream_(value, id);
}
}
).then(
function() {
storage.closeDatabaseConnection();
}
).catch(
function(e) {
storage.closeDatabaseConnection();
console.error('Error retrieving stored data', e);
}
);

app.onMpdChange();

playerControls.init(app.video_);

if ('dash' in params) {
document.getElementById('streamTypeList').value = 'dash';
app.onStreamTypeChange();
app.loadStream();
} else if ('http' in params) {
document.getElementById('streamTypeList').value = 'http';
app.onStreamTypeChange();
app.loadStream();
} else {
app.onStreamTypeChange();
}
app.onStreamTypeChange();

if ('cycleVideo' in params) {
app.cycleVideo();
Expand Down Expand Up @@ -362,6 +381,11 @@ app.cycleTracks_ =
* Deletes a group from storage.
*/
app.deleteStream = function() {
if (!app.player_) {
app.installPolyfills_();
app.initPlayer_();
}

var deleteButton = document.getElementById('deleteButton');
deleteButton.disabled = true;
deleteButton.innerText = 'Deleting stream...';
Expand All @@ -379,7 +403,7 @@ app.deleteStream = function() {
app.offlineStreams_.splice(deleted, 1);
offlineList.removeChild(offlineList.childNodes[deleted]);
var groups = app.getOfflineGroups_();
delete groups[text];
delete groups[groupId];
app.setOfflineGroups_(groups);
deleteButton.innerText = 'Delete stream from storage';
app.onStreamTypeChange();
Expand Down Expand Up @@ -416,7 +440,7 @@ app.storeStream = function() {
).then(
function(groupId) {
var groups = app.getOfflineGroups_();
groups[mediaUrl] = groupId;
groups[groupId] = mediaUrl;
app.setOfflineGroups_(groups);
app.addOfflineStream_(mediaUrl, groupId);
app.updateStoreButton_(true, 'Stream already stored');
Expand All @@ -432,15 +456,15 @@ app.storeStream = function() {

/**
* Get a map of MPD URLs to group IDs for all streams stored offline.
* @return {!Object.<string, number>}
* @return {!Object.<number, string>}
* @private
*/
app.getOfflineGroups_ = function() {
try {
var data = window.localStorage.getItem('offlineGroups') || '{}';
// JSON.parse can throw if the data stored isn't valid JSON.
var groups = JSON.parse(data);
return /** @type {!Object.<string, number>} */(groups);
return /** @type {!Object.<number, string>} */(groups);
} catch (exception) {
console.debug('Disregarding stored offlineGroups.');
return {};
Expand All @@ -449,8 +473,8 @@ app.getOfflineGroups_ = function() {


/**
* Store a map of MPD URLs to group IDs for all streams stored offline.
* @param {!Object.<string, number>} groups
* Store a map of group IDs to MPD URLs for all streams stored offline.
* @param {!Object.<number, string>} groups
* @private
*/
app.setOfflineGroups_ = function(groups) {
Expand Down
25 changes: 25 additions & 0 deletions lib/util/content_database.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,31 @@ shaka.util.ContentDatabase.prototype.retrieveGroup = function(groupId) {
};


/**
* Retrieves an array of all stored group IDs.
* @return {!Promise.<!Array.<number>>} The unique IDs of all of the
* stored groups.
*/
shaka.util.ContentDatabase.prototype.retrieveGroupIds = function() {
var p = new shaka.util.PublicPromise();
var groupIds = [];
var request = this.getGroupStore_().openCursor();

request.onerror = function(e) { p.reject(request.error); };
request.onsuccess = function(e) {
var cursor = e.target.result;
if (cursor) {
groupIds.push(cursor.key);
cursor.continue();
} else {
p.resolve(groupIds);
}
};

return p;
};


/**
* Retrieves an item from a store in the database.
* @param {!IDBObjectStore|!IDBIndex} store The store to request an item from.
Expand Down
18 changes: 18 additions & 0 deletions spec/content_database_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,24 @@ describe('ContentDatabase', function() {
});
});

it('retrieves a list of the stored group IDs', function(done) {
var initalGroupIdsLength = 0;
p.then(function() {
return db.retrieveGroupIds();
}).then(function(groupIds) {
initalGroupIdsLength = groupIds.length;
return db.insertGroup([4, 5, 6], ['HIJK', 'LMNO']);
}).then(function() {
return db.retrieveGroupIds();
}).then(function(groupIds) {
expect(groupIds.length - initalGroupIdsLength).toBe(1);
done();
}).catch(function(err) {
fail(err);
done();
});
});

it('deletes group information and throws error on retrieval', function(done) {
var streamIds = [];
var groupId;
Expand Down

0 comments on commit a0aa5e5

Please sign in to comment.