pubnub-react
Version:
Publish & Subscribe Real-time Messaging with PubNub
44 lines (37 loc) • 1.79 kB
JavaScript
import React, { useContext } from 'react';
import invariant from 'ts-invariant';
var PubNubContext = /*#__PURE__*/React.createContext(null);
function appendPnsdk(pubnub) {
if (typeof pubnub._addPnsdkSuffix === 'function') {
pubnub._addPnsdkSuffix('react', 'React/3.0.2');
}
}
var PubNubProvider = function PubNubProvider(_ref) {
var client = _ref.client,
children = _ref.children;
var contextValue = React.useMemo(function () {
return {
client: client
};
}, [client]);
!contextValue.client ? process.env.NODE_ENV !== "production" ? invariant(false, 'PubNubProvider was not passed a client instance. Make ' + 'sure you pass in your client via the "client" prop.') : invariant(false) : void 0;
React.useEffect(function () {
appendPnsdk(contextValue.client);
}, []);
return React.createElement(PubNubContext.Provider, {
value: contextValue
}, children);
};
var PubNubConsumer = function PubNubConsumer(_ref) {
var children = _ref.children;
var context = React.useContext(PubNubContext);
!(context && context.client) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Could not find "client" in the context of PubNubConsumer. ' + 'Wrap the root component in an <PubNubProvider>.') : invariant(false) : void 0;
return React.createElement(React.Fragment, null, children(context.client));
};
function usePubNub() {
var context = useContext(PubNubContext);
!(context && context.client) ? process.env.NODE_ENV !== "production" ? invariant(false, 'No PubNub Client instance can be found. Please ensure that you ' + 'have called `PubNubProvider` higher up in your tree.') : invariant(false) : void 0;
return context.client;
}
export { PubNubConsumer, PubNubProvider, usePubNub };
//# sourceMappingURL=pubnub-react.esm.js.map