{"version":3,"file":"node-adapter.mjs","sources":["../../src/node-adapter.ts"],"sourcesContent":["/**\n * Copyright 2019 Google Inc. All Rights Reserved.\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *     http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Endpoint } from \"./protocol\";\n\nexport interface NodeEndpoint {\n  postMessage(message: any, transfer?: any[]): void;\n  on(\n    type: string,\n    listener: EventListenerOrEventListenerObject,\n    options?: {}\n  ): void;\n  off(\n    type: string,\n    listener: EventListenerOrEventListenerObject,\n    options?: {}\n  ): void;\n  start?: () => void;\n}\n\nexport default function nodeEndpoint(nep: NodeEndpoint): Endpoint {\n  const listeners = new WeakMap();\n  return {\n    postMessage: nep.postMessage.bind(nep),\n    addEventListener: (_, eh) => {\n      const l = (data: any) => {\n        if (\"handleEvent\" in eh) {\n          eh.handleEvent({ data } as MessageEvent);\n        } else {\n          eh({ data } as MessageEvent);\n        }\n      };\n      nep.on(\"message\", l);\n      listeners.set(eh, l);\n    },\n    removeEventListener: (_, eh) => {\n      const l = listeners.get(eh);\n      if (!l) {\n        return;\n      }\n      nep.off(\"message\", l);\n      listeners.delete(eh);\n    },\n    start: nep.start && nep.start.bind(nep),\n  };\n}\n"],"names":[],"mappings":"AAAA;;;;;;;;;;;;SA8BwB,YAAY,CAAC,GAAiB;IACpD,MAAM,SAAS,GAAG,IAAI,OAAO,EAAE,CAAC;IAChC,OAAO;QACL,WAAW,EAAE,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC;QACtC,gBAAgB,EAAE,CAAC,CAAC,EAAE,EAAE;YACtB,MAAM,CAAC,GAAG,CAAC,IAAS;gBAClB,IAAI,aAAa,IAAI,EAAE,EAAE;oBACvB,EAAE,CAAC,WAAW,CAAC,EAAE,IAAI,EAAkB,CAAC,CAAC;iBAC1C;qBAAM;oBACL,EAAE,CAAC,EAAE,IAAI,EAAkB,CAAC,CAAC;iBAC9B;aACF,CAAC;YACF,GAAG,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;YACrB,SAAS,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;SACtB;QACD,mBAAmB,EAAE,CAAC,CAAC,EAAE,EAAE;YACzB,MAAM,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YAC5B,IAAI,CAAC,CAAC,EAAE;gBACN,OAAO;aACR;YACD,GAAG,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;YACtB,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;SACtB;QACD,KAAK,EAAE,GAAG,CAAC,KAAK,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC;KACxC,CAAC;AACJ;;;;"}