OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var createClassWrapper = requireNative('utils').createClassWrapper; | 5 var createClassWrapper = requireNative('utils').createClassWrapper; |
6 var nativeDeepCopy = requireNative('utils').deepCopy; | 6 var nativeDeepCopy = requireNative('utils').deepCopy; |
7 var schemaRegistry = requireNative('schema_registry'); | 7 var schemaRegistry = requireNative('schema_registry'); |
8 var CHECK = requireNative('logging').CHECK; | 8 var CHECK = requireNative('logging').CHECK; |
9 var WARNING = requireNative('logging').WARNING; | 9 var WARNING = requireNative('logging').WARNING; |
10 var context = requireNative('v8_context'); | |
10 | 11 |
11 /** | 12 /** |
12 * An object forEach. Calls |f| with each (key, value) pair of |obj|, using | 13 * An object forEach. Calls |f| with each (key, value) pair of |obj|, using |
13 * |self| as the target. | 14 * |self| as the target. |
14 * @param {Object} obj The object to iterate over. | 15 * @param {Object} obj The object to iterate over. |
15 * @param {function} f The function to call in each iteration. | 16 * @param {function} f The function to call in each iteration. |
16 * @param {Object} self The object to use as |this| in each function call. | 17 * @param {Object} self The object to use as |this| in each function call. |
17 */ | 18 */ |
18 function forEach(obj, f, self) { | 19 function forEach(obj, f, self) { |
19 for (var key in obj) { | 20 for (var key in obj) { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 } | 127 } |
127 | 128 |
128 /** | 129 /** |
129 * Returns a deep copy of |value|. The copy will have no references to nested | 130 * Returns a deep copy of |value|. The copy will have no references to nested |
130 * values of |value|. | 131 * values of |value|. |
131 */ | 132 */ |
132 function deepCopy(value) { | 133 function deepCopy(value) { |
133 return nativeDeepCopy(value); | 134 return nativeDeepCopy(value); |
134 } | 135 } |
135 | 136 |
137 /** | |
138 * Wrap an asynchronous API call in a promise. | |
not at google - send to devlin
2014/08/19 19:23:38
Explain how the call happens. I presume that |arg|
Sam McNally
2014/08/20 01:27:18
Done.
| |
139 */ | |
140 function promise(func) { | |
141 var args = $Array.slice(arguments, 1); | |
142 return new Promise(function(resolve, reject) { | |
143 args.push(function() { | |
144 if (chrome.runtime.lastError) { | |
145 reject(new Error(chrome.runtime.lastError)); | |
146 return; | |
147 } | |
148 if (arguments.length <= 1) | |
149 resolve(arguments[0]); | |
150 else | |
151 resolve($Array.slice(arguments)); | |
not at google - send to devlin
2014/08/19 19:23:38
This doesn't seem right; so if there are multiple
Sam McNally
2014/08/20 01:27:18
Promises only support one argument.
not at google - send to devlin
2014/08/20 01:47:22
Yikes. THE MORE YOU KNOW.
| |
152 }); | |
153 $Function.apply(func, null, args); | |
154 }); | |
155 } | |
156 | |
157 /** | |
158 * Asynchronously require a module from the background page, loading the | |
159 * background page if necessary. | |
not at google - send to devlin
2014/08/19 19:23:38
You might actually want to explain why you need th
Sam McNally
2014/08/20 01:27:18
Done and moved it into serial_custom_bindings.
| |
160 */ | |
161 function requireAsyncFromBackgroundPage(moduleName) { | |
162 return promise(chrome.runtime.getBackgroundPage).then(function(bgPage) { | |
163 return context.GetModuleSystem(bgPage).requireAsync(moduleName); | |
164 }).catch(function() { | |
165 return requireAsync(moduleName); | |
166 }); | |
167 } | |
168 | |
169 /** | |
170 * Create an async proxy for the resolved value of |targetPromise|. The returned | |
171 * object will contain a function for each name in |functionNames|. Calling this | |
172 * function will result in a call to the corresponding function on the resolved | |
173 * value of |targetPromise| and returns a promise to the result of that call. | |
174 */ | |
175 function createAsyncProxy(targetPromise, functionNames) { | |
not at google - send to devlin
2014/08/19 19:23:38
I'm having a hard time grasping what this method d
Sam McNally
2014/08/20 01:27:18
Fair enough. Removed it and inlined the uses.
| |
176 var functionProxies = {}; | |
177 $Array.forEach(functionNames, function(name) { | |
178 functionProxies[name] = function() { | |
179 var args = arguments; | |
180 return targetPromise.then(function(target) { | |
181 return $Function.apply(target[name], target, args); | |
182 }); | |
183 }; | |
184 }); | |
185 return functionProxies; | |
186 } | |
187 | |
136 exports.forEach = forEach; | 188 exports.forEach = forEach; |
137 exports.loadTypeSchema = loadTypeSchema; | 189 exports.loadTypeSchema = loadTypeSchema; |
138 exports.lookup = lookup; | 190 exports.lookup = lookup; |
139 exports.expose = expose; | 191 exports.expose = expose; |
140 exports.deepCopy = deepCopy; | 192 exports.deepCopy = deepCopy; |
193 exports.promise = promise; | |
194 exports.requireAsyncFromBackgroundPage = requireAsyncFromBackgroundPage; | |
195 exports.createAsyncProxy = createAsyncProxy; | |
OLD | NEW |