UNPKG

@helenejs/react

Version:

Real-time Web Apps for Node.js

36 lines 1.43 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useLocalEvent = useLocalEvent; exports.useRemoteEvent = useRemoteEvent; const react_1 = require("react"); const use_subscribe_1 = require("./use-subscribe"); const use_client_1 = require("./use-client"); const useCreation_1 = __importDefault(require("ahooks/lib/useCreation")); const isString_1 = __importDefault(require("lodash/isString")); const utils_1 = require("@helenejs/utils"); function useLocalEvent({ event, channel = utils_1.NO_CHANNEL }, fn, deps = []) { const _callback = (0, react_1.useCallback)(fn, deps); const client = (0, use_client_1.useClient)(); const ch = (0, useCreation_1.default)(() => { return channel && (0, isString_1.default)(channel) ? client.channel(channel) : client; }, [channel]); (0, react_1.useEffect)(() => { if (!channel) return; ch.on(event, _callback); return () => { ch.off(event, _callback); }; }, [event, channel, _callback]); } function useRemoteEvent({ event, channel = utils_1.NO_CHANNEL, active = true }, fn, deps = []) { return (0, use_subscribe_1.useSubscribe)({ event, channel, active, }, fn, deps); } //# sourceMappingURL=use-event.js.map