2016-10-28 12:06:35 -04:00
|
|
|
import { loadAdapter } from '../Adapters/AdapterLoader';
|
|
|
|
|
import {
|
|
|
|
|
EventEmitterPubSub
|
|
|
|
|
} from '../Adapters/PubSub/EventEmitterPubSub';
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
RedisPubSub
|
|
|
|
|
} from '../Adapters/PubSub/RedisPubSub';
|
2016-03-10 14:27:00 -08:00
|
|
|
|
2016-12-07 15:17:05 -08:00
|
|
|
const ParsePubSub = {};
|
2016-03-10 14:27:00 -08:00
|
|
|
|
|
|
|
|
function useRedis(config: any): boolean {
|
2016-12-07 15:17:05 -08:00
|
|
|
const redisURL = config.redisURL;
|
2016-03-10 14:27:00 -08:00
|
|
|
return typeof redisURL !== 'undefined' && redisURL !== '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParsePubSub.createPublisher = function(config: any): any {
|
|
|
|
|
if (useRedis(config)) {
|
2016-10-28 12:06:35 -04:00
|
|
|
return RedisPubSub.createPublisher(config);
|
2016-03-10 14:27:00 -08:00
|
|
|
} else {
|
2016-12-07 15:17:05 -08:00
|
|
|
const adapter = loadAdapter(config.pubSubAdapter, EventEmitterPubSub, config)
|
2016-10-28 12:06:35 -04:00
|
|
|
if (typeof adapter.createPublisher !== 'function') {
|
|
|
|
|
throw 'pubSubAdapter should have createPublisher()';
|
|
|
|
|
}
|
|
|
|
|
return adapter.createPublisher(config);
|
2016-03-10 14:27:00 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParsePubSub.createSubscriber = function(config: any): void {
|
|
|
|
|
if (useRedis(config)) {
|
2016-10-28 12:06:35 -04:00
|
|
|
return RedisPubSub.createSubscriber(config);
|
2016-03-10 14:27:00 -08:00
|
|
|
} else {
|
2016-12-07 15:17:05 -08:00
|
|
|
const adapter = loadAdapter(config.pubSubAdapter, EventEmitterPubSub, config)
|
2016-10-28 12:06:35 -04:00
|
|
|
if (typeof adapter.createSubscriber !== 'function') {
|
|
|
|
|
throw 'pubSubAdapter should have createSubscriber()';
|
|
|
|
|
}
|
|
|
|
|
return adapter.createSubscriber(config);
|
2016-03-10 14:27:00 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
ParsePubSub
|
|
|
|
|
}
|