tildefriends/packages/cory/chattest/chattest.js

76 lines
1.4 KiB
JavaScript
Raw Normal View History

"use strict";
//! {
//! "chat": {
//! "version": 1,
//! "settings": [
//! {"name": "nickname", "type": "text"},
//! {"name": "password", "type": "password"}
//! ]
//! },
//! "category": "libraries"
//! }
let gServices = {};
class ChatService {
constructor(options) {
let self = this;
self._name = options.name;
self._callback = options.callback;
setTimeout(function() {
self._callback({
action: "message",
from: "service",
to: self._name,
message: "Hello, world!",
conversation: null,
});
}, 1000);
}
sendMessage(target, message) {
let self = this;
setTimeout(function() {
self._callback({
action: "message",
from: target,
to: self._name,
message: "I saw you say: " + message,
conversation: target,
});
}, 500);
return self._callback({
action: "message",
from: self._name,
to: target,
message: message,
conversation: target,
});
}
}
core.register("onMessage", function(sender, message) {
print(message);
let service = gServices[message.name];
if (!service) {
service = new ChatService(message);
gServices[message.name] = service;
} else {
service._callback = message.callback || service._callback;
}
setTimeout(function() {
service._callback({
action: "message",
from: "service",
to: service._name,
message: "I got your message.",
conversation: null,
});
}, 500);
return {
sendMessage: service.sendMessage.bind(service),
};
});