Skip to content
Snippets Groups Projects
Select Git revision
  • a2bf05c619b1ab1d69954c0b5af577480f6b00f6
  • release default
  • 11-22-implement-kv-interface-defined-in-collectiveversionedkvgo
  • master protected
  • XX-4688/DbEncoding
  • hotfix/update
  • @XX-4682/Files
  • hotfix/XX-4655
  • dev protected
  • project/HavenNotifications
  • XX-4602/SilentMessageType
  • jono/npmTest
  • wasmTest2
  • XX-4461/FileUpload
  • XX-4505/blockuser
  • XX-4441
  • Jakub/Emoji-CI-Test
  • testing/websockets
  • fastReg
  • fast-registration
  • NewHostPool
  • v0.3.22
  • v0.3.21
  • v0.3.20
  • v0.3.18
  • v0.3.17
  • v0.3.16
  • v0.3.15
  • v0.3.14
  • v0.3.13
  • v0.3.12
  • v0.3.11
  • v0.3.10
  • v0.3.9
  • v0.3.8
  • v0.3.7
  • v0.3.6
  • v0.3.5
  • v0.3.4
  • 812b395df518ce096d01d5292596ca26f8fe92d9c4487ddfa515e190a51aa1a1
  • 76ba08e2dfa1798412a265404fa271840b52c035869111fce8e8cdb23a036a5a
41 results

init.go

  • init.go 1.95 KiB
    ////////////////////////////////////////////////////////////////////////////////
    // Copyright © 2022 xx foundation                                             //
    //                                                                            //
    // Use of this source code is governed by a license that can be found in the  //
    // LICENSE file.                                                              //
    ////////////////////////////////////////////////////////////////////////////////
    
    //go:build js && wasm
    
    package dm
    
    import (
    	"encoding/json"
    	"time"
    
    	"github.com/pkg/errors"
    
    	"gitlab.com/elixxir/client/v4/storage/utility"
    	"gitlab.com/elixxir/xxdk-wasm/storage"
    	"gitlab.com/elixxir/xxdk-wasm/worker"
    )
    
    // databaseSuffix is the suffix to be appended to the name of the database.
    const databaseSuffix = "_speakeasy_state"
    
    // NewStateMessage is JSON marshalled and sent to the worker for
    // [NewState].
    type NewStateMessage struct {
    	DatabaseName string `json:"databaseName"`
    }
    
    // NewState returns a [utility.WebState] backed by indexeddb.
    // The name should be a base64 encoding of the users public key.
    func NewState(path, wasmJsPath string) (utility.WebState, error) {
    	databaseName := path + databaseSuffix
    
    	wh, err := worker.NewManager(wasmJsPath, "stateIndexedDb", true)
    	if err != nil {
    		return nil, err
    	}
    
    	// Store the database name
    	err = storage.StoreIndexedDb(databaseName)
    	if err != nil {
    		return nil, err
    	}
    
    	msg := NewStateMessage{
    		DatabaseName: databaseName,
    	}
    
    	payload, err := json.Marshal(msg)
    	if err != nil {
    		return nil, err
    	}
    
    	dataChan := make(chan []byte)
    	wh.SendMessage(NewStateTag, payload,
    		func(data []byte) { dataChan <- data })
    
    	select {
    	case data := <-dataChan:
    		if len(data) > 0 {
    			return nil, errors.New(string(data))
    		}
    	case <-time.After(worker.ResponseTimeout):
    		return nil, errors.Errorf("timed out after %s waiting for indexedDB "+
    			"database in worker to initialize", worker.ResponseTimeout)
    	}
    
    	return &wasmModel{wh}, nil
    }