@webrtc-remote-control/react
Version:
Thin abstraction layer above peerjs that will let you be more productive at making WebRTC data channels based apps.
1 lines • 7.65 kB
Source Map (JSON)
{"version":3,"file":"react.cjs","sources":["../src/Provider.jsx","../src/hooks.js"],"sourcesContent":["// eslint-disable-next-line import/no-extraneous-dependencies\nimport React, { createContext, useEffect, useRef } from \"react\";\nimport PropTypes from \"prop-types\";\nimport { master, remote, prepareUtils } from \"@webrtc-remote-control/core\";\n\nexport const MyContext = createContext();\n\nexport function Provider({\n children,\n sessionStorageKey,\n humanErrors,\n mode,\n masterPeerId,\n init,\n}) {\n const allowedMode = [\"master\", \"remote\"];\n if (!allowedMode.includes(mode)) {\n throw new Error(\n `Unsupported \"${mode}\" mode. Only ${allowedMode\n .map((a) => `\"${a}\"`)\n .join(\", \")} accepted.`\n );\n }\n if (mode === \"master\" && masterPeerId) {\n console.log(typeof masterPeerId);\n throw new Error(\n `\\`masterPeerId\\` prop not allowed in \"master\" mode - \"${masterPeerId}\" was passed.`\n );\n }\n if (mode === \"remote\" && !masterPeerId) {\n throw new Error(`\\`masterPeerId\\` prop required in \"remote\" mode.`);\n }\n const utils = prepareUtils({\n sessionStorageKey,\n humanErrors,\n });\n const providerValue = useRef({\n peer: null,\n promise: null,\n mode,\n masterPeerId,\n });\n useEffect(() => {\n // expose the following on the ref forwarded to the provider\n providerValue.current.mode = mode;\n providerValue.current.humanizeError = utils.humanizeError;\n if (mode === \"master\") {\n providerValue.current.isConnectionFromRemote =\n utils.isConnectionFromRemote;\n }\n\n // init callback that should return a peer instance like:\n // `({ getPeerId }) => new Peer(getPeerId())`\n providerValue.current.peer = init({\n humanizeError: utils.humanizeError,\n getPeerId: utils.getPeerId,\n isConnectionFromRemote:\n mode === \"master\" ? utils.isConnectionFromRemote : undefined,\n });\n\n providerValue.current.promise = (mode === \"master\" ? master : remote)\n .default(utils)\n .bindConnection(\n providerValue.current.peer,\n remote ? masterPeerId : undefined\n );\n // start resolving the promise as soon as possible (it will be used in `usePeer`)\n providerValue.current.promise.then(() => {});\n return () => {\n if (providerValue.current) {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n providerValue.current.peer.disconnect();\n }\n };\n }, [mode, masterPeerId, init, utils]);\n return (\n <MyContext.Provider value={providerValue.current}>\n {children}\n </MyContext.Provider>\n );\n}\n\nProvider.propTypes = {\n children: PropTypes.any,\n sessionStorageKey: PropTypes.string,\n humanErrors: PropTypes.object,\n mode: PropTypes.oneOf([\"master\", \"remote\"]),\n masterPeerId: PropTypes.string,\n init: PropTypes.func,\n};\n","/* eslint-disable import/no-extraneous-dependencies */\nimport { useEffect, useContext, useRef, useState } from \"react\";\n\nimport { MyContext } from \"./Provider\";\n\nexport function usePeer() {\n // track if the hook is fully ready\n const [ready, setReady] = useState(false);\n // track if the peer object is ready (to allow consumer to subscribe to error event)\n const [, setPeerReady] = useState(false);\n const context = useContext(MyContext);\n const resolvedWrcApi = useRef(null);\n useEffect(() => {\n // run on next tick (ensure the `then` of the Provider has executed + retrieve the api from the resolve promise)\n Promise.resolve().then(() => {\n setPeerReady(true); // peer object is not null anymore\n context?.promise?.then((wrcApi) => {\n resolvedWrcApi.current = wrcApi;\n setReady(true);\n });\n });\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n return {\n ready,\n api: resolvedWrcApi.current,\n ...context,\n };\n}\n"],"names":["MyContext","createContext","Provider","children","sessionStorageKey","humanErrors","mode","masterPeerId","init","allowedMode","includes","Error","map","a","join","console","log","utils","prepareUtils","providerValue","useRef","peer","promise","useEffect","current","humanizeError","isConnectionFromRemote","getPeerId","undefined","master","remote","bindConnection","then","disconnect","React","propTypes","PropTypes","any","string","object","oneOf","func","usePeer","useState","ready","setReady","setPeerReady","context","useContext","resolvedWrcApi","Promise","resolve","wrcApi","api"],"mappings":";;;;;;;;;AAAA;AAKO,IAAMA,SAAS,GAAGC,mBAAa,EAA/B;AAEA,SAASC,QAAT,OAOJ;AAAA,MANDC,QAMC,QANDA,QAMC;AAAA,MALDC,iBAKC,QAL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