Interface DurableState
- All Superinterfaces:
org.teavm.jso.JSObject
Handed to every handler. Storage is strongly consistent and private to this instance, and an instance is single-threaded, so a read-modify-write needs no lock.
public class Counter implements DurableObject {
@Override
public Response fetch(Request request, DurableState state, Env env) {
long count = state.getLong("count", 0) + 1;
state.put("count", count);
return Bytebox.response(String.valueOf(count));
}
}
- Since:
- 1.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA Durable Object's storage. -
Method Summary
Modifier and TypeMethodDescriptionvoidacceptWebSocket(WebSocket socket) Takes the server end of a WebSocket into this instance's set.voidacceptWebSocket(WebSocket socket, org.teavm.jso.JSObject tags) Takes the server end into this instance's set, under tags.default longalarmAt()Returns when the alarm is due, in milliseconds since the epoch, or 0 when none is set.default intSends text to every open WebSocket this instance holds.default booleanRemoves a key.default voidCancels the alarm.default voidRemoves everything this instance holds, including the alarm.default TSObjectReads a value.default longReads a number.default StringReads text.default StringReturns this instance's identifier, stable for the life of the object.keys()Returns every key this instance holds.default voidWrites a number.default voidWrites a value.default voidWrites text.default voidsetAlarm(long at) Schedules the alarm handler.sockets()Returns every WebSocket this instance holds.default SQLRuns a query against this instance's own SQLite database.storage()Returns the storage itself, for anything the convenience methods here do not cover.org.teavm.jso.core.JSArrayReader<WebSocket> Methods inherited from interface org.teavm.jso.JSObject
cast
-
Method Details
-
storage
DurableState.Storage storage()Returns the storage itself, for anything the convenience methods here do not cover.- Returns:
- the storage itself, for anything the convenience methods here do not cover
-
get
Reads a value.- Parameters:
key- the key- Returns:
- the value, or
nullwhen absent
-
getLong
Reads a number.- Parameters:
key- the keyfallback- what to answer when the key is absent- Returns:
- the value
-
getString
Reads text.- Parameters:
key- the key- Returns:
- the value, or
nullwhen absent
-
put
Writes a value.- Parameters:
key- the keyvalue- the value
-
put
Writes text.- Parameters:
key- the keyvalue- the value
-
put
Writes a number.Stored as a JavaScript number, so a value past 2^53 loses precision. Write one that has to survive exactly with
String.valueOfinstead.- Parameters:
key- the keyvalue- the value
-
delete
Removes a key.- Parameters:
key- the key- Returns:
- whether it was there
-
keys
Returns every key this instance holds.- Returns:
- every key this instance holds
-
deleteAll
default void deleteAll()Removes everything this instance holds, including the alarm. -
sql
Runs a query against this instance's own SQLite database.Synchronous, unlike D1: the database is local to this instance, so there is no promise to wait on and no fiber suspension. Bind parameters are
?1,?2and so on.state.sql("create table if not exists visits (day text primary key, count integer)"); long count = state.sql("select count from visits where day = ?1", day) .one() .get("count") .asLong();- Parameters:
query- the SQLbindings- the values to bind, in order- Returns:
- what the query answered
-
setAlarm
default void setAlarm(long at) Schedules the alarm handler.One alarm per instance: setting it again moves the time rather than adding a second. The handler is retried on a throw, so it has to be safe to run twice.
- Parameters:
at- when, in milliseconds since the epoch
-
alarmAt
default long alarmAt()Returns when the alarm is due, in milliseconds since the epoch, or 0 when none is set.- Returns:
- when the alarm is due, in milliseconds since the epoch, or 0 when none is set
-
deleteAlarm
default void deleteAlarm()Cancels the alarm. -
acceptWebSocket
Takes the server end of a WebSocket into this instance's set.Accepted this way rather than with the socket's own
accept, because this is what lets the instance hibernate: the connection stays open with no instance in memory, and a message brings one back.- Parameters:
socket- the server end
-
acceptWebSocket
Takes the server end into this instance's set, under tags.- Parameters:
socket- the server endtags- labels to find it by later, at most 10 of at most 256 characters
-
sockets
Returns every WebSocket this instance holds.- Returns:
- every WebSocket this instance holds
-
broadcast
Sends text to every open WebSocket this instance holds.- Parameters:
message- the text- Returns:
- how many it reached
-
webSockets
org.teavm.jso.core.JSArrayReader<WebSocket> webSockets() -
identifier
Returns this instance's identifier, stable for the life of the object.- Returns:
- this instance's identifier, stable for the life of the object
-