@@ -5,16 +5,16 @@ import (
55 "net/http"
66 "syscall/js"
77
8+ jsclass "github.com/syumai/workers/internal/class"
89 jshttp "github.com/syumai/workers/internal/http"
9- jsutil "github.com/syumai/workers/internal/utils"
1010)
1111
1212type Cache struct {
1313 instance js.Value
1414}
1515
1616func (c * Cache ) Open (namespace string ) error {
17- v , err := jsutil . AwaitPromise ( jsutil . RuntimeCache .Call ("open" , namespace ))
17+ v , err := jsclass . Await ( jsclass . Caches .Call ("open" , namespace ))
1818 if err != nil {
1919 return err
2020 }
@@ -25,7 +25,7 @@ func (c *Cache) Open(namespace string) error {
2525
2626func New () * Cache {
2727 return & Cache {
28- instance : jsutil . RuntimeCache .Get ("default" ),
28+ instance : jsclass . Caches .Get ("default" ),
2929 }
3030}
3131
@@ -36,7 +36,9 @@ func New() *Cache {
3636// - Cache-Control instructs not to cache or if the response is too large.
3737// docs: https://developers.cloudflare.com/workers/runtime-apis/cache/#put
3838func (c * Cache ) Put (req * http.Request , res * http.Response ) error {
39- _ , err := jsutil .AwaitPromise (c .instance .Call ("put" , jshttp .ToJSRequest (req ), jshttp .ToJSResponse (res )))
39+ r := jshttp .ToJSRequest (req )
40+ rs := jshttp .ToJSResponse (res )
41+ _ , err := jsclass .Await (c .instance .Call ("put" , r , rs ))
4042 if err != nil {
4143 return err
4244 }
@@ -45,47 +47,41 @@ func (c *Cache) Put(req *http.Request, res *http.Response) error {
4547
4648var ErrCacheNotFound = errors .New ("cache not found" )
4749
48- // MatchOptions represents the options of the Match method.
4950type MatchOptions struct {
50- // IgnoreMethod - Consider the request method a GET regardless of its actual value.
5151 IgnoreMethod bool
5252}
5353
54- // toJS converts MatchOptions to JS object.
5554func (opts * MatchOptions ) toJS () js.Value {
5655 if opts == nil {
5756 return js .Undefined ()
5857 }
59- obj := jsutil . NewObject ()
58+ obj := jsclass . Object . New ()
6059 obj .Set ("ignoreMethod" , opts .IgnoreMethod )
6160 return obj
6261}
6362
6463// Match returns the response object keyed to that request.
6564// docs: https://developers.cloudflare.com/workers/runtime-apis/cache/#match
6665func (c * Cache ) Match (req * http.Request , opts * MatchOptions ) (* http.Response , error ) {
67- res , err := jsutil . AwaitPromise (c .instance .Call ("match" , jshttp .ToJSRequest (req ), opts .toJS ()))
66+ res , err := jsclass . Await (c .instance .Call ("match" , jshttp .ToJSRequest (req ), opts .toJS ()))
6867 if err != nil {
6968 return nil , err
7069 }
7170 if res .IsUndefined () {
7271 return nil , ErrCacheNotFound
7372 }
74- return jshttp .ToResponse (res )
73+ return jshttp .ToResponse (res ), nil
7574}
7675
77- // DeleteOptions represents the options of the Delete method.
7876type DeleteOptions struct {
79- // IgnoreMethod - Consider the request method a GET regardless of its actual value.
8077 IgnoreMethod bool
8178}
8279
83- // toJS converts DeleteOptions to JS object.
8480func (opts * DeleteOptions ) toJS () js.Value {
8581 if opts == nil {
8682 return js .Undefined ()
8783 }
88- obj := jsutil . NewObject ()
84+ obj := jsclass . Object . New ()
8985 obj .Set ("ignoreMethod" , opts .IgnoreMethod )
9086 return obj
9187}
@@ -94,7 +90,7 @@ func (opts *DeleteOptions) toJS() js.Value {
9490// This method only purges content of the cache in the data center that the Worker was invoked.
9591// Returns ErrCacheNotFount if the response was not cached.
9692func (c * Cache ) Delete (req * http.Request , opts * DeleteOptions ) error {
97- res , err := jsutil . AwaitPromise (c .instance .Call ("delete" , jshttp .ToJSRequest (req ), opts .toJS ()))
93+ res , err := jsclass . Await (c .instance .Call ("delete" , jshttp .ToJSRequest (req ), opts .toJS ()))
9894 if err != nil {
9995 return err
10096 }
0 commit comments