perform.js
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* A simple perform command which allows access to the "api" in a callback.
* Can be useful if you want to read variables set by other commands.
*
* ```
* this.demoTest = function (browser) {
* var elementValue;
* browser
* .getValue('.some-element', function(result) {
* elementValue = result.value;
* })
* // other stuff going on ...
*
* // self-completing callback
* .perform(function() {
* console.log('elementValue', elementValue);
* // without any defined parameters, perform
* // completes immediately (synchronously)
* })
*
* // asynchronous completion
* .perform(function(done) {
* console.log('elementValue', elementValue);
* // potentially other async stuff going on
* // on finished, call the done callback
* done();
* })
*
* // asynchronous completion including api (client)
* .perform(function(client, done) {
* console.log('elementValue', elementValue);
* // similar to before, but now with client
* // potentially other async stuff going on
* // on finished, call the done callback
* done();
* });
* };
* ```
*
* @method perform
* @param {function} callback The function to run as part of the queue. Its signature can have up to two parameters. No parameters: callback runs and perform completes immediately at the end of the execution of the callback. One parameter: allows for asynchronous execution within the callback providing a done callback function for completion as the first argument. Two parameters: allows for asynchronous execution with the "api" object passed in as the first argument, followed by the done callback.
* @api commands
*/
var util = require('util');
var events = require('events');
function Perform() {
events.EventEmitter.call(this);
}
util.inherits(Perform, events.EventEmitter);
Perform.prototype.command = function(callback) {
var self = this;
var doneCallback;
if (callback.length === 0) {
callback.call(self, self.client.api);
doneCallback = function() {
self.emit('complete');
};
} else {
doneCallback = function() {
var args = [function() {
self.emit('complete');
}];
if (callback.length > 1) {
args.unshift(self.client.api);
}
callback.apply(self, args);
};
}
process.nextTick(doneCallback);
return this;
};
module.exports = Perform;