Skip to content

Commit c09d144

Browse files
committed
match already existing api
1 parent b7b2b6a commit c09d144

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/python.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,19 @@ export class Callback {
214214
this.unsafe.close();
215215
}
216216

217-
// Static method for creating auto-managed callbacks
218-
static createAutoManaged(callback: PythonJSCallback): Callback {
219-
const cb = new Callback(callback);
220-
// Register the UnsafeCallback for automatic cleanup when the Callback is GC'd
221-
callbackRegistry.register(cb, cb.unsafe);
222-
return cb;
217+
/**
218+
* Marks this Callback instance as "owned" by registering it with the
219+
* FinalizationRegistry for automatic cleanup when the Callback is garbage collected.
220+
*
221+
* This allows the underlying Deno.UnsafeCallback to be closed automatically
222+
* when the Callback object is no longer referenced, helping to prevent resource leaks.
223+
* This is useful if you don't want to call destroy manually.
224+
*
225+
* @returns {Callback} The current Callback instance.
226+
*/
227+
get owned(): Callback {
228+
callbackRegistry.register(this, this.unsafe);
229+
return this;
223230
}
224231
}
225232

@@ -623,7 +630,7 @@ export class PyObject {
623630
}
624631

625632
if (typeof v === "function") {
626-
return PyObject.from(Callback.createAutoManaged(v));
633+
return PyObject.from(new Callback(v).owned);
627634
}
628635
}
629636

0 commit comments

Comments
 (0)