Skip to content

Commit

Permalink
add indexdb_factory test
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Apr 20, 2024
1 parent 25adf83 commit 4afe446
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ deps\:test:

.PHONY: test\:js
test:
GOOS=js GOARCH=wasm go test -exec wasmbrowsertest ./...
GOOS=js GOARCH=wasm go test -timeout 30s -exec wasmbrowsertest ./...
12 changes: 8 additions & 4 deletions indexed_db/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

package indexed_db

import "syscall/js"
import (
"syscall/js"

"github.com/sourcenetwork/goji"
)

const (
// The cursor shows all records, including duplicates.
Expand Down Expand Up @@ -56,7 +60,7 @@ func (c CursorValue) PrimaryKey() js.Value {
// https://developer.mozilla.org/en-US/docs/Web/API/IDBCursor/request
func (c CursorValue) Request() RequestValue {
res := js.Value(c).Get("request")
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// Source returns the IDBCursor request property.
Expand Down Expand Up @@ -92,15 +96,15 @@ func (c CursorValue) ContinuePrimaryKey(key, primaryKey js.Value) {
// https://developer.mozilla.org/en-US/docs/Web/API/IDBCursor/delete
func (c CursorValue) Delete() RequestValue {
res := js.Value(c).Call("delete")
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// Update wraps the IDBCursor update instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBCursor/update
func (c CursorValue) Update(value js.Value) RequestValue {
res := js.Value(c).Call("update", value)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// CursorWithValue is an instance of IDBCursorWithValue
Expand Down
18 changes: 10 additions & 8 deletions indexed_db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,57 @@ import (
// DatabaseValue is an instance of IDBDatabase
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase
type DatabaseValue goji.EventTargetValue
type DatabaseValue struct {
goji.EventTargetValue
}

// Name returns the IDBDatabase name property.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/name
func (d DatabaseValue) Name() string {
return js.Value(d).Get("name").String()
return js.Value(d.EventTargetValue).Get("name").String()
}

// ObjectStoreNames returns the IDBDatabase objectStoreNames property.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/objectStoreNames
func (d DatabaseValue) ObjectStoreNames() js.Value {
return js.Value(d).Get("objectStoreNames")
return js.Value(d.EventTargetValue).Get("objectStoreNames")
}

// Version returns the IDBDatabase version property.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/version
func (d DatabaseValue) Version() int {
return js.Value(d).Get("version").Int()
return js.Value(d.EventTargetValue).Get("version").Int()
}

// Close wraps the IDBDatabase close instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/close
func (d DatabaseValue) Close() {
js.Value(d).Call("close")
js.Value(d.EventTargetValue).Call("close")
}

// CreateObjectStore wraps the IDBDatabase createObjectStore instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/createObjectStore
func (d DatabaseValue) CreateObjectStore(name string, options js.Value) ObjectStoreValue {
res := js.Value(d).Call("createObjectStore", name, options)
res := js.Value(d.EventTargetValue).Call("createObjectStore", name, options)
return ObjectStoreValue(res)
}

// DeleteObjectStore wraps the IDBDatabase deleteObjectStore instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/deleteObjectStore
func (d DatabaseValue) DeleteObjectStore(name string) {
js.Value(d).Call("deleteObjectStore", name)
js.Value(d.EventTargetValue).Call("deleteObjectStore", name)
}

// Transaction wraps the IDBDatabase transaction instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/transaction
func (d DatabaseValue) Transaction(storeNames js.Value, mode string, options js.Value) TransactionValue {
res := js.Value(d).Call("transaction", storeNames, mode, options)
res := js.Value(d.EventTargetValue).Call("transaction", storeNames, mode, options)
return TransactionValue(res)
}
15 changes: 13 additions & 2 deletions indexed_db/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import (
"github.com/sourcenetwork/goji"
)

const (
// The blocked handler is executed when an open
// connection to a database is blocking a
// versionchange transaction on the same database.
BlockedEvent = "blocked"
// The upgradeneeded event is fired when an attempt
// was made to open a database with a version number
// higher than its current version.
UpgradeNeededEvent = "upgradeneeded"
)

func init() {
indexedDB = js.Global().Get("indexedDB")
}
Expand All @@ -19,15 +30,15 @@ var indexedDB js.Value
// https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory/open
func Open(name string, version uint) RequestValue {
res := indexedDB.Call("open", name, version)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// DeleteDatabase wraps the IDBFactory deleteDatabase method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory/deleteDatabase
func DeleteDatabase(name string, options js.Value) RequestValue {
res := indexedDB.Call("deleteDatabase", name, options)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// Cmp wraps the IDBFactory cmp method.
Expand Down
31 changes: 31 additions & 0 deletions indexed_db/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//go:build js

package indexed_db

import (
"testing"

"github.com/sourcenetwork/goji"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestOpen(t *testing.T) {
upgradeCalled := false
upgradeNeeded := goji.EventListener(func(event goji.EventValue) {
upgradeCalled = true
})
defer upgradeNeeded.Release()

request := Open("test", 1)
request.AddEventListener(UpgradeNeededEvent, upgradeNeeded.Value)

res, err := Await(request)
require.NoError(t, err)

// upgradeNeeded should have been called by this point
assert.True(t, upgradeCalled)

db := DatabaseValue{goji.EventTargetValue(res)}
assert.Equal(t, "test", db.Name())
}
16 changes: 9 additions & 7 deletions indexed_db/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package indexed_db

import (
"syscall/js"

"github.com/sourcenetwork/goji"
)

// IndexValue is an IDBIndex instance.
Expand Down Expand Up @@ -52,53 +54,53 @@ func (i IndexValue) Unique() bool {
// https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/count
func (i IndexValue) Count(key js.Value) RequestValue {
res := js.Value(i).Call("count", key)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// Get wraps the IDBIndex get instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/get
func (i IndexValue) Get(key js.Value) RequestValue {
res := js.Value(i).Call("get", key)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// GetAll wraps the IDBIndex getAll instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/getAll
func (i IndexValue) GetAll(query, count js.Value) RequestValue {
res := js.Value(i).Call("getAll", query, count)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// GetAllKeys wraps the IDBIndex getAllKeys instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/getAllKeys
func (i IndexValue) GetAllKeys(query, count js.Value) RequestValue {
res := js.Value(i).Call("getAllKeys", query, count)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// GetKey wraps the IDBIndex getKey instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/getKey
func (i IndexValue) GetKey(key js.Value) RequestValue {
res := js.Value(i).Call("getKey", key)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// OpenCursor wraps the IDBIndex openCursor instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/openCursor
func (i IndexValue) OpenCursor(key, direction js.Value) RequestValue {
res := js.Value(i).Call("openCursor", key, direction)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// OpenKeyCursor wraps the IDBIndex openKeyCursor instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/openKeyCursor
func (i IndexValue) OpenKeyCursor(key, direction js.Value) RequestValue {
res := js.Value(i).Call("openKeyCursor", key, direction)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}
28 changes: 16 additions & 12 deletions indexed_db/object_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

package indexed_db

import "syscall/js"
import (
"syscall/js"

"github.com/sourcenetwork/goji"
)

// ObjectStoreValue is an instance of IDBObjectStore.
type ObjectStoreValue js.Value
Expand Down Expand Up @@ -48,23 +52,23 @@ func (o ObjectStoreValue) Transaction() TransactionValue {
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/add
func (o ObjectStoreValue) Add(value js.Value, key js.Value) RequestValue {
res := js.Value(o).Call("add", value, key)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// Clear wraps the IDBObjectStore clear instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/clear
func (o ObjectStoreValue) Clear() RequestValue {
res := js.Value(o).Call("clear")
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// Count wraps the IDBObjectStore count instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/count
func (o ObjectStoreValue) Count(query js.Value) RequestValue {
res := js.Value(o).Call("count", query)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// CreateIndex wraps the IDBObjectStore createIndex instance method.
Expand All @@ -80,7 +84,7 @@ func (o ObjectStoreValue) CreateIndex(indexName, keyPath, options js.Value) Inde
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/delete
func (o ObjectStoreValue) Delete(key js.Value) RequestValue {
res := js.Value(o).Call("delete", key)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// DeleteIndex wraps the IDBObjectStore deleteIndex instance method.
Expand All @@ -95,31 +99,31 @@ func (o ObjectStoreValue) DeleteIndex(key js.Value) {
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/get
func (o ObjectStoreValue) Get(key js.Value) RequestValue {
res := js.Value(o).Call("get", key)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// GetAll wraps the IDBObjectStore getAll instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/getAll
func (o ObjectStoreValue) GetAll(key js.Value, count js.Value) RequestValue {
res := js.Value(o).Call("getAll", key, count)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// GetAllKeys wraps the IDBObjectStore getAllKeys instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/getAllKeys
func (o ObjectStoreValue) GetAllKeys(key js.Value, count js.Value) RequestValue {
res := js.Value(o).Call("getAllKeys", key, count)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// GetKey wraps the IDBObjectStore getKey instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/getKey
func (o ObjectStoreValue) GetKey(key js.Value) RequestValue {
res := js.Value(o).Call("getKey", key)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// Index wraps the IDBObjectStore index instance method.
Expand All @@ -135,21 +139,21 @@ func (o ObjectStoreValue) Index(name string) IndexValue {
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/openCursor
func (o ObjectStoreValue) OpenCursor(query, direction js.Value) RequestValue {
res := js.Value(o).Call("openCursor", query, direction)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// OpenKeyCursor wraps the IDBObjectStore openKeyCursor instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/openKeyCursor
func (o ObjectStoreValue) OpenKeyCursor(query, direction js.Value) RequestValue {
res := js.Value(o).Call("openKeyCursor", query, direction)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}

// Put wraps the IDBObjectStore put instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/put
func (o ObjectStoreValue) Put(item, key js.Value) RequestValue {
res := js.Value(o).Call("put", item, key)
return RequestValue(res)
return RequestValue{goji.EventTargetValue(res)}
}
Loading

0 comments on commit 4afe446

Please sign in to comment.