Skip to content

Commit

Permalink
Merge pull request #23 from mixmaxhq/vladimir/add_support_options
Browse files Browse the repository at this point in the history
feat: adding support for publications options
  • Loading branch information
vladimir-mixmax authored Apr 15, 2021
2 parents cf42f6f + ee73715 commit b971483
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist/
node_modules/
.idea/
30 changes: 29 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class PublicationClient extends EventEmitter {

// Hash the name and params to cache the subscription.
const subscriptionKey = JSON.stringify(_.toArray(arguments));
var subscription = this._subscriptions[subscriptionKey];
let subscription = this._subscriptions[subscriptionKey];
if (!subscription) {
subscription = this._subscriptions[subscriptionKey] = new Subscription(
String(id),
Expand All @@ -274,6 +274,34 @@ class PublicationClient extends EventEmitter {
return subscription;
}

/**
* Subscribe to the publication with the given name. Any other parameters
* are passed as arguments to the publication.
*
* @param {String} name The publication to subscribe to.
* @param {Object} options containing optional parameters such as bootstrap and/or
* bootstrapAsync.
* @param {*[]} params (optional) Params to pass to the publication.
* @returns {Subscription} The subscription to the desired publication.
*/
subscribeWithOptions(name, options, ...params) {
const id = this._nextSubscriptionId++;
const payload = [...params, options];

// Hash the name and params to cache the subscription including payload extended with options.
const subscriptionKey = JSON.stringify([name].concat(payload));
let subscription = this._subscriptions[subscriptionKey];
if (!subscription) {
subscription = this._subscriptions[subscriptionKey] = new Subscription(
String(id),
name,
payload,
this
);
}
return subscription;
}

/**
* Send the given message to the publication provider.
*
Expand Down
20 changes: 20 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,25 @@ describe('PublicationClient', () => {
expect(pub.emit).toHaveBeenCalledWith('proactivelyReconnected', 'test');
jest.clearAllTimers();
});

it('calls new method with correct parameters', () => {
const pub = new PublicationClient('https://127.0.0.1', {
lastDataTimeout: 1,
paranoid: true,
});
pub._lastDataTimestamp = 0;
pub.reconnectIfIdle('test');
const subscription = pub.subscribeWithOptions(
'test',
{ bootstrap: false },
{ key: 1 },
{ key: 2 }
);
expect(pub._subscriptions).not.toBe({});
expect(subscription._params).toStrictEqual([{ key: 1 }, { key: 2 }, { bootstrap: false }]);
subscription.stop();
expect(pub._subscriptions).toStrictEqual({});
jest.clearAllTimers();
});
});
});

0 comments on commit b971483

Please sign in to comment.