Sha256: 1fe3d365b7d36336968067325840511d3d0be29c0ce0363ba8d20c560ede7350

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

require 'grpc_kit/interceptor_registory'

module GrpcKit
  module Interceptors
    module Client
      class Streaming
        # @param interceptors [Array<GrpcKit::GRPC::ClientInterceptor>]
        def initialize(interceptors)
          @registry = GrpcKit::InterceptorRegistry.new(interceptors)
        end

        # @param metadata [Hash<String,String>]
        def intercept(call, metadata, &block)
          do_intercept(@registry.build, call, metadata, &block)
        end

        private

        def do_intercept(interceptors, call, metadata)
          if interceptors.empty?
            return yield(call, metadata)
          end

          interceptor = interceptors.pop
          invoke(interceptor, call, metadata) do |inter_call, meta|
            do_intercept(interceptors, inter_call, meta) do |c, m|
              yield(c, m)
            end
          end
        end
      end
    end

    module Server
      class Streaming
        # @param interceptors [Array<GrpcKit::GRPC::ServerInterceptor>]
        def initialize(interceptors)
          @registry = GrpcKit::InterceptorRegistry.new(interceptors)
        end

        def intercept(call, &block)
          do_intercept(@registry.build, call, &block)
        end

        private

        def do_intercept(interceptors, call)
          if interceptors.empty?
            return yield(call)
          end

          interceptor = interceptors.pop
          invoke(interceptor, call) do |inter_call|
            do_intercept(interceptors, inter_call) do |c|
              yield(c)
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grpc_kit-0.2.0 lib/grpc_kit/interceptors.rb