libkernel
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1,4 +0,39 @@ | ||
// TODO: Need to write the nonce framework. | ||
// Establish a system for matching messages to the kernel with responses from | ||
// the kernel. | ||
var nextNonce = 0; | ||
var queries = new Object(); | ||
// handleMessage will handle a message from the kernel, using the reponse to | ||
// resolve the appropriate promise in the set of queries. | ||
function handleMessage(event) { | ||
// Check the message source. | ||
if (event.source !== window) { | ||
return; | ||
} | ||
// Check that this message is a kernelResponse. | ||
if (!("data" in event) || !("method" in event.data)) { | ||
return; | ||
} | ||
if (event.data.method !== "kernelResponse") { | ||
return; | ||
} | ||
// Check that we have a promise for the provided nonce. | ||
if (!(event.data.nonce in queries)) { | ||
console.log("received message with missing nonce\n", event, "\n", queries); | ||
return; | ||
} | ||
// Either resolve or reject the promise associated with this response. | ||
if (event.data.resp !== null) { | ||
let resolve = queries[event.data.nonce].resolve; | ||
resolve(event.data.resp); | ||
} | ||
else if (event.data.resp !== null) { | ||
let reject = queries[event.data.nonce].reject; | ||
reject(event.data.err); | ||
} | ||
else { | ||
console.log("received malformed response from bridge\n", event.data); | ||
} | ||
delete queries[event.data.nonce]; | ||
console.log("got message from bridge\n", event, "\n", event.data); | ||
} | ||
// init will add an event listener for messages from the kernel bridge. | ||
@@ -9,11 +44,3 @@ // | ||
export function init() { | ||
window.addEventListener("message", function (event) { | ||
if (!("data" in event) || !("method" in event.data)) { | ||
return; | ||
} | ||
if (event.data.method !== "kernelResponse") { | ||
return; | ||
} | ||
console.log("got message from bridge\n", event, "\n", event.data); | ||
}); | ||
window.addEventListener("message", handleMessage); | ||
} | ||
@@ -27,6 +54,6 @@ // kernelRequestTest will send a message to the bridge asking for a kernel | ||
export function testMessage() { | ||
return new Promise(resolve => { | ||
return new Promise((resolve, reject) => { | ||
let nonce = nextNonce; | ||
nextNonce++; | ||
queries[nonce] = resolve; | ||
queries[nonce] = { resolve, reject }; | ||
window.postMessage({ | ||
@@ -33,0 +60,0 @@ method: "kernelMessage", |
{ | ||
"name": "libkernel", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "helper library to interact with the skynet kernel", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
2973
79