Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IDE messaging support to extensions API. Close #1406 #1407

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
this.registry.forEach(ext => ext.onSetStageSize(width, height));
}

onMessage(type, data) {
this.registry
.flatMap(ext => ext.getMessageHandlers(type))
.forEach(fn => fn(data))
}

register(Extension) {
if (this.isReady()) {
this.load(Extension);
Expand Down Expand Up @@ -185,6 +191,7 @@

function Extension (name) {
this.name = name;
this._handlers = {};
}

Extension.prototype.getMenu = function() {
Expand All @@ -211,6 +218,25 @@
return [];
};

Extension.prototype.addMessageListener = function(type, fn) {
if (!this._handlers[type]) {
this._handlers[type] = [];
}
this._handlers[type].push(fn);
};

Extension.prototype.removeMessageListener = function(type, fn) {
const handlers = this.getMessageHandlers(type);
const index = handlers.indexOf(fn);
if (index > -1) {
handlers.splice(index, 1);
}
};

Extension.prototype.getMessageHandlers = function(type) {
return this._handlers[type] || [];
};

Extension.prototype.onRunScripts =
Extension.prototype.onStopAllScripts =
Extension.prototype.onPauseAll =
Expand Down
4 changes: 4 additions & 0 deletions src/websockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ WebSocketManager.IDEMessageHandlers = {
};

WebSocketManager.MessageHandlers = {
'extension': function(msg) {
const {type, data} = msg.data;
NetsBloxExtensions.onMessage(type, data);
},
'ide-message': function(msg) {
const {data, sender} = msg;
let response, error;
Expand Down
25 changes: 25 additions & 0 deletions test/extensions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,29 @@ describe('extensions', function() {
assert(w == 123 && h == 456);
});
});

describe('messages', function () {
it('should pass recvd messages to ext', function(done) {
const {NetsBloxExtensions} = driver.globals();
const extension = NetsBloxExtensions.registry[0];
const payload = "hello there!";
extension.addMessageListener('test', data => {
console.log("hello?")
const err = data !== payload ?
new Error("Incorrect data payload: " + data) : null;
done(err);
});

// spoof receiving the message
const msg = {
type: 'extension',
data: {
type: 'test',
data: payload,
},
};
const rawMsg = {data: JSON.stringify(msg)};
driver.ide().sockets.websocket.onmessage(rawMsg);
});
});
});