Showing with 37 additions and 3 deletions.
  1. +23 −3 activity/activity.js
  2. +14 −0 bus.js
@@ -16,6 +16,20 @@ define(["webL10n",

l10n.start();

function onPause() {
activity.write(function () {});
}

function onStop() {
function onDataStored(error, result) {
activity.close(function () {});
}
activity.write(onDataStored);
}

bus.onNotification("activity.pause", onPause);
bus.onNotification("activity.stop", onStop);

datastoreObject = new datastore.DatastoreObject();

var activityButton = document.getElementById("activity-button");
@@ -33,9 +47,7 @@ define(["webL10n",

// Make the activity stop with the stop button.
var stopButton = document.getElementById("stop-button");
stopButton.addEventListener('click', function (e) {
activity.close();
});
stopButton.addEventListener('click', onStop);

shortcut.add("Ctrl", "Q", this.close);

@@ -78,6 +90,14 @@ define(["webL10n",
bus.sendMessage("activity.get_xo_color", [], onResponseReceived);
};

activity.write = function (callback) {
console.log("writing...");
setTimeout(function () {
console.log("writing done");
callback(null);
}, 3000);
};

activity.close = function (callback) {
function onResponseReceived(error, result) {
if (error === null) {
14 bus.js
@@ -1,6 +1,7 @@
define(["sugar-web/env"], function (env) {
var lastId = 0;
var callbacks = {};
var notificationCallbacks = {};
var client = null;
var inputStreams = [];

@@ -156,6 +157,10 @@ define(["sugar-web/env"], function (env) {
lastId++;
};

bus.onNotification = function (method, callback) {
notificationCallbacks[method] = callback;
};

bus.sendBinary = function (buffer, callback) {
client.send(buffer);
};
@@ -182,6 +187,15 @@ define(["sugar-web/env"], function (env) {

var parsed = JSON.parse(message.data);
var responseId = parsed.id;

if (parsed.method) {
var notificationCallback = notificationCallbacks[parsed.method];
if (notificationCallback !== undefined) {
notificationCallback(parsed.params);
}
return;
}

if (responseId in callbacks) {
var callback = callbacks[responseId];