Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promises to enable clean load test scripts as complexity increases #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,18 @@ With the MeteorDown script, you can call methods and invoke subscriptions. The f

This ddp client is based on [node-ddp-client](https://github.com/oortcloud/node-ddp-client) but with some changes to make it more Meteor like. Let's look at APIs:

###Meteor.call
###Meteor.call and Meteor.apply

~~~js
Meteor.call('name'[, args*], callback)
~~~

and

~~~js
Meteor.call('name'[, args], callback)
~~~

Call a Meteor method. Just like the browser client, the callback will receive 2 arguments Error and the Result.

###Meteor.subscribe
Expand All @@ -55,6 +61,15 @@ Meteor.subscribe('name'[, args*], callback)

The callback function will be called when the subscription is ready and all initial data is loaded to the client.

###Handling More Complex Pipelines with Promises

For more complicated load-testing pipelines, a `Promise`-enabled versions of `Meteor.call`, `Meteor.apply` and `Meteor.subscribe` are available as:
- `Meteor.call_GetPromise('name'[, args*])`
- `Meteor.apply_GetPromise('name'[, args])`
- `Meteor.subscribe_GetPromise('name'[, args*])`

Simply omit the callback parameter and work with the `Promise` that is returned.

###Meteor.kill

~~~js
Expand Down
66 changes: 66 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,57 @@ Client.prototype.call = function () {
});
}

Client.prototype.apply = function() {
if ((arguments.length === 0) || (typeof arguments[0] !== "string")) {
throw new Error("invalid method call")
}
var methodName = arguments[0];
var applyArgs = [methodName];
var args, cb;

if (arguments.length >= 2) {
args = arguments[1];
if (!_.isArray(args)) {
throw new Error("invalid apply arguments parameter");
}
applyArgs = applyArgs.concat(args);
}
if (arguments.length >= 3) {
cb = arguments[2];
if (!_.isFunction(cb)) {
throw new Error("invalid callback parameter");
}
applyArgs = applyArgs.concat(cb);
}
this.call.apply(this, applyArgs);
};

Client.prototype.call_GetPromise = function() {
var self = this;
var args = Array.prototype.slice.call(arguments);
return new Promise(function(resolve, reject) {
self.call.apply(self, args.concat(function(err, res) {
if (!!err) {
reject(err);
}
resolve(res);
}));
});
};

Client.prototype.apply_GetPromise = function() {
var self = this;
var args = Array.prototype.slice.call(arguments);
return new Promise(function(resolve, reject) {
self.apply.apply(self, args.concat(function(err, res) {
if (!!err) {
reject(err);
}
resolve(res);
}));
});
};

Client.prototype._subscribe = Client.prototype.subscribe;
Client.prototype.subscribe = function () {
var self = this;
Expand All @@ -60,6 +111,21 @@ Client.prototype.subscribe = function () {
});
}

Client.prototype.subscribe_GetPromise = function() {
var self = this;
var args = Array.prototype.slice.call(arguments);
return new Promise(function(resolve, reject) {
self.subscribe.apply(self, args.concat(function(err, res) {
if (!!err) {
reject(err);
}
resolve(res);
}));
});
};



Client.prototype.kill = function () {
this.close();
this.emit('disconnected');
Expand Down