Skip to content

Commit

Permalink
Revises some signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Feb 16, 2025
1 parent 245fc05 commit cc7d546
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lib/zeitwerk/core_ext/module.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module Zeitwerk::ConstAdded
module Zeitwerk::ConstAdded # :nodoc:
# @sig (Symbol) -> void
def const_added(cname)
if loader = Zeitwerk::Registry::ExplicitNamespaces.__loader_for(self, cname)
namespace = const_get(cname, false)
Expand Down
4 changes: 2 additions & 2 deletions lib/zeitwerk/cref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ def defined?
@mod.const_defined?(@cname, false)
end

# @sig (Object) -> Object
# @sig (top) -> top
def set(value)
@mod.const_set(@cname, value)
end

# @raise [NameError]
# @sig () -> Object
# @sig () -> top
def get
@mod.const_get(@cname, false)
end
Expand Down
10 changes: 5 additions & 5 deletions lib/zeitwerk/cref/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,30 @@ def initialize
@mutex = Mutex.new
end

# @sig (Zeitwerk::Cref, Object) -> Object
# @sig (Zeitwerk::Cref, V) -> V
def []=(cref, value)
@mutex.synchronize do
cnames = (@map[cref.mod] ||= {})
cnames[cref.cname] = value
end
end

# @sig (Zeitwerk::Cref) -> Object?
# @sig (Zeitwerk::Cref) -> top?
def [](cref)
@mutex.synchronize do
@map[cref.mod]&.[](cref.cname)
end
end

# @sig (Zeitwerk::Cref) -> Object?
# @sig (Zeitwerk::Cref) -> top?
def delete(cref)
delete_mod_cname(cref.mod, cref.cname)
end

# Ad-hoc for loader_for, called from const_added. That is a hot path, I prefer
# to not create a cref in every call, since that is global.
#
# @sig (Module, Symbol) -> Object?
# @sig (Module, Symbol) -> top?
def delete_mod_cname(mod, cname)
@mutex.synchronize do
if cnames = @map[mod]
Expand All @@ -77,7 +77,7 @@ def delete_mod_cname(mod, cname)
end
end

# @sig (Object) -> void
# @sig (top) -> void
def delete_by_value(value)
@mutex.synchronize do
@map.delete_if do |mod, cnames|
Expand Down
2 changes: 1 addition & 1 deletion lib/zeitwerk/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ def all_dirs
end
end

# @sig (String, Object, String) -> void
# @sig (String, top, String) -> void
private def run_on_unload_callbacks(cref, value, abspath)
# Order matters. If present, run the most specific one.
on_unload_callbacks[cref.path]&.each { |c| c.call(value, abspath) }
Expand Down
2 changes: 1 addition & 1 deletion lib/zeitwerk/loader/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module Zeitwerk::Loader::Callbacks # :nodoc: all

private

# @sig (String, Object) -> void
# @sig (String, top, String) -> void
def run_on_load_callbacks(cpath, value, abspath)
# Order matters. If present, run the most specific one.
callbacks = reloading_enabled? ? on_load_callbacks[cpath] : on_load_callbacks.delete(cpath)
Expand Down
16 changes: 8 additions & 8 deletions lib/zeitwerk/loader/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ module Zeitwerk::Loader::Config

# User-oriented callbacks to be fired when a constant is loaded.
#
# @sig Hash[String, Array[{ (Object, String) -> void }]]
# Hash[Symbol, Array[{ (String, Object, String) -> void }]]
# @sig Hash[String, Array[{ (top, String) -> void }]]
# Hash[Symbol, Array[{ (String, top, String) -> void }]]
attr_reader :on_load_callbacks
private :on_load_callbacks

# User-oriented callbacks to be fired before constants are removed.
#
# @sig Hash[String, Array[{ (Object, String) -> void }]]
# Hash[Symbol, Array[{ (String, Object, String) -> void }]]
# @sig Hash[String, Array[{ (top, String) -> void }]]
# Hash[Symbol, Array[{ (String, top, String) -> void }]]
attr_reader :on_unload_callbacks
private :on_unload_callbacks

Expand Down Expand Up @@ -247,8 +247,8 @@ def on_setup(&block)
# end
#
# @raise [TypeError]
# @sig (String) { (Object, String) -> void } -> void
# (:ANY) { (String, Object, String) -> void } -> void
# @sig (String) { (top, String) -> void } -> void
# (:ANY) { (String, top, String) -> void } -> void
def on_load(cpath = :ANY, &block)
raise TypeError, "on_load only accepts strings" unless cpath.is_a?(String) || cpath == :ANY

Expand All @@ -272,8 +272,8 @@ def on_load(cpath = :ANY, &block)
# end
#
# @raise [TypeError]
# @sig (String) { (Object) -> void } -> void
# (:ANY) { (String, Object) -> void } -> void
# @sig (String) { (top) -> void } -> void
# (:ANY) { (String, top) -> void } -> void
def on_unload(cpath = :ANY, &block)
raise TypeError, "on_unload only accepts strings" unless cpath.is_a?(String) || cpath == :ANY

Expand Down

0 comments on commit cc7d546

Please sign in to comment.