UNPKG

pusher-redux-observable

Version:
65 lines 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Pusher = require("pusher-js"); const rxjs_1 = require("rxjs"); const redux_observable_1 = require("redux-observable"); const utils_1 = require("./utils"); const actions_1 = require("./actions"); /** * An epic to create a connection to Pusher * * @param action$ * @param store */ exports.connectToPusher = (action$, store) => action$.ofType(actions_1.PUSHER_CONNECT) .mergeMap((action) => rxjs_1.Observable.create((observer) => { exports.pusherClient = new Pusher(action.key, action.options); exports.pusherClient.connection.bind('connected', function (result) { observer.next({ type: actions_1.PUSHER_CONNECTION_SUCCESS, result }); }); exports.pusherClient.connection.bind('error', (error) => { observer.next({ type: actions_1.PUSHER_CONNECTION_ERROR, error }); }); })); /** * An epic to disconnect from Pusher * * @param action$ * @param store */ exports.disconnectFromPusher = (action$, store) => action$.ofType(actions_1.PUSHER_DISCONNECT) .mergeMap((action) => rxjs_1.Observable.create((observer) => { exports.pusherClient.disconnect(); })); /** * An epic to subscribe to a channel * * @param action$ * @param store */ exports.subscribeToChannel = (action$, store) => action$.ofType(actions_1.PUSHER_SUBSCRIBE_CHANNEL) .mergeMap((action) => rxjs_1.Observable.create((observer) => { const channel = exports.pusherClient.subscribe(utils_1.formatChannelName(action.channel)); action.events.forEach(e => { channel.bind(e, (message) => { observer.next({ type: actions_1.PUSHER_MESSAGE_RECEIVED, message, channel: action.channel }); }); }); })); /** * An epic to unsubscribe from a channel * * @param action$ * @param store */ exports.unsubscribeFromChannel = (action$, store) => action$.ofType(actions_1.PUSHER_UNSUBSCRIBE_CHANNEL) .mergeMap((action) => rxjs_1.Observable.create((observer) => { const channel = exports.pusherClient.channels.channels[action.channel]; if (channel) channel.unsubscribe(); })); /** * Combine all the epics into a single pusher epic */ exports.pusherEpic = redux_observable_1.combineEpics(exports.connectToPusher, exports.disconnectFromPusher, exports.subscribeToChannel, exports.unsubscribeFromChannel); //# sourceMappingURL=epics.js.map