writer.flush();
} catch (IOException e) {
throw new MustacheException("Failed to flush writer", e);
}
final Writer originalWriter = writer;
final LatchedWriter latchedWriter = new LatchedWriter(writer);
writer = latchedWriter;
// Scopes must not cross thread boundaries as they
// are thread locally reused
final Object[] newScopes = scopes.clone();
les.execute(new Runnable() {
@Override
public void run() {
try {
Object call = callable.call();
Writer subWriter = handle(originalWriter, call, newScopes);
// Wait for the subwriter to complete
if (subWriter instanceof LatchedWriter) {
((LatchedWriter) subWriter).await();
}
// Tell the replacement writer that we are done
latchedWriter.done();
} catch (Throwable e) {
latchedWriter.failed(e);
}
}
});
}
return writer;