Skip to content

Commit

Permalink
Support Promise override. (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus authored Dec 18, 2017
1 parent d0faa75 commit 96fd4d6
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/iam.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ var is = require('is');
* // subscription.iam
*/
function IAM(pubsub, id) {
if (pubsub.Promise) {
this.Promise = pubsub.Promise;
}

this.pubsub = pubsub;
this.request = pubsub.request.bind(pubsub);
this.id = id;
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ function PubSub(options) {
this.api = {};
this.auth = googleAuth(this.options);
this.projectId = this.options.projectId || PROJECT_ID_PLACEHOLDER;

if (this.options.promise) {
this.Promise = this.options.promise;
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ var is = require('is');
* var publisher = topic.publisher();
*/
function Publisher(topic, options) {
if (topic.Promise) {
this.Promise = topic.Promise;
}

options = extend(
true,
{
Expand Down
4 changes: 4 additions & 0 deletions src/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ var is = require('is');
* });
*/
function Snapshot(parent, name) {
if (parent.Promise) {
this.Promise = parent.Promise;
}

this.parent = parent;
this.name = Snapshot.formatName_(parent.projectId, name);

Expand Down
4 changes: 4 additions & 0 deletions src/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ var Snapshot = require('./snapshot.js');
* subscription.removeListener('message', onMessage);
*/
function Subscription(pubsub, name, options) {
if (pubsub.Promise) {
this.Promise = pubsub.Promise;
}

options = options || {};

this.pubsub = pubsub;
Expand Down
4 changes: 4 additions & 0 deletions src/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ var Publisher = require('./publisher.js');
* var topic = pubsub.topic('my-topic');
*/
function Topic(pubsub, name) {
if (pubsub.Promise) {
this.Promise = pubsub.Promise;
}

this.name = Topic.formatName_(pubsub.projectId, name);
this.parent = this.pubsub = pubsub;
this.request = pubsub.request.bind(pubsub);
Expand Down
5 changes: 5 additions & 0 deletions test/iam.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('IAM', function() {

var PUBSUB = {
options: {},
Promise: {},
request: util.noop,
};
var ID = 'id';
Expand All @@ -53,6 +54,10 @@ describe('IAM', function() {
});

describe('initialization', function() {
it('should localize pubsub.Promise', function() {
assert.strictEqual(iam.Promise, PUBSUB.Promise);
});

it('should localize pubsub', function() {
assert.strictEqual(iam.pubsub, PUBSUB);
});
Expand Down
9 changes: 8 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ describe('PubSub', function() {
var PubSub;
var PROJECT_ID = 'test-project';
var pubsub;
var OPTIONS = {projectId: PROJECT_ID};
var OPTIONS = {
projectId: PROJECT_ID,
promise: {},
};

var PUBSUB_EMULATOR_HOST = process.env.PUBSUB_EMULATOR_HOST;

Expand Down Expand Up @@ -261,6 +264,10 @@ describe('PubSub', function() {
it('should set isEmulator to false by default', function() {
assert.strictEqual(pubsub.isEmulator, false);
});

it('should localize a Promise override', function() {
assert.strictEqual(pubsub.Promise, OPTIONS.promise);
});
});

describe('createSubscription', function() {
Expand Down
5 changes: 5 additions & 0 deletions test/publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('Publisher', function() {
var TOPIC_NAME = 'test-topic';
var TOPIC = {
name: TOPIC_NAME,
Promise: {},
request: fakeUtil.noop,
};

Expand All @@ -61,6 +62,10 @@ describe('Publisher', function() {
assert(promisified);
});

it('should localize topic.Promise', function() {
assert.strictEqual(publisher.Promise, TOPIC.Promise);
});

it('should localize the topic object', function() {
assert.strictEqual(publisher.topic, TOPIC);
});
Expand Down
7 changes: 6 additions & 1 deletion test/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ describe('Snapshot', function() {
};

var SUBSCRIPTION = {
pubsub: PUBSUB,
Promise: {},
projectId: PROJECT_ID,
pubsub: PUBSUB,
api: {},
createSnapshot: function() {},
seek: function() {},
Expand Down Expand Up @@ -81,6 +82,10 @@ describe('Snapshot', function() {
assert(promisified);
});

it('should localize parent.Promise', function() {
assert.strictEqual(snapshot.Promise, SUBSCRIPTION.Promise);
});

it('should localize the parent', function() {
assert.strictEqual(snapshot.parent, SUBSCRIPTION);
});
Expand Down
5 changes: 5 additions & 0 deletions test/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('Subscription', function() {

var PUBSUB = {
projectId: PROJECT_ID,
Promise: {},
request: fakeUtil.noop,
};

Expand All @@ -98,6 +99,10 @@ describe('Subscription', function() {
assert(promisified);
});

it('should localize pubsub.Promise', function() {
assert.strictEqual(subscription.Promise, PUBSUB.Promise);
});

it('should localize the pubsub object', function() {
assert.strictEqual(subscription.pubsub, PUBSUB);
});
Expand Down
5 changes: 5 additions & 0 deletions test/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('Topic', function() {
var TOPIC_UNFORMATTED_NAME = TOPIC_NAME.split('/').pop();

var PUBSUB = {
Promise: {},
projectId: PROJECT_ID,
createTopic: util.noop,
request: util.noop,
Expand Down Expand Up @@ -99,6 +100,10 @@ describe('Topic', function() {
assert(promisified);
});

it('should localize pubsub.Promise', function() {
assert.strictEqual(topic.Promise, PUBSUB.Promise);
});

it('should format the name', function() {
var formattedName = 'a/b/c/d';

Expand Down

0 comments on commit 96fd4d6

Please sign in to comment.