flush.go 576 B

123456789101112131415161718192021222324252627282930313233343536
  1. package templ
  2. import (
  3. "context"
  4. "io"
  5. )
  6. // Flush flushes the output buffer after all its child components have been rendered.
  7. func Flush() FlushComponent {
  8. return FlushComponent{}
  9. }
  10. type FlushComponent struct {
  11. }
  12. type flusherError interface {
  13. Flush() error
  14. }
  15. type flusher interface {
  16. Flush()
  17. }
  18. func (f FlushComponent) Render(ctx context.Context, w io.Writer) (err error) {
  19. if err = GetChildren(ctx).Render(ctx, w); err != nil {
  20. return err
  21. }
  22. switch w := w.(type) {
  23. case flusher:
  24. w.Flush()
  25. return nil
  26. case flusherError:
  27. return w.Flush()
  28. }
  29. return nil
  30. }