-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft of docs for weak callbacks
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#Weak Callbacks | ||
|
||
## NanWeakCallbackInfo | ||
Argument to weak callbacks. | ||
You may need to free any external resources attached to the object. | ||
```c++ | ||
template<typename T> | ||
class NanWeakCallbackInfo { | ||
public: | ||
typedef void (*Callback)(const NanWeakCallbackInfo<T>& data); | ||
|
||
v8::Isolate *GetIsolate() const; | ||
|
||
/** | ||
* Get the parameter that was associated with the weak handle. | ||
*/ | ||
T *GetParameter() const; | ||
|
||
/** | ||
* Get pointer from internal field, index can be 0 or 1. | ||
*/ | ||
void *GetInternalField(int index) const; | ||
}; | ||
``` | ||
### Example | ||
```c++ | ||
void weakCallback(const NanWeakCallbackInfo<int> &data) { | ||
int *parameter = data.GetParameter(); | ||
delete parameter; | ||
} | ||
NanPersistent<v8::Object> obj; | ||
int *data = new int(0); | ||
obj.SetWeak(data, callback, NanWeakCallbackType::kParameter); | ||
``` |