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 DCHECK = requireNative('logging').DCHECK; |
9 var WARNING = requireNative('logging').WARNING; | 10 var WARNING = requireNative('logging').WARNING; |
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) { |
(...skipping 107 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 to a function |func| in a promise. The |
| 139 * remaining arguments will be passed to |func|. Returns a promise that will be |
| 140 * resolved to the result passed to the callback or rejected if an error occurs |
| 141 * (if chrome.runtime.lastError is set). If there are multiple results, the |
| 142 * promise will be resolved with an array containing those results. |
| 143 * |
| 144 * For example, |
| 145 * promise(chrome.storage.get, 'a').then(function(result) { |
| 146 * // Use result. |
| 147 * }).catch(function(error) { |
| 148 * // Report error.message. |
| 149 * }); |
| 150 */ |
| 151 function promise(func) { |
| 152 var args = $Array.slice(arguments, 1); |
| 153 DCHECK(typeof func == 'function'); |
| 154 return new Promise(function(resolve, reject) { |
| 155 args.push(function() { |
| 156 if (chrome.runtime.lastError) { |
| 157 reject(new Error(chrome.runtime.lastError)); |
| 158 return; |
| 159 } |
| 160 if (arguments.length <= 1) |
| 161 resolve(arguments[0]); |
| 162 else |
| 163 resolve($Array.slice(arguments)); |
| 164 }); |
| 165 $Function.apply(func, null, args); |
| 166 }); |
| 167 } |
| 168 |
136 exports.forEach = forEach; | 169 exports.forEach = forEach; |
137 exports.loadTypeSchema = loadTypeSchema; | 170 exports.loadTypeSchema = loadTypeSchema; |
138 exports.lookup = lookup; | 171 exports.lookup = lookup; |
139 exports.expose = expose; | 172 exports.expose = expose; |
140 exports.deepCopy = deepCopy; | 173 exports.deepCopy = deepCopy; |
| 174 exports.promise = promise; |
OLD | NEW |