Simple registry for creating and reusing globally shared objects
const GlobalReg = require( '@debonet/es6globalreg' );
class MyClass{}
const a = await GlobalReg.get( "foo", () => new MyClass());
const b = await GlobalReg.get( "foo", () => new MyClass());
console.log( a === b ); // true
const GlobalReg = require( '@debonet/es6globalreg' );
class MyClass{}
const a = await GlobalReg.get( "foo", () => new MyClass());
const b = await GlobalReg.get( "foo", () => new MyClass());
// release only one copy
await GlobalReg.release( a );
// new get()'s share the global version
const c = await GlobalReg.get( "foo", () => new MyClass());
console.log( c === b ); // true
// release all copies
await GlobalReg.releaseAll( b );
// new get()'s don't share any more
const d = await GlobalReg.get( "foo", () => new MyClass());
console.log( c === d ); // false;
signature string - A unique string identifier for this resource
create_function - function() - A function with no arguments that gets called if the resource does not already exist
Creates a new object and adds it to the registry if none is found.
Returns a promise to deliver the gloablly registered object.
x - object - The object to be released from the registry
cleanup_function - optioanl function() defaults to () => {}` - A function with no arguments that gets called if all outstanding instances have been released
all - optioanl boolean - A function with no arguments that gets called if all outstanding instances have been released
Reduces the count the provided object. If no more instances are outstanding, will call the provided cleanup function
Returns a promise that resolves to the object { globalreg: # }
or to the result of the cleanup_function, if called
shorthand for GlobalReg.fRelease( x, cleanup_function, true )
x - object - The object of interest
Returns the count of outstanging instances of the indicated object in the registry