tags: api/syscall references:
The Service API exposes a simple service registry leveraged by various parts of SilverBullet. See Service.
service.define(spec)
Defines a service that can be discovered by selector.
Parameters:
spec (table) — Selector, match rule, and run callback.Example:
service.define { selector = "greeter", match = {}, run = function(name) return "Hello " .. name end }
service.discover(selector, data)
Discovers matching services sorted by descending priority.
Parameters:
selector (string) — Service selector.data — Value passed to match callbacks.Returns:
table — Matching service descriptors.service.invoke(match, data)
Invokes a previously discovered service match.
Parameters:
match (table) — Service match returned by service.discover.data — Value passed to the service.Returns:
service.invokeBestMatch(selector, data)
Discovers and invokes the highest-priority matching service.
Parameters:
selector (string) — Service selector.data — Value used for matching and invocation.Returns:
Example:
local greeting = service.invokeBestMatch("greeter", "Pete")
Services are built on top of Event|Events. When a service is defined, it registers two event listeners:
discover:<<selector>> for service discoveryservice:<<guid>> for invocationDiscovery broadcasts on the event bus and collects all matches, sorted by priority. Invocation calls the specific service's run callback.
service.define {
selector = "greeter-service",
match = {},
run = function(name)
return "Hello " .. name
end
}
service.define {
selector = "greeter-service",
match = function(name)
if name == "Pete" then
return {priority=10}
else
return nil
end
end,
run = function(name)
return "Hello Pete, so happy to see you!"
end
}
To invoke: ${service.invokeBestMatch("greeter-service", "Pete")} and ${service.invokeBestMatch("greeter-service", "Hank")}