This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
136 lines (99 loc) · 4.21 KB
/
index.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
module.exports = function(Nightmare) {
Nightmare.action('evaluateWithCallback', function() {
// expected args -> func, argn, done
// * func is the function passed in by the user to be executed in Electron
// * argn is an optional, variable number of parameters to be passed to func
// The callback will be passed at the end of the parameter list
// * done
var args = Array.prototype.slice.call(arguments);
var done = args.pop();
var func = args.splice(0, 1)[0];
if (!func || typeof func !== 'function') {
done('Execution function not provided');
return;
}
this.evaluate_now(function(func, args) {
// keep track of asyn evaluations inside of Electron
window.__asynceval = window.__asynceval || (function() {
var map = {};
// generates a unique handle
var generateHandle = function() {
var handle = Math.floor((Math.random() * 10000) + 1);
if (map[handle]) {
return generateHandle();
}
return handle;
}
// generates a self-managing callback function
var generateCallback = function(handle) {
var wasCalled = false;
var args;
var func = function() {
wasCalled = true;
args = Array.prototype.slice.call(arguments);
}
func.getHandle = function() {
return handle;
}
func.getArguments = function() {
return args;
}
func.wasCalled = function() {
return wasCalled;
}
return func;
}
// public methods
return {
generateCallback: function() {
var handle = generateHandle();
var callback = generateCallback(handle);
map[handle] = callback;
return callback;
},
wasCalled: function(handle) {
return map[handle] ? map[handle].wasCalled() : null;
},
getArguments: function(handle) {
return map[handle] ? map[handle].getArguments() : null;
}
}
})();
var callback = window.__asynceval.generateCallback();
args.push(callback);
// func was stringified to allow it to cross the execution boundry
// eval and execute
eval("("+func+")").apply(window, args);
// return the callback handle
return callback.getHandle();
}, function(error, handle) {
// if there was an execution error, bubble out
if (error) {
done.apply(this, arguments);
return;
}
// checks the handle on interval until it returns
var checkHandle = (function(handle) {
this.evaluate_now(function(handle) {
return {
wasCalled: window.__asynceval.wasCalled(handle),
arguments: window.__asynceval.getArguments(handle)
}
}, function(error, result) {
if (error) {
done.apply(this, arguments);
return;
}
// if the callback hasn't been invoked, check again later
if (result.wasCalled) {
done(undefined, result.arguments)
} else {
setTimeout(checkHandle, 10, handle);
}
}, handle);
}).bind(this); // tighten the scope
// first check is immediate
checkHandle(handle);
}.bind(this), func.toString(), args); // tighten the scope
})
}