Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strongly type SimpleInstrumentor #8421

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions common/lib/dependabot/simple_instrumentor.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
# typed: true
# typed: strong
# frozen_string_literal: true

require "sorbet-runtime"

module Dependabot
module SimpleInstrumentor
class << self
attr_accessor :events, :subscribers
extend T::Sig
extend T::Generic

sig { returns(T.nilable(T::Array[T.proc.params(name: String, params: T::Hash[Symbol, T.untyped]).void])) }
attr_accessor :subscribers

sig { params(block: T.proc.params(name: String, params: T::Hash[Symbol, T.untyped]).void).void }
def subscribe(&block)
@subscribers ||= []
@subscribers ||= T.let(
[],
T.nilable(T::Array[T.proc.params(name: String, params: T::Hash[Symbol, T.untyped]).void])
)
@subscribers << block
end

sig do
type_parameters(:T)
.params(
name: String,
params: T::Hash[Symbol, T.untyped],
block: T.proc.returns(T.type_parameter(:T))
)
.returns(T.nilable(T.type_parameter(:T)))
end
def instrument(name, params = {}, &block)
@subscribers&.each { |s| s.call(name, params) }
yield if block
Expand Down