Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure callback for “Macro apps” #1073

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,20 @@ unimplemented are marked as "optional".

— Method **myapp:new** *arg*

*Required*. Create an instance of the app with a given argument *arg*.
`Myapp:new` must return an instance of the app. The handling of *arg* is
up to the app but it is encouraged to use `core.config`'s `parse_app_arg`
to parse *arg*.
*Required*. Create an instance of *myapp* with a given argument *arg*. The
`new` method must return an instance of *myapp*. The handling of *arg* is up to
the app but it is encouraged to use `core.config`'s `parse_app_arg` to parse
*arg*.


— Method **myapp:configure** *configuration*, *name*, *arg*

*Optional*. If this method is defined the `new` method is ignored and not
required, and when *myapp* is configured using `config.app` this method is
called instead of instantiating *myapp* using `new`. The `configure` method is
called with the *configuration*, *name*, and *arg* given to `config.app`, and
is intended to be used to add arbitrary apps and links to *configuration* using
`config.app` and `config.link`.


— Field **myapp.input**
Expand Down
7 changes: 7 additions & 0 deletions src/core/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -526,4 +526,11 @@ function selftest ()
assert(app_table.app3 == orig_app3) -- should be the same
main({duration = 4, report = {showapps = true}})
assert(app_table.app3 ~= orig_app3) -- should be restarted
local c_macro, MacroApp = config.new(), {}
local args, got = {configuration=c_macro, name="macroTest", arg=42}, {}
function MacroApp:configure (c, name, arg)
got.configuration, got.name, got.arg = c, name, arg
end
config.app(c_macro, args.name, MacroApp, args.arg)
assert(lib.equal(args, got), "configure callback broken")
end
6 changes: 5 additions & 1 deletion src/core/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ function app (config, name, class, arg)
if status then arg = result
else error("failed to configure '"..name.."': "..result) end
end
config.apps[name] = { class = class, arg = arg}
if class.configure then
class:configure(config, name, arg)
else
config.apps[name] = { class = class, arg = arg}
end
end

-- API: Add a link to the configuration.
Expand Down