wagner-core
Wagner is primarily designed to be a more elegant take on orchestrator, hence the name. If you've used orchestrator for web apps and found it cumbersome, Wagner is for you.
API
As a dependency injector
Wagner includes a basic dependency injector that provides an API similar to AngularJS 1.x's dependency injector.
It allows you to execute async tasks based on parameter names
You register 'services' with Wagner using the factory()
function.
Services have a unique name - any function you pass through factory()
or invoke()
can list services in its parameter list.
wagner; wagner; var result = wagner; assert;
It allows you to use locals
A local is a value specific to a particular execution of
invoke()
. You can use locals like any other service.
wagner; wagner;
It only executes the factory function once
Service functions are only executed once, the value is cached for
all future calls to invoke()
.
var count = 0; wagner; assert; wagner; wagner; assert;
.get()
a dependency
It allows you to .constant(a, b)
is a convenient shorthand for
.factory(a, function() { return b; }
wagner; wagner; assert;
.constant()
function
It has a You can also use .get()
to explicitly get a dependency.
wagner; wagner;
As a way to reduce error-handling boilerplate
If you're a NodeJS developer, you've probably gotten sick of writing the following code:
{ if error return ; }
The wagner.safe()
function helps you make that cleaner.
It wraps callbacks to bubble up errors
wagner.safe()
returns an event emitter that has a try()
function.
Just wrap your callbacks in a try()
and all async errors get deferred
to your event emitter. Like domains, but with less suck.
var safe = wagner; var { ; }; ; safe;
It catches exceptions too
The try()
function also wraps your callbacks in a try/catch and emits.
any exceptions. Never again will a
TypeError: Cannot read property 'value' of undefined
in your callback crash your server.
var safe = wagner; var { ; }; ; safe;
As an async framework
Wagner also includes the ability to execute async tasks in a
dependency-injection-like way. Wagner has 2 functions, invokeAsync()
and task()
, that enable you to write neat modular async code.
invokeAsync()
It can execute async tasks using The task()
and invokeAsync()
are roughly analogous to factory
and invoke()
. There are 3 differences:
- The function you pass to
task()
takes a callback, which it uses to pass a value to dependent tasks. - The function you pass to
invokeAsync()
takes an error, which contains the first error that happened when executing the specified tasks. - Tasks are re-executed every time you call
invokeAsync()
, whereas services are cached forever.
wagner; wagner;
invokeAsync()
It re-executes tasks on subsequent calls to var called = 0; wagner; wagner;
It executes tasks with maximum parallelization
Tasks are executed at most once per call to invokeAsync()
, and tasks
are executed with maximum parallelization. That is, as soon as all a
tasks dependencies are ready, the task executes.
var executed = {}; wagner; wagner; wagner; wagner;