Class: Cordis::Registry

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

Overview

Plugin registry: keyed by the resolved callback — one Runtime per callback, applying the same plugin N times = N fibers under one Runtime.

Defined Under Namespace

Classes: Runtime

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ Registry

Returns a new instance of Registry.



9
10
11
12
13
# File 'lib/cordis/registry.rb', line 9

def initialize(ctx)
  @ctx = ctx
  @store = {}
  @counter = 0
end

Instance Method Details

#next_uidObject



15
# File 'lib/cordis/registry.rb', line 15

def next_uid = (@counter += 1)

#plugin(ctx, plugin, config = nil) ⇒ Object

Entry point for ctx.plugin. Plugin shapes: a callable, { apply:, inject:, name: }, or a Class (instantiated at load time; see Cordis::Service). Returns the fiber synchronously; the actual apply is deferred one tick (must be called inside Sync/Async).



23
24
25
26
27
28
# File 'lib/cordis/registry.rb', line 23

def plugin(ctx, plugin, config = nil)
  key, callback, inject, name = resolve(plugin)
  ctx.fiber.assert_active
  runtime = @store[key] ||= Runtime.new(name, callback, [], key)
  Fiber.new(ctx, config: config, inject: inject, runtime: runtime)
end

#remove(fiber) ⇒ Object



30
31
32
33
34
# File 'lib/cordis/registry.rb', line 30

def remove(fiber)
  runtime = fiber.runtime
  runtime.fibers.reject! { |f| f.equal?(fiber) }
  @store.delete(runtime.key) if runtime.fibers.empty?
end

#runtimesObject



17
# File 'lib/cordis/registry.rb', line 17

def runtimes = @store.values

#sizeObject



16
# File 'lib/cordis/registry.rb', line 16

def size = @store.size