Skip to content
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

AsyncWorker: introduce Destroy() method #488

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions doc/async_worker.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ class was created, passing in the error as the first parameter.
virtual void Napi::AsyncWorker::OnError(const Napi::Error& e);
```

### Destroy

This method is invoked when the instance must be deallocated. If
`SuppressDestruct()` was not called then this method will be called after either
`OnError()` or `OnOK()` complete. The default implementation of this method
causes the instance to delete itself using the `delete` operator. The method is
provided so as to ensure that instances allocated by means other than the `new`
operator can be deallocated upon work completion.

```cpp
virtual void Napi::AsyncWorker::Destroy();
```

### Constructor

Creates a new `Napi::AsyncWorker`.
Expand Down
6 changes: 5 additions & 1 deletion napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3529,6 +3529,10 @@ inline AsyncWorker::~AsyncWorker() {
}
}

inline void AsyncWorker::Destroy() {
delete this;
}

inline AsyncWorker::AsyncWorker(AsyncWorker&& other) {
_env = other._env;
other._env = nullptr;
Expand Down Expand Up @@ -3623,7 +3627,7 @@ inline void AsyncWorker::OnWorkComplete(
});
}
if (!self->_suppress_destruct) {
delete self;
self->Destroy();
}
}

Expand Down
1 change: 1 addition & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,7 @@ namespace Napi {
virtual void Execute() = 0;
virtual void OnOK();
virtual void OnError(const Error& e);
virtual void Destroy();

void SetError(const std::string& error);

Expand Down