Interface DurableObject

All Known Subinterfaces:
AlarmObject, SocketObject

public interface DurableObject
A Durable Object written in Java.

Name the class with durableObjects(...) and the build writes the JavaScript class the runtime instantiates, the binding, and the migration. One instance of this class exists per Durable Object instance, so a field on it is that object's memory: strongly consistent, single-threaded, and gone when the instance is evicted.

 public class Counter implements DurableObject {
 	private long cached = -1;

 	@Override
 	public Response fetch(Request request, DurableState state, Env env) {
 		if (cached < 0) cached = state.getLong("count", 0);
 		state.put("count", ++cached);
 		return Bytebox.response(String.valueOf(cached));
 	}
 }

A field is a cache, not the record. An instance can be evicted between requests, so anything that has to survive goes through DurableState.

Implement SocketObject as well to take WebSockets, and AlarmObject to take alarms. Which of the three a class implements decides what the generated JavaScript class exposes, so an object that handles no alarm has no alarm handler at all.

Since:
1.0.0
  • Method Summary

    Modifier and Type
    Method
    Description
    default Response
    fetch(Request request, DurableState state, Env env)
    Answers a request routed to this instance.
  • Method Details

    • fetch

      default Response fetch(Request request, DurableState state, Env env)
      Answers a request routed to this instance.
      Parameters:
      request - the request
      state - this instance's storage, alarm and sockets
      env - the bindings
      Returns:
      the response