Class: Cloudflare::DurableObjectNamespace
- Inherits:
-
Object
- Object
- Cloudflare::DurableObjectNamespace
- Defined in:
- lib/homura/runtime/durable_object.rb
Overview
Wraps a DurableObjectNamespace binding (env.COUNTER). The binding object itself is opaque; we only need three methods from it —idFromName, newUniqueId, get — but we also expose a high-level ‘get_by_name` helper because “get a stub by name” is the most common call site and the unwrapped form requires two steps.
Instance Method Summary collapse
-
#get(id) ⇒ Object
Get a stub for an id (or a DurableObjectId wrapper).
-
#get_by_name(name) ⇒ Object
Convenience: derive id-from-name and return the stub in one call.
-
#id_from_name(name) ⇒ Object
Hash-derived DurableObjectId.
-
#id_from_string(hex) ⇒ Object
Parse a hex id string back into a DurableObjectId.
-
#initialize(js) ⇒ DurableObjectNamespace
constructor
A new instance of DurableObjectNamespace.
-
#new_unique_id ⇒ Object
Random unique DurableObjectId.
Constructor Details
#initialize(js) ⇒ DurableObjectNamespace
Returns a new instance of DurableObjectNamespace.
81 82 83 |
# File 'lib/homura/runtime/durable_object.rb', line 81 def initialize(js) @js = js end |
Instance Method Details
#get(id) ⇒ Object
Get a stub for an id (or a DurableObjectId wrapper).
108 109 110 111 112 |
# File 'lib/homura/runtime/durable_object.rb', line 108 def get(id) js_ns = @js js_id = id.is_a?(DurableObjectId) ? id.js_id : id DurableObjectStub.new(`#{js_ns}.get(#{js_id})`) end |
#get_by_name(name) ⇒ Object
Convenience: derive id-from-name and return the stub in one call.
stub = env_ns.get_by_name('global-counter')
resp = stub.fetch('/inc').__await__
118 119 120 |
# File 'lib/homura/runtime/durable_object.rb', line 118 def get_by_name(name) get(id_from_name(name)) end |
#id_from_name(name) ⇒ Object
Hash-derived DurableObjectId. Two calls with the same ‘name` in the same namespace return equal ids, so `get_by_name(“foo”)` is the idiomatic “single-writer per name” pattern.
88 89 90 91 |
# File 'lib/homura/runtime/durable_object.rb', line 88 def id_from_name(name) js_ns = @js DurableObjectId.new(`#{js_ns}.idFromName(#{name.to_s})`) end |
#id_from_string(hex) ⇒ Object
Parse a hex id string back into a DurableObjectId. Matches the Workers ‘namespace.idFromString(hex)` method.
102 103 104 105 |
# File 'lib/homura/runtime/durable_object.rb', line 102 def id_from_string(hex) js_ns = @js DurableObjectId.new(`#{js_ns}.idFromString(#{hex.to_s})`) end |
#new_unique_id ⇒ Object
Random unique DurableObjectId. Used when the caller wants an ephemeral / request-scoped actor (e.g. one DO per user session).
95 96 97 98 |
# File 'lib/homura/runtime/durable_object.rb', line 95 def new_unique_id js_ns = @js DurableObjectId.new(`#{js_ns}.newUniqueId()`) end |