@angular-wave/angular.ts 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/.eslintignore +1 -0
  2. package/.eslintrc.cjs +29 -0
  3. package/.github/workflows/playwright.yml +27 -0
  4. package/CHANGELOG.md +17974 -0
  5. package/CODE_OF_CONDUCT.md +3 -0
  6. package/CONTRIBUTING.md +246 -0
  7. package/DEVELOPERS.md +488 -0
  8. package/LICENSE +22 -0
  9. package/Makefile +31 -0
  10. package/README.md +115 -0
  11. package/RELEASE.md +98 -0
  12. package/SECURITY.md +16 -0
  13. package/TRIAGING.md +135 -0
  14. package/css/angular.css +22 -0
  15. package/dist/angular-ts.cjs.js +36843 -0
  16. package/dist/angular-ts.esm.js +36841 -0
  17. package/dist/angular-ts.umd.js +36848 -0
  18. package/dist/build/angular-animate.js +4272 -0
  19. package/dist/build/angular-aria.js +426 -0
  20. package/dist/build/angular-message-format.js +1072 -0
  21. package/dist/build/angular-messages.js +829 -0
  22. package/dist/build/angular-mocks.js +3757 -0
  23. package/dist/build/angular-parse-ext.js +1275 -0
  24. package/dist/build/angular-resource.js +911 -0
  25. package/dist/build/angular-route.js +1266 -0
  26. package/dist/build/angular-sanitize.js +891 -0
  27. package/dist/build/angular-touch.js +368 -0
  28. package/dist/build/angular.js +36600 -0
  29. package/e2e/unit.spec.ts +15 -0
  30. package/images/android-chrome-192x192.png +0 -0
  31. package/images/android-chrome-512x512.png +0 -0
  32. package/images/apple-touch-icon.png +0 -0
  33. package/images/favicon-16x16.png +0 -0
  34. package/images/favicon-32x32.png +0 -0
  35. package/images/favicon.ico +0 -0
  36. package/images/site.webmanifest +1 -0
  37. package/index.html +104 -0
  38. package/package.json +47 -0
  39. package/playwright.config.ts +78 -0
  40. package/public/circle.html +1 -0
  41. package/public/my_child_directive.html +1 -0
  42. package/public/my_directive.html +1 -0
  43. package/public/my_other_directive.html +1 -0
  44. package/public/test.html +1 -0
  45. package/rollup.config.js +31 -0
  46. package/src/animations/animateCache.js +55 -0
  47. package/src/animations/animateChildrenDirective.js +105 -0
  48. package/src/animations/animateCss.js +1139 -0
  49. package/src/animations/animateCssDriver.js +291 -0
  50. package/src/animations/animateJs.js +367 -0
  51. package/src/animations/animateJsDriver.js +67 -0
  52. package/src/animations/animateQueue.js +851 -0
  53. package/src/animations/animation.js +506 -0
  54. package/src/animations/module.js +779 -0
  55. package/src/animations/ngAnimateSwap.js +119 -0
  56. package/src/animations/rafScheduler.js +50 -0
  57. package/src/animations/shared.js +378 -0
  58. package/src/constants.js +20 -0
  59. package/src/core/animate.js +845 -0
  60. package/src/core/animateCss.js +73 -0
  61. package/src/core/animateRunner.js +195 -0
  62. package/src/core/attributes.js +199 -0
  63. package/src/core/cache.js +45 -0
  64. package/src/core/compile.js +4727 -0
  65. package/src/core/controller.js +225 -0
  66. package/src/core/exceptionHandler.js +63 -0
  67. package/src/core/filter.js +146 -0
  68. package/src/core/interpolate.js +442 -0
  69. package/src/core/interval.js +188 -0
  70. package/src/core/intervalFactory.js +57 -0
  71. package/src/core/location.js +1086 -0
  72. package/src/core/parser/parse.js +2562 -0
  73. package/src/core/parser/parse.md +13 -0
  74. package/src/core/q.js +746 -0
  75. package/src/core/rootScope.js +1596 -0
  76. package/src/core/sanitizeUri.js +85 -0
  77. package/src/core/sce.js +1161 -0
  78. package/src/core/taskTrackerFactory.js +125 -0
  79. package/src/core/timeout.js +121 -0
  80. package/src/core/urlUtils.js +187 -0
  81. package/src/core/utils.js +1349 -0
  82. package/src/directive/a.js +37 -0
  83. package/src/directive/attrs.js +283 -0
  84. package/src/directive/bind.js +51 -0
  85. package/src/directive/bind.md +142 -0
  86. package/src/directive/change.js +12 -0
  87. package/src/directive/change.md +25 -0
  88. package/src/directive/cloak.js +12 -0
  89. package/src/directive/cloak.md +24 -0
  90. package/src/directive/events.js +75 -0
  91. package/src/directive/events.md +166 -0
  92. package/src/directive/form.js +725 -0
  93. package/src/directive/init.js +15 -0
  94. package/src/directive/init.md +41 -0
  95. package/src/directive/input.js +1783 -0
  96. package/src/directive/list.js +46 -0
  97. package/src/directive/list.md +22 -0
  98. package/src/directive/ngClass.js +249 -0
  99. package/src/directive/ngController.js +64 -0
  100. package/src/directive/ngCsp.js +82 -0
  101. package/src/directive/ngIf.js +134 -0
  102. package/src/directive/ngInclude.js +217 -0
  103. package/src/directive/ngModel.js +1356 -0
  104. package/src/directive/ngModelOptions.js +509 -0
  105. package/src/directive/ngOptions.js +670 -0
  106. package/src/directive/ngRef.js +90 -0
  107. package/src/directive/ngRepeat.js +650 -0
  108. package/src/directive/ngShowHide.js +255 -0
  109. package/src/directive/ngSwitch.js +178 -0
  110. package/src/directive/ngTransclude.js +98 -0
  111. package/src/directive/non-bindable.js +11 -0
  112. package/src/directive/non-bindable.md +17 -0
  113. package/src/directive/script.js +30 -0
  114. package/src/directive/select.js +624 -0
  115. package/src/directive/style.js +25 -0
  116. package/src/directive/style.md +23 -0
  117. package/src/directive/validators.js +329 -0
  118. package/src/exts/aria.js +544 -0
  119. package/src/exts/messages.js +852 -0
  120. package/src/filters/filter.js +207 -0
  121. package/src/filters/filter.md +69 -0
  122. package/src/filters/filters.js +239 -0
  123. package/src/filters/json.md +16 -0
  124. package/src/filters/limit-to.js +43 -0
  125. package/src/filters/limit-to.md +19 -0
  126. package/src/filters/order-by.js +183 -0
  127. package/src/filters/order-by.md +83 -0
  128. package/src/index.js +13 -0
  129. package/src/injector.js +1034 -0
  130. package/src/jqLite.js +1117 -0
  131. package/src/loader.js +1320 -0
  132. package/src/public.js +215 -0
  133. package/src/routeToRegExp.js +41 -0
  134. package/src/services/anchorScroll.js +135 -0
  135. package/src/services/browser.js +321 -0
  136. package/src/services/cacheFactory.js +398 -0
  137. package/src/services/cookieReader.js +72 -0
  138. package/src/services/document.js +64 -0
  139. package/src/services/http.js +1537 -0
  140. package/src/services/httpBackend.js +206 -0
  141. package/src/services/log.js +160 -0
  142. package/src/services/templateRequest.js +139 -0
  143. package/test/angular.spec.js +2153 -0
  144. package/test/aria/aria.spec.js +1245 -0
  145. package/test/binding.spec.js +504 -0
  146. package/test/build-test.html +14 -0
  147. package/test/injector.spec.js +2327 -0
  148. package/test/jasmine/jasmine-5.1.2/boot0.js +65 -0
  149. package/test/jasmine/jasmine-5.1.2/boot1.js +133 -0
  150. package/test/jasmine/jasmine-5.1.2/jasmine-html.js +963 -0
  151. package/test/jasmine/jasmine-5.1.2/jasmine.css +320 -0
  152. package/test/jasmine/jasmine-5.1.2/jasmine.js +10824 -0
  153. package/test/jasmine/jasmine-5.1.2/jasmine_favicon.png +0 -0
  154. package/test/jasmine/jasmine-browser.json +17 -0
  155. package/test/jasmine/jasmine.json +9 -0
  156. package/test/jqlite.spec.js +2133 -0
  157. package/test/loader.spec.js +219 -0
  158. package/test/messages/messages.spec.js +1146 -0
  159. package/test/min-err.spec.js +174 -0
  160. package/test/mock-test.html +13 -0
  161. package/test/module-test.html +15 -0
  162. package/test/ng/anomate.spec.js +606 -0
  163. package/test/ng/cache-factor.spec.js +334 -0
  164. package/test/ng/compile.spec.js +17956 -0
  165. package/test/ng/controller-provider.spec.js +227 -0
  166. package/test/ng/cookie-reader.spec.js +98 -0
  167. package/test/ng/directive/a.spec.js +192 -0
  168. package/test/ng/directive/bind.spec.js +334 -0
  169. package/test/ng/directive/boolean.spec.js +136 -0
  170. package/test/ng/directive/change.spec.js +71 -0
  171. package/test/ng/directive/class.spec.js +858 -0
  172. package/test/ng/directive/click.spec.js +38 -0
  173. package/test/ng/directive/cloak.spec.js +44 -0
  174. package/test/ng/directive/constoller.spec.js +194 -0
  175. package/test/ng/directive/element-style.spec.js +92 -0
  176. package/test/ng/directive/event.spec.js +282 -0
  177. package/test/ng/directive/form.spec.js +1518 -0
  178. package/test/ng/directive/href.spec.js +143 -0
  179. package/test/ng/directive/if.spec.js +402 -0
  180. package/test/ng/directive/include.spec.js +828 -0
  181. package/test/ng/directive/init.spec.js +68 -0
  182. package/test/ng/directive/input.spec.js +3810 -0
  183. package/test/ng/directive/list.spec.js +170 -0
  184. package/test/ng/directive/model-options.spec.js +1008 -0
  185. package/test/ng/directive/model.spec.js +1905 -0
  186. package/test/ng/directive/non-bindable.spec.js +55 -0
  187. package/test/ng/directive/options.spec.js +3583 -0
  188. package/test/ng/directive/ref.spec.js +575 -0
  189. package/test/ng/directive/repeat.spec.js +1675 -0
  190. package/test/ng/directive/script.spec.js +52 -0
  191. package/test/ng/directive/scrset.spec.js +67 -0
  192. package/test/ng/directive/select.spec.js +2541 -0
  193. package/test/ng/directive/show-hide.spec.js +253 -0
  194. package/test/ng/directive/src.spec.js +157 -0
  195. package/test/ng/directive/style.spec.js +178 -0
  196. package/test/ng/directive/switch.spec.js +647 -0
  197. package/test/ng/directive/validators.spec.js +717 -0
  198. package/test/ng/document.spec.js +52 -0
  199. package/test/ng/filter/filter.spec.js +714 -0
  200. package/test/ng/filter/filters.spec.js +35 -0
  201. package/test/ng/filter/limit-to.spec.js +251 -0
  202. package/test/ng/filter/order-by.spec.js +891 -0
  203. package/test/ng/filter.spec.js +149 -0
  204. package/test/ng/http-backend.spec.js +398 -0
  205. package/test/ng/http.spec.js +4071 -0
  206. package/test/ng/interpolate.spec.js +642 -0
  207. package/test/ng/interval.spec.js +343 -0
  208. package/test/ng/location.spec.js +3488 -0
  209. package/test/ng/on.spec.js +229 -0
  210. package/test/ng/parse.spec.js +4655 -0
  211. package/test/ng/prop.spec.js +805 -0
  212. package/test/ng/q.spec.js +2904 -0
  213. package/test/ng/root-element.spec.js +16 -0
  214. package/test/ng/sanitize-uri.spec.js +249 -0
  215. package/test/ng/sce.spec.js +660 -0
  216. package/test/ng/scope.spec.js +3442 -0
  217. package/test/ng/template-request.spec.js +236 -0
  218. package/test/ng/timeout.spec.js +351 -0
  219. package/test/ng/url-utils.spec.js +156 -0
  220. package/test/ng/utils.spec.js +144 -0
  221. package/test/original-test.html +21 -0
  222. package/test/public.spec.js +34 -0
  223. package/test/sanitize/bing-html.spec.js +36 -0
  224. package/test/server/express.js +158 -0
  225. package/test/test-utils.js +11 -0
  226. package/tsconfig.json +17 -0
  227. package/types/angular.d.ts +138 -0
  228. package/types/global.d.ts +9 -0
  229. package/types/index.d.ts +2357 -0
  230. package/types/jqlite.d.ts +558 -0
  231. package/vite.config.js +14 -0
package/src/core/q.js ADDED
@@ -0,0 +1,746 @@
1
+ /**
2
+ * @ngdoc service
3
+ * @name $q
4
+ * @requires $rootScope
5
+ *
6
+ * @description
7
+ * A service that helps you run functions asynchronously, and use their return values (or exceptions)
8
+ * when they are done processing.
9
+ *
10
+ * This is a [Promises/A+](https://promisesaplus.com/)-compliant implementation of promises/deferred
11
+ * objects inspired by [Kris Kowal's Q](https://github.com/kriskowal/q).
12
+ *
13
+ * $q can be used in two fashions --- one which is more similar to Kris Kowal's Q or jQuery's Deferred
14
+ * implementations, and the other which resembles ES6 (ES2015) promises to some degree.
15
+ *
16
+ * ## $q constructor
17
+ *
18
+ * The streamlined ES6 style promise is essentially just using $q as a constructor which takes a `resolver`
19
+ * function as the first argument. This is similar to the native Promise implementation from ES6,
20
+ * see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).
21
+ *
22
+ * While the constructor-style use is supported, not all of the supporting methods from ES6 promises are
23
+ * available yet.
24
+ *
25
+ * It can be used like so:
26
+ *
27
+ * ```js
28
+ * // for the purpose of this example let's assume that variables `$q` and `okToGreet`
29
+ * // are available in the current lexical scope (they could have been injected or passed in).
30
+ *
31
+ * function asyncGreet(name) {
32
+ * // perform some asynchronous operation, resolve or reject the promise when appropriate.
33
+ * return $q(function(resolve, reject) {
34
+ * setTimeout(function() {
35
+ * if (okToGreet(name)) {
36
+ * resolve('Hello, ' + name + '!');
37
+ * } else {
38
+ * reject('Greeting ' + name + ' is not allowed.');
39
+ * }
40
+ * }, 1000);
41
+ * });
42
+ * }
43
+ *
44
+ * let promise = asyncGreet('Robin Hood');
45
+ * promise.then(function(greeting) {
46
+ * alert('Success: ' + greeting);
47
+ * }, function(reason) {
48
+ * alert('Failed: ' + reason);
49
+ * });
50
+ * ```
51
+ *
52
+ * Note: progress/notify callbacks are not currently supported via the ES6-style interface.
53
+ *
54
+ * Note: unlike ES6 behavior, an exception thrown in the constructor function will NOT implicitly reject the promise.
55
+ *
56
+ * However, the more traditional CommonJS-style usage is still available, and documented below.
57
+ *
58
+ * [The CommonJS Promise proposal](http://wiki.commonjs.org/wiki/Promises) describes a promise as an
59
+ * interface for interacting with an object that represents the result of an action that is
60
+ * performed asynchronously, and may or may not be finished at any given point in time.
61
+ *
62
+ * From the perspective of dealing with error handling, deferred and promise APIs are to
63
+ * asynchronous programming what `try`, `catch` and `throw` keywords are to synchronous programming.
64
+ *
65
+ * ```js
66
+ * // for the purpose of this example let's assume that variables `$q` and `okToGreet`
67
+ * // are available in the current lexical scope (they could have been injected or passed in).
68
+ *
69
+ * function asyncGreet(name) {
70
+ * let deferred = $q.defer();
71
+ *
72
+ * setTimeout(function() {
73
+ * deferred.notify('About to greet ' + name + '.');
74
+ *
75
+ * if (okToGreet(name)) {
76
+ * deferred.resolve('Hello, ' + name + '!');
77
+ * } else {
78
+ * deferred.reject('Greeting ' + name + ' is not allowed.');
79
+ * }
80
+ * }, 1000);
81
+ *
82
+ * return deferred.promise;
83
+ * }
84
+ *
85
+ * let promise = asyncGreet('Robin Hood');
86
+ * promise.then(function(greeting) {
87
+ * alert('Success: ' + greeting);
88
+ * }, function(reason) {
89
+ * alert('Failed: ' + reason);
90
+ * }, function(update) {
91
+ * alert('Got notification: ' + update);
92
+ * });
93
+ * ```
94
+ *
95
+ * At first it might not be obvious why this extra complexity is worth the trouble. The payoff
96
+ * comes in the way of guarantees that promise and deferred APIs make, see
97
+ * https://github.com/kriskowal/uncommonjs/blob/master/promises/specification.md.
98
+ *
99
+ * Additionally the promise api allows for composition that is very hard to do with the
100
+ * traditional callback ([CPS](http://en.wikipedia.org/wiki/Continuation-passing_style)) approach.
101
+ * For more on this please see the [Q documentation](https://github.com/kriskowal/q) especially the
102
+ * section on serial or parallel joining of promises.
103
+ *
104
+ * ## The Deferred API
105
+ *
106
+ * A new instance of deferred is constructed by calling `$q.defer()`.
107
+ *
108
+ * The purpose of the deferred object is to expose the associated Promise instance as well as APIs
109
+ * that can be used for signaling the successful or unsuccessful completion, as well as the status
110
+ * of the task.
111
+ *
112
+ * **Methods**
113
+ *
114
+ * - `resolve(value)` – resolves the derived promise with the `value`. If the value is a rejection
115
+ * constructed via `$q.reject`, the promise will be rejected instead.
116
+ * - `reject(reason)` – rejects the derived promise with the `reason`. This is equivalent to
117
+ * resolving it with a rejection constructed via `$q.reject`.
118
+ * - `notify(value)` - provides updates on the status of the promise's execution. This may be called
119
+ * multiple times before the promise is either resolved or rejected.
120
+ *
121
+ * **Properties**
122
+ *
123
+ * - promise – `{Promise}` – promise object associated with this deferred.
124
+ *
125
+ *
126
+ * ## The Promise API
127
+ *
128
+ * A new promise instance is created when a deferred instance is created and can be retrieved by
129
+ * calling `deferred.promise`.
130
+ *
131
+ * The purpose of the promise object is to allow for interested parties to get access to the result
132
+ * of the deferred task when it completes.
133
+ *
134
+ * **Methods**
135
+ *
136
+ * - `then(successCallback, [errorCallback], [notifyCallback])` – regardless of when the promise was or
137
+ * will be resolved or rejected, `then` calls one of the success or error callbacks asynchronously
138
+ * as soon as the result is available. The callbacks are called with a single argument: the result
139
+ * or rejection reason. Additionally, the notify callback may be called zero or more times to
140
+ * provide a progress indication, before the promise is resolved or rejected.
141
+ *
142
+ * This method *returns a new promise* which is resolved or rejected via the return value of the
143
+ * `successCallback`, `errorCallback` (unless that value is a promise, in which case it is resolved
144
+ * with the value which is resolved in that promise using
145
+ * [promise chaining](http://www.html5rocks.com/en/tutorials/es6/promises/#toc-promises-queues)).
146
+ * It also notifies via the return value of the `notifyCallback` method. The promise cannot be
147
+ * resolved or rejected from the notifyCallback method. The errorCallback and notifyCallback
148
+ * arguments are optional.
149
+ *
150
+ * - `catch(errorCallback)` – shorthand for `promise.then(null, errorCallback)`
151
+ *
152
+ * - `finally(callback, notifyCallback)` – allows you to observe either the fulfillment or rejection of a promise,
153
+ * but to do so without modifying the final value. This is useful to release resources or do some
154
+ * clean-up that needs to be done whether the promise was rejected or resolved. See the [full
155
+ * specification](https://github.com/kriskowal/q/wiki/API-Reference#promisefinallycallback) for
156
+ * more information.
157
+ *
158
+ * ## Chaining promises
159
+ *
160
+ * Because calling the `then` method of a promise returns a new derived promise, it is easily
161
+ * possible to create a chain of promises:
162
+ *
163
+ * ```js
164
+ * promiseB = promiseA.then(function(result) {
165
+ * return result + 1;
166
+ * });
167
+ *
168
+ * // promiseB will be resolved immediately after promiseA is resolved and its value
169
+ * // will be the result of promiseA incremented by 1
170
+ * ```
171
+ *
172
+ * It is possible to create chains of any length and since a promise can be resolved with another
173
+ * promise (which will defer its resolution further), it is possible to pause/defer resolution of
174
+ * the promises at any point in the chain. This makes it possible to implement powerful APIs like
175
+ * $http's response interceptors.
176
+ *
177
+ *
178
+ * ## Differences between Kris Kowal's Q and $q
179
+ *
180
+ * There are two main differences:
181
+ *
182
+ * - $q is integrated with the {@link ng.$rootScope.Scope} Scope model observation
183
+ * mechanism in AngularJS, which means faster propagation of resolution or rejection into your
184
+ * models and avoiding unnecessary browser repaints, which would result in flickering UI.
185
+ * - Q has many more features than $q, but that comes at a cost of bytes. $q is tiny, but contains
186
+ * all the important functionality needed for common async tasks.
187
+ *
188
+ * ## Testing
189
+ *
190
+ * ```js
191
+ * it('should simulate promise', inject(function($q, $rootScope) {
192
+ * let deferred = $q.defer();
193
+ * let promise = deferred.promise;
194
+ * let resolvedValue;
195
+ *
196
+ * promise.then(function(value) { resolvedValue = value; });
197
+ * expect(resolvedValue).toBeUndefined();
198
+ *
199
+ * // Simulate resolving of promise
200
+ * deferred.resolve(123);
201
+ * // Note that the 'then' function does not get called synchronously.
202
+ * // This is because we want the promise API to always be async, whether or not
203
+ * // it got called synchronously or asynchronously.
204
+ * expect(resolvedValue).toBeUndefined();
205
+ *
206
+ * // Propagate promise resolution to 'then' functions using $apply().
207
+ * $rootScope.$apply();
208
+ * expect(resolvedValue).toEqual(123);
209
+ * }));
210
+ * ```
211
+ *
212
+ * @param {function(function, function)} resolver Function which is responsible for resolving or
213
+ * rejecting the newly created promise. The first parameter is a function which resolves the
214
+ * promise, the second parameter is a function which rejects the promise.
215
+ *
216
+ * @returns {Promise} The newly created promise.
217
+ */
218
+
219
+ import {
220
+ forEach,
221
+ isArray,
222
+ minErr,
223
+ extend,
224
+ isUndefined,
225
+ isFunction,
226
+ isObject,
227
+ isDefined,
228
+ isError,
229
+ toDebugString,
230
+ isPromiseLike,
231
+ } from "./utils";
232
+
233
+ /**
234
+ * @ngdoc provider
235
+ * @name $qProvider
236
+ *
237
+ *
238
+ * @description
239
+ */
240
+ export function $QProvider() {
241
+ let errorOnUnhandledRejections = true;
242
+ this.$get = [
243
+ "$rootScope",
244
+ "$exceptionHandler",
245
+ function ($rootScope, $exceptionHandler) {
246
+ return qFactory(
247
+ (callback) => {
248
+ $rootScope.$evalAsync(callback);
249
+ },
250
+ $exceptionHandler,
251
+ errorOnUnhandledRejections,
252
+ );
253
+ },
254
+ ];
255
+
256
+ /**
257
+ * @ngdoc method
258
+ * @name $qProvider#errorOnUnhandledRejections
259
+ * @kind function
260
+ *
261
+ * @description
262
+ * Retrieves or overrides whether to generate an error when a rejected promise is not handled.
263
+ * This feature is enabled by default.
264
+ *
265
+ * @param {boolean=} value Whether to generate an error when a rejected promise is not handled.
266
+ * @returns {boolean|ng.$qProvider} Current value when called without a new value or self for
267
+ * chaining otherwise.
268
+ */
269
+ this.errorOnUnhandledRejections = function (value) {
270
+ if (isDefined(value)) {
271
+ errorOnUnhandledRejections = value;
272
+ return this;
273
+ }
274
+ return errorOnUnhandledRejections;
275
+ };
276
+ }
277
+ export function $$QProvider() {
278
+ let errorOnUnhandledRejections = true;
279
+ this.$get = [
280
+ "$browser",
281
+ "$exceptionHandler",
282
+ function ($browser, $exceptionHandler) {
283
+ return qFactory(
284
+ (callback) => {
285
+ $browser.defer(callback);
286
+ },
287
+ $exceptionHandler,
288
+ errorOnUnhandledRejections,
289
+ );
290
+ },
291
+ ];
292
+
293
+ this.errorOnUnhandledRejections = function (value) {
294
+ if (isDefined(value)) {
295
+ errorOnUnhandledRejections = value;
296
+ return this;
297
+ }
298
+ return errorOnUnhandledRejections;
299
+ };
300
+ }
301
+
302
+ /**
303
+ * Constructs a promise manager.
304
+ *
305
+ * @param {function(function)} nextTick Function for executing functions in the next turn.
306
+ * @param {function(...*)} exceptionHandler Function into which unexpected exceptions are passed for
307
+ * debugging purposes.
308
+ * @param {boolean=} errorOnUnhandledRejections Whether an error should be generated on unhandled
309
+ * promises rejections.
310
+ * @returns {object} Promise manager.
311
+ */
312
+ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
313
+ const $qMinErr = minErr("$q");
314
+ let queueSize = 0;
315
+ const checkQueue = [];
316
+
317
+ /**
318
+ * @ngdoc method
319
+ * @name ng.$q#defer
320
+ * @kind function
321
+ *
322
+ * @description
323
+ * Creates a `Deferred` object which represents a task which will finish in the future.
324
+ *
325
+ * @returns {Deferred} Returns a new instance of deferred.
326
+ */
327
+ function defer() {
328
+ return new Deferred();
329
+ }
330
+
331
+ function Deferred() {
332
+ const promise = (this.promise = new Promise());
333
+ // Non prototype methods necessary to support unbound execution :/
334
+ this.resolve = function (val) {
335
+ resolvePromise(promise, val);
336
+ };
337
+ this.reject = function (reason) {
338
+ rejectPromise(promise, reason);
339
+ };
340
+ this.notify = function (progress) {
341
+ notifyPromise(promise, progress);
342
+ };
343
+ }
344
+
345
+ function Promise() {
346
+ this.$$state = { status: 0 };
347
+ }
348
+
349
+ extend(Promise.prototype, {
350
+ then(onFulfilled, onRejected, progressBack) {
351
+ if (
352
+ isUndefined(onFulfilled) &&
353
+ isUndefined(onRejected) &&
354
+ isUndefined(progressBack)
355
+ ) {
356
+ return this;
357
+ }
358
+ const result = new Promise();
359
+
360
+ this.$$state.pending = this.$$state.pending || [];
361
+ this.$$state.pending.push([
362
+ result,
363
+ onFulfilled,
364
+ onRejected,
365
+ progressBack,
366
+ ]);
367
+ if (this.$$state.status > 0) scheduleProcessQueue(this.$$state);
368
+
369
+ return result;
370
+ },
371
+
372
+ catch(callback) {
373
+ return this.then(null, callback);
374
+ },
375
+
376
+ finally(callback, progressBack) {
377
+ return this.then(
378
+ (value) => handleCallback(value, resolve, callback),
379
+ (error) => handleCallback(error, reject, callback),
380
+ progressBack,
381
+ );
382
+ },
383
+ });
384
+
385
+ function processQueue(state) {
386
+ let fn;
387
+ let promise;
388
+ let pending;
389
+
390
+ pending = state.pending;
391
+ state.processScheduled = false;
392
+ state.pending = undefined;
393
+ try {
394
+ for (let i = 0, ii = pending.length; i < ii; ++i) {
395
+ markQStateExceptionHandled(state);
396
+ promise = pending[i][0];
397
+ fn = pending[i][state.status];
398
+ try {
399
+ if (isFunction(fn)) {
400
+ resolvePromise(promise, fn(state.value));
401
+ } else if (state.status === 1) {
402
+ resolvePromise(promise, state.value);
403
+ } else {
404
+ rejectPromise(promise, state.value);
405
+ }
406
+ } catch (e) {
407
+ rejectPromise(promise, e);
408
+ // This error is explicitly marked for being passed to the $exceptionHandler
409
+ if (e && e.$$passToExceptionHandler === true) {
410
+ exceptionHandler(e);
411
+ }
412
+ }
413
+ }
414
+ } finally {
415
+ --queueSize;
416
+ if (errorOnUnhandledRejections && queueSize === 0) {
417
+ nextTick(processChecks);
418
+ }
419
+ }
420
+ }
421
+
422
+ function processChecks() {
423
+ while (!queueSize && checkQueue.length) {
424
+ const toCheck = checkQueue.shift();
425
+ if (!isStateExceptionHandled(toCheck)) {
426
+ markQStateExceptionHandled(toCheck);
427
+ const errorMessage = `Possibly unhandled rejection: ${toDebugString(toCheck.value)}`;
428
+ if (isError(toCheck.value)) {
429
+ exceptionHandler(toCheck.value, errorMessage);
430
+ } else {
431
+ exceptionHandler(errorMessage);
432
+ }
433
+ }
434
+ }
435
+ }
436
+
437
+ function scheduleProcessQueue(state) {
438
+ if (
439
+ errorOnUnhandledRejections &&
440
+ !state.pending &&
441
+ state.status === 2 &&
442
+ !isStateExceptionHandled(state)
443
+ ) {
444
+ if (queueSize === 0 && checkQueue.length === 0) {
445
+ nextTick(processChecks);
446
+ }
447
+ checkQueue.push(state);
448
+ }
449
+ if (state.processScheduled || !state.pending) return;
450
+ state.processScheduled = true;
451
+ ++queueSize;
452
+ nextTick(() => {
453
+ processQueue(state);
454
+ });
455
+ }
456
+
457
+ function resolvePromise(promise, val) {
458
+ if (promise.$$state.status) return;
459
+ if (val === promise) {
460
+ $$reject(
461
+ promise,
462
+ $qMinErr(
463
+ "qcycle",
464
+ "Expected promise to be resolved with value other than itself '{0}'",
465
+ val,
466
+ ),
467
+ );
468
+ } else {
469
+ $$resolve(promise, val);
470
+ }
471
+ }
472
+
473
+ function $$resolve(promise, val) {
474
+ let then;
475
+ let done = false;
476
+ try {
477
+ if (isObject(val) || isFunction(val)) then = val.then;
478
+ if (isFunction(then)) {
479
+ promise.$$state.status = -1;
480
+ then.call(val, doResolve, doReject, doNotify);
481
+ } else {
482
+ promise.$$state.value = val;
483
+ promise.$$state.status = 1;
484
+ scheduleProcessQueue(promise.$$state);
485
+ }
486
+ } catch (e) {
487
+ doReject(e);
488
+ }
489
+
490
+ function doResolve(val) {
491
+ if (done) return;
492
+ done = true;
493
+ $$resolve(promise, val);
494
+ }
495
+ function doReject(val) {
496
+ if (done) return;
497
+ done = true;
498
+ $$reject(promise, val);
499
+ }
500
+ function doNotify(progress) {
501
+ notifyPromise(promise, progress);
502
+ }
503
+ }
504
+
505
+ function rejectPromise(promise, reason) {
506
+ if (promise.$$state.status) return;
507
+ $$reject(promise, reason);
508
+ }
509
+
510
+ function $$reject(promise, reason) {
511
+ promise.$$state.value = reason;
512
+ promise.$$state.status = 2;
513
+ scheduleProcessQueue(promise.$$state);
514
+ }
515
+
516
+ function notifyPromise(promise, progress) {
517
+ const callbacks = promise.$$state.pending;
518
+
519
+ if (promise.$$state.status <= 0 && callbacks && callbacks.length) {
520
+ nextTick(() => {
521
+ let callback;
522
+ let result;
523
+ for (let i = 0, ii = callbacks.length; i < ii; i++) {
524
+ result = callbacks[i][0];
525
+ callback = callbacks[i][3];
526
+ try {
527
+ notifyPromise(
528
+ result,
529
+ isFunction(callback) ? callback(progress) : progress,
530
+ );
531
+ } catch (e) {
532
+ exceptionHandler(e);
533
+ }
534
+ }
535
+ });
536
+ }
537
+ }
538
+
539
+ /**
540
+ * @ngdoc method
541
+ * @name $q#reject
542
+ * @kind function
543
+ *
544
+ * @description
545
+ * Creates a promise that is resolved as rejected with the specified `reason`. This api should be
546
+ * used to forward rejection in a chain of promises. If you are dealing with the last promise in
547
+ * a promise chain, you don't need to worry about it.
548
+ *
549
+ * When comparing deferreds/promises to the familiar behavior of try/catch/throw, think of
550
+ * `reject` as the `throw` keyword in JavaScript. This also means that if you "catch" an error via
551
+ * a promise error callback and you want to forward the error to the promise derived from the
552
+ * current promise, you have to "rethrow" the error by returning a rejection constructed via
553
+ * `reject`.
554
+ *
555
+ * ```js
556
+ * promiseB = promiseA.then(function(result) {
557
+ * // success: do something and resolve promiseB
558
+ * // with the old or a new result
559
+ * return result;
560
+ * }, function(reason) {
561
+ * // error: handle the error if possible and
562
+ * // resolve promiseB with newPromiseOrValue,
563
+ * // otherwise forward the rejection to promiseB
564
+ * if (canHandle(reason)) {
565
+ * // handle the error and recover
566
+ * return newPromiseOrValue;
567
+ * }
568
+ * return $q.reject(reason);
569
+ * });
570
+ * ```
571
+ *
572
+ * @param {*} reason Constant, message, exception or an object representing the rejection reason.
573
+ * @returns {Promise} Returns a promise that was already resolved as rejected with the `reason`.
574
+ */
575
+ function reject(reason) {
576
+ const result = new Promise();
577
+ rejectPromise(result, reason);
578
+ return result;
579
+ }
580
+
581
+ function handleCallback(value, resolver, callback) {
582
+ let callbackOutput = null;
583
+ try {
584
+ if (isFunction(callback)) callbackOutput = callback();
585
+ } catch (e) {
586
+ return reject(e);
587
+ }
588
+ if (isPromiseLike(callbackOutput)) {
589
+ return callbackOutput.then(() => resolver(value), reject);
590
+ }
591
+ return resolver(value);
592
+ }
593
+
594
+ /**
595
+ * @ngdoc method
596
+ * @name $q#when
597
+ * @kind function
598
+ *
599
+ * @description
600
+ * Wraps an object that might be a value or a (3rd party) then-able promise into a $q promise.
601
+ * This is useful when you are dealing with an object that might or might not be a promise, or if
602
+ * the promise comes from a source that can't be trusted.
603
+ *
604
+ * @param {*} value Value or a promise
605
+ * @param {Function=} successCallback
606
+ * @param {Function=} errorCallback
607
+ * @param {Function=} progressCallback
608
+ * @returns {Promise} Returns a promise of the passed value or promise
609
+ */
610
+
611
+ function when(value, callback, errback, progressBack) {
612
+ const result = new Promise();
613
+ resolvePromise(result, value);
614
+ return result.then(callback, errback, progressBack);
615
+ }
616
+
617
+ /**
618
+ * @ngdoc method
619
+ * @name $q#resolve
620
+ * @kind function
621
+ *
622
+ * @description
623
+ * Alias of {@link ng.$q#when when} to maintain naming consistency with ES6.
624
+ *
625
+ * @param {*} value Value or a promise
626
+ * @param {Function=} successCallback
627
+ * @param {Function=} errorCallback
628
+ * @param {Function=} progressCallback
629
+ * @returns {Promise} Returns a promise of the passed value or promise
630
+ */
631
+ let resolve = when;
632
+
633
+ /**
634
+ * @ngdoc method
635
+ * @name $q#all
636
+ * @kind function
637
+ *
638
+ * @description
639
+ * Combines multiple promises into a single promise that is resolved when all of the input
640
+ * promises are resolved.
641
+ *
642
+ * @param {Array.<Promise>|Object.<Promise>} promises An array or hash of promises.
643
+ * @returns {Promise} Returns a single promise that will be resolved with an array/hash of values,
644
+ * each value corresponding to the promise at the same index/key in the `promises` array/hash.
645
+ * If any of the promises is resolved with a rejection, this resulting promise will be rejected
646
+ * with the same rejection value.
647
+ */
648
+
649
+ function all(promises) {
650
+ const result = new Promise();
651
+ let counter = 0;
652
+ const results = isArray(promises) ? [] : {};
653
+
654
+ forEach(promises, (promise, key) => {
655
+ counter++;
656
+ when(promise).then(
657
+ (value) => {
658
+ results[key] = value;
659
+ if (!--counter) resolvePromise(result, results);
660
+ },
661
+ (reason) => {
662
+ rejectPromise(result, reason);
663
+ },
664
+ );
665
+ });
666
+
667
+ if (counter === 0) {
668
+ resolvePromise(result, results);
669
+ }
670
+
671
+ return result;
672
+ }
673
+
674
+ /**
675
+ * @ngdoc method
676
+ * @name $q#race
677
+ * @kind function
678
+ *
679
+ * @description
680
+ * Returns a promise that resolves or rejects as soon as one of those promises
681
+ * resolves or rejects, with the value or reason from that promise.
682
+ *
683
+ * @param {Array.<Promise>|Object.<Promise>} promises An array or hash of promises.
684
+ * @returns {Promise} a promise that resolves or rejects as soon as one of the `promises`
685
+ * resolves or rejects, with the value or reason from that promise.
686
+ */
687
+
688
+ function race(promises) {
689
+ const deferred = defer();
690
+
691
+ forEach(promises, (promise) => {
692
+ when(promise).then(deferred.resolve, deferred.reject);
693
+ });
694
+
695
+ return deferred.promise;
696
+ }
697
+
698
+ function $Q(resolver) {
699
+ if (!isFunction(resolver)) {
700
+ throw $qMinErr("norslvr", "Expected resolverFn, got '{0}'", resolver);
701
+ }
702
+
703
+ const promise = new Promise();
704
+
705
+ function resolveFn(value) {
706
+ resolvePromise(promise, value);
707
+ }
708
+
709
+ function rejectFn(reason) {
710
+ rejectPromise(promise, reason);
711
+ }
712
+
713
+ resolver(resolveFn, rejectFn);
714
+
715
+ return promise;
716
+ }
717
+
718
+ // Let's make the instanceof operator work for promises, so that
719
+ // `new $q(fn) instanceof $q` would evaluate to true.
720
+ $Q.prototype = Promise.prototype;
721
+
722
+ $Q.defer = defer;
723
+ $Q.reject = reject;
724
+ $Q.when = when;
725
+ $Q.resolve = resolve;
726
+ $Q.all = all;
727
+ $Q.race = race;
728
+
729
+ return $Q;
730
+ }
731
+
732
+ function isStateExceptionHandled(state) {
733
+ return !!state.pur;
734
+ }
735
+ function markQStateExceptionHandled(state) {
736
+ state.pur = true;
737
+ }
738
+ export function markQExceptionHandled(q) {
739
+ // Built-in `$q` promises will always have a `$$state` property. This check is to allow
740
+ // overwriting `$q` with a different promise library (e.g. Bluebird + angular-bluebird-promises).
741
+ // (Currently, this is the only method that might be called with a promise, even if it is not
742
+ // created by the built-in `$q`.)
743
+ if (q.$$state) {
744
+ markQStateExceptionHandled(q.$$state);
745
+ }
746
+ }