Skip to content

Commit

Permalink
Merge pull request #9 from parse-server-modules/feature/disable-fan-out
Browse files Browse the repository at this point in the history
Add option for disabling fan out
  • Loading branch information
ranhsd authored Nov 9, 2017
2 parents f98a0f7 + 5945162 commit badc608
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
35 changes: 21 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ class Publisher {

class Subscriber extends events.EventEmitter {

constructor() {
constructor(options) {
super();
this.disableFanOut = options.disableFanOut || false;
}

subscribe(channel) {
Expand Down Expand Up @@ -72,27 +73,32 @@ class Subscriber extends events.EventEmitter {
}

_createSubscription(topic, channel) {

const subscriptionUUID = uuid.v1();
var subscriptionName = `${namePrefix}-${channel}-${subscriptionUUID}`;
let subscriptionName;
if (!this.disableFanOut) {
const subscriptionUUID = uuid.v1();
subscriptionName = `${namePrefix}-${channel}-${subscriptionUUID}`;
} else {
subscriptionName = `${namePrefix}-${channel}`;
}

topic.subscribe(subscriptionName, (err, subscription) => {

if (err) {
console.error(`Failed to create subscription ${err}`);
return;
}

console.log(`Subscription ${subscription.name} created.`);

function deleteSubscription() {
const deleteSubscription = () => {
removeListeners();
console.log('Subscriber: Signal received, deleting subscription');
subscription.delete().then(() => {
console.log('Subscriber: subscription deleted...');
}, (err) => {
console.error(`Subscriber: Error deleting subscription`, err);
});
if (!this.disableFanOut) {
console.log('Subscriber: Signal received, deleting subscription');
subscription.delete().then(() => {
console.log('Subscriber: subscription deleted...');
}, (err) => {
console.error(`Subscriber: Error deleting subscription`, err);
});
}
}

function messageHandler(message) {
Expand Down Expand Up @@ -121,6 +127,7 @@ class Subscriber extends events.EventEmitter {
function removeListeners() {
subscription.removeListener('message', onMessage);
subscription.removeListener('error', onError);

process.removeListener('SIGTERM', deleteSubscription);
process.removeListener('SIGINT', deleteSubscription);
}
Expand All @@ -140,8 +147,8 @@ function createPublisher() {
return new Publisher(emitter);
}

function createSubscriber() {
return new Subscriber();
function createSubscriber(options) {
return new Subscriber(options);
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "parse-server-gcloud-pubsub",
"version": "2.0.1",
"version": "2.1.0",
"description": "google cloud pub/sub adapter for parse-server",
"main": "lib/index.js",
"engines": {
Expand Down

0 comments on commit badc608

Please sign in to comment.