annotate lib/IMPL/Object/EventSource.pm @ 408:5c80e33f1218 ref20150831

added 'coarsen' function
author cin
date Mon, 07 Sep 2015 01:35:25 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Object::EventSource;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 require IMPL::Exception;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use IMPL::Class::Property;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 sub CreateEvent {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 my ($class,$event) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 die new IMPL::Exception('A name is required for the event') unless $event;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 (my $fullEventName = "$class$event") =~ s/:://g;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 my $globalEventTable = new IMPL::Object::EventSource::EventTable($fullEventName);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 my $propEventTable = $event.'Table';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 public CreateProperty($class,$propEventTable,prop_all);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 public CreateProperty($class,$event,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 get => sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 my $this = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 if (not defined wantarray and caller(1) eq $class) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 (ref $this ? $this->$propEventTable() || $globalEventTable : $globalEventTable)->Invoke($this);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 if (ref $this) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 if (my $table = $this->$propEventTable()) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 return $table;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 $table = new IMPL::Object::EventSource::EventTable($fullEventName,$globalEventTable);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 $this->$propEventTable($table);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 return $table;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 return $globalEventTable;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 set => sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 (ref $_[0] ? $_[0]->$propEventTable() || $globalEventTable : $globalEventTable)->Invoke(@_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 sub CreateStaticEvent {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 my ($class,$event) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 die new IMPL::Exception('A name is required for the event') unless $event;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 (my $fullEventName = "$class$event") =~ s/:://g;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 my $globalEventTable = new IMPL::Object::EventSource::EventTable($fullEventName);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 no strict 'refs';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 *{"${class}::$event"} = sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 if (not @_) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 if (not defined wantarray and caller(1) eq $class) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 $globalEventTable->Invoke($class);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 return $globalEventTable;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 $globalEventTable->Invoke($class,@_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 package IMPL::Object::EventSource::EventTable;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 use parent qw(IMPL::Object);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 use IMPL::Class::Property;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 use Scalar::Util qw(weaken);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 use overload
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 '+=' => \&opSubscribe,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 'fallback' => 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 public _direct property Name => prop_get;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 public _direct property Handlers => { get => \&get_handlers };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 private _direct property Next => prop_all;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 private _direct property NextId => prop_all;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 my $this = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 $this->{$Handlers} = {};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 $this->{$Name} = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 $this->{$Next} = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 $this->{$NextId} = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 sub get_handlers {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 my $this = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 return values %{$this->{$Handlers}};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 sub Invoke {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 my $this = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 my $tmp;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 $tmp = $_ and local($_) or &$tmp(@_) foreach values %{$this->{$Handlers}};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 $this->{$Next}->Invoke(@_) if $this->{$Next};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 sub Subscribe {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 my ($this,$consumer,$nameHandler) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 my $id = $this->{$NextId} ++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 if (ref $consumer eq 'CODE') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 $this->{$Handlers}{$id} = $consumer;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 $nameHandler ||= $this->Name or die new IMPL::Exception('The name for the event handler method must be specified');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 my $method = $consumer->can($nameHandler) or die new IMPL::Exception('Can\'t find the event handler method',$nameHandler,$consumer);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 weaken($consumer) if ref $consumer;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 $this->{$Handlers}{$id} = sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 unshift @_, $consumer;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 $consumer ? goto &$method : delete $this->{$Handlers}{$id};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 return $id;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 sub Remove {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 my ($this,$id) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 return delete $this->{$Handlers}{$id};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 package Foo;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 use parent qw(IMPL::Object IMPL::Object::EventSource);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 # declare events
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 __PACKAGE__->CreateEvent('OnUpdate');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 __PACKAGE__->CreateStaticEvent('OnNewObject');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 my $this = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 // rise static event
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 $this->OnNewObject();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 sub Update {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 my ($this,$val) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 // rise object event
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 $this->OnUpdate($val);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 package Bar;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 // subscribe static event
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 Foo->OnNewObject->Subscribe(sub { warn "New $_[0] created" } );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 sub LookForFoo {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 my ($this,$foo) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164 // subscribe object event
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165 $foo->OnUpdate->Subscribe($this,'OnFooUpdate');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168 // event handler
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169 sub OnFooUpdate {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170 my ($this,$sender,$value) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174 Позволяет объявлять и инициировать события. События делятся на статические и
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175 локальные. Статические события объявляются для класса и при возникновении
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176 данного события вызываются всегда все подписчики. Статические события могут быть
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177 вызваны как для класса, так и для объекта, что приведет к одинаковым результатам.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179 Локальные события состоят из статической (как статические события) и локальной
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180 части. Если подписываться на события класса, то обработчики будут вызываться при
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181 любых вариантах инициации данного события (как у статических событий). При
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182 подписке на события объекта, обработчик будет вызван только при возникновении
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 событий у данного объекта.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185 =head1 METHODS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
186 =level 4
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
187 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
188
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
189 =head1 EventTable
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
190
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
191 =cut