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..34c3669 100644 --- a/lib/graphql/subscriptions/anycable_subscriptions.rb +++ b/lib/graphql/subscriptions/anycable_subscriptions.rb @@ -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 = {} @@ -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__) 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