Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

add-disposable-resource.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. var call = require('../internals/function-call');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var bind = require('../internals/function-bind-context');
  5. var anObject = require('../internals/an-object');
  6. var aCallable = require('../internals/a-callable');
  7. var isNullOrUndefined = require('../internals/is-null-or-undefined');
  8. var getMethod = require('../internals/get-method');
  9. var wellKnownSymbol = require('../internals/well-known-symbol');
  10. var ASYNC_DISPOSE = wellKnownSymbol('asyncDispose');
  11. var DISPOSE = wellKnownSymbol('dispose');
  12. var push = uncurryThis([].push);
  13. // `GetDisposeMethod` abstract operation
  14. // https://tc39.es/proposal-explicit-resource-management/#sec-getdisposemethod
  15. var getDisposeMethod = function (V, hint) {
  16. if (hint === 'async-dispose') {
  17. var method = getMethod(V, ASYNC_DISPOSE);
  18. if (method !== undefined) return method;
  19. method = getMethod(V, DISPOSE);
  20. if (method === undefined) return method;
  21. return function () {
  22. call(method, this);
  23. };
  24. } return getMethod(V, DISPOSE);
  25. };
  26. // `CreateDisposableResource` abstract operation
  27. // https://tc39.es/proposal-explicit-resource-management/#sec-createdisposableresource
  28. var createDisposableResource = function (V, hint, method) {
  29. if (arguments.length < 3 && !isNullOrUndefined(V)) {
  30. method = aCallable(getDisposeMethod(anObject(V), hint));
  31. }
  32. return method === undefined ? function () {
  33. return undefined;
  34. } : bind(method, V);
  35. };
  36. // `AddDisposableResource` abstract operation
  37. // https://tc39.es/proposal-explicit-resource-management/#sec-adddisposableresource
  38. module.exports = function (disposable, V, hint, method) {
  39. var resource;
  40. if (arguments.length < 4) {
  41. // When `V`` is either `null` or `undefined` and hint is `async-dispose`,
  42. // we record that the resource was evaluated to ensure we will still perform an `Await` when resources are later disposed.
  43. if (isNullOrUndefined(V) && hint === 'sync-dispose') return;
  44. resource = createDisposableResource(V, hint);
  45. } else {
  46. resource = createDisposableResource(undefined, hint, method);
  47. }
  48. push(disposable.stack, resource);
  49. };