Skip to content

Commit

Permalink
Fix watcher for supporting multiple/sequential handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Salceanu committed Jun 7, 2024
1 parent 509da9f commit 06c2c25
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/Generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,9 @@ function write_secrets_file(app_path::String = ".") :: Nothing

open(joinpath(secrets_path, Genie.Secrets.SECRETS_FILE_NAME), "w") do f
write(f, """try
Genie.Secrets.secret_token!("$(Genie.Secrets.secret())")
catch
Genie.Util.isprecompiling() || Genie.Secrets.secret_token!("$(Genie.Secrets.secret())")
catch ex
@error "Failed to generate secrets file: $ex"
end
""")
end
Expand Down
38 changes: 22 additions & 16 deletions src/Watch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ using Logging
using Dates

const WATCHED_FOLDERS = Ref{Vector{String}}(String[])
const WATCHING = Ref{Bool}(false)
const WATCHING = Ref{Vector{UInt}}(UInt[])
const WATCH_HANDLERS = Ref{Dict{String,Vector{Function}}}(Genie.config.watch_handlers)

function collect_watched_files(files::Vector{String} = WATCHED_FOLDERS[], extensions::Vector{String} = Genie.config.watch_extensions) :: Vector{String}
function collect_watched_files(files::Vector{S} = WATCHED_FOLDERS[], extensions::Vector{S} = Genie.config.watch_extensions)::Vector{S} where {S<:AbstractString}
result = String[]

for f in files
Expand All @@ -19,7 +19,7 @@ function collect_watched_files(files::Vector{String} = WATCHED_FOLDERS[], extens
result |> sort |> unique
end

function watchpath(path::Union{String,Vector{String}})
function watchpath(path::Union{S,Vector{S}}) where {S<:AbstractString}
isa(path, Vector) || (path = String[path])
push!(WATCHED_FOLDERS[], path...)
end
Expand All @@ -28,31 +28,37 @@ function delete_handlers(key::Any)
delete!(WATCH_HANDLERS[], string(key))
end

function handlers!(key::String, handlers::Vector{Function})
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)
end

function handlers!(key::S, handlers::Vector{<: Function}) where {S<:AbstractString}
WATCH_HANDLERS[][key] = handlers
end

function handlers() :: Vector{Function}
function handlers() :: Vector{<: Function}
WATCH_HANDLERS[] |> values |> collect |> Iterators.flatten |> collect
end

function watch(files::Vector{String}, extensions::Vector{String} = Genie.config.watch_extensions; handlers::Vector{Function} = handlers()) :: Nothing
push!(WATCHED_FOLDERS[], files...)
WATCHED_FOLDERS[] = unique(WATCHED_FOLDERS[])
last_watched = now()
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 ! WATCHING[]
WATCHING[] = true
if ! (hash(handlers) in WATCHING[])
push!(WATCHING[], hash(handlers))
else
@warn "Handlers already registered"
return
end

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

@show handlers

try
for f in handlers
Base.invokelatest(f)
Expand All @@ -67,16 +73,16 @@ function watch(files::Vector{String}, extensions::Vector{String} = Genie.config.
nothing
end

function watch(handler::Function, files::Union{String,Vector{String}}, extensions::Vector{String} = Genie.config.watch_extensions)
function watch(handler::F, files::Union{A,Vector{A}}, extensions::Vector{A} = Genie.config.watch_extensions) where {F<:Function, A<:AbstractString}
isa(files, Vector) || (files = String[files])
watch(files, extensions; handlers = Function[handler])
end

watch(files::String, extensions::Vector{String} = Genie.config.watch_extensions; handlers::Vector{Function} = handlers()) = watch(String[files], extensions; handlers)
watch(files...; extensions::Vector{String} = Genie.config.watch_extensions, handlers::Vector{Function} = handlers()) = watch(String[files...], extensions; handlers)
watch(files::A, extensions::Vector{A} = Genie.config.watch_extensions; handlers::Vector{F} = handlers()) where {F<:Function, A<:AbstractString} = watch(String[files], extensions; handlers)
watch(files...; extensions::Vector{A} = Genie.config.watch_extensions, handlers::Vector{F} = handlers()) where {F<:Function, A<:AbstractString} = watch(String[files...], extensions; handlers)
watch() = watch(String[])

function unwatch(files::Vector{String}) :: Nothing
function unwatch(files::Vector{A})::Nothing where {A<:AbstractString}
filter!(e -> !(e in files), WATCHED_FOLDERS[])

nothing
Expand Down

2 comments on commit 06c2c25

@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/108475

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.1 -m "<description of version>" 06c2c2542ae511d4e8768b9bd03915290230c03b
git push origin v5.30.1

Please sign in to comment.