From 5ff8588b3b62574582033435cc4627e10f399939 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Wed, 16 Aug 2023 08:57:32 -0400 Subject: [PATCH] store the document only once when several field keys are supplied --- .../phase/subscription/subscribe_self.ex | 3 +-- lib/absinthe/subscription.ex | 16 +++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/absinthe/phase/subscription/subscribe_self.ex b/lib/absinthe/phase/subscription/subscribe_self.ex index ffb86860a6..3a4a97ec80 100644 --- a/lib/absinthe/phase/subscription/subscribe_self.ex +++ b/lib/absinthe/phase/subscription/subscribe_self.ex @@ -25,8 +25,7 @@ defmodule Absinthe.Phase.Subscription.SubscribeSelf do field_keys = get_field_keys(field, config) subscription_id = get_subscription_id(config, blueprint, options) - for field_key <- field_keys, - do: Absinthe.Subscription.subscribe(pubsub, field_key, subscription_id, blueprint) + Absinthe.Subscription.subscribe(pubsub, field_keys, subscription_id, blueprint) {:replace, blueprint, [ diff --git a/lib/absinthe/subscription.ex b/lib/absinthe/subscription.ex index 60058eb455..757d7c47be 100644 --- a/lib/absinthe/subscription.ex +++ b/lib/absinthe/subscription.ex @@ -140,7 +140,9 @@ defmodule Absinthe.Subscription do defp fetch_fields(_, _), do: [] @doc false - def subscribe(pubsub, field_key, doc_id, doc) do + def subscribe(pubsub, field_keys, doc_id, doc) do + field_keys = List.wrap(field_keys) + registry = pubsub |> registry_name doc_value = %{ @@ -148,8 +150,12 @@ defmodule Absinthe.Subscription do source: doc.source } - pdict_add_field(doc_id, field_key) - {:ok, _} = Registry.register(registry, field_key, doc_id) + pdict_add_fields(doc_id, field_keys) + + for field_key <- field_keys do + {:ok, _} = Registry.register(registry, field_key, doc_id) + end + {:ok, _} = Registry.register(registry, doc_id, doc_value) end @@ -157,8 +163,8 @@ defmodule Absinthe.Subscription do Process.get({__MODULE__, doc_id}, []) end - defp pdict_add_field(doc_id, field) do - Process.put({__MODULE__, doc_id}, [field | pdict_fields(doc_id)]) + defp pdict_add_fields(doc_id, field_keys) do + Process.put({__MODULE__, doc_id}, field_keys ++ pdict_fields(doc_id)) end defp pdict_delete_fields(doc_id) do