RFC: Hooks.jl

Sometimes one package wants to do something when an event happens in another package, but they don’t necessarily want to introduce a dependency. I threw together Hooks.jl to make this easy.

For example, say Atom.jl wants to be able to notify other packages when it’s loaded, so it might have:

# Atom.jl
function __init__()
    # some initialization code
    notify(hook"atom-initialized")
end

Then some other package can take some action

function __init__()
    handle(hook"atom-initialized") do
        # this is from Revise.jl
        setup_atom(getfield(Main, :Atom)::Module)
    end
end

Now this initialization will happen regardless of which order Atom and the OtherPkg are loaded in.

Good idea? Terrible idea?

@tim.holy @pfitzseb

7 Likes

It does seem like it could be useful to have a lightweight package for communication between packages. Interesting, I’ll keep it in mind.

1 Like

Packages are no longer automatically loaded into Main anymore. I imagine there’s still some sort of way of getting at the module, but I don’t know what it is. What you have now won’t necessarily work, though.

Base.loaded_modules is your friend. You can iterate and match on UUID.

EDIT: or easier

julia> Main.Markdown
ERROR: UndefVarError: Markdown not defined

julia> Base.root_module(Main, :Markdown)
Markdown
2 Likes