annotate Lib/IMPL/Object/EventSource.pm @ 43:009aa9ca2e48

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