Skip to content

Commit

Permalink
Added shutdown params + small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tisztamo committed Jun 14, 2020
1 parent 64f5c49 commit a1a6e58
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Plugins"
uuid = "231a7ae5-8495-40ed-9891-ff920a14b9bd"
authors = ["Krisztián Schaffer"]
version = "0.1.1"
version = "0.1.2"

[compat]
julia = "1.3"
Expand Down
14 changes: 8 additions & 6 deletions src/Plugins.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export PluginStack, Plugin, hooks, symbol, setup!, shutdown!
abstract type Plugin end

symbol(plugin::Plugin) = :nothing
setup!(plugin::Plugin, scheduler) = nothing
shutdown!(plugin::Plugin) = nothing
setup!(plugin::Plugin, x...) = nothing
shutdown!(plugin::Plugin, x...) = nothing

struct PluginStack
plugins::Array{Plugin}
Expand All @@ -27,25 +27,27 @@ struct HookList{TNext, THandler, TPlugin, TFramework}
framework::TFramework
end

HookListTerminal = HookList{Nothing, Nothing, Nothing, Nothing}

@inline function (hook::HookList)(params...)::Bool
if hook.handler(hook.plugin, hook.framework, params...) !== false
return hook.next(params...)
end
return false
end

(hook::HookList{Nothing, T, Nothing, Nothing})(::Vararg{Any}) where T = true
(hook::HookListTerminal)(::Vararg{Any}) = true

length(l::HookList{Nothing, T, Nothing, Nothing}) where T = 0
length(l::HookListTerminal) = 0
length(l::HookList) = 1 + length(l.next)

iterate(l::HookList) = (l, l.next)
iterate(l::HookList, state) = isnothing(state) ? nothing : (state, state.next)
iterate(l::HookList, state::HookList{Nothing, T, Nothing, Nothing}) where T = nothing
iterate(l::HookList, state::HookListTerminal) = nothing

function hooks(plugins::Array{TPlugins}, handler::THandler, framework::TFramework) where {TFramework, THandler, TPlugins}
if length(plugins) == 0
return HookList(nothing, nothing, nothing, nothing)
return HookListTerminal(nothing, nothing, nothing, nothing)
end
plugin = plugins[1]
if length(methods(handler, (typeof(plugin), TFramework, Vararg{Any}))) > 0
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Plugins
import Plugins.symbol, Plugins.setup!
import Plugins.symbol, Plugins.setup!, Plugins.setup!
using Test

struct Framework
Expand Down Expand Up @@ -189,7 +189,7 @@ end
@test setup!(app.plugins, app).allok == true
@test plugin.setupcalledwith === app

shutdown!(app.plugins, app)
@test shutdown!(app.plugins, app).allok == true
@test plugin.shutdowncalledwith === app
@test shutdown!(app.plugins, 42).allok === false
@test plugin.shutdowncalledwith === 42
Expand Down

2 comments on commit a1a6e58

@tisztamo
Copy link
Owner Author

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/16377

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 v0.1.2 -m "<description of version>" a1a6e583076aaf83ac8eb049c7892f3597057911
git push origin v0.1.2

Please sign in to comment.