Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should get the comm info', async () => {
const commPromise = new PromiseDelegate();
const hook = (comm: Kernel.IComm, msg: KernelMessage.ICommOpenMsg) => {
commPromise.resolve(comm);
};
kernel.registerCommTarget('test', hook);
// Request the comm creation.
await kernel.requestExecute({ code: SEND }, true).done;
// Get the comm.
const comm = await commPromise.promise;
// Ask the kernel for the list of current comms.
const msg = await kernel.requestCommInfo({});
if (msg.content.status !== 'ok') {
throw new Error('Message error');
const { commands, current, stop } = this;
const { request } = current;
const routed = this._routed;
const rules = this._rules;
const matches: Private.Rule[] = [];
// Collect all rules that match the URL.
rules.forEach((rule, pattern) => {
if (request?.match(pattern)) {
matches.push(rule);
}
});
// Order the matching rules by rank and enqueue them.
const queue = matches.sort((a, b) => b.rank - a.rank);
const done = new PromiseDelegate();
// Process each enqueued command sequentially and short-circuit if a promise
// resolves with the `stop` token.
const next = async () => {
if (!queue.length) {
routed.emit(current);
done.resolve(undefined);
return;
}
const { command } = queue.pop()!;
try {
const request = this.current.request;
const result = await commands.execute(command, current);
if (result === stop) {
launch(): Promise> {
// Return the existing dialog if already open.
if (this._promise) {
return this._promise.promise;
}
const promise = (this._promise = new PromiseDelegate>());
let promises = Promise.all(Private.launchQueue);
Private.launchQueue.push(this._promise.promise);
return promises.then(() => {
Widget.attach(this, this._host);
return promise.promise;
});
}
it('should be called when a widget is closed', async () => {
const widget = manager.createWidget(widgetFactory, context);
const delegate = new PromiseDelegate();
widget.disposed.connect(async () => {
expect(manager.methods).to.contain('onClose');
await dismissDialog();
delegate.resolve(undefined);
});
widget.close();
});
it('should inject relevant cells into the parent', async () => {
const code = 'print("#onIOPubMessage:enabled")';
const promise = new PromiseDelegate();
handler.enabled = true;
const parent = handler.parent as TestParent;
expect(parent.widgets.length).to.equal(0);
let called = false;
handler.injected.connect(() => {
expect(parent.widgets.length).to.be.greaterThan(0);
called = true;
promise.resolve(void 0);
});
await foreign.kernel.requestExecute({ code, stop_on_error: true }).done;
await promise.promise;
expect(called).to.equal(true);
});
it('should resolve to true only after a promise is fulfilled', async () => {
const p = new PromiseDelegate();
expect(await isFulfilled(p.promise)).to.equal(false);
p.resolve(10);
expect(await isFulfilled(p.promise)).to.equal(true);
});
it('should restore the widgets in a tracker', async () => {
const tracker = new WidgetTracker({ namespace: 'foo-widget' });
const registry = new CommandRegistry();
const state = new StateDB();
const ready = new PromiseDelegate();
const restorer = new LayoutRestorer({
connector: state,
first: ready.promise,
registry
});
let called = false;
const key = `${tracker.namespace}:${tracker.namespace}`;
registry.addCommand(tracker.namespace, {
execute: () => {
called = true;
}
});
await state.save(key, { data: null });
ready.resolve(undefined);
await restorer.restore(tracker, {
it('should send a binary message', async () => {
const done = new PromiseDelegate();
const msgId = UUID.uuid4();
tester.onMessage(msg => {
try {
const decoder = new TextDecoder('utf8');
const item = msg.buffers[0] as DataView;
expect(decoder.decode(item)).to.equal('hello');
} catch (e) {
done.reject(e);
throw e;
}
done.resolve();
});
const encoder = new TextEncoder();
const data = encoder.encode('hello');
constructor() {
const port = 8081;
this._server = new WebSocket.Server({ port });
this.serverSettings = ServerConnection.makeSettings({
wsUrl: `ws://localhost:${port}/`,
WebSocket: WebSocket as any
});
this._ready = new PromiseDelegate();
this._server.on('connection', ws => {
this._ws = ws;
this.onSocket(ws);
this._ready.resolve(undefined);
const connect = this._onConnect;
if (connect) {
connect(ws);
}
});
}