add on-message-input-change and on-message-send listeners
logging in jail
This commit is contained in:
Roman Volosovskyi 2017-04-27 16:43:38 +03:00 committed by Roman Volosovskyi
parent 1fbb447761
commit d044b1cf43
4 changed files with 92 additions and 80 deletions

View File

@ -43,45 +43,6 @@ var jsDescriptionStyle = {
color: "#838c93de"
};
var messages = [];
console = (function (old) {
return {
log: function (text) {
old.log(text);
var message = {
type: 'log',
message: JSON.stringify(text)
};
messages.push(message);
context.messages.push(message);
},
info: function (text) {
old.info(text);
context.messages.push({
type: 'info',
message: JSON.stringify(text)
});
},
warn: function (text) {
old.warn(text);
context.messages.push({
type: 'warn',
message: JSON.stringify(text)
});
},
error: function (text) {
old.error(text);
context.messages.push({
type: 'error',
message: JSON.stringify(text)
});
}
};
}(console));
if (!String.prototype.startsWith) {
String.prototype.startsWith = function (searchString, position) {
position = position || 0;
@ -257,7 +218,7 @@ function getPartialSuggestions(doc, fullCode, code) {
suggestions.push(createObjectSuggestion(suggestedFunction, docInfo, null, parameters.length - 1));
}
}
console.log(suggestions);
//console.debug(suggestions);
return suggestions;
}
@ -265,10 +226,10 @@ function getJsSuggestions(code, context) {
var suggestions = [];
var doc = DOC_MAP;
// TODO: what's /c / doing there ???
console.log(code);
//console.debug(code);
if (!code || code == "" || code == "c ") {
code = "";
console.log("Last message: " + context.data);
//console.debug("Last message: " + context.data);
if (context.data != null) {
suggestions.push({
title: 'Last command used:',
@ -306,8 +267,8 @@ function getJsSuggestions(code, context) {
suggestions = getPartialSuggestions(doc, originalCode, levelCode);
}
console.log("Final code: " + code);
console.log("Level code: " + levelCode);
//console.debug("Final code: " + code);
//console.debug("Level code: " + levelCode);
suggestions = suggestions.concat(getPartialSuggestions(doc, originalCode, code));
}
return suggestions;
@ -344,7 +305,7 @@ function createMarkupText(text) {
)
);
}
console.log(parts);
//console.debug(parts);
return parts;
}
@ -392,7 +353,7 @@ function jsHandler(params, context) {
};
messages = [];
try {
result.data = JSON.stringify(eval(params.code));
result["text-message"] = JSON.stringify(eval(params.code));
localStorage.set(params.code);
} catch (e) {
result.err = e;
@ -743,10 +704,10 @@ status.response({
}
});
status.registerFunction("message-suggestions", function (params, context) {
status.addListener("on-message-input-change", function (params, context) {
return jsSuggestions({code: params.message}, context);
});
status.registerFunction("message-handler", function (params, context) {
status.addListener("on-message-send", function (params, context) {
return jsHandler({code: params.message}, context);
});

View File

@ -51,10 +51,15 @@ Response.prototype.onReceiveResponse = function (handler) {
this.onReceive = handler;
};
var context = {}
var context = {};
function addContext(ns, key, value) {
context[ns][key] = value;
function addContext(key, value) {
context[status.message_id][key] = value;
}
function getContext(key) {
return context[status.message_id][key];
}
function call(pathStr, paramsStr) {
@ -147,7 +152,7 @@ var status = {
var response = new Response();
return response.create(h);
},
registerFunction: function (name, fn) {
addListener: function (name, fn) {
_status_catalog.functions[name] = fn;
},
localizeNumber: function (num, del, sep) {
@ -179,5 +184,49 @@ var status = {
webView: webView,
validationMessage: validationMessage,
bridgedWebView: bridgedWebView
},
setSuggestions: function (view) {
addContext("suggestions", view);
},
sendMessage: function (text) {
addContext("text-message", text);
},
addLogMessage: function (type, message) {
var message = {
type: type,
message: JSON.stringify(message)
};
var logMessages = getContext("log-messages");
if (!logMessages) {
logMessages = [];
}
logMessages.push(message);
addContext("log-messages", logMessages);
}
};
console = (function (old) {
return {
log: function (text) {
old.log(text);
status.addLogMessage('log', text);
},
debug: function (text) {
old.debug(text);
status.addLogMessage('debug', text);
},
info: function (text) {
old.info(text);
status.addLogMessage('info', text);
},
warn: function (text) {
old.warn(text);
status.addLogMessage('warn', text);
},
error: function (text) {
old.error(text);
status.addLogMessage('error', text);
}
};
}(console));

View File

@ -129,11 +129,11 @@
(status/call-jail jail-id
path
params
#(dispatch [:suggestions-handler
#(dispatch [:received-bot-response
{:chat-id current-chat-id
:command command
:parameter-index parameter-index
:result %}]))))))))
:parameter-index parameter-index}
%]))))))))
(handlers/register-handler
::send-message
@ -278,16 +278,14 @@
(handlers/side-effect!
(fn [db [_ chat-id text]]
(let [data (get-in db [:local-storage chat-id])
path [:functions
:message-suggestions]
path [:functions :on-message-input-change]
params {:parameters {:message text}
:context {:data data}}]
(status/call-jail chat-id
path
params
(fn [{:keys [result] :as data}]
(dispatch [:suggestions-handler {:chat-id chat-id
:result data}])))))))
#(dispatch [:received-bot-response
{:chat-id chat-id} %]))))))
(handlers/register-handler
:clear-seq-arguments

View File

@ -192,36 +192,40 @@
(u/side-effect!
(fn [db [_ chat-id {:keys [content]}]]
(let [data (get-in db [:local-storage chat-id])
path [:functions
:message-handler]
path [:functions :on-message-send]
params {:parameters {:message content}
:context {:data data}}]
(status/call-jail chat-id
path
params
(fn [{:keys [result]}]
(log/debug "Message handler result: " result)
(dispatch [::received-dapp-message chat-id result])))))))
(fn [resp]
(log/debug "Message handler result: " resp)
(dispatch [:received-bot-response
{:chat-id chat-id} resp])))))))
(register-handler ::received-dapp-message
(register-handler :received-bot-response
(u/side-effect!
(fn [_ [_ chat-id {:keys [returned]}]]
(let [{:keys [data messages err]} returned
content (or err data)]
(doseq [message messages]
(fn [_ [_ {:keys [chat-id] :as params} {:keys [result] :as data}]]
(let [{:keys [returned context]} result
{:keys [markup text-message]} returned
{:keys [log-messages]} context]
(when markup
(dispatch [:suggestions-handler (assoc params :result data)]))
(doseq [message log-messages]
(let [{:keys [message type]} message]
(dispatch [:received-message
{:message-id (random/id)
:content (str type ": " message)
:content-type text-content-type
:outgoing false
:chat-id chat-id
:from chat-id
:to "me"}])))
(when content
(when (or (not= type "debug") js/goog.DEBUG)
(dispatch [:received-message
{:message-id (random/id)
:content (str type ": " message)
:content-type text-content-type
:outgoing false
:chat-id chat-id
:from chat-id
:to "me"}]))))
(when text-message
(dispatch [:received-message
{:message-id (random/id)
:content (str content)
:content (str text-message)
:content-type text-content-type
:outgoing false
:chat-id chat-id