-
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Promise resolver and promise result (#76)
* Promise resolver and promise result * update persistent value to match master * Add docs, test and Exception types * remove the exception error functions * add test for when value is not a promise * update changelog
- Loading branch information
Showing
8 changed files
with
307 additions
and
7 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
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
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,87 @@ | ||
// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package v8go | ||
|
||
// #include <stdlib.h> | ||
// #include "v8go.h" | ||
import "C" | ||
import ( | ||
"errors" | ||
) | ||
|
||
// PromiseState is the state of the Promise. | ||
type PromiseState int | ||
|
||
const ( | ||
Pending PromiseState = iota | ||
Fulfilled | ||
Rejected | ||
) | ||
|
||
// PromiseResolver is the resolver object for the promise. | ||
// Most cases will create a new PromiseResolver and return | ||
// the associated Promise from the resolver. | ||
type PromiseResolver struct { | ||
*Object | ||
prom *Promise | ||
} | ||
|
||
// Promise is the JavaScript promise object defined in ES6 | ||
type Promise struct { | ||
*Object | ||
} | ||
|
||
// MewPromiseResolver creates a new Promise resolver for the given context. | ||
// The associated Promise will be in a Pending state. | ||
func NewPromiseResolver(ctx *Context) (*PromiseResolver, error) { | ||
if ctx == nil { | ||
return nil, errors.New("v8go: Context is required") | ||
} | ||
ptr := C.NewPromiseResolver(ctx.ptr) | ||
val := &Value{ptr, ctx} | ||
return &PromiseResolver{&Object{val}, nil}, nil | ||
} | ||
|
||
// GetPromise returns the associated Promise object for this resolver. | ||
// The Promise object is unique to the resolver and returns the same object | ||
// on multiple calls. | ||
func (r *PromiseResolver) GetPromise() *Promise { | ||
if r.prom == nil { | ||
ptr := C.PromiseResolverGetPromise(r.ptr) | ||
val := &Value{ptr, r.ctx} | ||
r.prom = &Promise{&Object{val}} | ||
} | ||
return r.prom | ||
} | ||
|
||
// Resolve invokes the Promise resolve state with the given value. | ||
// The Promise state will transition from Pending to Fulfilled. | ||
func (r *PromiseResolver) Resolve(val Valuer) bool { | ||
r.ctx.register() | ||
defer r.ctx.deregister() | ||
return C.PromiseResolverResolve(r.ptr, val.value().ptr) != 0 | ||
} | ||
|
||
// Reject invokes the Promise reject state with the given value. | ||
// The Promise state will transition from Pending to Rejected. | ||
func (r *PromiseResolver) Reject(err *Value) bool { | ||
r.ctx.register() | ||
defer r.ctx.deregister() | ||
return C.PromiseResolverReject(r.ptr, err.ptr) != 0 | ||
} | ||
|
||
// State returns the current state of the Promise. | ||
func (p *Promise) State() PromiseState { | ||
return PromiseState(C.PromiseState(p.ptr)) | ||
} | ||
|
||
// Result is the value result of the Promise. The Promise must | ||
// NOT be in a Pending state, otherwise may panic. Call promise.State() | ||
// to validate state before calling for the result. | ||
func (p *Promise) Result() *Value { | ||
ptr := C.PromiseResult(p.ptr) | ||
val := &Value{ptr, p.ctx} | ||
return val | ||
} |
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,47 @@ | ||
// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package v8go_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"rogchap.com/v8go" | ||
) | ||
|
||
func TestPromise(t *testing.T) { | ||
t.Parallel() | ||
|
||
iso, _ := v8go.NewIsolate() | ||
ctx, _ := v8go.NewContext(iso) | ||
if _, err := v8go.NewPromiseResolver(nil); err == nil { | ||
t.Error("expected error with <nil> Context") | ||
} | ||
|
||
res1, _ := v8go.NewPromiseResolver(ctx) | ||
prom1 := res1.GetPromise() | ||
if s := prom1.State(); s != v8go.Pending { | ||
t.Errorf("unexpected state for Promise, want Pending (0) got: %v", s) | ||
} | ||
|
||
val1, _ := v8go.NewValue(iso, "foo") | ||
res1.Resolve(val1) | ||
|
||
if s := prom1.State(); s != v8go.Fulfilled { | ||
t.Fatalf("unexpected state for Promise, want Fulfilled (1) got: %v", s) | ||
} | ||
|
||
if result := prom1.Result(); result.String() != val1.String() { | ||
t.Errorf("expected the Promise result to match the resolve value, but got: %s", result) | ||
} | ||
|
||
res2, _ := v8go.NewPromiseResolver(ctx) | ||
val2, _ := v8go.NewValue(iso, "Bad Foo") | ||
res2.Reject(val2) | ||
|
||
prom2 := res2.GetPromise() | ||
if s := prom2.State(); s != v8go.Rejected { | ||
t.Fatalf("unexpected state for Promise, want Rejected (2) got: %v", s) | ||
} | ||
} |
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
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
Oops, something went wrong.