diff --git a/indexedDb/init.go b/indexedDb/init.go index 271300b4bafcec519a1b15ea05518fb0cfae5d6a..22ea3319981f8c59c58d0d1cae0b113eefb193a3 100644 --- a/indexedDb/init.go +++ b/indexedDb/init.go @@ -10,14 +10,13 @@ package indexedDb import ( + "github.com/hack-pad/go-indexeddb/idb" "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" cryptoChannel "gitlab.com/elixxir/crypto/channel" "gitlab.com/elixxir/xxdk-wasm/storage" "syscall/js" - "github.com/hack-pad/go-indexeddb/idb" - jww "github.com/spf13/jwalterweatherman" - "gitlab.com/elixxir/client/channels" "gitlab.com/xx_network/primitives/id" ) @@ -94,27 +93,7 @@ func newWASMModel(databaseName string, encryption cryptoChannel.Cipher, return nil, err } - // FIXME: The below is a hack that for some reason prevents moving on with - // uninitialized database despite the previous call to Await. - // It would be idea to find a different solution. - // Close and open again to ensure the state is finalized - err = db.Close() - if err != nil { - return nil, err - } - openRequest, err = idb.Global().Open(ctx, databaseName, currentVersion, - func(db *idb.Database, oldVersion, newVersion uint) error { - return nil - }) - if err != nil { - return nil, err - } - // Wait for database open to finish - db, err = openRequest.Await(ctx) - if err != nil { - return nil, err - } - + // Save the encryption status to storage encryptionStatus := encryption != nil loadedEncryptionStatus, err := storage.StoreIndexedDbEncryptionStatus( databaseName, encryptionStatus) @@ -122,13 +101,17 @@ func newWASMModel(databaseName string, encryption cryptoChannel.Cipher, return nil, err } + // Verify encryption status does not change if encryptionStatus != loadedEncryptionStatus { return nil, errors.New( "Cannot load database with different encryption status.") } else if !encryptionStatus { jww.WARN.Printf("IndexedDb encryption disabled!") } - return &wasmModel{db: db, receivedMessageCB: cb, cipher: encryption}, err + + // Attempt to ensure the database has been properly initialized + wrapper := &wasmModel{db: db, receivedMessageCB: cb, cipher: encryption} + return wrapper, wrapper.hackTestDb() } // v1Upgrade performs the v0 -> v1 database upgrade. @@ -195,3 +178,28 @@ func v1Upgrade(db *idb.Database) error { return nil } + +// hackTestDb is a horrible function that exists as the result of an extremely +// long discussion about why initializing the IndexedDb sometimes silently +// fails. It ultimately tries to prevent an unrecoverable situation by actually +// inserting some nonsense data and then checking to see if it persists. +// If this function still exists in 2023, god help us all. Amen. +func (w *wasmModel) hackTestDb() error { + testMessage := &Message{ + ID: 0, + Nickname: "test", + MessageID: id.DummyUser.Marshal(), + } + msgId, helper := w.receiveHelper(testMessage, false) + if helper != nil { + return helper + } + result, err := w.get(messageStoreName, js.ValueOf(msgId)) + if err != nil { + return err + } + if len(result) == 0 { + return errors.Errorf("Failed to test db, record not present") + } + return nil +}