-
Notifications
You must be signed in to change notification settings - Fork 465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Promise and Reference docs #243
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,70 @@ | ||
# Promise | ||
|
||
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](https://nodejs.github.io/node-addon-api/) | ||
[C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) | ||
|
||
# Promise | ||
|
||
The Promise class, along with its Promise::Deferred class, implement the ability to create, resolve, and reject Promise objects. | ||
|
||
The basic approach is to create a Promise::Deferred object and return to your caller the value returned by the Promise::Deferred::Promise method. For example: | ||
|
||
```cpp | ||
Value YourFunction(const CallbackInfo& info) { | ||
// your code goes here... | ||
Promise::Deferred deferred = Promise::Deferred::New(info.Env()); | ||
// deferred needs to survive this call... | ||
return deferred.Promise(); | ||
} | ||
``` | ||
|
||
Later, when the asynchronous process completes, call either the `Resolve` or `Reject` method on the Promise::Deferred object created earlier: | ||
|
||
```cpp | ||
deferred.Resolve(String::New(info.Env(), "OK")); | ||
``` | ||
|
||
## Promise::Deferred Methods | ||
|
||
### Factory Method | ||
|
||
```cpp | ||
static Promise::Deferred Promise::Deferred::New(napi_env env); | ||
``` | ||
|
||
* `[in] env`: The `napi_env` environment in which to create the Deferred object. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: looks like other docs use |
||
|
||
### Constructor | ||
|
||
```cpp | ||
Promise::Deferred(napi_env env); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gabrielschulhof Do you remember why we expose both a factory and a constructor? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you mean the fact that we have both a deferred and a promise, then the reason for that is that with V8 N-API cannot resolve a promise coming in from JavaScript, because it doesn't have the corresponding In reality we could have gone with a single object, as @addaleax points out, but I chose not to because the coincidence between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I should have been more clear. It seems like these objects have a lot of different mechanisms for construction. Buffer (which I'm documenting) has four factory methods and two constructors. In the process of writing the documentation the factory methods seem clear, but I haven't quite understood the purpose of the public constructors. They seem like the end up putting the object into a weird (and potentially undesirable state). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps it's so compilation units written in C can interact with those written in C++? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gabrielschulhof I don't think it was anything like that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
``` | ||
|
||
* `[in] env`: The `napi_env` environment in which to construct the Deferred object. | ||
|
||
### Promise | ||
|
||
```cpp | ||
Promise Promise::Deferred::Promise() const; | ||
``` | ||
|
||
Returns the Promise object held by the Promise::Deferred object. | ||
|
||
### Resolve | ||
|
||
```cpp | ||
void Promise::Deferred::Resolve(napi_value value) const; | ||
``` | ||
|
||
Resolves the Promise object held by the Promise::Deferred object. | ||
|
||
* `[in] value`: The N-API primitive value with which to resolve the Promise. | ||
|
||
### Reject | ||
|
||
```cpp | ||
void Promise::Deferred::Reject(napi_value value) const; | ||
``` | ||
|
||
Rejects the Promise object held by the Promise::Deferred object. | ||
|
||
* `[in] value`: The N-API primitive value with which to reject the Promise. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,115 @@ | ||
# Reference | ||
|
||
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](https://nodejs.github.io/node-addon-api/) | ||
[C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) | ||
|
||
# Reference (template) | ||
|
||
Holds a counted reference to a [Value](value.md) 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: | ||
|
||
* [ObjectWrap](object_wrap.md) | ||
* [ObjectReference](object_reference.md) | ||
* [FunctionReference](function_reference.md) | ||
|
||
## Methods | ||
|
||
### Factory Method | ||
|
||
```cpp | ||
static Reference<T> New(const T& value, uint32_t initialRefcount = 0); | ||
``` | ||
|
||
* `[in] value`: The value which is to be referenced. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should say The Value object which is to be referenced. |
||
|
||
* `[in] initialRefcount`: The initial reference count. | ||
|
||
### Empty Constructor | ||
|
||
```cpp | ||
Reference(); | ||
``` | ||
|
||
Creates a new _empty_ Reference instance. | ||
|
||
### Constructor | ||
|
||
```cpp | ||
Reference(napi_env env, napi_value value); | ||
``` | ||
|
||
* `[in] env`: The `napi_env` environment in which to construct the Reference object. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: extra whitespaces between list items |
||
* `[in] value`: The N-API primitive value to be held by the Reference. | ||
|
||
### Env | ||
|
||
```cpp | ||
Napi::Env Env() const; | ||
``` | ||
|
||
Returns the `Env` value in which the Reference was instantiated. | ||
|
||
### IsEmpty | ||
|
||
```cpp | ||
bool IsEmpty() const; | ||
``` | ||
|
||
Determines whether the value held by the Reference is empty. | ||
|
||
### Value | ||
|
||
```cpp | ||
T Value() const; | ||
``` | ||
|
||
Returns the value held by the Reference. | ||
|
||
### Ref | ||
|
||
```cpp | ||
uint32_t Ref(); | ||
``` | ||
|
||
Increments the reference count for the Reference and returns the resulting reference count. Throws an error if the increment fails. | ||
|
||
### Unref | ||
|
||
```cpp | ||
uint32_t Unref(); | ||
``` | ||
|
||
Decrements the reference count for the Reference and returns the resulting reference count. Throws an error if the decrement fails. | ||
|
||
### Reset (Empty) | ||
|
||
```cpp | ||
void Reset(); | ||
``` | ||
|
||
Sets the value held by the Reference to be empty. | ||
|
||
### Reset | ||
|
||
```cpp | ||
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. | ||
|
||
### SuppressDestruct | ||
|
||
```cpp | ||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: common typo: 'continuos' -> 'continuous'