Prompt tag now only prompts once per request maximum

This commit is contained in:
Gregory Schier 2018-06-27 00:58:34 -04:00
parent 15e230c77a
commit 8a8c4b3979
3 changed files with 27 additions and 6 deletions

View File

@ -51,6 +51,10 @@ export async function removeByKey(plugin: string, key: string): Promise<void> {
return db.removeWhere(type, { plugin, key });
}
export async function removeAll(plugin: string): Promise<void> {
return db.removeWhere(type, { plugin });
}
export async function getByKey(
plugin: string,
key: string

View File

@ -18,6 +18,9 @@ export function init(plugin: Plugin) {
},
async removeItem(key: string): Promise<void> {
await models.pluginData.removeByKey(plugin.name, key);
},
async clear(key: string): Promise<void> {
await models.pluginData.removeAll(plugin.name);
}
}
};

View File

@ -1,4 +1,12 @@
const STORE = {};
module.exports.responseHooks = [
context => {
const requestId = context.response.getRequestId();
// Delete cached value so it will prompt again on the next request
context.store.removeItem(requestId);
}
];
module.exports.templateTags = [
{
displayName: 'Prompt',
@ -30,10 +38,17 @@ module.exports.templateTags = [
'something else.'
}
],
async run(context, title, label, defaultValue, storageKey) {
if (STORE[storageKey]) {
async run(context, title, label, defaultValue, explicitStorageKey) {
// If we don't have a key, default to request ID.
// We do this because we may render the prompt multiple times per request.
// We cache it under the requestId so it only prompts once. We then clear
// the cache in a response hook when the request is sent.
const storageKey = explicitStorageKey || context.meta.requestId;
const cachedValue = await context.store.getItem(storageKey);
if (cachedValue) {
console.log(`[prompt] Used cached value under ${storageKey}`);
return STORE[storageKey];
return cachedValue;
}
const value = await context.app.prompt(title || 'Enter Value', {
@ -41,10 +56,9 @@ module.exports.templateTags = [
defaultValue
});
// Store if a key is set
if (storageKey) {
console.log(`[prompt] Stored value under ${storageKey}`);
STORE[storageKey] = value;
await context.store.setItem(storageKey, value);
}
return value;