{"version":3,"sources":["../../src/sdkCore/ensureChannelReady.ts"],"sourcesContent":["import {isNufiMessage} from '../dappCore/nufiMessage'\nimport type {\n  ConnectorPlatform,\n  PingChannelMessage,\n  UntypedConnectorKind,\n} from '../dappCore/types'\nimport {logger} from '../utils/logging'\n\n// Note that storing all previous \"interval and handlers\" and knowing which\n// to invalidate/remove would be complex. Therefore we always keep only the\n// last one.\n// Note that this means that if user called `ensureChannelIsReady` multiple times,\n// only the last call would eventually resolve.\n// This should not be an issue as we are preventing re-injection of `injectConnectors`\n// when the session is not meant to change.\nlet channelReadyGlobals: {\n  interval: NodeJS.Timeout\n  handler: (e: MessageEvent<unknown>) => unknown\n} | null = null\n\nexport const ensureChannelIsReady = (\n  appId: string,\n  connectorPlatform: ConnectorPlatform,\n  connectorKind: UntypedConnectorKind,\n  sendPostMessage: (message: unknown) => void,\n) => {\n  const channelPingMessage: PingChannelMessage = {\n    connectorPlatform,\n    appId,\n    method: 'channelPing',\n    data: {\n      connectorKind,\n      connectorPlatform,\n    },\n  }\n\n  return new Promise((resolve) => {\n    const channelReadyHandler = (e: MessageEvent<unknown>) => {\n      // We are only checking message structure here, not the origin,\n      // as this code anyways runs in untrusted environment.\n      if (!isNufiMessage(e, connectorPlatform)) return\n      const _e = e as MessageEvent<PingChannelMessage>\n      if (_e.data.method === 'channelPing') {\n        logger.debug('\"ensureChannelIsReady\": received ping response')\n\n        // Note that it is safe to clear interval and listener as each time when\n        // calling `ensureChannelIsReady`, old interval and lister are cleared.\n        // Therefore these must be the values associated with the same `ensureChannelIsReady` call.\n        if (channelReadyGlobals != null) {\n          clearInterval(channelReadyGlobals.interval)\n          window.removeEventListener('message', channelReadyGlobals.handler)\n          channelReadyGlobals = null\n        }\n        resolve(true)\n      }\n    }\n\n    // When calling `ensureChannelIsReady` multiple times in a row, we want to ensure that\n    // interval and listener from previous calls are cleared.\n    // We can be certain that the handler is not called in the meantime as this function is sync.\n    if (channelReadyGlobals != null) {\n      clearInterval(channelReadyGlobals.interval)\n      window.removeEventListener('message', channelReadyGlobals.handler)\n    }\n\n    channelReadyGlobals = {\n      // Ping until we can safely establish port connection. Note that it must be registered into\n      // global immediately after creation to avoid \"stale\" timeouts.\n      interval: setInterval(() => {\n        logger.debug('\"ensureChannelIsReady\": sending ping request')\n        sendPostMessage(channelPingMessage)\n      }, 500),\n      handler: channelReadyHandler,\n    }\n    // Only register once we stored the callback in global variable.\n    window.addEventListener('message', channelReadyGlobals.handler)\n  })\n}\n\nexport type EnsureChannelIsReady = typeof ensureChannelIsReady\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAA4B;AAM5B,qBAAqB;AASrB,IAAI,sBAGO;AAEJ,IAAM,uBAAuB,CAClC,OACA,mBACA,eACA,oBACG;AACH,QAAM,qBAAyC;AAAA,IAC7C;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,IACR,MAAM;AAAA,MACJ;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,SAAO,IAAI,QAAQ,CAAC,YAAY;AAC9B,UAAM,sBAAsB,CAAC,MAA6B;AAGxD,UAAI,KAAC,kCAAc,GAAG,iBAAiB,EAAG;AAC1C,YAAM,KAAK;AACX,UAAI,GAAG,KAAK,WAAW,eAAe;AACpC,8BAAO,MAAM,gDAAgD;AAK7D,YAAI,uBAAuB,MAAM;AAC/B,wBAAc,oBAAoB,QAAQ;AAC1C,iBAAO,oBAAoB,WAAW,oBAAoB,OAAO;AACjE,gCAAsB;AAAA,QACxB;AACA,gBAAQ,IAAI;AAAA,MACd;AAAA,IACF;AAKA,QAAI,uBAAuB,MAAM;AAC/B,oBAAc,oBAAoB,QAAQ;AAC1C,aAAO,oBAAoB,WAAW,oBAAoB,OAAO;AAAA,IACnE;AAEA,0BAAsB;AAAA;AAAA;AAAA,MAGpB,UAAU,YAAY,MAAM;AAC1B,8BAAO,MAAM,8CAA8C;AAC3D,wBAAgB,kBAAkB;AAAA,MACpC,GAAG,GAAG;AAAA,MACN,SAAS;AAAA,IACX;AAEA,WAAO,iBAAiB,WAAW,oBAAoB,OAAO;AAAA,EAChE,CAAC;AACH;","names":[]}