libkernel
Advanced tools
Comparing version 0.0.16 to 0.0.17
@@ -1,3 +0,2 @@ | ||
export declare var queries: any; | ||
export declare function getNonce(): number; | ||
export declare function postKernelMessage(resolve: Function, reject: Function, message: any): void; | ||
export declare function init(): Promise<void>; |
@@ -13,7 +13,17 @@ var initialized; | ||
var nextNonce = 1; | ||
export var queries = new Object(); | ||
export function getNonce() { | ||
var queries = new Object(); | ||
// postKernelMessage will send a postMessage to the kernel, handling details | ||
// like the nonce and the resolve/reject upon receiving a response. The inputs | ||
// are a resolve and reject function of a promise that should be resolved when | ||
// the response is received, and the message that is going to the kernel | ||
// itself. | ||
export function postKernelMessage(resolve, reject, message) { | ||
let nonce = nextNonce; | ||
nextNonce++; | ||
return nonce; | ||
queries[nonce] = { resolve, reject }; | ||
window.postMessage({ | ||
method: "kernelMessage", | ||
nonce, | ||
kernelMessage: message, | ||
}, window.location.origin); | ||
} | ||
@@ -94,3 +104,3 @@ // handleBridgeTest will handle a response from the bridge indicating that the | ||
}); | ||
// After 100ms, check whether the bridge has responded. If not, | ||
// After 2 seconds, check whether the bridge has responded. If not, | ||
// fail the bridge. | ||
@@ -97,0 +107,0 @@ setTimeout(function () { |
@@ -1,1 +0,1 @@ | ||
export declare function testMessage(): Promise<unknown>; | ||
export declare function testMessage(): Promise<string>; |
@@ -1,20 +0,27 @@ | ||
import { init, getNonce, queries } from './init'; | ||
// kernelRequestTest will send a message to the bridge asking for a kernel | ||
// test. | ||
import { init, postKernelMessage } from './init'; | ||
// testMessage will send a test message to the kernel, ensuring that basic | ||
// kernel communications are working. | ||
// | ||
// NOTE: Anyone looking to extend libkernel can use this function as an example | ||
// of how messaging works within libkernel. | ||
export function testMessage() { | ||
let blockForBridge = init(); | ||
// Retrun a promise that will resolve when a response is received from | ||
// the kernel. | ||
return new Promise((resolve, reject) => { | ||
blockForBridge | ||
// Every message should start with init. If initialization has | ||
// already happened, this will be a no-op that instantly | ||
// resolves. | ||
init() | ||
.then(x => { | ||
let nonce = getNonce(); | ||
queries[nonce] = { resolve, reject }; | ||
window.postMessage({ | ||
method: "kernelMessage", | ||
nonce, | ||
kernelMessage: { | ||
kernelMethod: "requestTest", | ||
}, | ||
}, window.location.origin); | ||
// Send a 'requestTest' message to the kernel. The | ||
// request test message uniquely doesn't have any other | ||
// parameters. | ||
postKernelMessage(resolve, reject, { | ||
kernelMethod: "requestTest", | ||
}); | ||
}) | ||
.catch(x => { | ||
// For some reason, the bridge is not available. | ||
// Likely, this means that the user has not installed | ||
// the browser extension. | ||
reject(x); | ||
@@ -21,0 +28,0 @@ }); |
{ | ||
"name": "libkernel", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "helper library to interact with the skynet kernel", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
6193
158