本文整理汇总了TypeScript中lib/classes/connection.Connection类的典型用法代码示例。如果您正苦于以下问题:TypeScript Connection类的具体用法?TypeScript Connection怎么用?TypeScript Connection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Connection类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: fullyStartupConnection
return fullyStartupConnection().then(() => {
port.postMessage.resetHistory();
const p = connection.sendMessage("bar_value");
const {id} = port.postMessage.getCall(0).args[0];
port.onMessage.dispatch({
id,
isResponse: true,
target: moduleName,
value: "baz_response",
});
return p;
}).then((response) => {
示例2: describe
describe("connection", function() {
const sinon = require("sinon").sandbox.create();
const browser = createBrowserApi();
const moduleName = "my-module";
const targetName = "my-target";
let port;
let connection: Connection<any, any>;
beforeEach(() => {
port = createPort(sinon);
const log = new Log();
connection = new Connection(
moduleName,
log,
targetName,
() => Promise.resolve(port)
);
});
afterEach(() => {
browser.flush();
sinon.restore();
});
function startupUntilStartupMessageSent() {
const pStartupMessageSent = new Promise((resolve) => {
port.postMessage.onFirstCall().callsFake(resolve);
});
connection.startup();
return pStartupMessageSent;
}
function fullyStartupConnection() {
return startupUntilStartupMessageSent().then(() => {
port.onMessage.dispatch({
id: "startup",
isResponse: true,
target: moduleName,
value: "ready",
});
return;
});
}
it("first add message listener, then send message", function() {
return startupUntilStartupMessageSent().then(() => {
sinon.assert.callOrder(
port.onMessage.addListener,
port.postMessage
);
sinon.assert.calledWithMatch(port.postMessage, {
id: "startup",
isResponse: false,
target: targetName,
value: "ready",
});
return;
});
});
it("responds to startup message from target", function() {
return startupUntilStartupMessageSent().then(() => {
port.postMessage.reset();
const pResponseSent = new Promise((resolve) => {
port.postMessage.callsFake(resolve);
});
port.onMessage.dispatch({
id: "startup",
isResponse: false,
target: moduleName,
value: "ready",
});
return pResponseSent;
}).then(() => {
sinon.assert.calledOnce(port.postMessage);
sinon.assert.calledWithMatch(port.postMessage, {
id: "startup",
isResponse: true,
target: targetName,
value: "ready",
});
return;
});
});
it("is not ready before target startup", function() {
return startupUntilStartupMessageSent().then(() => {
assert.strictEqual(connection.isReady(), false);
return;
});
});
it("is ready after receiving startup response", function() {
return startupUntilStartupMessageSent().then(() => {
port.onMessage.dispatch({
id: "startup",
isResponse: true,
target: moduleName,
//.........这里部分代码省略.........
示例3: startupUntilStartupMessageSent
function startupUntilStartupMessageSent() {
const pStartupMessageSent = new Promise((resolve) => {
port.postMessage.onFirstCall().callsFake(resolve);
});
connection.startup();
return pStartupMessageSent;
}
示例4:
}).then(() => {
assert.strictEqual(connection.isReady(), true);
return;
});