line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:YANICK'; |
2
|
|
|
|
|
|
|
# ABSTRACT: a MoobX object reacting to observable variable changes |
3
|
|
|
|
|
|
|
$MoobX::Observer::VERSION = '0.1.1'; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use 5.20.0; |
6
|
8
|
|
|
8
|
|
86
|
|
|
8
|
|
|
|
|
20
|
|
7
|
|
|
|
|
|
|
use Scalar::Util 'refaddr'; |
8
|
8
|
|
|
8
|
|
39
|
use Carp; |
|
8
|
|
|
|
|
11
|
|
|
8
|
|
|
|
|
351
|
|
9
|
8
|
|
|
8
|
|
37
|
|
|
8
|
|
|
|
|
11
|
|
|
8
|
|
|
|
|
402
|
|
10
|
|
|
|
|
|
|
use Moose; |
11
|
8
|
|
|
8
|
|
3428
|
|
|
8
|
|
|
|
|
2788370
|
|
|
8
|
|
|
|
|
43
|
|
12
|
|
|
|
|
|
|
use experimental 'signatures'; |
13
|
8
|
|
|
8
|
|
52381
|
|
|
8
|
|
|
|
|
18973
|
|
|
8
|
|
|
|
|
42
|
|
14
|
|
|
|
|
|
|
use overload |
15
|
|
|
|
|
|
|
'""' => sub { $_[0]->value }, |
16
|
23
|
|
|
23
|
|
12737
|
fallback => 1; |
17
|
8
|
|
|
8
|
|
1609
|
|
|
8
|
|
|
|
|
18
|
|
|
8
|
|
|
|
|
77
|
|
18
|
|
|
|
|
|
|
use MooseX::MungeHas 'is_ro'; |
19
|
8
|
|
|
8
|
|
5043
|
|
|
8
|
|
|
|
|
28034
|
|
|
8
|
|
|
|
|
49
|
|
20
|
|
|
|
|
|
|
has value => ( |
21
|
|
|
|
|
|
|
builder => 1, |
22
|
|
|
|
|
|
|
lazy => 1, |
23
|
|
|
|
|
|
|
predicate => 1, |
24
|
|
|
|
|
|
|
clearer => 1, |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
after clear_value => sub($self) { |
28
|
|
|
|
|
|
|
$self->value if $self->autorun; |
29
|
|
|
|
|
|
|
}; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
has generator => ( |
32
|
|
|
|
|
|
|
required => 1, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has autorun => ( |
36
|
|
|
|
|
|
|
is => 'ro', |
37
|
|
|
|
|
|
|
trigger => sub($self,@) { |
38
|
|
|
|
|
|
|
$self->value |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
map { |
44
|
0
|
|
|
0
|
0
|
0
|
$MoobX::graph->get_vertex_attribute( $_, 'info' ); |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
45
|
|
|
|
|
|
|
} $MoobX::graph->successors( refaddr($self) ) |
46
|
0
|
|
|
|
|
0
|
} |
|
0
|
|
|
|
|
0
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $self = shift; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
local $MoobX::WATCHING = 1; |
51
|
36
|
|
|
36
|
|
865
|
local @MoobX::DEPENDENCIES = @MoobX::DEPENDENCIES; |
52
|
|
|
|
|
|
|
|
53
|
36
|
|
|
|
|
61
|
my $new_value = $self->generator->(); |
54
|
36
|
|
|
|
|
80
|
|
55
|
|
|
|
|
|
|
local $Carp::CarpLevel = 2; |
56
|
36
|
|
|
|
|
729
|
carp "MoobX observer doesn't observe anything" |
57
|
|
|
|
|
|
|
unless @MoobX::DEPENDENCIES; |
58
|
36
|
|
|
|
|
8448
|
|
59
|
36
|
100
|
|
|
|
114
|
MoobX::dependencies_for( $self, @MoobX::DEPENDENCIES ); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
return $new_value; |
62
|
36
|
|
|
|
|
840
|
} |
63
|
|
|
|
|
|
|
|
64
|
36
|
|
|
|
|
11750
|
1; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=pod |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=encoding UTF-8 |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 NAME |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
MoobX::Observer - a MoobX object reacting to observable variable changes |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 VERSION |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
version 0.1.1 |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 SYNOPSIS |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
use MoobX; |
82
|
|
|
|
|
|
|
use MoobX::Observer; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
observable( my $foo = 'hi' ); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $obs = MoobX::Observer->new( |
87
|
|
|
|
|
|
|
generator => sub { scalar reverse $foo } |
88
|
|
|
|
|
|
|
); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
$foo = 'hello'; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
say $obs; # prints 'olleh' |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 DESCRIPTION |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
This class implements the observer object used by L<MoobX>. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 OVERLOADED OPERATIONS |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
MoobX::Observer objects are stringified using their C<value> attribute. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 METHODS |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 new |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $obs = MoobX::Observer->new( |
107
|
|
|
|
|
|
|
generator => sub { ... }, |
108
|
|
|
|
|
|
|
autorun => 1, |
109
|
|
|
|
|
|
|
); |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Constructor. Accepts two arguments: |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=over |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=item generator |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Function generating the observer value. Required. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=item autorun |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
If set to true, the observer will eagerly compute its value |
122
|
|
|
|
|
|
|
at creation time, and recompute it as soon as a dependency changes. |
123
|
|
|
|
|
|
|
Defaults to C<false>. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=back |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 value |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Returns the currently cached observer's value. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 AUTHOR |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Yanick Champoux <yanick@cpan.org> |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
This software is copyright (c) 2022, 2017 by Yanick Champoux. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
140
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=cut |