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 committed Jun 19, 2024
1 parent 7ee771e commit 4557443
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions lib/SharedPointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Handler


/**
* @template {{ get(): U }} T
* @template {{ get(): any }} T
* @typedef {T & ReturnType<T['get']>} Proxy
*
* objects returned by {@link SharedPointer} are proxies that
Expand All @@ -52,16 +52,19 @@ class Handler
*/

/**
* @template {{ get(): U }} T
* @template {{ get(): any }} T
* @returns {Proxy<T>}
*/
function SharedPointer(
/** @type {T} */ obj,
/** @type {Map<Object, Proxy>} */ cache)
/** @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 @@ -75,6 +78,17 @@ function SharedPointer(
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 @@ -86,9 +100,3 @@ SharedPointer.wrapNativeModule = function (module)
}

module.exports = SharedPointer;






0 comments on commit 4557443

Please sign in to comment.