reloadable<A, R> static method

R Function(A) reloadable<A, R>(
  1. R Function(A) build()
)

build's result, built again onReassemble: the handler itself, for a server whose state lives elsewhere and whose handler is all it needs to make again. What build is handed is kept; make the handler in a named function, since the closure build is keeps its old body.

var handler = FlutterwareServer.reloadable(() => routes(store));
await shelf_io.serve(handler, 'localhost', 8080);

Handler routes(Store store) => const Pipeline()
    .addMiddleware(inspect())
    .addHandler((Router()..get('/orders', store.list)).call);

A build that throws leaves the last one serving. Where nothing reloads the process, it is built once and costs a call.

Implementation

static R Function(A) reloadable<A, R>(R Function(A) Function() build) {
  var current = build();
  onReassemble(() => current = build());
  return (argument) => current(argument);
}