Skip to content

Commit

Permalink
Refresh config after action
Browse files Browse the repository at this point in the history
  • Loading branch information
javalikescript committed Feb 2, 2025
1 parent 0f9f6c5 commit 1c45a0f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
4 changes: 2 additions & 2 deletions extensions/web-base/www/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@
</section>
<section v-if="actions.length > 0">
<p>Action</p>
<button v-for="(action, index) in actions" v-if="action.active === true && !action.arguments" :title="action.description" v-on:click="triggerAction(index + 1)">{{ action.name }}</button>
<button v-for="(action, index) in actions" v-if="action.active === true && !action.arguments" :title="action.description" v-on:click="triggerAction(index)">{{ action.name }}</button>
</section>
<section v-if="schema">
<json :name="'Configuration'" :obj="config" :schema="schema"></json>
Expand Down Expand Up @@ -266,7 +266,7 @@
<article class="content">
<section v-if="actions.length > 0">
<p>Action</p>
<button v-for="(action, index) in actions" v-if="action.active === false && !action.arguments" :title="action.description" v-on:click="triggerAction(index + 1)">{{ action.name }}</button>
<button v-for="(action, index) in actions" v-if="action.active === false && !action.arguments" :title="action.description" v-on:click="triggerAction(index)">{{ action.name }}</button>
</section>
<section>
<p>{{ info.name }}</p>
Expand Down
26 changes: 22 additions & 4 deletions extensions/web-base/www/app/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,32 @@ function onShowExtension(extensionId) {
}));
}

function refreshConfig() {
return fetch('/engine/extensions/' + this.extensionId + '/config').then(getJson).then(function(config) {
this.config = config;
}.bind(this));
}

function triggerAction(index) {
return fetch('/engine/extensions/' + this.extensionId + '/action/' + index, {
console.info('triggerAction(' + index + ')');
var action = this.actions[index];
if (!action) {
return Promise.reject('No action #' + index);
}
return fetch('/engine/extensions/' + this.extensionId + '/action/' + (index + 1), {
method: 'POST',
headers: { "Content-Type": 'application/json' },
body: '[]' // TODO ask arguments
}).then(assertIsOk).then(getResponseText).then(function(text) {
toaster.toast('Action triggered: ' + text);
});
}).then(assertIsOk).then(getJson).then(function(response) {
if (response.success) {
toaster.toast('Action triggered, ' + response.message);
if (action.active === false) {
refreshConfig.call(this);
}
} else {
toaster.toast('Action failed, ' + response.message);
}
}.bind(this));
}

new Vue({
Expand Down
2 changes: 1 addition & 1 deletion lha/restEngine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ local REST_EXTENSIONS = {
elseif (action.arguments and #action.arguments or 0) ~= (arguments and #arguments or 0) then
HttpExchange.badRequest(exchange, 'The action arguments are invalid')
else
return method(extension, arguments)
return utils.toResponse(method, extension, arguments)
end
else
HttpExchange.notFound(exchange)
Expand Down
32 changes: 31 additions & 1 deletion lha/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local logger = require('jls.lang.logger'):get(...)
local event = require('jls.lang.event')
local system = require('jls.lang.system')
local Promise = require('jls.lang.Promise')
local Exception = require('jls.lang.Exception')
local File = require('jls.io.File')
local Date = require('jls.util.Date')
local json = require('jls.util.json')
Expand Down Expand Up @@ -75,12 +76,41 @@ function utils.rejectIfNotOk(response)
end)
end

local function toResponse(success, message)
return {
success = success == true,
message = tostring(message)
}
end

function utils.toResponse(v, ...)
if type(v) == 'function' then
local status, message = Exception.pcall(v, ...)
if not status then
return toResponse(status, message)
end
v = message
end
if type(v) == 'boolean' then
return toResponse(v, (...))
elseif v == nil then
return toResponse(false, (...))
elseif Promise.isPromise(v) then
return v:next(function(message)
return toResponse(true, message)
end, function(message)
return toResponse(false, message)
end);
end
return toResponse(true, v)
end

function utils.timeout(promise, delayMs, reason)
return Promise:new(function(resolve, reject)
local timer = event:setTimeout(function()
reject(reason or 'timeout')
end, delayMs or 30000)
promise:finally(function(...)
promise:finally(function()
event:clearTimeout(timer)
end)
promise:next(resolve, reject)
Expand Down

0 comments on commit 1c45a0f

Please sign in to comment.