summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/internal/util.mjs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2024-06-14 23:07:02 +0100
committerGitHub <noreply@github.com>2024-06-15 00:07:02 +0200
commit78b12a43b307c3405cd63cf4612517210330b487 (patch)
treeef9a6a258f13f7d4151e8152ed9702572126c165 /ext/node/polyfills/internal/util.mjs
parent184a85eaecbe7055b5b3969391f49c4723ac44fb (diff)
fix(ext/node): better support for `node:diagnostics_channel` module (#24088)
Closes https://github.com/denoland/deno/issues/24060
Diffstat (limited to 'ext/node/polyfills/internal/util.mjs')
-rw-r--r--ext/node/polyfills/internal/util.mjs36
1 files changed, 36 insertions, 0 deletions
diff --git a/ext/node/polyfills/internal/util.mjs b/ext/node/polyfills/internal/util.mjs
index 596599859..e6b32d17d 100644
--- a/ext/node/polyfills/internal/util.mjs
+++ b/ext/node/polyfills/internal/util.mjs
@@ -15,6 +15,10 @@ import {
} from "ext:deno_node/internal/primordials.mjs";
import { ERR_UNKNOWN_SIGNAL } from "ext:deno_node/internal/errors.ts";
import { os } from "ext:deno_node/internal_binding/constants.ts";
+import { primordials } from "ext:core/mod.js";
+const {
+ SafeWeakRef,
+} = primordials;
export const customInspectSymbol = Symbol.for("nodejs.util.inspect.custom");
export const kEnumerableProperty = Object.create(null);
@@ -135,6 +139,38 @@ export function convertToValidSignal(signal) {
throw new ERR_UNKNOWN_SIGNAL(signal);
}
+export class WeakReference {
+ #weak = null;
+ #strong = null;
+ #refCount = 0;
+ constructor(object) {
+ this.#weak = new SafeWeakRef(object);
+ }
+
+ incRef() {
+ this.#refCount++;
+ if (this.#refCount === 1) {
+ const derefed = this.#weak.deref();
+ if (derefed !== undefined) {
+ this.#strong = derefed;
+ }
+ }
+ return this.#refCount;
+ }
+
+ decRef() {
+ this.#refCount--;
+ if (this.#refCount === 0) {
+ this.#strong = null;
+ }
+ return this.#refCount;
+ }
+
+ get() {
+ return this.#weak.deref();
+ }
+}
+
promisify.custom = kCustomPromisifiedSymbol;
export default {