1 package App::Clifton::Component;
3 # define this up here to avoid uninitialised warnings
5 my ($self, $args) = @_;
6 "${self}: ".join ', ', map "$_ => ".$args->{$_}, keys %$args;
10 use Log::Contextual qw(:log);
13 extends 'Moo::Object', 'IO::Async::Notifier';
15 # we're replacing Notifier's constructor so need to set up its default
16 has children => (is => 'bare', default => sub { [] });
19 my ($self, $args) = @_;
21 "Constructing "._debug_self($self, $args);
23 if (my $parent = $args->{parent_component}) {
24 $parent->add_child($self);
29 my ($self, $class, $args) = @_;
30 if ($class->isa('App::Clifton::Component')) {
31 $class->new(%{$args||{}}, parent_component => $self);
33 my $new = $class->new(%{$args||{}});
34 $self->add_child($new);
39 around _replace_weakself => sub {
40 my ($orig, $self) = (shift, shift);
41 $self->_eval_cb($self->$orig(@_));
44 around _capture_weakself => sub {
45 my ($orig, $self) = (shift, shift);
46 $self->_eval_cb($self->$orig(@_));
50 my ($self, $code) = @_;
51 $self->get_loop->later($self->_eval_cb($code));
55 my ($self, $code) = @_;
59 try { $code->(@args) } catch { log_error { "Exception from ${self}: $_" } }
65 log_debug { "Destroying "._debug_self($self, $self) };