當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript connection.Connection類代碼示例

本文整理匯總了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) => {
開發者ID:RequestPolicyContinued,項目名稱:requestpolicy,代碼行數:12,代碼來源:test-connection.ts

示例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,
//.........這裏部分代碼省略.........
開發者ID:RequestPolicyContinued,項目名稱:requestpolicy,代碼行數:101,代碼來源:test-connection.ts

示例3: startupUntilStartupMessageSent

 function startupUntilStartupMessageSent() {
   const pStartupMessageSent = new Promise((resolve) => {
     port.postMessage.onFirstCall().callsFake(resolve);
   });
   connection.startup();
   return pStartupMessageSent;
 }
開發者ID:RequestPolicyContinued,項目名稱:requestpolicy,代碼行數:7,代碼來源:test-connection.ts

示例4:

 }).then(() => {
   assert.strictEqual(connection.isReady(), true);
   return;
 });
開發者ID:RequestPolicyContinued,項目名稱:requestpolicy,代碼行數:4,代碼來源:test-connection.ts


注:本文中的lib/classes/connection.Connection類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。