Creating IndexedDB API wrappers

This commit is contained in:
Alexey Andreev 2015-04-03 18:51:03 +04:00
parent 6bf9a728a9
commit 2e4af24c99
20 changed files with 647 additions and 13 deletions

View File

@ -23,6 +23,6 @@ import org.teavm.jso.JSObject;
* @author Alexey Andreev
*/
@JSFunctor
public interface EventListener extends JSObject {
void handleEvent(Event evt);
public interface EventListener<E extends Event> extends JSObject {
void handleEvent(E evt);
}

View File

@ -22,13 +22,13 @@ import org.teavm.jso.JSObject;
* @author Alexey Andreev
*/
public interface EventTarget extends JSObject {
void addEventListener(String type, EventListener listener, boolean useCapture);
void addEventListener(String type, EventListener<?> listener, boolean useCapture);
void addEventListener(String type, EventListener listener);
void addEventListener(String type, EventListener<?> listener);
void removeEventListener(String type, EventListener listener, boolean useCapture);
void removeEventListener(String type, EventListener<?> listener, boolean useCapture);
void removeEventListener(String type, EventListener listener);
void removeEventListener(String type, EventListener<?> listener);
boolean dispatchEvent(Event evt);
}

View File

@ -15,6 +15,7 @@
*/
package org.teavm.dom.html;
import org.teavm.dom.events.Event;
import org.teavm.dom.events.EventListener;
import org.teavm.jso.JSProperty;
@ -24,24 +25,24 @@ import org.teavm.jso.JSProperty;
*/
public interface HTMLBodyElement extends HTMLElement {
@JSProperty("onbeforeunload")
void setOnBeforeUnload(EventListener listener);
void setOnBeforeUnload(EventListener<Event> listener);
@JSProperty("onerror")
void setOnError(EventListener listener);
void setOnError(EventListener<Event> listener);
@JSProperty("onload")
void setOnLoad(EventListener listener);
void setOnLoad(EventListener<Event> listener);
@JSProperty("onmessage")
void setOnMessage(EventListener listener);
void setOnMessage(EventListener<Event> listener);
@JSProperty("onoffline")
void setOnOffline(EventListener listener);
void setOnOffline(EventListener<Event> listener);
@JSProperty("ononline")
void setOnOnline(EventListener listener);
void setOnOnline(EventListener<Event> listener);
@JSProperty("ononunload")
void setOnUnload(EventListener listener);
void setOnUnload(EventListener<Event> listener);
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JSFunctor;
import org.teavm.jso.JSObject;
/**
*
* @author Alexey Andreev
*/
@JSFunctor
public interface EventHandler extends JSObject {
void handleEvent();
}

View File

@ -0,0 +1,27 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface IDBCountRequest extends IDBRequest {
@JSProperty
int getResult();
}

View File

@ -0,0 +1,26 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JSObject;
/**
*
* @author Alexey Andreev
*/
public interface IDBCursor extends JSObject {
}

View File

@ -0,0 +1,27 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface IDBCursorRequest extends IDBRequest {
@JSProperty
IDBCursor getResult();
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.dom.events.EventTarget;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface IDBDatabase extends EventTarget {
String TRANSACTION_READONLY = "readonly";
String TRANSACTION_READWRITE = "readwrite";
String TRANSACTION_VERSIONCHANGE = "versionchange";
@JSProperty
String getName();
@JSProperty
int getVersion();
@JSProperty
String[] getObjectStoreNames();
IDBObjectStore createObjectStore(String name, IDBObjectStoreParameters optionalParameters);
IDBObjectStore createObjectStore(String name);
void deleteObjectStore(String name);
IDBTransaction transaction(String storeName, String transactionMode);
IDBTransaction transaction(String storeName);
IDBTransaction transaction(String[] storeNames, String transactionMode);
IDBTransaction transaction(String[] storeNames);
void close();
@JSProperty("onabort")
void setOnAbort(EventHandler handler);
@JSProperty("onerror")
void setOnError(EventHandler handler);
@JSProperty("onversionchange")
void setOnVersionChange(EventHandler handler);
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public abstract class IDBError implements JSObject {
@JSProperty
public abstract String getName();
}

View File

@ -0,0 +1,48 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JS;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject;
/**
*
* @author Alexey Andreev
*/
public abstract class IDBFactory implements JSObject {
public static boolean isSupported() {
return !JS.isUndefined(getInstanceImpl());
}
public static IDBFactory getInstance() {
IDBFactory factory = getInstanceImpl();
if (JS.isUndefined(factory)) {
throw new IllegalStateException("IndexedDB is not supported in this browser");
}
return factory;
}
@JSBody(params = {}, script = "return window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || " +
"window.msIndexedDB;")
static native IDBFactory getInstanceImpl();
public abstract IDBOpenDBRequest open(String name, int version);
public abstract IDBOpenDBRequest deleteDatabase(String name);
public abstract int cmp(JSObject a, JSObject b);
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface IDBGetRequest extends IDBRequest {
@JSProperty
JSObject getResult();
}

View File

@ -0,0 +1,26 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JSObject;
/**
*
* @author Alexey Andreev
*/
public interface IDBIndex extends JSObject {
}

View File

@ -0,0 +1,73 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.*;
/**
*
* @author Alexey Andreev
*/
public abstract class IDBObjectStore implements JSObject {
@JSProperty
public abstract String getName();
@JSProperty("keyPath")
abstract JSObject getKeyPathImpl();
public final String[] getKeyPath() {
JSObject result = getKeyPathImpl();
if (JS.getType(result) == JSType.STRING) {
return new String[] { JS.unwrapString(result) };
} else {
return JS.unwrapArray((JSStringArray)result);
}
}
@JSProperty
public abstract String[] getIndexNames();
@JSProperty
public abstract boolean isAutoIncrement();
public abstract IDBRequest put(JSObject value, JSObject key);
public abstract IDBRequest put(JSObject value);
public abstract IDBRequest add(JSObject value, JSObject key);
public abstract IDBRequest add(JSObject value);
public abstract IDBRequest delete(JSObject key);
public abstract IDBGetRequest get(JSObject key);
public abstract IDBRequest clear();
public abstract IDBCursorRequest openCursor();
public abstract IDBIndex createIndex(String name, String key);
public abstract IDBIndex createIndex(String name, String[] keys);
public abstract IDBIndex index(String name);
public abstract void deleteIndex(String name);
public abstract IDBCountRequest count();
public abstract IDBCountRequest count(JSObject key);
}

View File

@ -0,0 +1,45 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public abstract class IDBObjectStoreParameters implements JSObject {
@JSBody(params = "", script = "return {};")
public static native IDBObjectStoreParameters create();
public final IDBObjectStoreParameters keyPath(String... keys) {
setKeyPath(keys);
return this;
}
public final IDBObjectStoreParameters autoIncrement(boolean autoIncrement) {
setAutoIncrement(autoIncrement);
return this;
}
@JSProperty
abstract void setKeyPath(String[] keys);
@JSProperty
abstract void setAutoIncrement(boolean autoIncrement);
}

View File

@ -0,0 +1,31 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.dom.events.EventListener;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface IDBOpenDBRequest extends IDBRequest {
@JSProperty
void setOnBlocked(EventHandler handler);
@JSProperty("onupgradeneeded")
void setOnUpgradeNeeded(EventListener<IDBVersionChangeEvent> listener);
}

View File

@ -0,0 +1,46 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.dom.events.EventTarget;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface IDBRequest extends EventTarget {
String STATE_PENDING = "pending";
String STATE_DONE = "done";
@JSProperty
IDBError getError();
@JSProperty
IDBRequestSource getSource();
@JSProperty
IDBTransaction getTransaction();
@JSProperty
String getReadyState();
@JSProperty("onerror")
void setOnError(EventHandler handler);
@JSProperty("onsuccess")
void setOnSuccess(EventHandler handler);
}

View File

@ -0,0 +1,25 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JSObject;
/**
*
* @author Alexey Andreev
*/
public interface IDBRequestSource extends JSObject {
}

View File

@ -0,0 +1,26 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JSObject;
/**
*
* @author Alexey Andreev
*/
public interface IDBTransaction extends JSObject {
}

View File

@ -0,0 +1,31 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.dom.events.Event;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface IDBVersionChangeEvent extends Event {
@JSProperty
int getOldVersion();
@JSProperty
int getNewVersion();
}

View File

@ -15,6 +15,7 @@
*/
package org.teavm.jso;
import java.lang.reflect.Array;
import java.util.Iterator;
import org.teavm.dependency.PluggableDependency;
import org.teavm.javascript.spi.InjectedBy;
@ -347,6 +348,58 @@ public final class JS {
@InjectedBy(JSNativeGenerator.class)
public static native char unwrapCharacter(JSObject obj);
public static <T extends JSObject> T[] unwrapArray(Class<T> type, JSArray<T> array) {
@SuppressWarnings("unchecked")
T[] result = (T[])Array.newInstance(type, array.getLength());
for (int i = 0; i < result.length; ++i) {
result[i] = array.get(i);
}
return result;
}
public static <T extends JSObject> T[][] unwrapArray2(Class<T> type, JSArray<JSArray<T>> array) {
@SuppressWarnings("unchecked")
T[][] result = (T[][])Array.newInstance(Array.newInstance(type, 0).getClass(), array.getLength());
for (int i = 0; i < result.length; ++i) {
result[i] = unwrapArray(type, array.get(i));
}
return result;
}
public static <T extends JSObject> T[][][] unwrapArray3(Class<T> type, JSArray<JSArray<JSArray<T>>> array) {
Class<?> baseType = Array.newInstance(type, 0).getClass();
@SuppressWarnings("unchecked")
T[][][] result = (T[][][])Array.newInstance(Array.newInstance(baseType, 0).getClass(), array.getLength());
for (int i = 0; i < result.length; ++i) {
result[i] = unwrapArray2(type, array.get(i));
}
return result;
}
public static String[] unwrapArray(JSStringArray array) {
String[] result = new String[array.getLength()];
for (int i = 0; i < result.length; ++i) {
result[i] = array.get(i);
}
return result;
}
public static String[][] unwrapArray2(JSArray<JSStringArray> array) {
String[][] result = new String[array.getLength()][];
for (int i = 0; i < result.length; ++i) {
result[i] = unwrapArray(array.get(i));
}
return result;
}
public static String[][][] unwrapArray3(JSArray<JSArray<JSStringArray>> array) {
String[][][] result = new String[array.getLength()][][];
for (int i = 0; i < result.length; ++i) {
result[i] = unwrapArray2(array.get(i));
}
return result;
}
@InjectedBy(JSNativeGenerator.class)
public static native boolean isUndefined(JSObject obj);