lib/rails/graphql/subscription/store/memory.rb in rails-graphql-1.0.0.beta vs lib/rails/graphql/subscription/store/memory.rb in rails-graphql-1.0.0.rc1

- old
+ new

@@ -47,24 +47,29 @@ def add(subscription) if has?(subscription.sid) raise ::ArgumentError, +"SID #{subscription.sid} is already taken." end + # Rewrite the scope, to save memory + scope = possible_scopes(subscription.scope)&.first + subscription.instance_variable_set(:@scope, scope) + + # Save to the list and to the index list[subscription.sid] = subscription index_set = subscription_to_index(subscription).reduce(index, &:[]) index_set << subscription.sid subscription.sid end def fetch(*sids) - if sids.none? - nil - elsif sids.one? - list[sids.first] - else - sids.map(&list.method(:[])) + return if sids.none? + + items = sids.map do |item| + instance?(item) ? item : list[item] end + + items.one? ? items.first : items end def remove(item) return unless has?(item) @@ -80,10 +85,14 @@ s_level.delete(path[2]) if a_level.empty? f_level.delete(path[1]) if s_level.empty? index.delete(path[0]) if f_level.empty? end + def update!(item) + (instance?(item) ? item : fetch(item)).update! + end + def has?(item) list.key?(instance?(item) ? item.sid : item) end def search(**xargs, &block) @@ -113,12 +122,12 @@ protected # Turn the request subscription into into the path of the index def subscription_to_index(subscription) [ - hash_for(subscription.field), - possible_scopes(subscription.scope)&.first, - hash_for(subscription.args), + subscription.field.hash, + subscription.scope, + subscription.args.hash, ] end end end end