e6d592c5b81927db49a4d39c70b7dbf850430bbf
[scpubgit/Clifton.git] / lib / App / Clifton / Component.pm
1 package App::Clifton::Component;
2
3 # define this up here to avoid uninitialised warnings
4 sub _debug_self {
5   my ($self, $args) = @_;
6   "${self}: ".join ', ', map "$_ => ".$args->{$_}, keys %$args;
7 }
8
9 use Log::Contextual qw(:log);
10 use Moo;
11
12 extends 'Moo::Object', 'IO::Async::Notifier';
13
14 # we're replacing Notifier's constructor so need to set up its default
15 has children => (is => 'bare', default => sub { [] });
16
17 sub BUILD {
18   my ($self, $args) = @_;
19   log_debug {
20     "Constructing "._debug_self($self, $args);
21   };
22   if (my $parent = $args->{parent_component}) {
23     $parent->add_child($self);
24   }
25 }
26
27 sub _new_child {
28   my ($self, $class, $args) = @_;
29   $class->new(%{$args||{}}, parent_component => $self);
30 }
31
32 sub _schedule {
33   my ($self, $code) = @_;
34   $self->get_loop->later($code);
35 }
36
37 sub DESTROY {
38   my ($self) = @_;
39   log_debug { "Destroying "._debug_self($self, $self) };
40 }
41
42 1;