index.es.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import { append, curry2, curry3, reduce } from '@most/prelude';
  2. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  3. var dispose = function dispose(disposable) {
  4. return disposable.dispose();
  5. };
  6. var asyncGenerator = function () {
  7. function AwaitValue(value) {
  8. this.value = value;
  9. }
  10. function AsyncGenerator(gen) {
  11. var front, back;
  12. function send(key, arg) {
  13. return new Promise(function (resolve, reject) {
  14. var request = {
  15. key: key,
  16. arg: arg,
  17. resolve: resolve,
  18. reject: reject,
  19. next: null
  20. };
  21. if (back) {
  22. back = back.next = request;
  23. } else {
  24. front = back = request;
  25. resume(key, arg);
  26. }
  27. });
  28. }
  29. function resume(key, arg) {
  30. try {
  31. var result = gen[key](arg);
  32. var value = result.value;
  33. if (value instanceof AwaitValue) {
  34. Promise.resolve(value.value).then(function (arg) {
  35. resume("next", arg);
  36. }, function (arg) {
  37. resume("throw", arg);
  38. });
  39. } else {
  40. settle(result.done ? "return" : "normal", result.value);
  41. }
  42. } catch (err) {
  43. settle("throw", err);
  44. }
  45. }
  46. function settle(type, value) {
  47. switch (type) {
  48. case "return":
  49. front.resolve({
  50. value: value,
  51. done: true
  52. });
  53. break;
  54. case "throw":
  55. front.reject(value);
  56. break;
  57. default:
  58. front.resolve({
  59. value: value,
  60. done: false
  61. });
  62. break;
  63. }
  64. front = front.next;
  65. if (front) {
  66. resume(front.key, front.arg);
  67. } else {
  68. back = null;
  69. }
  70. }
  71. this._invoke = send;
  72. if (typeof gen.return !== "function") {
  73. this.return = undefined;
  74. }
  75. }
  76. if (typeof Symbol === "function" && Symbol.asyncIterator) {
  77. AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
  78. return this;
  79. };
  80. }
  81. AsyncGenerator.prototype.next = function (arg) {
  82. return this._invoke("next", arg);
  83. };
  84. AsyncGenerator.prototype.throw = function (arg) {
  85. return this._invoke("throw", arg);
  86. };
  87. AsyncGenerator.prototype.return = function (arg) {
  88. return this._invoke("return", arg);
  89. };
  90. return {
  91. wrap: function (fn) {
  92. return function () {
  93. return new AsyncGenerator(fn.apply(this, arguments));
  94. };
  95. },
  96. await: function (value) {
  97. return new AwaitValue(value);
  98. }
  99. };
  100. }();
  101. var classCallCheck = function (instance, Constructor) {
  102. if (!(instance instanceof Constructor)) {
  103. throw new TypeError("Cannot call a class as a function");
  104. }
  105. };
  106. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  107. var disposeNone = function disposeNone() {
  108. return NONE;
  109. };
  110. var NONE = /*#__PURE__*/new (function () {
  111. function DisposeNone() {
  112. classCallCheck(this, DisposeNone);
  113. }
  114. DisposeNone.prototype.dispose = function dispose() {};
  115. return DisposeNone;
  116. }())();
  117. var isDisposeNone = function isDisposeNone(d) {
  118. return d === NONE;
  119. };
  120. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  121. // Wrap an existing disposable (which may not already have been once()d)
  122. // so that it will only dispose its underlying resource at most once.
  123. var disposeOnce = function disposeOnce(disposable) {
  124. return new DisposeOnce(disposable);
  125. };
  126. var DisposeOnce = /*#__PURE__*/function () {
  127. function DisposeOnce(disposable) {
  128. classCallCheck(this, DisposeOnce);
  129. this.disposed = false;
  130. this.disposable = disposable;
  131. }
  132. DisposeOnce.prototype.dispose = function dispose() {
  133. if (!this.disposed) {
  134. this.disposed = true;
  135. this.disposable.dispose();
  136. this.disposable = undefined;
  137. }
  138. };
  139. return DisposeOnce;
  140. }();
  141. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  142. // Create a Disposable that will use the provided
  143. // dispose function to dispose the resource
  144. var disposeWith = /*#__PURE__*/curry2(function (dispose, resource) {
  145. return disposeOnce(new DisposeWith(dispose, resource));
  146. });
  147. // Disposable represents a resource that must be
  148. // disposed/released. It aggregates a function to dispose
  149. // the resource and a handle to a key/id/handle/reference
  150. // that identifies the resource
  151. var DisposeWith = /*#__PURE__*/function () {
  152. function DisposeWith(dispose, resource) {
  153. classCallCheck(this, DisposeWith);
  154. this._dispose = dispose;
  155. this._resource = resource;
  156. }
  157. DisposeWith.prototype.dispose = function dispose() {
  158. this._dispose(this._resource);
  159. };
  160. return DisposeWith;
  161. }();
  162. /** @license MIT License (c) copyright 2010 original author or authors */
  163. // Aggregate a list of disposables into a DisposeAll
  164. var disposeAll = function disposeAll(ds) {
  165. var merged = reduce(merge, [], ds);
  166. return merged.length === 0 ? disposeNone() : new DisposeAll(merged);
  167. };
  168. // Convenience to aggregate 2 disposables
  169. var disposeBoth = /*#__PURE__*/curry2(function (d1, d2) {
  170. return disposeAll([d1, d2]);
  171. });
  172. var merge = function merge(ds, d) {
  173. return isDisposeNone(d) ? ds : d instanceof DisposeAll ? ds.concat(d.disposables) : append(d, ds);
  174. };
  175. var DisposeAll = /*#__PURE__*/function () {
  176. function DisposeAll(disposables) {
  177. classCallCheck(this, DisposeAll);
  178. this.disposables = disposables;
  179. }
  180. DisposeAll.prototype.dispose = function dispose() {
  181. throwIfErrors(disposeCollectErrors(this.disposables));
  182. };
  183. return DisposeAll;
  184. }();
  185. // Dispose all, safely collecting errors into an array
  186. var disposeCollectErrors = function disposeCollectErrors(disposables) {
  187. return reduce(appendIfError, [], disposables);
  188. };
  189. // Call dispose and if throws, append thrown error to errors
  190. var appendIfError = function appendIfError(errors, d) {
  191. try {
  192. d.dispose();
  193. } catch (e) {
  194. errors.push(e);
  195. }
  196. return errors;
  197. };
  198. // Throw DisposeAllError if errors is non-empty
  199. var throwIfErrors = function throwIfErrors(errors) {
  200. if (errors.length > 0) {
  201. throw new DisposeAllError(errors.length + ' errors', errors);
  202. }
  203. };
  204. var DisposeAllError = /*#__PURE__*/function (Error) {
  205. function DisposeAllError(message, errors) {
  206. Error.call(this, message);
  207. this.message = message;
  208. this.name = DisposeAllError.name;
  209. this.errors = errors;
  210. if (Error.captureStackTrace) {
  211. Error.captureStackTrace(this, DisposeAllError);
  212. }
  213. this.stack = '' + this.stack + formatErrorStacks(this.errors);
  214. }
  215. DisposeAllError.prototype = /*#__PURE__*/Object.create(Error.prototype);
  216. return DisposeAllError;
  217. }(Error);
  218. var formatErrorStacks = function formatErrorStacks(errors) {
  219. return reduce(formatErrorStack, '', errors);
  220. };
  221. var formatErrorStack = function formatErrorStack(s, e, i) {
  222. return s + ('\n[' + (i + 1) + '] ' + e.stack);
  223. };
  224. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  225. // Try to dispose the disposable. If it throws, send
  226. // the error to sink.error with the provided Time value
  227. var tryDispose = /*#__PURE__*/curry3(function (t, disposable, sink) {
  228. try {
  229. disposable.dispose();
  230. } catch (e) {
  231. sink.error(t, e);
  232. }
  233. });
  234. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  235. export { dispose, disposeNone, isDisposeNone, disposeWith, disposeOnce, disposeAll, disposeBoth, DisposeAllError, tryDispose };
  236. //# sourceMappingURL=index.es.js.map