Add local default icon + implements files search cache

This commit is contained in:
2015-09-02 14:26:23 +02:00
parent 99c60aae12
commit 411894586b
7 changed files with 84 additions and 13 deletions

33
js/util/cache.js Normal file
View File

@ -0,0 +1,33 @@
function Cache() {
this._store = {};
}
Cache.prototype.get = function(key) {
key = this._serialize(key);
return key in this._store ? this._store[key] : undefined;
};
Cache.prototype.set = function(key, value) {
key = this._serialize(key);
this._store[key] = value;
return this;
};
Cache.prototype._serialize = function(mixedKey) {
var json = JSON.stringify(mixedKey);
return this._hash(json);
};
Cache.prototype._hash = function(str) {
var hash = 0, i, chr, len;
if (str.length === 0) return hash;
for (i = 0, len = str.length; i < len; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
module.exports = Cache;

View File

@ -1,2 +1,3 @@
exports.System = require('./system');
exports.DesktopApps = require('./desktop-apps');
exports.Cache = require('./cache');

View File

@ -2,6 +2,7 @@ var fs = require('fs');
var cp = require('child_process');
var glob = require('glob');
var ini = require('ini');
var Cache = require('./cache');
/**
* Load a JSON file
@ -52,12 +53,22 @@ exports.runApp = function(execPath) {
});
};
var _searchCache = new Cache();
exports.findFiles = function(pattern, opts) {
return new Promise(function(resolve, reject) {
var cachedResult = _searchCache.get([pattern, opts]);
if( cachedResult !== undefined) {
return resolve(cachedResult);
}
glob(pattern, opts, function(err, files) {
if(err) return reject(err);
_searchCache.set([pattern, opts], files);
return resolve(files);
});
});
};