From 5947128f4f0530ac7c8bf2c6698d8b0e9bbe38ce Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Tue, 13 Aug 2024 15:31:39 +0300 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Andrey Novikov --- lib/graphql-anycable.rb | 7 ++++++- lib/graphql/subscriptions/anycable_subscriptions.rb | 2 +- spec/graphql/anycable_spec.rb | 2 +- spec/redis_helper.rb | 2 +- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/graphql-anycable.rb b/lib/graphql-anycable.rb index c892b09..571e195 100644 --- a/lib/graphql-anycable.rb +++ b/lib/graphql-anycable.rb @@ -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 @@ -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 diff --git a/lib/graphql/subscriptions/anycable_subscriptions.rb b/lib/graphql/subscriptions/anycable_subscriptions.rb index 9592fa4..aa1dab7 100644 --- a/lib/graphql/subscriptions/anycable_subscriptions.rb +++ b/lib/graphql/subscriptions/anycable_subscriptions.rb @@ -194,7 +194,7 @@ def delete_channel_subscriptions(channel) private - def delete_subscription(redis, subscription_id) + def delete_subscription(subscription_id, redis: with_redis { |conn| conn })) events = redis.hget(redis_key(SUBSCRIPTION_PREFIX) + subscription_id, :events) events = events ? JSON.parse(events) : {} fingerprint_subscriptions = {} diff --git a/spec/graphql/anycable_spec.rb b/spec/graphql/anycable_spec.rb index 9f54ec4..e6c7862 100644 --- a/spec/graphql/anycable_spec.rb +++ b/spec/graphql/anycable_spec.rb @@ -122,7 +122,7 @@ ) end - let(:redis) { AnycableSchema.subscriptions.with_redis { _1 } } + let(:redis) { $redis } subject do AnycableSchema.subscriptions.delete_channel_subscriptions(channel) diff --git a/spec/redis_helper.rb b/spec/redis_helper.rb index b2abd8f..a42aff6 100644 --- a/spec/redis_helper.rb +++ b/spec/redis_helper.rb @@ -10,6 +10,6 @@ RSpec.configure do |config| config.before do - GraphQL::AnyCable.with_redis { _1.flushdb } + GraphQL::AnyCable.with_redis(&:flushdb) end end