1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-11 19:55:06 -05:00
mail/src/js/service/lawnchair.js

236 lines
6.1 KiB
JavaScript
Raw Normal View History

'use strict';
var ngModule = angular.module('woServices');
ngModule.service('appConfigLawnchair', LawnchairDAO);
ngModule.service('accountLawnchair', LawnchairDAO);
module.exports = LawnchairDAO;
2013-04-02 09:02:57 -04:00
/**
* Handles generic caching of JSON objects in a lawnchair adapter
*/
2014-12-11 12:12:37 -05:00
function LawnchairDAO() {}
/**
* Initialize the lawnchair database
* @param {String} dbName The name of the database
* @return {Promise}
*/
LawnchairDAO.prototype.init = function(dbName) {
var self = this;
2014-12-11 12:12:37 -05:00
return new Promise(function(resolve, reject) {
if (!dbName) {
2014-12-10 10:29:09 -05:00
throw new Error('Lawnchair DB name must be specified!');
}
self._db = new Lawnchair({
name: dbName
}, function(success) {
if (success) {
resolve();
} else {
reject(new Error('Lawnchair initialization ' + dbName + ' failed!'));
}
});
2014-10-02 16:05:44 -04:00
});
};
2013-09-26 07:26:57 -04:00
2014-10-02 16:05:44 -04:00
/**
* Create or update an object
* @return {Promise}
2014-10-02 16:05:44 -04:00
*/
2014-11-29 10:50:24 -05:00
LawnchairDAO.prototype.persist = function(key, object) {
var self = this;
2014-12-11 12:12:37 -05:00
return new Promise(function(resolve, reject) {
2014-11-29 10:50:24 -05:00
if (!key || !object) {
2014-12-10 10:29:09 -05:00
throw new Error('Key and Object must be set!');
}
2014-11-29 10:50:24 -05:00
self._db.save({
key: key,
object: object
}, function(persisted) {
if (persisted.key !== key) {
reject(new Error('Persisting failed!'));
return;
}
resolve();
});
2014-10-02 16:05:44 -04:00
});
};
2014-10-02 16:05:44 -04:00
/**
* Persist a bunch of items at once
* @return {Promise}
2014-10-02 16:05:44 -04:00
*/
2014-11-29 10:50:24 -05:00
LawnchairDAO.prototype.batch = function(list) {
var self = this;
2014-12-11 12:12:37 -05:00
return new Promise(function(resolve, reject) {
2014-11-29 10:50:24 -05:00
if (!(list instanceof Array)) {
2014-12-10 10:29:09 -05:00
throw new Error('Input must be of type Array!');
}
2014-11-29 10:50:24 -05:00
self._db.batch(list, function(res) {
if (!res) {
reject(new Error('Persisting batch failed!'));
2014-12-10 10:29:09 -05:00
} else {
resolve();
2014-11-29 10:50:24 -05:00
}
});
2014-10-02 16:05:44 -04:00
});
};
2014-10-02 16:05:44 -04:00
/**
* Read a single item by its key
* @return {Promise}
2014-10-02 16:05:44 -04:00
*/
2014-11-29 10:50:24 -05:00
LawnchairDAO.prototype.read = function(key) {
var self = this;
2014-12-11 12:12:37 -05:00
return new Promise(function(resolve) {
2014-11-29 10:50:24 -05:00
if (!key) {
2014-12-10 10:29:09 -05:00
throw new Error('Key must be specified!');
}
2014-11-29 10:50:24 -05:00
self._db.get(key, function(o) {
if (o) {
resolve(o.object);
} else {
resolve();
}
});
2014-10-02 16:05:44 -04:00
});
};
2014-10-02 16:05:44 -04:00
/**
* List all the items of a certain type
* @param type [String] The type of item e.g. 'email'
* @param offset [Number] The offset of items to fetch (0 is the last stored item)
* @param num [Number] The number of items to fetch (null means fetch all)
* @return {Promise}
2014-10-02 16:05:44 -04:00
*/
2014-11-29 10:50:24 -05:00
LawnchairDAO.prototype.list = function(type, offset, num) {
var self = this;
2014-12-11 12:12:37 -05:00
return new Promise(function(resolve) {
2014-11-29 10:50:24 -05:00
var i, from, to,
matchingKeys = [],
intervalKeys = [],
list = [];
// validate input
if (!type || typeof offset === 'undefined' || typeof num === 'undefined') {
2014-12-10 10:29:09 -05:00
throw new Error('Args not is not set!');
2014-11-29 10:50:24 -05:00
}
2014-11-29 10:50:24 -05:00
// get all keys
self._db.keys(function(keys) {
// check if key begins with type
keys.forEach(function(key) {
if (key.indexOf(type) === 0) {
matchingKeys.push(key);
}
});
2014-11-29 10:50:24 -05:00
// sort keys
matchingKeys.sort();
// set window of items to fetch
// if num is null, list all items
from = (num) ? matchingKeys.length - offset - num : 0;
to = matchingKeys.length - 1 - offset;
// filter items within requested interval
for (i = 0; i < matchingKeys.length; i++) {
if (i >= from && i <= to) {
intervalKeys.push(matchingKeys[i]);
}
}
2014-11-29 10:50:24 -05:00
// return if there are no matching keys
if (intervalKeys.length === 0) {
resolve(list);
return;
}
2014-11-29 10:50:24 -05:00
// fetch all items from data-store with matching key
self._db.get(intervalKeys, function(intervalList) {
intervalList.forEach(function(item) {
list.push(item.object);
});
2014-11-29 10:50:24 -05:00
// return only the interval between offset and num
resolve(list);
});
});
2014-10-02 16:05:44 -04:00
});
};
2014-10-02 16:05:44 -04:00
/**
* Removes an object liter from local storage by its key (delete)
* @return {Promise}
2014-10-02 16:05:44 -04:00
*/
2014-11-29 10:50:24 -05:00
LawnchairDAO.prototype.remove = function(key) {
var self = this;
2014-12-11 12:12:37 -05:00
return new Promise(function(resolve, reject) {
2014-11-29 10:50:24 -05:00
self._db.remove(key, function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
2014-10-02 16:05:44 -04:00
};
/**
* Removes an object liter from local storage by its key (delete)
* @return {Promise}
2014-10-02 16:05:44 -04:00
*/
2014-11-29 10:50:24 -05:00
LawnchairDAO.prototype.removeList = function(type) {
var self = this;
2014-12-11 12:12:37 -05:00
return new Promise(function(resolve) {
2014-11-29 10:50:24 -05:00
var matchingKeys = [],
after;
2014-11-29 10:50:24 -05:00
// validate type
if (!type) {
2014-12-10 10:29:09 -05:00
throw new Error('Type is not set!');
2014-10-02 16:05:44 -04:00
}
2014-11-29 10:50:24 -05:00
// get all keys
self._db.keys(function(keys) {
// check if key begins with type
keys.forEach(function(key) {
if (key.indexOf(type) === 0) {
matchingKeys.push(key);
}
});
if (matchingKeys.length < 1) {
resolve();
return;
}
// remove all matching keys
after = _.after(matchingKeys.length, resolve);
_.each(matchingKeys, function(key) {
self._db.remove(key, after);
});
});
2014-10-02 16:05:44 -04:00
});
};
2014-10-02 16:05:44 -04:00
/**
* Clears the whole local storage cache
* @return {Promise}
2014-10-02 16:05:44 -04:00
*/
2014-11-29 10:50:24 -05:00
LawnchairDAO.prototype.clear = function() {
var self = this;
2014-12-11 12:12:37 -05:00
return new Promise(function(resolve, reject) {
2014-11-29 10:50:24 -05:00
self._db.nuke(function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
};