{"version":3,"file":"node-adapter.min.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":["nep","listeners","WeakMap","postMessage","bind","addEventListener","_","eh","l","data","handleEvent","on","set","removeEventListener","get","off","delete","start"],"mappings":"wBA8BqCA,GACnC,MAAMC,EAAY,IAAIC,QACtB,MAAO,CACLC,YAAaH,EAAIG,YAAYC,KAAKJ,GAClCK,iBAAkB,CAACC,EAAGC,KACpB,MAAMC,EAAKC,IACL,gBAAiBF,EACnBA,EAAGG,YAAY,CAAED,KAAAA,IAEjBF,EAAG,CAAEE,KAAAA,KAGTT,EAAIW,GAAG,UAAWH,GAClBP,EAAUW,IAAIL,EAAIC,IAEpBK,oBAAqB,CAACP,EAAGC,KACvB,MAAMC,EAAIP,EAAUa,IAAIP,GACnBC,IAGLR,EAAIe,IAAI,UAAWP,GACnBP,EAAUe,OAAOT,KAEnBU,MAAOjB,EAAIiB,OAASjB,EAAIiB,MAAMb,KAAKJ"}