Skip to content

Instantly share code, notes, and snippets.

@dead-claudia
Last active July 30, 2017 03:39
Show Gist options
  • Save dead-claudia/7a61be6223543c1cc9ffdb4ca7d3726f to your computer and use it in GitHub Desktop.
Save dead-claudia/7a61be6223543c1cc9ffdb4ca7d3726f to your computer and use it in GitHub Desktop.
Super simple cancellation token
/**
* A super simple cancel token utility, with support all the way to ES3. Works
* in browsers, AMD, and CommonJS environments. 874 bytes minified and gzipped.
*
* The API is pretty simple:
*
* `const source = cancelSource(parentTokens?)`
*
* - `source.cancel(reason?)`
* Trigger a cancel with some reason, and save the cancel result to
* potentially rethrow.
*
* - `source.close()`
* Close the token without canceling
*
* - `const token = source.token`
* A reference to the corresponding token for the source
*
* - `const tok = source.token.listen(callback)`
* Register a callback to be called if/when the token is canceled, with
* the cancel instance (if canceled) or `undefined` (if closed)
*
* - `source.token.unlisten(tok)`
* Remove a registered callback
*
* - `const inst = source.token.canceled()`
* Returns the instance if the token was canceled, `undefined` otherwise
*
* - `source.token.throwIfCanceled()`
* Throw the cancel result if the token was canceled
*
* - `inst.type`
* Set to `"cancel"`, so it's easy to identify them
*
* - `inst.reason`
* The reason given when it was first created
*/
/**
* @license ISC
*
* Copyright (c) 2017 and later, Isiah Meadows
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
;(function (global, cancelSource) {
"use strict"
if (typeof module === "object" && module && module.exports) {
module.exports = cancelSource
} else if (typeof exports === "object" && exports) {
exports.cancelSource = cancelSource
} else if (typeof define === "function" && define.amd) {
define("cancel-source", function () { return cancelSource })
} else {
global.cancelSource = cancelSource
}
})(
typeof window === "object" && window ||
typeof self === "object" && self ||
this,
function (parents) {
"use strict"
var callbacks = [], inst
function end(cancel) {
if (callbacks) {
var list = callbacks
for (var i = callbacks = 0; i < list.length; i++) {
(0, list[i].f)(inst = cancel)
}
}
}
if (parents) for (var i = 0; i < parents.length; i++) parents[i].listen(end)
return {
cancel: function (reason) {
end({type: "cancel", reason: reason})
},
close: function () {
end(/* undefined */)
},
token: {
listen: function (func) {
callbacks.push(func = {f: func})
return func
},
unlisten: function (func) {
for (var i = 0; i < callbacks.length; i++) {
if (callbacks[i] === func) {
callbacks.splice(i, 1)
break
}
}
},
canceled: function () {
return inst
},
throwIfCanceled: function () {
if (inst) throw inst
}
}
}
});
/**
* @license ISC
*
* Copyright (c) 2017 and later, Isiah Meadows
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
!function(e,n){"use strict";"object"==typeof module&&module&&module.exports?module.exports=n:"object"==typeof exports&&exports?exports.cancelSource=n:"function"==typeof define&&define.amd?define("cancel-source",function(){return n}):e.cancelSource=n}("object"==typeof window&&window||"object"==typeof self&&self||this,function(e){"use strict";function n(e){if(o)for(var n=o,c=o=0;c<n.length;c++)(0,n[c].f)(t=e)}var t,o=[];if(e)for(var c=0;c<e.length;c++)e[c].listen(n);return{cancel:function(e){n({type:"cancel",reason:e})},close:function(){n()},token:{listen:function(e){return o.push(e={f:e}),e},unlisten:function(e){for(var n=0;n<o.length;n++)if(o[n]===e){o.splice(n,1);break}},canceled:function(){return t},throwIfCanceled:function(){if(t)throw t}}}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment