mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-21 07:56:16 -04:00
plugins download and search: converted to Promises
Also fixed a bug where the system would make a request to the central server for the plugin list for every search even if the list was already cached.
This commit is contained in:
parent
5ef4a2d1d5
commit
0c2d662541
2 changed files with 45 additions and 44 deletions
|
@ -42,14 +42,10 @@ exports.socketio = function(hook_name, args, cb) {
|
||||||
socket.emit("results:installed", {installed: installed});
|
socket.emit("results:installed", {installed: installed});
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("checkUpdates", function() {
|
socket.on("checkUpdates", async function() {
|
||||||
// Check plugins for updates
|
// Check plugins for updates
|
||||||
installer.getAvailablePlugins(/*maxCacheAge:*/ 60 * 10, function(er, results) {
|
try {
|
||||||
if (er) {
|
let results = await installer.getAvailablePlugins(/*maxCacheAge:*/ 60 * 10);
|
||||||
console.warn(er);
|
|
||||||
socket.emit("results:updatable", {updatable: {}});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var updatable = _(plugins.plugins).keys().filter(function(plugin) {
|
var updatable = _(plugins.plugins).keys().filter(function(plugin) {
|
||||||
if (!results[plugin]) return false;
|
if (!results[plugin]) return false;
|
||||||
|
@ -61,27 +57,26 @@ exports.socketio = function(hook_name, args, cb) {
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.emit("results:updatable", {updatable: updatable});
|
socket.emit("results:updatable", {updatable: updatable});
|
||||||
});
|
} catch (er) {
|
||||||
|
console.warn(er);
|
||||||
|
|
||||||
|
socket.emit("results:updatable", {updatable: {}});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("getAvailable", function(query) {
|
socket.on("getAvailable", async function(query) {
|
||||||
installer.getAvailablePlugins(/*maxCacheAge:*/ false, function(er, results) {
|
try {
|
||||||
if (er) {
|
let results = await installer.getAvailablePlugins(/*maxCacheAge:*/ false);
|
||||||
console.error(er);
|
socket.emit("results:available", results);
|
||||||
results = {};
|
} catch (er) {
|
||||||
}
|
console.error(er);
|
||||||
|
socket.emit("results:available", {});
|
||||||
socket.emit("results:available", results);
|
}
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("search", function(query) {
|
socket.on("search", async function(query) {
|
||||||
installer.search(query.searchTerm, /*maxCacheAge:*/ 60 * 10, function(er, results) {
|
try {
|
||||||
if (er) {
|
let results = await installer.search(query.searchTerm, /*maxCacheAge:*/ 60 * 10);
|
||||||
console.error(er);
|
|
||||||
results = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
var res = Object.keys(results)
|
var res = Object.keys(results)
|
||||||
.map(function(pluginName) {
|
.map(function(pluginName) {
|
||||||
return results[pluginName];
|
return results[pluginName];
|
||||||
|
@ -92,7 +87,11 @@ exports.socketio = function(hook_name, args, cb) {
|
||||||
res = sortPluginList(res, query.sortBy, query.sortDir)
|
res = sortPluginList(res, query.sortBy, query.sortDir)
|
||||||
.slice(query.offset, query.offset+query.limit);
|
.slice(query.offset, query.offset+query.limit);
|
||||||
socket.emit("results:search", {results: res, query: query});
|
socket.emit("results:search", {results: res, query: query});
|
||||||
});
|
} catch (er) {
|
||||||
|
console.error(er);
|
||||||
|
|
||||||
|
socket.emit("results:search", {results: {}, query: query});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("install", function(plugin_name) {
|
socket.on("install", function(plugin_name) {
|
||||||
|
|
|
@ -77,33 +77,35 @@ exports.install = function(plugin_name, cb) {
|
||||||
exports.availablePlugins = null;
|
exports.availablePlugins = null;
|
||||||
var cacheTimestamp = 0;
|
var cacheTimestamp = 0;
|
||||||
|
|
||||||
exports.getAvailablePlugins = function(maxCacheAge, cb) {
|
exports.getAvailablePlugins = function(maxCacheAge) {
|
||||||
request("https://static.etherpad.org/plugins.json", function(er, response, plugins){
|
var nowTimestamp = Math.round(Date.now() / 1000);
|
||||||
if (er) return cb && cb(er);
|
|
||||||
|
|
||||||
if (exports.availablePlugins && maxCacheAge && Math.round(+ new Date / 1000) - cacheTimestamp <= maxCacheAge) {
|
return new Promise(function(resolve, reject) {
|
||||||
return cb && cb(null, exports.availablePlugins);
|
// check cache age before making any request
|
||||||
|
if (exports.availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) {
|
||||||
|
return resolve(exports.availablePlugins);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
request("https://static.etherpad.org/plugins.json", function(er, response, plugins) {
|
||||||
plugins = JSON.parse(plugins);
|
if (er) return reject(er);
|
||||||
} catch (err) {
|
|
||||||
console.error('error parsing plugins.json:', err);
|
|
||||||
plugins = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.availablePlugins = plugins;
|
try {
|
||||||
cacheTimestamp = Math.round(+ new Date / 1000);
|
plugins = JSON.parse(plugins);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('error parsing plugins.json:', err);
|
||||||
|
plugins = [];
|
||||||
|
}
|
||||||
|
|
||||||
cb && cb(null, plugins);
|
exports.availablePlugins = plugins;
|
||||||
|
cacheTimestamp = nowTimestamp;
|
||||||
|
resolve(plugins);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
exports.search = function(searchTerm, maxCacheAge, cb) {
|
exports.search = function(searchTerm, maxCacheAge) {
|
||||||
exports.getAvailablePlugins(maxCacheAge, function(er, results) {
|
return exports.getAvailablePlugins(maxCacheAge).then(function(results) {
|
||||||
if (er) return cb && cb(er);
|
|
||||||
|
|
||||||
var res = {};
|
var res = {};
|
||||||
|
|
||||||
if (searchTerm) {
|
if (searchTerm) {
|
||||||
|
@ -127,6 +129,6 @@ exports.search = function(searchTerm, maxCacheAge, cb) {
|
||||||
res[pluginName] = results[pluginName];
|
res[pluginName] = results[pluginName];
|
||||||
}
|
}
|
||||||
|
|
||||||
cb && cb(null, res);
|
return res;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue