Sha256: a14a3bb3511cb41a1236c119642baf045a06611ed3774e4c88f1a1fb39c00006

Contents?: true

Size: 1.57 KB

Versions: 7

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module Expo
  module Push
    class Chunk # rubocop:disable Style/Documentation
      def self.for(notifications) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
        Array(notifications).each_with_object([]) do |notification, chunks|
          # There can be at most n notifications in a chunk. This finds the last chunk,
          # checks how much space is left, and generates a new chunk if necessary.
          chunk = chunks.last || Chunk.new.tap { |c| chunks << c }

          targets = notification.recipients.dup

          while targets.length.positive?
            chunk = Chunk.new.tap { |c| chunks << c } if chunk.remaining <= 0

            # Take at most <remaining> destinations for this notificiation.
            count = [targets.length, chunk.remaining].min
            chunk_targets = targets.slice(0, count)

            # Prepare the notification
            chunk << notification.prepare(chunk_targets)

            # Remove targets from the targets list
            targets = targets.drop(count)
          end
        end
      end

      attr_reader :remaining

      def initialize
        self.notifications = []
        self.remaining = PUSH_NOTIFICATION_CHUNK_LIMIT
      end

      def <<(notification)
        self.remaining -= notification.count
        notifications << notification

        self
      end

      def count
        notifications.sum(&:count)
      end

      def as_json
        notifications.map(&:as_json)
      end

      private

      attr_accessor :notifications
      attr_writer :remaining
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
expo-server-sdk-0.1.6 lib/push/chunk.rb
expo-server-sdk-0.1.5 lib/push/chunk.rb
expo-server-sdk-0.1.4 lib/push/chunk.rb
expo-server-sdk-0.1.3 lib/push/chunk.rb
expo-server-sdk-0.1.2 lib/push/chunk.rb
expo-server-sdk-0.1.1 lib/push/chunk.rb
expo-server-sdk-0.1.0 lib/push/chunk.rb