Promise_NonGeneric.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. using ZenFulcrum.EmbeddedBrowser.Promises;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace ZenFulcrum.EmbeddedBrowser
  8. {
  9. /// <summary>
  10. /// Implements a non-generic C# promise, this is a promise that simply resolves without delivering a value.
  11. /// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
  12. ///
  13. /// This can also be waited on in a Unity coroutine.
  14. /// </summary>
  15. public interface IPromise
  16. {
  17. /// <summary>
  18. /// Set the name of the promise, useful for debugging.
  19. /// </summary>
  20. IPromise WithName(string name);
  21. /// <summary>
  22. /// Completes the promise.
  23. /// onResolved is called on successful completion.
  24. /// onRejected is called on error.
  25. /// </summary>
  26. void Done(Action onResolved, Action<Exception> onRejected);
  27. /// <summary>
  28. /// Completes the promise.
  29. /// onResolved is called on successful completion.
  30. /// Adds a default error handler.
  31. /// </summary>
  32. void Done(Action onResolved);
  33. /// <summary>
  34. /// Complete the promise. Adds a default error handler.
  35. /// </summary>
  36. void Done();
  37. /// <summary>
  38. /// Handle errors for the promise.
  39. /// </summary>
  40. IPromise Catch(Action<Exception> onRejected);
  41. /// <summary>
  42. /// Add a resolved callback that chains a value promise (optionally converting to a different value type).
  43. /// </summary>
  44. IPromise<ConvertedT> Then<ConvertedT>(Func<IPromise<ConvertedT>> onResolved);
  45. /// <summary>
  46. /// Add a resolved callback that chains a non-value promise.
  47. /// </summary>
  48. IPromise Then(Func<IPromise> onResolved);
  49. /// <summary>
  50. /// Add a resolved callback.
  51. /// </summary>
  52. IPromise Then(Action onResolved);
  53. /// <summary>
  54. /// Add a resolved callback and a rejected callback.
  55. /// The resolved callback chains a value promise (optionally converting to a different value type).
  56. /// </summary>
  57. IPromise<ConvertedT> Then<ConvertedT>(Func<IPromise<ConvertedT>> onResolved, Action<Exception> onRejected);
  58. /// <summary>
  59. /// Add a resolved callback and a rejected callback.
  60. /// The resolved callback chains a non-value promise.
  61. /// </summary>
  62. IPromise Then(Func<IPromise> onResolved, Action<Exception> onRejected);
  63. /// <summary>
  64. /// Add a resolved callback and a rejected callback.
  65. /// </summary>
  66. IPromise Then(Action onResolved, Action<Exception> onRejected);
  67. /// <summary>
  68. /// Chain an enumerable of promises, all of which must resolve.
  69. /// The resulting promise is resolved when all of the promises have resolved.
  70. /// It is rejected as soon as any of the promises have been rejected.
  71. /// </summary>
  72. IPromise ThenAll(Func<IEnumerable<IPromise>> chain);
  73. /// <summary>
  74. /// Chain an enumerable of promises, all of which must resolve.
  75. /// Converts to a non-value promise.
  76. /// The resulting promise is resolved when all of the promises have resolved.
  77. /// It is rejected as soon as any of the promises have been rejected.
  78. /// </summary>
  79. IPromise<IEnumerable<ConvertedT>> ThenAll<ConvertedT>(Func<IEnumerable<IPromise<ConvertedT>>> chain);
  80. /// <summary>
  81. /// Chain a sequence of operations using promises.
  82. /// Reutrn a collection of functions each of which starts an async operation and yields a promise.
  83. /// Each function will be called and each promise resolved in turn.
  84. /// The resulting promise is resolved after each promise is resolved in sequence.
  85. /// </summary>
  86. IPromise ThenSequence(Func<IEnumerable<Func<IPromise>>> chain);
  87. /// <summary>
  88. /// Takes a function that yields an enumerable of promises.
  89. /// Returns a promise that resolves when the first of the promises has resolved.
  90. /// </summary>
  91. IPromise ThenRace(Func<IEnumerable<IPromise>> chain);
  92. /// <summary>
  93. /// Takes a function that yields an enumerable of promises.
  94. /// Converts to a value promise.
  95. /// Returns a promise that resolves when the first of the promises has resolved.
  96. /// </summary>
  97. IPromise<ConvertedT> ThenRace<ConvertedT>(Func<IEnumerable<IPromise<ConvertedT>>> chain);
  98. /// <summary>
  99. /// Returns an enumerable that yields null until the promise is settled.
  100. /// ("To WaitFor" like the WaitForXXYY functions Unity provides.)
  101. /// Suitable for use with a Unity coroutine's "yield return promise.ToWaitFor()"
  102. ///
  103. /// If throwOnFail is true, the coroutine will abort on promise rejection.
  104. /// </summary>
  105. /// <returns></returns>
  106. IEnumerator ToWaitFor(bool abortOnFail = false);
  107. }
  108. /// <summary>
  109. /// Interface for a promise that can be rejected or resolved.
  110. /// </summary>
  111. public interface IPendingPromise : IRejectable
  112. {
  113. /// <summary>
  114. /// Resolve the promise with a particular value.
  115. /// </summary>
  116. void Resolve();
  117. }
  118. /// <summary>
  119. /// Used to list information of pending promises.
  120. /// </summary>
  121. public interface IPromiseInfo
  122. {
  123. /// <summary>
  124. /// Id of the promise.
  125. /// </summary>
  126. int Id { get; }
  127. /// <summary>
  128. /// Human-readable name for the promise.
  129. /// </summary>
  130. string Name { get; }
  131. }
  132. /// <summary>
  133. /// Arguments to the UnhandledError event.
  134. /// </summary>
  135. public class ExceptionEventArgs : EventArgs
  136. {
  137. internal ExceptionEventArgs(Exception exception)
  138. {
  139. // Argument.NotNull(() => exception);
  140. this.Exception = exception;
  141. }
  142. public Exception Exception
  143. {
  144. get;
  145. private set;
  146. }
  147. }
  148. /// <summary>
  149. /// Represents a handler invoked when the promise is rejected.
  150. /// </summary>
  151. public struct RejectHandler
  152. {
  153. /// <summary>
  154. /// Callback fn.
  155. /// </summary>
  156. public Action<Exception> callback;
  157. /// <summary>
  158. /// The promise that is rejected when there is an error while invoking the handler.
  159. /// </summary>
  160. public IRejectable rejectable;
  161. }
  162. /// <summary>
  163. /// Implements a non-generic C# promise, this is a promise that simply resolves without delivering a value.
  164. /// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
  165. /// </summary>
  166. public class Promise : IPromise, IPendingPromise, IPromiseInfo
  167. {
  168. static Promise()
  169. {
  170. UnhandledException += (sender, args) => {
  171. UnityEngine.Debug.LogWarning("Rejection: " + args.Exception.Message + "\n" + args.Exception.StackTrace);
  172. };
  173. }
  174. /// <summary>
  175. /// Set to true to enable tracking of promises.
  176. /// </summary>
  177. public static bool EnablePromiseTracking = false;
  178. /// <summary>
  179. /// Event raised for unhandled errors.
  180. /// For this to work you have to complete your promises with a call to Done().
  181. /// </summary>
  182. public static event EventHandler<ExceptionEventArgs> UnhandledException
  183. {
  184. add { unhandlerException += value; }
  185. remove { unhandlerException -= value; }
  186. }
  187. private static EventHandler<ExceptionEventArgs> unhandlerException;
  188. /// <summary>
  189. /// Id for the next promise that is created.
  190. /// </summary>
  191. internal static int nextPromiseId = 0;
  192. /// <summary>
  193. /// Information about pending promises.
  194. /// </summary>
  195. internal static HashSet<IPromiseInfo> pendingPromises = new HashSet<IPromiseInfo>();
  196. /// <summary>
  197. /// Information about pending promises, useful for debugging.
  198. /// This is only populated when 'EnablePromiseTracking' is set to true.
  199. /// </summary>
  200. public static IEnumerable<IPromiseInfo> GetPendingPromises()
  201. {
  202. return pendingPromises;
  203. }
  204. /// <summary>
  205. /// The exception when the promise is rejected.
  206. /// </summary>
  207. private Exception rejectionException;
  208. /// <summary>
  209. /// Error handlers.
  210. /// </summary>
  211. private List<RejectHandler> rejectHandlers;
  212. /// <summary>
  213. /// Represents a handler invoked when the promise is resolved.
  214. /// </summary>
  215. public struct ResolveHandler
  216. {
  217. /// <summary>
  218. /// Callback fn.
  219. /// </summary>
  220. public Action callback;
  221. /// <summary>
  222. /// The promise that is rejected when there is an error while invoking the handler.
  223. /// </summary>
  224. public IRejectable rejectable;
  225. }
  226. /// <summary>
  227. /// Completed handlers that accept no value.
  228. /// </summary>
  229. private List<ResolveHandler> resolveHandlers;
  230. /// <summary>
  231. /// ID of the promise, useful for debugging.
  232. /// </summary>
  233. public int Id { get; private set; }
  234. /// <summary>
  235. /// Name of the promise, when set, useful for debugging.
  236. /// </summary>
  237. public string Name { get; private set; }
  238. /// <summary>
  239. /// Tracks the current state of the promise.
  240. /// </summary>
  241. public PromiseState CurState { get; private set; }
  242. public Promise()
  243. {
  244. this.CurState = PromiseState.Pending;
  245. if (EnablePromiseTracking)
  246. {
  247. pendingPromises.Add(this);
  248. }
  249. }
  250. public Promise(Action<Action, Action<Exception>> resolver)
  251. {
  252. this.CurState = PromiseState.Pending;
  253. if (EnablePromiseTracking)
  254. {
  255. pendingPromises.Add(this);
  256. }
  257. try
  258. {
  259. resolver(
  260. // Resolve
  261. () => Resolve(),
  262. // Reject
  263. ex => Reject(ex)
  264. );
  265. }
  266. catch (Exception ex)
  267. {
  268. Reject(ex);
  269. }
  270. }
  271. /// <summary>
  272. /// Add a rejection handler for this promise.
  273. /// </summary>
  274. private void AddRejectHandler(Action<Exception> onRejected, IRejectable rejectable)
  275. {
  276. if (rejectHandlers == null)
  277. {
  278. rejectHandlers = new List<RejectHandler>();
  279. }
  280. rejectHandlers.Add(new RejectHandler()
  281. {
  282. callback = onRejected,
  283. rejectable = rejectable
  284. });
  285. }
  286. /// <summary>
  287. /// Add a resolve handler for this promise.
  288. /// </summary>
  289. private void AddResolveHandler(Action onResolved, IRejectable rejectable)
  290. {
  291. if (resolveHandlers == null)
  292. {
  293. resolveHandlers = new List<ResolveHandler>();
  294. }
  295. resolveHandlers.Add(new ResolveHandler()
  296. {
  297. callback = onResolved,
  298. rejectable = rejectable
  299. });
  300. }
  301. /// <summary>
  302. /// Invoke a single error handler.
  303. /// </summary>
  304. private void InvokeRejectHandler(Action<Exception> callback, IRejectable rejectable, Exception value)
  305. {
  306. // Argument.NotNull(() => callback);
  307. // Argument.NotNull(() => rejectable);
  308. try
  309. {
  310. callback(value);
  311. }
  312. catch (Exception ex)
  313. {
  314. rejectable.Reject(ex);
  315. }
  316. }
  317. /// <summary>
  318. /// Invoke a single resolve handler.
  319. /// </summary>
  320. private void InvokeResolveHandler(Action callback, IRejectable rejectable)
  321. {
  322. // Argument.NotNull(() => callback);
  323. // Argument.NotNull(() => rejectable);
  324. try
  325. {
  326. callback();
  327. }
  328. catch (Exception ex)
  329. {
  330. rejectable.Reject(ex);
  331. }
  332. }
  333. /// <summary>
  334. /// Helper function clear out all handlers after resolution or rejection.
  335. /// </summary>
  336. private void ClearHandlers()
  337. {
  338. rejectHandlers = null;
  339. resolveHandlers = null;
  340. }
  341. /// <summary>
  342. /// Invoke all reject handlers.
  343. /// </summary>
  344. private void InvokeRejectHandlers(Exception ex)
  345. {
  346. // Argument.NotNull(() => ex);
  347. if (rejectHandlers != null)
  348. {
  349. rejectHandlers.Each(handler => InvokeRejectHandler(handler.callback, handler.rejectable, ex));
  350. }
  351. ClearHandlers();
  352. }
  353. /// <summary>
  354. /// Invoke all resolve handlers.
  355. /// </summary>
  356. private void InvokeResolveHandlers()
  357. {
  358. if (resolveHandlers != null)
  359. {
  360. resolveHandlers.Each(handler => InvokeResolveHandler(handler.callback, handler.rejectable));
  361. }
  362. ClearHandlers();
  363. }
  364. /// <summary>
  365. /// Reject the promise with an exception.
  366. /// </summary>
  367. public void Reject(Exception ex)
  368. {
  369. // Argument.NotNull(() => ex);
  370. if (CurState != PromiseState.Pending)
  371. {
  372. throw new ApplicationException("Attempt to reject a promise that is already in state: " + CurState + ", a promise can only be rejected when it is still in state: " + PromiseState.Pending);
  373. }
  374. rejectionException = ex;
  375. CurState = PromiseState.Rejected;
  376. if (EnablePromiseTracking)
  377. {
  378. pendingPromises.Remove(this);
  379. }
  380. InvokeRejectHandlers(ex);
  381. }
  382. /// <summary>
  383. /// Resolve the promise with a particular value.
  384. /// </summary>
  385. public void Resolve()
  386. {
  387. if (CurState != PromiseState.Pending)
  388. {
  389. throw new ApplicationException("Attempt to resolve a promise that is already in state: " + CurState + ", a promise can only be resolved when it is still in state: " + PromiseState.Pending);
  390. }
  391. CurState = PromiseState.Resolved;
  392. if (EnablePromiseTracking)
  393. {
  394. pendingPromises.Remove(this);
  395. }
  396. InvokeResolveHandlers();
  397. }
  398. /// <summary>
  399. /// Completes the promise.
  400. /// onResolved is called on successful completion.
  401. /// onRejected is called on error.
  402. /// </summary>
  403. public void Done(Action onResolved, Action<Exception> onRejected)
  404. {
  405. Then(onResolved, onRejected)
  406. .Catch(ex =>
  407. Promise.PropagateUnhandledException(this, ex)
  408. );
  409. }
  410. /// <summary>
  411. /// Completes the promise.
  412. /// onResolved is called on successful completion.
  413. /// Adds a default error handler.
  414. /// </summary>
  415. public void Done(Action onResolved)
  416. {
  417. Then(onResolved)
  418. .Catch(ex =>
  419. Promise.PropagateUnhandledException(this, ex)
  420. );
  421. }
  422. /// <summary>
  423. /// Complete the promise. Adds a defualt error handler.
  424. /// </summary>
  425. public void Done()
  426. {
  427. Catch(ex =>
  428. Promise.PropagateUnhandledException(this, ex)
  429. );
  430. }
  431. /// <summary>
  432. /// Set the name of the promise, useful for debugging.
  433. /// </summary>
  434. public IPromise WithName(string name)
  435. {
  436. this.Name = name;
  437. return this;
  438. }
  439. /// <summary>
  440. /// Handle errors for the promise.
  441. /// </summary>
  442. public IPromise Catch(Action<Exception> onRejected)
  443. {
  444. // Argument.NotNull(() => onRejected);
  445. var resultPromise = new Promise();
  446. resultPromise.WithName(Name);
  447. Action resolveHandler = () =>
  448. {
  449. resultPromise.Resolve();
  450. };
  451. Action<Exception> rejectHandler = ex =>
  452. {
  453. onRejected(ex);
  454. resultPromise.Reject(ex);
  455. };
  456. ActionHandlers(resultPromise, resolveHandler, rejectHandler);
  457. return resultPromise;
  458. }
  459. /// <summary>
  460. /// Add a resolved callback that chains a value promise (optionally converting to a different value type).
  461. /// </summary>
  462. public IPromise<ConvertedT> Then<ConvertedT>(Func<IPromise<ConvertedT>> onResolved)
  463. {
  464. return Then(onResolved, null);
  465. }
  466. /// <summary>
  467. /// Add a resolved callback that chains a non-value promise.
  468. /// </summary>
  469. public IPromise Then(Func<IPromise> onResolved)
  470. {
  471. return Then(onResolved, null);
  472. }
  473. /// <summary>
  474. /// Add a resolved callback.
  475. /// </summary>
  476. public IPromise Then(Action onResolved)
  477. {
  478. return Then(onResolved, null);
  479. }
  480. /// <summary>
  481. /// Add a resolved callback and a rejected callback.
  482. /// The resolved callback chains a value promise (optionally converting to a different value type).
  483. /// </summary>
  484. public IPromise<ConvertedT> Then<ConvertedT>(Func<IPromise<ConvertedT>> onResolved, Action<Exception> onRejected)
  485. {
  486. // This version of the function must supply an onResolved.
  487. // Otherwise there is now way to get the converted value to pass to the resulting promise.
  488. // Argument.NotNull(() => onResolved);
  489. var resultPromise = new Promise<ConvertedT>();
  490. resultPromise.WithName(Name);
  491. Action resolveHandler = () =>
  492. {
  493. onResolved()
  494. .Then(
  495. // Should not be necessary to specify the arg type on the next line, but Unity (mono) has an internal compiler error otherwise.
  496. (ConvertedT chainedValue) => resultPromise.Resolve(chainedValue),
  497. ex => resultPromise.Reject(ex)
  498. );
  499. };
  500. Action<Exception> rejectHandler = ex =>
  501. {
  502. if (onRejected != null)
  503. {
  504. onRejected(ex);
  505. }
  506. resultPromise.Reject(ex);
  507. };
  508. ActionHandlers(resultPromise, resolveHandler, rejectHandler);
  509. return resultPromise;
  510. }
  511. /// <summary>
  512. /// Add a resolved callback and a rejected callback.
  513. /// The resolved callback chains a non-value promise.
  514. /// </summary>
  515. public IPromise Then(Func<IPromise> onResolved, Action<Exception> onRejected)
  516. {
  517. var resultPromise = new Promise();
  518. resultPromise.WithName(Name);
  519. Action resolveHandler = () =>
  520. {
  521. if (onResolved != null)
  522. {
  523. onResolved()
  524. .Then(
  525. () => resultPromise.Resolve(),
  526. ex => resultPromise.Reject(ex)
  527. );
  528. }
  529. else
  530. {
  531. resultPromise.Resolve();
  532. }
  533. };
  534. Action<Exception> rejectHandler = ex =>
  535. {
  536. if (onRejected != null)
  537. {
  538. onRejected(ex);
  539. }
  540. resultPromise.Reject(ex);
  541. };
  542. ActionHandlers(resultPromise, resolveHandler, rejectHandler);
  543. return resultPromise;
  544. }
  545. /// <summary>
  546. /// Add a resolved callback and a rejected callback.
  547. /// </summary>
  548. public IPromise Then(Action onResolved, Action<Exception> onRejected)
  549. {
  550. var resultPromise = new Promise();
  551. resultPromise.WithName(Name);
  552. Action resolveHandler = () =>
  553. {
  554. if (onResolved != null)
  555. {
  556. onResolved();
  557. }
  558. resultPromise.Resolve();
  559. };
  560. Action<Exception> rejectHandler = ex =>
  561. {
  562. if (onRejected != null)
  563. {
  564. onRejected(ex);
  565. }
  566. resultPromise.Reject(ex);
  567. };
  568. ActionHandlers(resultPromise, resolveHandler, rejectHandler);
  569. return resultPromise;
  570. }
  571. /// <summary>
  572. /// Helper function to invoke or register resolve/reject handlers.
  573. /// </summary>
  574. private void ActionHandlers(IRejectable resultPromise, Action resolveHandler, Action<Exception> rejectHandler)
  575. {
  576. if (CurState == PromiseState.Resolved)
  577. {
  578. InvokeResolveHandler(resolveHandler, resultPromise);
  579. }
  580. else if (CurState == PromiseState.Rejected)
  581. {
  582. InvokeRejectHandler(rejectHandler, resultPromise, rejectionException);
  583. }
  584. else
  585. {
  586. AddResolveHandler(resolveHandler, resultPromise);
  587. AddRejectHandler(rejectHandler, resultPromise);
  588. }
  589. }
  590. /// <summary>
  591. /// Chain an enumerable of promises, all of which must resolve.
  592. /// The resulting promise is resolved when all of the promises have resolved.
  593. /// It is rejected as soon as any of the promises have been rejected.
  594. /// </summary>
  595. public IPromise ThenAll(Func<IEnumerable<IPromise>> chain)
  596. {
  597. return Then(() => Promise.All(chain()));
  598. }
  599. /// <summary>
  600. /// Chain an enumerable of promises, all of which must resolve.
  601. /// Converts to a non-value promise.
  602. /// The resulting promise is resolved when all of the promises have resolved.
  603. /// It is rejected as soon as any of the promises have been rejected.
  604. /// </summary>
  605. public IPromise<IEnumerable<ConvertedT>> ThenAll<ConvertedT>(Func<IEnumerable<IPromise<ConvertedT>>> chain)
  606. {
  607. return Then(() => Promise<ConvertedT>.All(chain()));
  608. }
  609. /// <summary>
  610. /// Returns a promise that resolves when all of the promises in the enumerable argument have resolved.
  611. /// Returns a promise of a collection of the resolved results.
  612. /// </summary>
  613. public static IPromise All(params IPromise[] promises)
  614. {
  615. return All((IEnumerable<IPromise>)promises); // Cast is required to force use of the other All function.
  616. }
  617. /// <summary>
  618. /// Returns a promise that resolves when all of the promises in the enumerable argument have resolved.
  619. /// Returns a promise of a collection of the resolved results.
  620. /// </summary>
  621. public static IPromise All(IEnumerable<IPromise> promises)
  622. {
  623. var promisesArray = promises.ToArray();
  624. if (promisesArray.Length == 0)
  625. {
  626. return Promise.Resolved();
  627. }
  628. var remainingCount = promisesArray.Length;
  629. var resultPromise = new Promise();
  630. resultPromise.WithName("All");
  631. promisesArray.Each((promise, index) =>
  632. {
  633. promise
  634. .Catch(ex =>
  635. {
  636. if (resultPromise.CurState == PromiseState.Pending)
  637. {
  638. // If a promise errorred and the result promise is still pending, reject it.
  639. resultPromise.Reject(ex);
  640. }
  641. })
  642. .Then(() =>
  643. {
  644. --remainingCount;
  645. if (remainingCount <= 0)
  646. {
  647. // This will never happen if any of the promises errorred.
  648. resultPromise.Resolve();
  649. }
  650. })
  651. .Done();
  652. });
  653. return resultPromise;
  654. }
  655. /// <summary>
  656. /// Chain a sequence of operations using promises.
  657. /// Reutrn a collection of functions each of which starts an async operation and yields a promise.
  658. /// Each function will be called and each promise resolved in turn.
  659. /// The resulting promise is resolved after each promise is resolved in sequence.
  660. /// </summary>
  661. public IPromise ThenSequence(Func<IEnumerable<Func<IPromise>>> chain)
  662. {
  663. return Then(() => Sequence(chain()));
  664. }
  665. /// <summary>
  666. /// Chain a number of operations using promises.
  667. /// Takes a number of functions each of which starts an async operation and yields a promise.
  668. /// </summary>
  669. public static IPromise Sequence(params Func<IPromise>[] fns)
  670. {
  671. return Sequence((IEnumerable<Func<IPromise>>)fns);
  672. }
  673. /// <summary>
  674. /// Chain a sequence of operations using promises.
  675. /// Takes a collection of functions each of which starts an async operation and yields a promise.
  676. /// </summary>
  677. public static IPromise Sequence(IEnumerable<Func<IPromise>> fns)
  678. {
  679. return fns.Aggregate(
  680. Promise.Resolved(),
  681. (prevPromise, fn) =>
  682. {
  683. return prevPromise.Then(() => fn());
  684. }
  685. );
  686. }
  687. /// <summary>
  688. /// Takes a function that yields an enumerable of promises.
  689. /// Returns a promise that resolves when the first of the promises has resolved.
  690. /// </summary>
  691. public IPromise ThenRace(Func<IEnumerable<IPromise>> chain)
  692. {
  693. return Then(() => Promise.Race(chain()));
  694. }
  695. /// <summary>
  696. /// Takes a function that yields an enumerable of promises.
  697. /// Converts to a value promise.
  698. /// Returns a promise that resolves when the first of the promises has resolved.
  699. /// </summary>
  700. public IPromise<ConvertedT> ThenRace<ConvertedT>(Func<IEnumerable<IPromise<ConvertedT>>> chain)
  701. {
  702. return Then(() => Promise<ConvertedT>.Race(chain()));
  703. }
  704. /// <summary>
  705. /// Returns a promise that resolves when the first of the promises in the enumerable argument have resolved.
  706. /// Returns the value from the first promise that has resolved.
  707. /// </summary>
  708. public static IPromise Race(params IPromise[] promises)
  709. {
  710. return Race((IEnumerable<IPromise>)promises); // Cast is required to force use of the other function.
  711. }
  712. /// <summary>
  713. /// Returns a promise that resolves when the first of the promises in the enumerable argument have resolved.
  714. /// Returns the value from the first promise that has resolved.
  715. /// </summary>
  716. public static IPromise Race(IEnumerable<IPromise> promises)
  717. {
  718. var promisesArray = promises.ToArray();
  719. if (promisesArray.Length == 0)
  720. {
  721. throw new ApplicationException("At least 1 input promise must be provided for Race");
  722. }
  723. var resultPromise = new Promise();
  724. resultPromise.WithName("Race");
  725. promisesArray.Each((promise, index) =>
  726. {
  727. promise
  728. .Catch(ex =>
  729. {
  730. if (resultPromise.CurState == PromiseState.Pending)
  731. {
  732. // If a promise errorred and the result promise is still pending, reject it.
  733. resultPromise.Reject(ex);
  734. }
  735. })
  736. .Then(() =>
  737. {
  738. if (resultPromise.CurState == PromiseState.Pending)
  739. {
  740. resultPromise.Resolve();
  741. }
  742. })
  743. .Done();
  744. });
  745. return resultPromise;
  746. }
  747. /// <summary>
  748. /// Convert a simple value directly into a resolved promise.
  749. /// </summary>
  750. public static IPromise Resolved()
  751. {
  752. var promise = new Promise();
  753. promise.Resolve();
  754. return promise;
  755. }
  756. /// <summary>
  757. /// Convert an exception directly into a rejected promise.
  758. /// </summary>
  759. public static IPromise Rejected(Exception ex)
  760. {
  761. // Argument.NotNull(() => ex);
  762. var promise = new Promise();
  763. promise.Reject(ex);
  764. return promise;
  765. }
  766. /// <summary>
  767. /// Raises the UnhandledException event.
  768. /// </summary>
  769. internal static void PropagateUnhandledException(object sender, Exception ex)
  770. {
  771. if (unhandlerException != null)
  772. {
  773. unhandlerException(sender, new ExceptionEventArgs(ex));
  774. }
  775. }
  776. class Enumerated : IEnumerator {
  777. private Promise promise;
  778. private bool abortOnFail;
  779. public Enumerated(Promise promise, bool abortOnFail) {
  780. this.promise = promise;
  781. this.abortOnFail = abortOnFail;
  782. }
  783. public bool MoveNext() {
  784. if (abortOnFail && promise.CurState == PromiseState.Rejected) {
  785. throw promise.rejectionException;
  786. }
  787. return promise.CurState == PromiseState.Pending;
  788. }
  789. public void Reset() { }
  790. public object Current { get { return null; } }
  791. }
  792. public IEnumerator ToWaitFor(bool abortOnFail = false) {
  793. var ret = new Enumerated(this, abortOnFail);
  794. //someone will poll for completion, so act like we've been terminated
  795. Done(() => {}, ex => {});
  796. return ret;
  797. }
  798. }
  799. }