Abstract out cache mechanisms, move search elements to their own component, disable locks in development mode, fix database cleanup in Electron renderer processes, add version display for search results
parent
a94e22b926
commit
63e7c883f3
@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
require("../search-box");
|
||||
require("../search-results");
|
||||
require("../search");
|
||||
|
||||
require("./component");
|
@ -0,0 +1,83 @@
|
||||
search
|
||||
search-box
|
||||
search-results(results="{results}")
|
||||
|
||||
script.
|
||||
const Promise = require("bluebird");
|
||||
const path = require("path");
|
||||
const dotty = require("dotty");
|
||||
const {ipcRenderer} = require("electron");
|
||||
const rfr = require("rfr");
|
||||
|
||||
const appDirectory = require("appdirectory");
|
||||
let appDirectories = new appDirectory("npmbar");
|
||||
|
||||
const jsonDB = rfr("lib/db/json-db");
|
||||
const jsonDBCache = rfr("lib/db/json-db-cache");
|
||||
|
||||
this.mixin(require("riot-query").mixin);
|
||||
|
||||
Object.assign(this, {
|
||||
results: []
|
||||
});
|
||||
|
||||
let lastQuery;
|
||||
|
||||
this.on("updated", () => {
|
||||
global.triggerWindowResize();
|
||||
});
|
||||
|
||||
this.on("mount", () => {
|
||||
Promise.try(() => {
|
||||
return Promise.all([
|
||||
jsonDB(path.join(appDirectories.userData(), "search-cache.json")),
|
||||
jsonDB(path.join(appDirectories.userData(), "package-cache.json"))
|
||||
]);
|
||||
}).spread((searchCacheDB, packageCacheDB) => {
|
||||
let searchCache = jsonDBCache(searchCacheDB, "searchCache");
|
||||
let packageCache = jsonDBCache(packageCacheDB, "packageCache");
|
||||
|
||||
const search = rfr("lib/search/constructor-io")("CD06z4gVeqSXRiDL2ZNK", searchCache);
|
||||
const lookupPackage = rfr("lib/package/fetch-metadata")(packageCache);
|
||||
|
||||
let searchBox = this.queryOne("search-box");
|
||||
let searchResults = this.queryOne("search-results");
|
||||
|
||||
searchBox.on("selectionUp", () => {
|
||||
searchResults.moveSelectionUp();
|
||||
});
|
||||
|
||||
searchBox.on("selectionDown", () => {
|
||||
searchResults.moveSelectionDown();
|
||||
});
|
||||
|
||||
searchBox.on("queryChanged", (query) => {
|
||||
Promise.try(() => {
|
||||
lastQuery = query;
|
||||
return search(query);
|
||||
}).then((results) => {
|
||||
/* Sometimes, search results may come back out of order. Here we check whether
|
||||
* the lastQuery is still the same as when we initially made the request. */
|
||||
if (lastQuery === query) {
|
||||
this.results = results.packages;
|
||||
this.update();
|
||||
|
||||
this.results.forEach((result, i) => {
|
||||
Promise.try(() => {
|
||||
return lookupPackage(result.name);
|
||||
}).then((metadata) => {
|
||||
if (dotty.exists(metadata, "dist-tags.latest")) {
|
||||
result.latestVersion = metadata["dist-tags"].latest;
|
||||
this.update();
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
searchBox.on("cancel", () => {
|
||||
ipcRenderer.send("closeSearch");
|
||||
})
|
||||
});
|
||||
});
|
@ -0,0 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
require("../search-box");
|
||||
require("../search-results");
|
||||
|
||||
require("./component");
|
@ -0,0 +1,38 @@
|
||||
'use strict';
|
||||
|
||||
const Promise = require("bluebird");
|
||||
|
||||
/* NOTE: This assumes that the first argument to the cacheized call, will be
|
||||
* the cache key. */
|
||||
|
||||
module.exports = function(cache, cb) {
|
||||
function getFromRemote(...args) {
|
||||
return Promise.try(() => {
|
||||
return cb(...args);
|
||||
}).tap((result) => {
|
||||
cache.set(args[0], Object.assign({
|
||||
$cacheKey: args[0]
|
||||
}, result));
|
||||
});
|
||||
}
|
||||
|
||||
function getFromCache(...args) {
|
||||
return Promise.try(() => {
|
||||
return cache.get(args[0]);
|
||||
}).then((metadata) => {
|
||||
if (metadata._cacheExpiry < Date.now()) {
|
||||
throw new cache.CacheError("Cached data expired");
|
||||
} else {
|
||||
return metadata;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return function(...args) {
|
||||
return Promise.try(() => {
|
||||
return getFromCache(...args);
|
||||
}).catch(cache.CacheError, (err) => {
|
||||
return getFromRemote(...args);
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
const rfr = require("rfr");
|
||||
const errors = rfr("lib/util/errors");
|
||||
|
||||
module.exports = function(db, collectionName) {
|
||||
let cacheCollection = db.collection(collectionName);
|
||||
|
||||
return {
|
||||
CacheError: errors.CacheError,
|
||||
get: function(packageName) {
|
||||
try {
|
||||
return cacheCollection.findOne({$cacheKey: packageName});
|
||||
} catch (err) { // FIXME
|
||||
throw new errors.CacheError("Not in cache");
|
||||
}
|
||||
},
|
||||
set: function(packageName, metadata) {
|
||||
return cacheCollection.upsertBy("$cacheKey", metadata);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue