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