Skip to content

Commit 12c5607

Browse files
committed
wip indexeddb
1 parent 0f61612 commit 12c5607

File tree

5 files changed

+185
-3
lines changed

5 files changed

+185
-3
lines changed

backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaApplicationConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class TeaApplicationConfiguration {
2020
*/
2121
public boolean preloadAssets = true;
2222

23+
public boolean useIndexedDB = false;
24+
2325
/**
2426
* The prefix for the browser storage. If you have multiple apps on the same server and want to keep the
2527
* data separate for those applications, you will need to set unique prefixes. This is useful if you are

backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/filesystem/FileDBManager.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.badlogic.gdx.Gdx;
44
import com.github.xpenatan.gdx.backends.teavm.TeaApplication;
5+
import com.github.xpenatan.gdx.backends.teavm.TeaApplicationConfiguration;
56
import com.github.xpenatan.gdx.backends.teavm.TeaFileHandle;
67

78
import java.io.InputStream;
@@ -22,13 +23,28 @@ public final class FileDBManager extends FileDB {
2223
*/
2324
private final FileDBStorage memory;
2425

26+
/**
27+
* IndexedDB is async, we store in memory while we save it. It will delete from memory when it's done.
28+
*/
29+
private FileDB indexedDB;
30+
2531
FileDBManager() {
26-
localStorage = new FileDBStorage(new StoreLocal(((TeaApplication)Gdx.app).getConfig().storagePrefix));
32+
TeaApplicationConfiguration config = ((TeaApplication)Gdx.app).getConfig();
33+
String storagePrefix = config.storagePrefix;
34+
localStorage = new FileDBStorage(new StoreLocal(storagePrefix));
2735
memory = new FileDBStorage(new StoreMemory());
36+
37+
if(config.useIndexedDB) {
38+
indexedDB = new IndexedDBStorage();
39+
}
2840
}
2941

3042
@Override
3143
public InputStream read(TeaFileHandle file) {
44+
if(indexedDB != null) {
45+
return indexedDB.read(file);
46+
}
47+
3248
if(memory.exists(file)) {
3349
return memory.read(file);
3450
}
@@ -39,6 +55,11 @@ public InputStream read(TeaFileHandle file) {
3955

4056
@Override
4157
protected void writeInternal(TeaFileHandle file, byte[] data, boolean append, int expectedLength) {
58+
if(indexedDB != null) {
59+
indexedDB.writeInternal(file, data, append, expectedLength);
60+
return;
61+
}
62+
4263
// write larger files into memory: up to 16.384kb into local storage (permanent)
4364
int localStorageMax = 16384;
4465
if((data.length >= localStorageMax) || (append && (expectedLength >= localStorageMax))) {
@@ -54,6 +75,10 @@ protected void writeInternal(TeaFileHandle file, byte[] data, boolean append, in
5475

5576
@Override
5677
protected String[] paths(TeaFileHandle file) {
78+
if(indexedDB != null) {
79+
return indexedDB.paths(file);
80+
}
81+
5782
// combine & return the paths of memory & local storage
5883
String[] pathsMemory = memory.paths(file);
5984
String[] pathsLocalStorage = localStorage.paths(file);
@@ -65,6 +90,10 @@ protected String[] paths(TeaFileHandle file) {
6590

6691
@Override
6792
public boolean isDirectory(TeaFileHandle file) {
93+
if(indexedDB != null) {
94+
return indexedDB.isDirectory(file);
95+
}
96+
6897
if(memory.exists(file)) {
6998
return memory.isDirectory(file);
7099
}
@@ -75,16 +104,29 @@ public boolean isDirectory(TeaFileHandle file) {
75104

76105
@Override
77106
public void mkdirs(TeaFileHandle file) {
107+
if(indexedDB != null) {
108+
indexedDB.mkdirs(file);
109+
return;
110+
}
111+
78112
localStorage.mkdirs(file);
79113
}
80114

81115
@Override
82116
public boolean exists(TeaFileHandle file) {
117+
if(indexedDB != null) {
118+
return indexedDB.exists(file);
119+
}
120+
83121
return memory.exists(file) || localStorage.exists(file);
84122
}
85123

86124
@Override
87125
public boolean delete(TeaFileHandle file) {
126+
if(indexedDB != null) {
127+
return indexedDB.delete(file);
128+
}
129+
88130
if(memory.exists(file)) {
89131
return memory.delete(file);
90132
}
@@ -95,6 +137,10 @@ public boolean delete(TeaFileHandle file) {
95137

96138
@Override
97139
public long length(TeaFileHandle file) {
140+
if(indexedDB != null) {
141+
return indexedDB.length(file);
142+
}
143+
98144
if(memory.exists(file)) {
99145
return memory.length(file);
100146
}
@@ -105,6 +151,11 @@ public long length(TeaFileHandle file) {
105151

106152
@Override
107153
public void rename(TeaFileHandle source, TeaFileHandle target) {
154+
if(indexedDB != null) {
155+
indexedDB.rename(source, target);
156+
return;
157+
}
158+
108159
if(memory.exists(source)) {
109160
memory.rename(source, target);
110161
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.github.xpenatan.gdx.backends.teavm.filesystem;
2+
3+
import com.github.xpenatan.gdx.backends.teavm.TeaFileHandle;
4+
import java.io.InputStream;
5+
import org.teavm.jso.indexeddb.IDBDatabase;
6+
import org.teavm.jso.indexeddb.IDBFactory;
7+
import org.teavm.jso.indexeddb.IDBOpenDBRequest;
8+
9+
public class IndexedDBStorage extends FileDB {
10+
11+
private IDBDatabase result = null;
12+
13+
public IndexedDBStorage() {
14+
15+
IDBFactory instance = IDBFactory.getInstance();
16+
17+
IDBOpenDBRequest request = instance.open("TeaVM", 1);
18+
request.setOnSuccess(() -> {
19+
result = request.getResult();
20+
System.out.println("SUCCESS");
21+
});
22+
23+
request.setOnError(() -> {
24+
System.out.println("ERROR");
25+
});
26+
}
27+
28+
@Override
29+
public InputStream read(TeaFileHandle file) {
30+
return null;
31+
}
32+
33+
@Override
34+
protected void writeInternal(TeaFileHandle file, byte[] data, boolean append, int expectedLength) {
35+
36+
}
37+
38+
@Override
39+
protected String[] paths(TeaFileHandle file) {
40+
return new String[0];
41+
}
42+
43+
@Override
44+
public boolean isDirectory(TeaFileHandle file) {
45+
return false;
46+
}
47+
48+
@Override
49+
public void mkdirs(TeaFileHandle file) {
50+
51+
}
52+
53+
@Override
54+
public boolean exists(TeaFileHandle file) {
55+
return false;
56+
}
57+
58+
@Override
59+
public boolean delete(TeaFileHandle file) {
60+
return false;
61+
}
62+
63+
@Override
64+
public long length(TeaFileHandle file) {
65+
return 0;
66+
}
67+
68+
@Override
69+
public void rename(TeaFileHandle source, TeaFileHandle target) {
70+
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.github.xpenatan.gdx.backends.teavm.filesystem.indexeddb;
2+
3+
import com.github.xpenatan.gdx.backends.teavm.gen.Emulate;
4+
import org.teavm.jso.JSBody;
5+
import org.teavm.jso.JSByRef;
6+
import org.teavm.jso.JSObject;
7+
import org.teavm.jso.JSProperty;
8+
import org.teavm.jso.core.JSString;
9+
import org.teavm.jso.indexeddb.IDBCountRequest;
10+
import org.teavm.jso.indexeddb.IDBCursorRequest;
11+
import org.teavm.jso.indexeddb.IDBCursorSource;
12+
import org.teavm.jso.indexeddb.IDBGetRequest;
13+
import org.teavm.jso.indexeddb.IDBIndex;
14+
import org.teavm.jso.indexeddb.IDBKeyRange;
15+
16+
@Emulate(IDBIndex.class)
17+
public abstract class IDBIndexEmu implements JSObject, IDBCursorSource {
18+
@JSProperty
19+
public abstract String getName();
20+
21+
@JSProperty("keyPath")
22+
abstract JSObject getKeyPathImpl();
23+
24+
public final String[] getKeyPath() {
25+
JSObject result = getKeyPathImpl();
26+
if (JSString.isInstance(result)) {
27+
return new String[] { result.<JSString>cast().stringValue() };
28+
} else {
29+
return unwrapStringArray(result);
30+
}
31+
}
32+
33+
@JSBody(params = "obj", script = "return obj;")
34+
private static native String[] unwrapStringArray(JSObject obj);
35+
36+
@JSProperty
37+
public abstract boolean isMultiEntry();
38+
39+
@JSProperty
40+
public abstract boolean isUnique();
41+
42+
public abstract IDBCursorRequest openCursor();
43+
44+
public abstract IDBCursorRequest openCursor(IDBKeyRange range);
45+
46+
public abstract IDBCursorRequest openKeyCursor();
47+
48+
public abstract IDBGetRequest get(JSObject key);
49+
50+
public abstract IDBGetRequest getKey(JSObject key);
51+
52+
public abstract IDBCountRequest count(JSObject key);
53+
54+
public abstract IDBCountRequest count();
55+
}

examples/core/core/src/main/java/com/github/xpenatan/gdx/examples/tests/FilesTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,16 @@ public void create() {
113113

114114
BufferedReader in = null;
115115
try {
116-
in = new BufferedReader(new InputStreamReader(Gdx.files.local("test.txt").read()));
116+
FileHandle testFile = Gdx.files.local("test.txt");
117+
InputStream read = testFile.read();
118+
in = new BufferedReader(new InputStreamReader(read));
117119
if(!in.readLine().equals("test"))
118120
message += "Read result wrong\n";
119121
else
120122
message += "Read local success\n";
121123
} catch(GdxRuntimeException ex) {
122124
message += "Couldn't open localstorage/test.txt\n";
123-
} catch(IOException e) {
125+
} catch(Throwable e) {
124126
message += "Couldn't read localstorage/test.txt\n";
125127
} finally {
126128
StreamUtils.closeQuietly(in);

0 commit comments

Comments
 (0)