forked from kzahel/web-server-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chromise.js
183 lines (154 loc) · 4.35 KB
/
chromise.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @author Alexey Kuzmin <alexey@alexeykuzmin.com>
* @fileoverview Promise based wrapper for Chrome Extension API.
* @see https://developer.chrome.com/extensions/api_index
* @license MIT
* @version 3.1.0
*/
;(function(global) {
'use strict';
let apiProxy = {
/**
* @param {!Object} apiObject
* @param {string} methodName
* @param {Arguments} callArguments Arguments to be passes to method call.
*/
callMethod(apiObject, methodName, callArguments) {
let originalMethod = apiObject[methodName];
let callArgumentsArray = Array.from(callArguments);
return new Promise((resolve, reject) => {
let callback = apiProxy.processResponse_.bind(null, resolve, reject);
callArgumentsArray.push(callback);
originalMethod.apply(apiObject, callArgumentsArray);
});
},
/**
* @param {!Function} callback
* @param {!Function} errback
* @param {!Array} response Response from Extension API.
* @private
*/
processResponse_(callback, errback, ...response) {
let error = global.chrome.runtime.lastError;
if (typeof error == 'object') {
errback(new Error(error.message));
return;
}
if (response.length < 2)
response = response[0]; // undefined if response is empty
callback(response);
}
};
let classifier = {
/**
* @param {string} letter
* @return {boolean}
* @private
*/
isCapitalLetter_(letter) {
return letter == letter.toUpperCase();
},
/**
* @param {string} string
* @return {boolean}
* @private
*/
startsWithCapitalLetter_(string) {
return classifier.isCapitalLetter_(string[0]);
},
/**
* We need to decide should given property be wrapped or not
* by its name only. Retrieving its value would cause API initialization,
* that can take a long time (dozens of ms).
* @param {string} propName
* @return {boolean}
*/
propertyNeedsWrapping(propName) {
if (classifier.startsWithCapitalLetter_(propName)) {
// Either constructor, enum, or constant.
return false;
}
if (propName.startsWith('on') &&
classifier.isCapitalLetter_(propName[2])) {
// Extension API event, e.g. 'onUpdated'.
return false;
}
// Must be a namespace or a method.
return true;
}
};
let wrapGuy = {
/**
* @param {!Object} api API object to wrap.
* @return {!Object}
*/
wrapApi(api) {
return wrapGuy.wrapObject_(api);
},
/**
* Wraps API object.
* @param {!Object} apiObject
* @return {!Object}
* @private
*/
wrapObject_(apiObject) {
let wrappedObject = {};
Object.keys(apiObject)
.filter(classifier.propertyNeedsWrapping)
.forEach(keyName => {
Object.defineProperty(wrappedObject, keyName, {
enumerable: true,
configurable: true,
get() {
return wrapGuy.wrapObjectField_(apiObject, keyName);
}
});
});
return wrappedObject;
},
/**
* @type {!Map}
* @private
*/
wrappedFieldsCache_: new Map(),
/**
* Wraps single object field.
* @param {!Object} apiObject
* @param {string} keyName
* @return {?|undefined}
* @private
*/
wrapObjectField_(apiObject, keyName) {
let apiEntry = apiObject[keyName];
if (wrapGuy.wrappedFieldsCache_.has(apiEntry)) {
return wrapGuy.wrappedFieldsCache_.get(apiEntry);
}
let entryType = typeof apiEntry;
let wrappedField;
if (entryType == 'function') {
wrappedField = wrapGuy.wrapMethod_(apiObject, keyName);
}
if (entryType == 'object') {
wrappedField = wrapGuy.wrapObject_(apiEntry);
}
if (wrappedField) {
wrapGuy.wrappedFieldsCache_.set(apiEntry, wrappedField);
return wrappedField;
}
},
/**
* Wraps API method.
* @param {!Object} apiObject
* @param {string} methodName
* @return {!Function}
* @private
*/
wrapMethod_(apiObject, methodName) {
return function() {
return apiProxy.callMethod(apiObject, methodName, arguments);
}
}
};
let chromise = wrapGuy.wrapApi(global.chrome);
global.chromise = chromise;
}(this));