{"version":3,"file":"index.js","sources":["../src/dispose.js","../src/disposeNone.js","../src/disposeOnce.js","../src/disposeWith.js","../src/disposeAll.js","../src/tryDispose.js","../src/index.js"],"sourcesContent":["/** @license MIT License (c) copyright 2010-2017 original author or authors */\n\nexport const dispose = disposable =>\n disposable.dispose()\n","/** @license MIT License (c) copyright 2010-2017 original author or authors */\n\nexport const disposeNone = () => NONE\nconst NONE = new (class DisposeNone {\n dispose () {}\n})()\n\nexport const isDisposeNone = d =>\n d === NONE\n","/** @license MIT License (c) copyright 2010-2017 original author or authors */\n\n// Wrap an existing disposable (which may not already have been once()d)\n// so that it will only dispose its underlying resource at most once.\nexport const disposeOnce = disposable =>\n new DisposeOnce(disposable)\n\nclass DisposeOnce {\n constructor (disposable) {\n this.disposed = false\n this.disposable = disposable\n }\n\n dispose () {\n if (!this.disposed) {\n this.disposed = true\n this.disposable.dispose()\n this.disposable = undefined\n }\n }\n}\n","/** @license MIT License (c) copyright 2010-2017 original author or authors */\nimport { disposeOnce } from './disposeOnce'\nimport { curry2 } from '@most/prelude'\n\n// Create a Disposable that will use the provided\n// dispose function to dispose the resource\nexport const disposeWith = curry2((dispose, resource) =>\n disposeOnce(new DisposeWith(dispose, resource)))\n\n// Disposable represents a resource that must be\n// disposed/released. It aggregates a function to dispose\n// the resource and a handle to a key/id/handle/reference\n// that identifies the resource\nclass DisposeWith {\n constructor (dispose, resource) {\n this._dispose = dispose\n this._resource = resource\n }\n\n dispose () {\n this._dispose(this._resource)\n }\n}\n","/** @license MIT License (c) copyright 2010 original author or authors */\nimport { append, reduce, curry2 } from '@most/prelude'\nimport { disposeNone, isDisposeNone } from './disposeNone'\n\n// Aggregate a list of disposables into a DisposeAll\nexport const disposeAll = ds => {\n const merged = reduce(merge, [], ds)\n return merged.length === 0 ? disposeNone() : new DisposeAll(merged)\n}\n\n// Convenience to aggregate 2 disposables\nexport const disposeBoth = curry2((d1, d2) =>\n disposeAll([d1, d2]))\n\nconst merge = (ds, d) =>\n isDisposeNone(d) ? ds\n : d instanceof DisposeAll ? ds.concat(d.disposables)\n : append(d, ds)\n\nclass DisposeAll {\n constructor (disposables) {\n this.disposables = disposables\n }\n\n dispose () {\n throwIfErrors(disposeCollectErrors(this.disposables))\n }\n}\n\n// Dispose all, safely collecting errors into an array\nconst disposeCollectErrors = disposables =>\n reduce(appendIfError, [], disposables)\n\n// Call dispose and if throws, append thrown error to errors\nconst appendIfError = (errors, d) => {\n try {\n d.dispose()\n } catch (e) {\n errors.push(e)\n }\n return errors\n}\n\n// Throw DisposeAllError if errors is non-empty\nconst throwIfErrors = errors => {\n if (errors.length > 0) {\n throw new DisposeAllError(`${errors.length} errors`, errors)\n }\n}\n\nexport const DisposeAllError = (Error => {\n function DisposeAllError (message, errors) {\n Error.call(this, message)\n this.message = message\n this.name = DisposeAllError.name\n this.errors = errors\n\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, DisposeAllError)\n }\n\n this.stack = `${this.stack}${formatErrorStacks(this.errors)}`\n }\n\n DisposeAllError.prototype = Object.create(Error.prototype)\n\n return DisposeAllError\n})(Error)\n\nconst formatErrorStacks = errors =>\n reduce(formatErrorStack, '', errors)\n\nconst formatErrorStack = (s, e, i) =>\n s + `\\n[${(i + 1)}] ${e.stack}`\n","/** @license MIT License (c) copyright 2010-2017 original author or authors */\nimport { curry3 } from '@most/prelude'\n\n// Try to dispose the disposable. If it throws, send\n// the error to sink.error with the provided Time value\nexport const tryDispose = curry3((t, disposable, sink) => {\n try {\n disposable.dispose()\n } catch (e) {\n sink.error(t, e)\n }\n})\n","/** @license MIT License (c) copyright 2010-2017 original author or authors */\n\nexport * from './dispose'\nexport * from './disposeNone'\nexport * from './disposeWith'\nexport * from './disposeOnce'\nexport * from './disposeAll'\nexport * from './tryDispose'\n"],"names":["dispose","disposable","disposeNone","NONE","isDisposeNone","d","disposeOnce","DisposeOnce","disposed","undefined","disposeWith","curry2","resource","DisposeWith","_dispose","_resource","disposeAll","merged","reduce","merge","ds","length","DisposeAll","disposeBoth","d1","d2","concat","disposables","append","disposeCollectErrors","appendIfError","errors","e","push","throwIfErrors","DisposeAllError","message","call","name","Error","captureStackTrace","stack","formatErrorStacks","prototype","Object","create","formatErrorStack","s","i","tryDispose","curry3","t","sink","error"],"mappings":";;;;;;AAAA;;AAEA,AAAO,IAAMA,UAAU,SAAVA,OAAU;SACrBC,WAAWD,OAAX,EADqB;CAAhB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACFP;;AAEA,AAAO,IAAME,cAAc,SAAdA,WAAc;SAAMC,IAAN;CAApB;AACP,IAAMA,oBAAO;;;;;wBACXH,OADW,sBACA,EADA;;;MAAb;;AAIA,AAAO,IAAMI,gBAAgB,SAAhBA,aAAgB;SAC3BC,MAAMF,IADqB;CAAtB;;ACPP;;;;AAIA,AAAO,IAAMG,cAAc,SAAdA,WAAc;SACzB,IAAIC,WAAJ,CAAgBN,UAAhB,CADyB;CAApB;;IAGDM;uBACSN,UAAb,EAAyB;;;SAClBO,QAAL,GAAgB,KAAhB;SACKP,UAAL,GAAkBA,UAAlB;;;wBAGFD,6BAAW;QACL,CAAC,KAAKQ,QAAV,EAAoB;WACbA,QAAL,GAAgB,IAAhB;WACKP,UAAL,CAAgBD,OAAhB;WACKC,UAAL,GAAkBQ,SAAlB;;;;;;;ACjBN;AACA,AAGA;;AAEA,AAAO,IAAMC,2BAAcC,eAAO,UAACX,OAAD,EAAUY,QAAV;SAChCN,YAAY,IAAIO,WAAJ,CAAgBb,OAAhB,EAAyBY,QAAzB,CAAZ,CADgC;CAAP,CAApB;;;;;;;IAODC;uBACSb,OAAb,EAAsBY,QAAtB,EAAgC;;;SACzBE,QAAL,GAAgBd,OAAhB;SACKe,SAAL,GAAiBH,QAAjB;;;wBAGFZ,6BAAW;SACJc,QAAL,CAAc,KAAKC,SAAn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