You are reading a draft of the next documentation and it's in continuos update so if you don't find what you need please refer to: C++ wrapper classes for the ABI-stable C APIs for Node.js
Holds a counted reference to a Value object; initially a weak reference unless otherwise specified, may be changed to/from a strong reference by adjusting the refcount.
The referenced Value is not immediately destroyed when the reference count is zero; it is merely then eligible for garbage-collection if there are no other references to the Value.
Reference objects allocated in static space, such as a global static instance, must call the SuppressDestruct
method to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid.
The following classes inherit, either directly or indirectly, from Reference:
static Reference<T> New(const T& value, uint32_t initialRefcount = 0);
-
[in] value
: The value which is to be referenced. -
[in] initialRefcount
: The initial reference count.
Reference();
Creates a new empty Reference instance.
Reference(napi_env env, napi_value value);
-
[in] env
: Thenapi_env
environment in which to construct the Reference object. -
[in] value
: The N-API primitive value to be held by the Reference.
Napi::Env Env() const;
Returns the Env
value in which the Reference was instantiated.
bool IsEmpty() const;
Determines whether the value held by the Reference is empty.
T Value() const;
Returns the value held by the Reference.
uint32_t Ref();
Increments the reference count for the Reference and returns the resulting reference count. Throws an error if the increment fails.
uint32_t Unref();
Decrements the reference count for the Reference and returns the resulting reference count. Throws an error if the decrement fails.
void Reset();
Sets the value held by the Reference to be empty.
void Reset(const T& value, uint32_t refcount = 0);
-
[in] value
: The value which is to be referenced. -
[in] initialRefcount
: The initial reference count.
Sets the value held by the Reference.
void SuppressDestruct();
Call this method on a Reference that is declared as static data to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid.