Skip to content

Commit

Permalink
Use in-memory storage for browser environments
Browse files Browse the repository at this point in the history
  • Loading branch information
albrow committed Aug 20, 2019
1 parent 405555a commit b636812
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
14 changes: 0 additions & 14 deletions browser/index.html
Expand Up @@ -3,18 +3,6 @@
<head>
<meta charset="utf-8">
<title>0x Mesh in the browser</title>
<script src="resources/browserfs-1.4.3.js"></script>
<script>
BrowserFS.configure({
fs: "LocalStorage",
}, function(e) {
if (e) {
throw e;
}
});
// In browsers, goleveldb expects the global variable "browserFS" to be set.
window.browserFS = BrowserFS.BFSRequire('fs');
</script>
<script src="resources/wasm_exec.js"></script>
<script>
const go = new Go();
Expand All @@ -24,7 +12,5 @@
</script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
</body>
</html>
12 changes: 0 additions & 12 deletions db/db.go
Expand Up @@ -36,18 +36,6 @@ type DB struct {
colLock sync.Mutex
}

// Open creates a new database using the given file path for permanent storage.
// It is not safe to have multiple DBs using the same file path.
func Open(path string) (*DB, error) {
ldb, err := leveldb.OpenFile(path, nil)
if err != nil {
return nil, err
}
return &DB{
ldb: ldb,
}, nil
}

// Close closes the database. It is not safe to call Close if there are any
// other methods that have not yet returned. It is safe to call Close multiple
// times.
Expand Down
17 changes: 17 additions & 0 deletions db/open.go
@@ -0,0 +1,17 @@
// +build !js

package db

import "github.com/syndtr/goleveldb/leveldb"

// Open creates a new database using the given file path for permanent storage.
// It is not safe to have multiple DBs using the same file path.
func Open(path string) (*DB, error) {
ldb, err := leveldb.OpenFile(path, nil)
if err != nil {
return nil, err
}
return &DB{
ldb: ldb,
}, nil
}
20 changes: 20 additions & 0 deletions db/open_js.go
@@ -0,0 +1,20 @@
// +build js,wasm

package db

import (
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/storage"
)

// Open creates a new database using in-memory storage. The given file path is
// ignored.
func Open(path string) (*DB, error) {
ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
if err != nil {
return nil, err
}
return &DB{
ldb: ldb,
}, nil
}

0 comments on commit b636812

Please sign in to comment.