Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,53 @@ const channel = new MessageChannel();
channel.port2.postMessage(signal, [signal]);
```

## `util.aborted(signal, resource)`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

* `signal` {AbortSignal}
* `resource` {Object} Any non-null entity, reference to which is held weakly.
* Returns: {Promise}

Listens to abort event on the provided `signal` and
returns a promise that resolves when the `signal` is
Comment thread
debadree25 marked this conversation as resolved.
Outdated
aborted. The function is dependent on the `resource` and
the returned promise shall remain pending if `resource`
goes out of memory. The function shall automatically
cleanup any event listeners it attaches to `signal`.
Comment thread
debadree25 marked this conversation as resolved.
Outdated

```js
Comment thread
debadree25 marked this conversation as resolved.
Outdated
const { aborted } = require('util');
Comment thread
debadree25 marked this conversation as resolved.
Outdated

const dependent = obtainSomethingAbortable();

aborted(dependent.signal, dependent).then(() => {
// Do something when dependent is aborted.
});

dependent.on('event', () => {
dependent.abort();
});
```

```mjs
import { aborted } from 'node:util';

const dependent = obtainSomethingAbortable();

aborted(dependent.signal, dependent).then(() => {
// Do something when dependent is aborted.
});

dependent.on('event', () => {
dependent.abort();
});
```

## `util.types`

<!-- YAML
Expand Down
21 changes: 21 additions & 0 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
Symbol,
SymbolToStringTag,
WeakRef,
PromiseResolve,
Comment thread
aduh95 marked this conversation as resolved.
Outdated
} = primordials;

const {
Expand All @@ -22,11 +23,13 @@ const {
kTrustEvent,
kNewListener,
kRemoveListener,
kWeakHandler,
} = require('internal/event_target');
const {
customInspectSymbol,
kEnumerableProperty,
kEmptyObject,
createDeferredPromise,
Comment thread
debadree25 marked this conversation as resolved.
Outdated
} = require('internal/util');
const { inspect } = require('internal/util/inspect');
const {
Expand All @@ -39,6 +42,8 @@ const {

const {
validateUint32,
validateAbortSignal: abortSignalValidator,
validateObject,
Comment thread
aduh95 marked this conversation as resolved.
} = require('internal/validators');

const {
Expand Down Expand Up @@ -357,6 +362,21 @@ function transferableAbortController() {
return AbortController[kMakeTransferable]();
}

/**
* @param {AbortSignal} signal
* @param {any} resource
* @returns {Promise<void>}
*/
async function aborted(signal, resource) {
abortSignalValidator(signal, 'signal');
Comment thread
aduh95 marked this conversation as resolved.
Outdated
validateObject(resource, 'resource', { nullable: false, allowFunction: true, allowArray: true });
Comment thread
debadree25 marked this conversation as resolved.
if (signal.aborted)
return PromiseResolve();
const abortPromise = createDeferredPromise();
signal.addEventListener('abort', abortPromise.resolve, { [kWeakHandler]: resource, once: true });
Comment thread
aduh95 marked this conversation as resolved.
Comment thread
aduh95 marked this conversation as resolved.
return abortPromise.promise;
}

ObjectDefineProperties(AbortController.prototype, {
signal: kEnumerableProperty,
abort: kEnumerableProperty,
Expand All @@ -377,4 +397,5 @@ module.exports = {
ClonedAbortSignal,
transferableAbortSignal,
transferableAbortController,
aborted,
Comment thread
debadree25 marked this conversation as resolved.
Outdated
};
3 changes: 3 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ module.exports = {
get transferableAbortController() {
return lazyAbortController().transferableAbortController;
},
get aborted() {
return lazyAbortController().aborted;
},
types
};

Expand Down
44 changes: 44 additions & 0 deletions test/parallel/test-aborted-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Flags: --expose-gc
'use strict';

const common = require('../common');
const { aborted } = require('util');
const assert = require('assert');
const { getEventListeners } = require('events');

{
// Test aborted works when provided a resource
const ac = new AbortController();
aborted(ac.signal, {}).then(common.mustCall());
ac.abort();
assert.strictEqual(ac.signal.aborted, true);
assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0);
}

{
// Test aborted with gc cleanup
const ac = new AbortController();
aborted(ac.signal, {}).then(common.mustNotCall());
setImmediate(() => {
global.gc();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does GC accomplish here? That the aborted() promise is collected before abort() is called?

I don't know if that kind of GC observability is a good thing. Maybe it works now but GC changes can break such patterns. Looks fragile.

Copy link
Copy Markdown
Member Author

@debadree25 debadree25 Feb 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically want to cleanup the empty object to check if the listener is removed when the resource is cleaned by the gc, unsure of any other ways to test this

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's just for testing, then it's probably fine. I mean, it could still break in the future but we can revisit if and when that happens.

I'd advise against documenting that behavior though. It's simply not very robust.

ac.abort();
assert.strictEqual(ac.signal.aborted, true);
assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0);
});
}

{
// Fails with error if not provided abort signal
const sig = new EventTarget();
assert.rejects(aborted(sig, {}), {
name: 'TypeError',
});
Comment thread
debadree25 marked this conversation as resolved.
Outdated
}

{
// Fails if not provided a resource
const ac = new AbortController();
assert.rejects(aborted(ac.signal, null), {
name: 'TypeError',
});
Comment thread
debadree25 marked this conversation as resolved.
Outdated
}
Comment thread
debadree25 marked this conversation as resolved.