2016-10-28 12:06:35 -04:00
|
|
|
import { loadAdapter } from '../Adapters/AdapterLoader';
|
2018-09-01 13:58:06 -04:00
|
|
|
import { EventEmitterPubSub } from '../Adapters/PubSub/EventEmitterPubSub';
|
2016-10-28 12:06:35 -04:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
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 !== '';
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 13:06:52 -05:00
|
|
|
ParsePubSub.createPublisher = function (config: any): any {
|
2016-03-10 14:27:00 -08:00
|
|
|
if (useRedis(config)) {
|
2016-10-28 12:06:35 -04:00
|
|
|
return RedisPubSub.createPublisher(config);
|
2016-03-10 14:27:00 -08:00
|
|
|
} else {
|
2020-10-25 15:06:58 -05: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
|
|
|
}
|
2018-09-01 13:58:06 -04:00
|
|
|
};
|
2016-03-10 14:27:00 -08:00
|
|
|
|
2020-07-13 13:06:52 -05:00
|
|
|
ParsePubSub.createSubscriber = function (config: any): void {
|
2016-03-10 14:27:00 -08:00
|
|
|
if (useRedis(config)) {
|
2016-10-28 12:06:35 -04:00
|
|
|
return RedisPubSub.createSubscriber(config);
|
2016-03-10 14:27:00 -08:00
|
|
|
} else {
|
2020-10-25 15:06:58 -05: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
|
|
|
}
|
2018-09-01 13:58:06 -04:00
|
|
|
};
|
2016-03-10 14:27:00 -08:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
export { ParsePubSub };
|