Skip to content

Commit 1177f2d

Browse files
committed
Configuration#subscribe, for Sourced::UI to auto-configure itself from Sourced configuration
Subscribers are triggered on Configuration#setup! ex. Sourced.config.subscribe do |config| # do something depending on Sourced configuration end
1 parent 240ee1a commit 1177f2d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/sourced/configuration.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,20 @@ def initialize
6262
@housekeeping_interval = 3
6363
@housekeeping_heartbeat_interval = 5
6464
@housekeeping_claim_ttl_seconds = 120
65+
@subscribers = []
66+
end
67+
68+
def subscribe(callable = nil, &block)
69+
callable ||= block
70+
@subscribers << callable
71+
self
6572
end
6673

6774
def setup!
6875
return if @setup
6976

7077
@backend.setup!(self) if @backend.respond_to?(:setup!)
78+
@subscribers.each { |s| s.call(self) }
7179
@setup = true
7280
end
7381

spec/configuration_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@
6767
expect(config.executor).to be_a(Sourced::AsyncExecutor)
6868
end
6969

70+
describe 'subscribers' do
71+
it 'triggers subscribers on #setup!' do
72+
executor_class = nil
73+
config.subscribe do |c|
74+
executor_class = c.executor.class
75+
end
76+
config.executor = :thread
77+
config.setup!
78+
expect(executor_class).to eq(Sourced::ThreadExecutor)
79+
end
80+
end
81+
7082
describe '#executor=()' do
7183
specify ':async' do
7284
config.executor = :async

0 commit comments

Comments
 (0)