Skip to content

Commit

Permalink
Watchers cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Salceanu committed Jun 10, 2024
1 parent 1cca8ff commit 501456d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
name = "Genie"
uuid = "c43c736e-a2d1-11e8-161f-af95117fbd1e"
authors = ["Adrian Salceanu <[email protected]> and the amazing Genie contributors ♥️"]
version = "5.30.2"
version = "5.30.3"

[deps]
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
Expand Down
4 changes: 3 additions & 1 deletion src/Util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 20,9 @@ end
Recursively walks dir and `produce`s non directories. If `only_files`, directories will be skipped. If `only_dirs`, files will be skipped.
"""
function walk_dir(dir, paths = String[];
only_extensions = ["jl"], only_files = true, only_dirs = false,
only_extensions = ["jl"],
only_files = true,
only_dirs = false,
exceptions = Genie.config.watch_exceptions,
autoload_ignorefile = Genie.config.autoload_ignore_file) :: Vector{String}
f = readdir(dir)
Expand Down
20 changes: 12 additions & 8 deletions src/Watch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 22,7 @@ end
function watchpath(path::Union{S,Vector{S}}) where {S<:AbstractString}
isa(path, Vector) || (path = String[path])
push!(WATCHED_FOLDERS[], path...)
unique!(WATCHED_FOLDERS[])
end

function delete_handlers(key::Any)
Expand All @@ -31,6 32,7 @@ end
function add_handler!(key::S, handler::F) where {F<:Function, S<:AbstractString}
haskey(WATCH_HANDLERS[], key) || (WATCH_HANDLERS[][key] = Function[])
push!(WATCH_HANDLERS[][key], handler)
unique!(WATCH_HANDLERS[][key])
end

function handlers!(key::S, handlers::Vector{<: Function}) where {S<:AbstractString}
Expand All @@ -41,24 43,26 @@ function handlers() :: Vector{<: Function}
WATCH_HANDLERS[] |> values |> collect |> Iterators.flatten |> collect
end

function watch(files::Vector{<: AbstractString}, extensions::Vector{<: AbstractString} = Genie.config.watch_extensions; handlers::Vector{<: Function} = handlers()) :: Nothing
function watch(files::Vector{<: AbstractString},
extensions::Vector{<: AbstractString} = Genie.config.watch_extensions;
handlers::Vector{<: Function} = handlers()) :: Nothing
last_watched = now() - Millisecond(Genie.config.watch_frequency) # to trigger the first watch

Genie.Configuration.isdev() && Revise.revise()

if ! (hash(handlers) in WATCHING[])
push!(WATCHING[], hash(handlers))
else
@warn "Handlers already registered"
return
end
# if ! (hash(handlers) in WATCHING[])
# push!(WATCHING[], hash(handlers))
# else
# Genie.Configuration.isdev() && @warn("Handlers already registered")
# return
# end

entr(collect_watched_files(WATCHED_FOLDERS[], extensions); all = true) do
now() - last_watched > Millisecond(Genie.config.watch_frequency) || return
last_watched = now()

try
for f in handlers
for f in unique!(handlers)
Base.invokelatest(f)
end
catch ex
Expand Down

2 comments on commit 501456d

@essenciary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/108669

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v5.30.3 -m "<description of version>" 501456d1f9238799f72e6fd238ff3ad87b7d4052
git push origin v5.30.3

Please sign in to comment.