Repository navigation
feat(js): add js.spawn async task DSL #76
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
497c227
feat(js): add js.spawn async task DSL
nazarhussain 60871f2
style(examples): drop section number from async task heading
nazarhussain 4e12b52
fix(js): settle promise when async resolve leaves a pending exception
nazarhussain 43eed0d
docs(js): trim async task comments to one line each
nazarhussain f8eba46
refactor(js): replace errorMessage hook with js.errorWithMessage
nazarhussain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| const std = @import("std"); | ||
| const js = @import("zapi").js; | ||
| const napi = @import("zapi").napi; | ||
| const Number = js.Number; | ||
| const String = js.String; | ||
| const Boolean = js.Boolean; | ||
|
|
@@ -722,6 +723,108 @@ pub var mutable_counter: u32 = 5; | |
| pub const IDENTITY_MATRIX = [_]u32{ 1, 0, 0, 1 }; | ||
| pub const VERSION_INFO = .{ .major = 3, .minor = 1 }; | ||
|
|
||
| // ============================================================================ | ||
| // Section 18: Async Tasks | ||
| // ============================================================================ | ||
|
|
||
|
Comment on lines
+726
to
+729
Contributor
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. Can we remove this task number comments by AI?
Contributor
Author
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. It was not just added by AI, but rather followed existing pattern. |
||
| /// Doubles a number on the libuv worker pool. | ||
| /// | ||
| /// `resolve` returns a DSL `js.Number` rather than a raw `napi.Value`, which | ||
| /// works because the async complete callback establishes the DSL env context. | ||
| const DoubleTask = struct { | ||
| value: i32, | ||
|
|
||
| pub fn compute(self: *DoubleTask) !void { | ||
| self.value *= 2; | ||
| } | ||
|
|
||
| pub fn resolve(self: *DoubleTask, _: napi.Env) !Number { | ||
| return Number.from(self.value); | ||
| } | ||
|
|
||
| pub fn deinit(_: *DoubleTask) void {} | ||
| }; | ||
|
|
||
| /// JS: asyncDouble(n): Promise<number> | ||
| pub fn asyncDouble(n: Number) !Value { | ||
| return js.spawn(DoubleTask, .{ .value = n.assertI32() }, "asyncDouble"); | ||
| } | ||
|
|
||
| /// Scales a Uint32Array on the worker pool and hands the result back without | ||
| /// copying, composing `js.AsyncTask` with `js.OwnedUint32Array`. | ||
| const ScaleTask = struct { | ||
| data: []u32, | ||
| factor: u32, | ||
|
|
||
| pub fn compute(self: *ScaleTask) !void { | ||
| for (self.data) |*value| value.* *= self.factor; | ||
| } | ||
|
|
||
| pub fn resolve(self: *ScaleTask, _: napi.Env) !js.OwnedUint32Array { | ||
| const owned: js.OwnedUint32Array = .fromOwnedSlice(js.allocator(), self.data); | ||
| self.data = &.{}; | ||
| return owned; | ||
| } | ||
|
|
||
| pub fn deinit(self: *ScaleTask) void { | ||
| // Empty (a no-op free) once resolve transferred ownership to JS. | ||
| js.allocator().free(self.data); | ||
| } | ||
| }; | ||
|
|
||
| /// JS: asyncScale(data, factor): Promise<Uint32Array> | ||
| pub fn asyncScale(data: js.Uint32Array, factor: Number) !Value { | ||
| const copy = try js.allocator().dupe(u32, try data.toSlice()); | ||
| errdefer js.allocator().free(copy); | ||
| return js.spawn(ScaleTask, .{ | ||
| .data = copy, | ||
| .factor = @intCast(factor.assertI32()), | ||
| }, "asyncScale"); | ||
| } | ||
|
|
||
| /// Rejects with a task-supplied message via the optional `errorMessage` decl. | ||
| const FailTask = struct { | ||
| pub fn compute(_: *FailTask) !void { | ||
| return error.ComputeFailed; | ||
| } | ||
|
|
||
| pub fn resolve(_: *FailTask, _: napi.Env) !Number { | ||
| return Number.from(0); | ||
| } | ||
|
|
||
| pub fn errorMessage(err: anyerror) [:0]const u8 { | ||
| return switch (err) { | ||
| error.ComputeFailed => "worker could not finish the job", | ||
| else => @errorName(err), | ||
| }; | ||
| } | ||
|
|
||
| pub fn deinit(_: *FailTask) void {} | ||
| }; | ||
|
|
||
| /// JS: asyncFail(): Promise<never> | ||
| pub fn asyncFail() !Value { | ||
| return js.spawn(FailTask, .{}, "asyncFail"); | ||
| } | ||
|
|
||
| /// Without `errorMessage`, the rejection message defaults to `@errorName`. | ||
| const BareFailTask = struct { | ||
| pub fn compute(_: *BareFailTask) !void { | ||
| return error.Unlucky; | ||
| } | ||
|
|
||
| pub fn resolve(_: *BareFailTask, _: napi.Env) !Number { | ||
| return Number.from(0); | ||
| } | ||
|
|
||
| pub fn deinit(_: *BareFailTask) void {} | ||
| }; | ||
|
|
||
| /// JS: asyncFailBare(): Promise<never> | ||
| pub fn asyncFailBare() !Value { | ||
| return js.spawn(BareFailTask, .{}, "asyncFailBare"); | ||
| } | ||
|
|
||
| comptime { | ||
| js.exportModule(@This(), .{ | ||
| .identity = @import("zapi_addon_identity"), | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.