about summary refs log tree commit diff
path: root/lib/chewy/strategy/mastodon.rb
blob: ee8b921865e218726db2c9665328dd3f1ccb18c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# frozen_string_literal: true

module Chewy
  class Strategy
    class Mastodon < Base
      def initialize
        super

        @stash = Hash.new { |hash, key| hash[key] = [] }
      end

      def update(type, objects, _options = {})
        @stash[type].concat(type.root.id ? Array.wrap(objects) : type.adapter.identify(objects)) if Chewy.enabled?
      end

      def leave
        RedisConfiguration.with do |redis|
          redis.pipelined do |pipeline|
            @stash.each do |type, ids|
              pipeline.sadd("chewy:queue:#{type.name}", ids)
            end
          end
        end
      end
    end
  end
end