本文整理匯總了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;
});