Skip to content
Draft
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
50 changes: 24 additions & 26 deletions pouchdb/bindings/pouchdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,24 @@ func (p *PouchDB) Version() string {
return p.Get("version").String()
}

func setTimeout(ctx context.Context, options map[string]interface{}) map[string]interface{} {
func setFetchOptions(ctx context.Context, options map[string]interface{}) map[string]interface{} {
if ctx == nil { // Just to be safe
return options
}
deadline, ok := ctx.Deadline()
if !ok {
return options
}
if options == nil {
options = make(map[string]interface{})
}
if _, ok := options["ajax"]; !ok {
options["ajax"] = make(map[string]interface{})
}
ajax := options["ajax"].(map[string]interface{})
timeout := int(time.Until(deadline) * 1000) //nolint:gomnd
// Used by ajax calls
ajax["timeout"] = timeout
// Used by changes and replications
options["timeout"] = timeout
controller := js.Global.Get("AbortController").New()
options["fetch"] = js.MakeFunc(func(_ *js.Object, args []*js.Object) interface{} {
url := args[0]
return js.Global.Call("fetch", url, map[string]interface{}{
"signal": controller.Get("signal"),
})
})
go func() {
<-ctx.Done()
controller.Call("abort")
}()
return options
}

Expand Down Expand Up @@ -200,7 +198,7 @@ func (db *DB) Info(ctx context.Context) (*DBInfo, error) {
// Put creates a new document or update an existing document.
// See https://pouchdb.com/api.html#create_document
func (db *DB) Put(ctx context.Context, doc interface{}, opts map[string]interface{}) (rev string, err error) {
result, err := callBack(ctx, db, "put", doc, setTimeout(ctx, opts))
result, err := callBack(ctx, db, "put", doc, setFetchOptions(ctx, opts))
if err != nil {
return "", err
}
Expand All @@ -210,7 +208,7 @@ func (db *DB) Put(ctx context.Context, doc interface{}, opts map[string]interfac
// Post creates a new document and lets PouchDB auto-generate the ID.
// See https://pouchdb.com/api.html#using-dbpost
func (db *DB) Post(ctx context.Context, doc interface{}, opts map[string]interface{}) (docID, rev string, err error) {
result, err := callBack(ctx, db, "post", doc, setTimeout(ctx, opts))
result, err := callBack(ctx, db, "post", doc, setFetchOptions(ctx, opts))
if err != nil {
return "", "", err
}
Expand All @@ -220,7 +218,7 @@ func (db *DB) Post(ctx context.Context, doc interface{}, opts map[string]interfa
// Get fetches the requested document from the database.
// See https://pouchdb.com/api.html#fetch_document
func (db *DB) Get(ctx context.Context, docID string, opts map[string]interface{}) (doc []byte, rev string, err error) {
result, err := callBack(ctx, db, "get", docID, setTimeout(ctx, opts))
result, err := callBack(ctx, db, "get", docID, setFetchOptions(ctx, opts))
if err != nil {
return nil, "", err
}
Expand All @@ -231,7 +229,7 @@ func (db *DB) Get(ctx context.Context, docID string, opts map[string]interface{}
// Delete marks a document as deleted.
// See https://pouchdb.com/api.html#delete_document
func (db *DB) Delete(ctx context.Context, docID, rev string, opts map[string]interface{}) (newRev string, err error) {
result, err := callBack(ctx, db, "remove", docID, rev, setTimeout(ctx, opts))
result, err := callBack(ctx, db, "remove", docID, rev, setFetchOptions(ctx, opts))
if err != nil {
return "", err
}
Expand All @@ -252,7 +250,7 @@ func (db *DB) Purge(ctx context.Context, docID, rev string) ([]string, error) {
if !db.indexeddb() {
return nil, &internal.Error{Status: http.StatusNotImplemented, Message: "kivik: purge only supported with indexedDB adapter"}
}
result, err := callBack(ctx, db, "purge", docID, rev, setTimeout(ctx, nil))
result, err := callBack(ctx, db, "purge", docID, rev, setFetchOptions(ctx, nil))
if err != nil {
return nil, err
}
Expand All @@ -266,18 +264,18 @@ func (db *DB) Purge(ctx context.Context, docID, rev string) ([]string, error) {

// Destroy destroys the database.
func (db *DB) Destroy(ctx context.Context, options map[string]interface{}) error {
_, err := callBack(ctx, db, "destroy", setTimeout(ctx, options))
_, err := callBack(ctx, db, "destroy", setFetchOptions(ctx, options))
return err
}

// AllDocs returns a list of all documents in the database.
func (db *DB) AllDocs(ctx context.Context, options map[string]interface{}) (*js.Object, error) {
return callBack(ctx, db, "allDocs", setTimeout(ctx, options))
return callBack(ctx, db, "allDocs", setFetchOptions(ctx, options))
}

// Query queries a map/reduce function.
func (db *DB) Query(ctx context.Context, ddoc, view string, options map[string]interface{}) (*js.Object, error) {
o := setTimeout(ctx, options)
o := setFetchOptions(ctx, options)
return callBack(ctx, db, "query", ddoc+"/"+view, o)
}

Expand Down Expand Up @@ -350,17 +348,17 @@ func (db *DB) BulkDocs(ctx context.Context, docs []interface{}, options map[stri
jsDocs[i] = jsJSON.Call("parse", string(jsonDoc))
}
if options == nil {
return callBack(ctx, db, "bulkDocs", jsDocs, setTimeout(ctx, nil))
return callBack(ctx, db, "bulkDocs", jsDocs, setFetchOptions(ctx, nil))
}
return callBack(ctx, db, "bulkDocs", jsDocs, options, setTimeout(ctx, nil))
return callBack(ctx, db, "bulkDocs", jsDocs, options, setFetchOptions(ctx, nil))
}

// Changes returns an event emitter object.
//
// See https://pouchdb.com/api.html#changes
func (db *DB) Changes(ctx context.Context, options map[string]interface{}) (changes *js.Object, e error) {
defer RecoverError(&e)
return db.Call("changes", setTimeout(ctx, options)), nil
return db.Call("changes", setFetchOptions(ctx, options)), nil
}

// PutAttachment attaches a binary object to a document.
Expand Down Expand Up @@ -406,7 +404,7 @@ func attachmentObject(contentType string, content io.Reader) (att *js.Object, er
//
// See https://pouchdb.com/api.html#get_attachment
func (db *DB) GetAttachment(ctx context.Context, docID, filename string, options map[string]interface{}) (*js.Object, error) {
return callBack(ctx, db, "getAttachment", docID, filename, setTimeout(ctx, options))
return callBack(ctx, db, "getAttachment", docID, filename, setFetchOptions(ctx, options))
}

// RemoveAttachment deletes an attachment from a document.
Expand Down