Class: Ruact::ServerFunctions::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/ruact/server_functions/registry.rb

Overview

In-memory storage for server-function entries. One instance backs ‘Ruact.action_registry`; another backs `Ruact.query_registry` — kept separate so the JSON snapshot can emit a `kind` field per entry without the call sites having to thread an extra parameter through. Cross-registry JS-identifier collisions are detected by Snapshot at snapshot time (a single registry only sees its own entries).

Thread-safety: not thread-safe by design. Registration happens at controller-class load time (‘config.to_prepare` in dev, eager-load in production), single-threaded. Reads from #entries return a frozen snapshot of the internal hash so concurrent readers cannot observe a partial registration.

Constant Summary collapse

ALLOWED_KINDS =

The only kinds the codegen knows how to emit. Story 8.1 owns ‘:action`, Story 9.1 owns `:query`. Any other value is rejected at registration time — silent acceptance would otherwise let an unknown kind fall through and be emitted as an action signature.

%i[action query].freeze

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



24
25
26
# File 'lib/ruact/server_functions/registry.rb', line 24

def initialize
  @entries = {}
end

Instance Method Details

#clear!self

Wipes the registry. Used by ‘config.to_prepare` (between dev reloads) and by tests that need a clean slate.

Returns:

  • (self)


69
70
71
72
# File 'lib/ruact/server_functions/registry.rb', line 69

def clear!
  @entries.clear
  self
end

#empty?Boolean

Returns whether the registry has no entries.

Returns:

  • (Boolean)

    whether the registry has no entries.



80
81
82
# File 'lib/ruact/server_functions/registry.rb', line 80

def empty?
  @entries.empty?
end

#entriesHash{Symbol => Ruact::ServerFunctions::RegistryEntry}

Returns frozen snapshot of the current entries, ordered by insertion.

Returns:



61
62
63
# File 'lib/ruact/server_functions/registry.rb', line 61

def entries
  @entries.dup.freeze
end

#register(symbol, kind:, controller: nil) { ... } ⇒ Ruact::ServerFunctions::RegistryEntry

Adds symbol to the registry.

Parameters:

  • symbol (Symbol)

    the Ruby identifier (snake_case).

  • kind (Symbol)

    ‘:action` or `:query`. Other values raise.

  • controller (Class, nil) (defaults to: nil)

    the controller class registering the function. Used in collision-error messages.

Yields:

  • the implementation body; stored verbatim for Story 8.1 / 9.1 to invoke. May be nil for Story 8.0a’s bootstrap (registries are empty until 8.1 and 9.1 land).

Returns:

Raises:

  • (Ruact::ConfigurationError)

    when symbol fails the naming-bridge rule, when kind is not in ALLOWED_KINDS, or when a different Ruby symbol already maps to the same JS identifier in THIS registry. Cross- registry collisions (one action + one query sharing a JS identifier) are detected later by Snapshot.functions_payload.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruact/server_functions/registry.rb', line 43

def register(symbol, kind:, controller: nil, &block)
  validate_kind!(symbol, kind, controller)
  js_identifier = translate_symbol(symbol, controller)
  detect_collision!(symbol, js_identifier, controller)

  entry = RegistryEntry.new(
    ruby_symbol: symbol,
    js_identifier: js_identifier,
    kind: kind,
    controller: controller,
    block: block
  )
  @entries[symbol] = entry
  entry
end

#sizeInteger

Returns number of registered entries.

Returns:

  • (Integer)

    number of registered entries.



75
76
77
# File 'lib/ruact/server_functions/registry.rb', line 75

def size
  @entries.size
end