Skip to content

Instantly share code, notes, and snippets.

@tofumatt
Created May 8, 2013 22:50
Show Gist options
  • Save tofumatt/5544282 to your computer and use it in GitHub Desktop.
Save tofumatt/5544282 to your computer and use it in GitHub Desktop.
'use strict';
// A very simple, key/value-based IndexedDB library for storing binary data
// in IndexedDB. Used because Podcasts needs to store large binary files (
// podcast image covers and audio files) but can't use Appcache or localStorage.
// Because most data is manipulated with Backbone.js and stored in localStorage
// though, we just use IndexedDB as blob storage.
//
// This library lets a user store arbitrary "big data" in IndexedDB with a
// stupid simple API:
//
// // To get a blob:
// DataStore.get('some-key', callback)
//
// // To set a key:
// DataStore.set('some-key', blobData, callback)
//
// // And to destroy data:
// DataStore.destroy('some-key', callback)
define(function(require) {
// Create/open database.
var deviceStorage = window.navigator.getDeviceStorage('podcasts');
window.alert(deviceStorage);
// Connect to the database; should be fired on app start, with the app
// initialization running after the database is ensured to be working.
function load(callback) {
callback();
}
// Utility function to destroy everything in the datastore. Probably not
// useful.
function clearAll() {
window.alert('TODO');
}
// Destroy a blob based on its id key.
function destroy(id, callback) {
var deleteRequest = deviceStorage.delete(id);
if (callback) {
deleteRequest.onsuccess = callback;
}
return deleteRequest;
}
// Get blob data based on a simple, string key. Sends the request event
// and the blob data (if it exists) as the two arguments to a callback.
// TODO: Supply event data to callback.
function get(id, callback) {
var request = deviceStorage.get(id);
request.onsuccess = function(event) {
callback(request.result);
}
}
// Save data based on a key to IndexedDB. This can take some time, be warned.
function set(id, blob, callback) {
var request = deviceStorage.addNamed(blob, id);
request.onerror = function() {
window.alert('SET FAILED ' + id);
}
if (callback) {
request.onsuccess = callback;
}
}
return {
destroy: destroy,
get: get,
load: load,
set: set
};
});
{
"version": "0.1",
"name": "Podcasts",
"description": "Listen to your favourite podcasts on your HTML5-powered device or browser.",
"icons": {
"16": "/img/icon-16.png",
"30": "/img/icon-30.png",
"32": "/img/icon-32.png",
"48": "/img/icon-48.png",
"60": "/img/icon-60.png",
"64": "/img/icon-64.png",
"128": "/img/icon-128.png",
"256": "/img/icon-256.png"
},
"developer": {
"name": "Mozilla",
"url": "http://mozilla.org/"
},
"launch_path": "/index.html",
"installs_allowed_from": ["https://marketplace.firefox.com", "*"],
"type": "privileged",
"permissions": {
"device-storage:sdcard": {
"access": "readcreate",
"description": "Required to store podcast audio files."
},
"storage": {
"description": "Required to store podcast images."
},
"systemXHR": {
"description": "Required to download podcasts."
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment