UNPKG

redis-fake

Version:

A fake (test double fake) redis client

78 lines (58 loc) 2.59 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var RedisClientFake = function () { function RedisClientFake() { _classCallCheck(this, RedisClientFake); this.channelsSubscribedTo = []; } /** * Register an event handler for an event * * @param {string} event - a valid [Redis]{@link https://www.npmjs.com/package/redis} Publish/Subscribe event. * Supported events include: * message * @param {function} handler - function to call when the event arrives. Function will receive the channel name * and the message object as parameters */ _createClass(RedisClientFake, [{ key: 'on', value: function on(event, handler) { if (event === 'message') { this.messageHandler = handler; } } /** * Publish a message to a channel. Any event handler registered for the 'message' event will be notified. * * @param {string} channel - the channel that the message will be published to * @param {string} message - the message to publish to the channel */ }, { key: 'publish', value: function publish(channel, message) { var foundChannel = this.channelsSubscribedTo.find(function (subChannel) { return subChannel === channel; }); if (foundChannel && this.messageHandler) { this.messageHandler(channel, message); } } /** * subscribe to be notified of messages published to the specified channel. You must be registered and listening * for 'message' events to be notified of any published events to this channel. * * @param {string} channel - channel to listen on */ }, { key: 'subscribe', value: function subscribe(channel) { this.channelsSubscribedTo.push(channel); } }]); return RedisClientFake; }(); exports.default = RedisClientFake;