… | |
… | |
10 | # |
10 | # |
11 | # and expects the following exti message types |
11 | # and expects the following exti message types |
12 | # w_r rid res # widget call return |
12 | # w_r rid res # widget call return |
13 | # w_e id name args # widget_event |
13 | # w_e id name args # widget_event |
14 | |
14 | |
|
|
15 | our $DEBUG = 1; |
|
|
16 | |
15 | cf::client->attach ( |
17 | cf::client->attach ( |
16 | on_connect => sub { |
18 | on_connect => sub { |
17 | my ($ns) = @_; |
19 | my ($ns) = @_; |
18 | |
20 | |
19 | Scalar::Util::weaken (my $weakns = $ns); |
21 | Scalar::Util::weaken (my $weakns = $ns); |
… | |
… | |
125 | my $id = ++$self->{id}; |
127 | my $id = ++$self->{id}; |
126 | |
128 | |
127 | my $ws = bless { |
129 | my $ws = bless { |
128 | id => $id, |
130 | id => $id, |
129 | ns => $self, |
131 | ns => $self, |
130 | w => {}, |
132 | _w => {}, |
131 | }, "ext::widget::set"; |
133 | }, "ext::widget::set"; |
132 | |
134 | |
133 | $ws->msg (ws_n => id => $id); |
135 | $ws->msg (ws_n => id => $id); |
134 | |
136 | |
135 | $ws |
137 | $ws |
… | |
… | |
155 | sub msg { |
157 | sub msg { |
156 | my ($self, $type, %msg) = @_; |
158 | my ($self, $type, %msg) = @_; |
157 | |
159 | |
158 | if (my $ns = shift->{ns}) { |
160 | if (my $ns = shift->{ns}) { |
159 | $msg{msgtype} = $type; |
161 | $msg{msgtype} = $type; |
|
|
162 | warn "msg " . $ns->{json_coder}->encode (\%msg) if $DEBUG;#d# |
160 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
163 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
161 | } |
164 | } |
162 | } |
165 | } |
163 | |
166 | |
164 | sub new { |
167 | sub new { |
165 | my ($self, $class, %args) = @_; |
168 | my ($self, $class, %args) = @_; |
166 | |
169 | |
167 | my $id = ++$self->{ns}{id}; |
170 | my $id = ++$self->{ns}{id}; |
168 | |
171 | |
169 | my $proxy = $self->{w}{$id} = bless { |
172 | my $proxy = bless { |
170 | id => $id, |
173 | id => $id, |
171 | }, "ext::widget::proxy"; |
174 | }, "ext::widget::proxy"; |
172 | |
175 | |
|
|
176 | Scalar::Util::weaken ($self->{_w}{$id} = $proxy); |
173 | Scalar::Util::weaken ($proxy->{ws} = $self); |
177 | Scalar::Util::weaken ($proxy->{ws} = $self); |
174 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
178 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
175 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
179 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
176 | |
180 | |
177 | for my $ev (grep /^on_/, keys %args) { |
181 | for my $ev (grep /^on_/, keys %args) { |
178 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
182 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
179 | $args{$ev} = 0; |
183 | $args{$ev} = 0; |
180 | } |
184 | } |
181 | |
185 | |
182 | $self->msg (ws_c => |
186 | $self->msg (ws_c => |
183 | ws => $self->{w}{id}, |
187 | ws => $proxy->{id}, |
184 | id => $id, |
188 | id => $id, |
185 | class => $class, |
189 | class => $class, |
186 | args => \%args, |
190 | args => \%args, |
187 | ); |
191 | ); |
188 | |
192 | |
… | |
… | |
197 | my ($self) = @_; |
201 | my ($self) = @_; |
198 | |
202 | |
199 | delete $self->{ns}{widget}{$self->{id}}; |
203 | delete $self->{ns}{widget}{$self->{id}}; |
200 | |
204 | |
201 | if (my $ws = $self->{ws}) { |
205 | if (my $ws = $self->{ws}) { |
202 | delete $ws->{w}{$self->{id}}; |
|
|
203 | $self->msg (w_c => name => "destroy"); |
206 | $self->msg (w_c => name => "destroy"); |
|
|
207 | delete $ws->{_w}{$self->{id}}; |
204 | } |
208 | } |
205 | } |
209 | } |
206 | |
210 | |
207 | sub msg { |
211 | sub msg { |
208 | my ($self, $type, %msg) = @_; |
212 | my ($self, $type, %msg) = @_; |
… | |
… | |
243 | |
247 | |
244 | () |
248 | () |
245 | } |
249 | } |
246 | |
250 | |
247 | sub set { |
251 | sub set { |
248 | my ($self, $member, $value) = @_; |
252 | my ($self, @kv) = @_; |
249 | |
253 | |
250 | $self->msg (w_s => attr => [ [$member, $value] ]); |
254 | $self->msg (w_s => attr => \@kv); |
251 | } |
255 | } |
252 | |
256 | |
253 | sub get { |
257 | sub get { |
254 | my ($self, $member, $cb) = @_; |
258 | my ($self, $member, $cb) = @_; |
255 | |
259 | |