index.js 7.9 KB

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