Skip to content

Commit

Permalink
SharedPointer.js: sync with other projects
Browse files Browse the repository at this point in the history
see medooze/media-server-node#261 and the fixups in master afterwards
  • Loading branch information
mildsunrise authored Jun 19, 2024
1 parent 6972ebf commit 5e82f53
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions lib/SharedPointer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ts-nocheck

function proxy(ret,cache)
{
if (typeof ret === "object" && ret.constructor.name.match(/_exports_(.*)Shared/))
Expand Down Expand Up @@ -39,11 +41,30 @@ class Handler
};


function SharedPointer(obj, cache)
/**
* @template {{ get(): any }} T
* @typedef {T & ReturnType<T['get']>} Proxy
*
* objects returned by {@link SharedPointer} are proxies that
* implement both the operations of the shared pointer itself
* and of the pointed-to object. they can be used in place of
* both.
*/

/**
* @template {{ get(): any }} T
* @returns {Proxy<T>}
*/
function SharedPointer(
/** @type {T} */ obj,
/** @type {Map<Object, Proxy<any>>} */ cache)
{
//If what we get passed is already a proxy, return it unchanged
if (obj[SharedPointer.Target])
return obj;
//If we already have a proxy for that object
if (cache && cache.has(obj))
//Return
//Return
return cache.get(obj)
//Create new proxy
const proxy = new Proxy(obj, new Handler());
Expand All @@ -57,6 +78,17 @@ function SharedPointer(obj, cache)
SharedPointer.Target = Symbol("target");
SharedPointer.Pointer = Symbol("pointer");

/**
* @template {{ get(): any }} T
* @param {Proxy<T>} ptr
* @returns {T}
*/
SharedPointer.getPointer = function (ptr)
{
return ptr[SharedPointer.Pointer];
}



SharedPointer.wrapNativeModule = function (module)
{
Expand All @@ -68,9 +100,3 @@ SharedPointer.wrapNativeModule = function (module)
}

module.exports = SharedPointer;






0 comments on commit 5e82f53

Please sign in to comment.