Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Andrey Novikov <[email protected]>
  • Loading branch information
palkan and Envek committed Aug 13, 2024
1 parent b2d23f2 commit 835a31f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
7 changes: 6 additions & 1 deletion lib/graphql-anycable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def stats(**opts)
Stats.new(**opts).collect
end

def redis
warn "Usage of `GraphQL::AnyCable.redis` is deprecated. Instead of `GraphQL::AnyCable.redis.whatever` use `GraphQL::AnyCable.with_redis { |redis| redis.whatever }`"
@redis ||= with_redis { |conn| conn }
end

def redis=(connector)
@redis_connector = if connector.is_a?(::Proc)
connector
Expand All @@ -30,7 +35,7 @@ def redis=(connector)

def with_redis(&block)
@redis_connector || default_redis_connector
@redis_connector.call { |conn| block.call(conn) }
@redis_connector.call(&block)
end

def config
Expand Down
8 changes: 4 additions & 4 deletions lib/graphql/subscriptions/anycable_subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,13 @@ def delete_channel_subscriptions(channel)

with_redis do |redis|
redis.smembers(redis_key(CHANNEL_PREFIX) + channel_id).each do |subscription_id|
delete_subscription(redis, subscription_id)
delete_subscription(subscription_id, redis: redis)
end
redis.del(redis_key(CHANNEL_PREFIX) + channel_id)
end
end

private

def delete_subscription(redis, subscription_id)
def delete_subscription(subscription_id, redis: AnyCable.redis)
events = redis.hget(redis_key(SUBSCRIPTION_PREFIX) + subscription_id, :events)
events = events ? JSON.parse(events) : {}
fingerprint_subscriptions = {}
Expand All @@ -215,6 +213,8 @@ def delete_subscription(redis, subscription_id)
end
end

private

def read_subscription_id(channel)
return channel.instance_variable_get(:@__sid__) if channel.instance_variable_defined?(:@__sid__)

Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/anycable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
)
end

let(:redis) { AnycableSchema.subscriptions.with_redis { _1 } }
let(:redis) { $redis }

subject do
AnycableSchema.subscriptions.delete_channel_subscriptions(channel)
Expand Down
2 changes: 1 addition & 1 deletion spec/redis_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

RSpec.configure do |config|
config.before do
GraphQL::AnyCable.with_redis { _1.flushdb }
GraphQL::AnyCable.with_redis(&:flushdb)
end
end

0 comments on commit 835a31f

Please sign in to comment.