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