<commit_before>module Switches
  module Backends
    class Memory < Backend
      def initialize(uri, instance)
        @instance = instance
        $switches_data ||= {}
        $switches_listeners ||= Set.new
      end

      def set(item)
        $switches_data[item.key] = item.to_json
      end

      def get(item)
        if data = $switches_data[item.key]
          parse(data)
        end
      end

      def listen
        $switches_listeners.add(@instance)
      end

      def notify(update)
        $switches_listeners.each do |listener|
          listener.notified(update)
        end
      end

      def clear
        $switches_data.clear
        $switches_listeners.clear
      end
    end
  end
end<commit_msg>More closely model listen/notify in memory backend<commit_after>module Switches
  module Backends
    class Memory < Backend
      class Bus
        def initialize
          @listeners = []
        end

        def subscribe(&block)
          @listeners << block
        end

        def publish(data)
          @listeners.each do |listener|
            listener.call(data)
          end
        end

        def clear
          @listeners.clear
        end
      end

      def self.bus
        @bus ||= Bus.new
      end

      def self.data
        @data ||= {}
      end

      def self.clear
        bus.clear
        data.clear
      end

      def initialize(uri, instance)
        @instance = instance
      end

      def set(item)
        data[item.key] = item.to_json
      end

      def get(item)
        if json = data[item.key]
          parse(json)
        end
      end

      def listen
        bus.subscribe do |update|
          process(update)
        end
      end

      def notify(update)
        bus.publish(update.to_json)
      end

      def clear
        self.class.clear
      end

      private

      def bus
        self.class.bus
      end

      def data
        self.class.data
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.06802030456852792, 'fraction_numerical': 0.0, 'mean_word_length': 1.3861985472154963, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6695732', 'n_tokens_mistral': 578, 'n_tokens_neox': 556, 'n_words': 146}