1 | #! perl # mandatory depends=login |
1 | #! perl # mandatory depends=login |
2 | |
2 | |
3 | # sends the following ext message types |
3 | # sends the following ext message types |
4 | # ws_n id # widgetset new |
4 | # ws_n ws # widgetset new |
5 | # ws_d id # widgetset destroy |
5 | # ws_d ws # widgetset destroy |
6 | # ws_c ws id class args # widgetset create |
6 | # ws_c ws id class args # widgetset create |
7 | # w_c id [rid] name args # widget method call |
7 | # w_c id rid name args # widget method call |
8 | # w_s id name value # widget member set |
8 | # w_s id @attr # widget member set |
9 | # w_g id rid name # widget member get |
9 | # w_g id rid @attr # widget member get |
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); |
20 | |
22 | |
21 | $ns->{id} = "a"; |
23 | $ns->{id} = "a"; |
22 | $ns->{json_coder}->filter_json_single_key_object (__widget_ref__ => sub { |
24 | $ns->{json_coder}->filter_json_single_key_object (__w_ => sub { |
23 | # cannot deserialise ATM |
25 | # cannot deserialise ATM |
24 | undef |
26 | undef |
25 | }); |
27 | }); |
26 | }, |
28 | }, |
27 | ); |
29 | ); |
… | |
… | |
66 | [5, "Pow"], |
68 | [5, "Pow"], |
67 | [6, "Cha"], |
69 | [6, "Cha"], |
68 | ) { |
70 | ) { |
69 | my ($x, $label) = @$_; |
71 | my ($x, $label) = @$_; |
70 | |
72 | |
71 | $statstable->add ($x, 0, $ws->new (Label => |
73 | $statstable->add_at ($x, 0, $ws->new (Label => |
72 | can_hover => 1, can_events => 1, |
74 | can_hover => 1, can_events => 1, |
73 | align => +1, text => $label, tooltip => "#stat_$label", |
75 | align => +1, text => $label, tooltip => "#stat_$label", |
74 | )); |
76 | )); |
75 | $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
77 | $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
76 | can_hover => 1, can_events => 1, |
78 | can_hover => 1, can_events => 1, |
77 | align => +1, template => "88", tooltip => "#stat_$label", |
79 | align => +1, template => "88", tooltip => "#stat_$label", |
78 | )); |
80 | )); |
79 | } |
81 | } |
80 | |
82 | |
… | |
… | |
90 | return unless $cf::CFG{devel}; |
92 | return unless $cf::CFG{devel}; |
91 | |
93 | |
92 | my $ns = $pl->ns; |
94 | my $ns = $pl->ns; |
93 | |
95 | |
94 | return unless $ns->{can_widget}; |
96 | return unless $ns->{can_widget}; |
95 | |
|
|
96 | csc_start $ns; |
97 | #csc_start $ns; |
97 | }, |
98 | }, |
98 | ); |
99 | ); |
99 | |
100 | |
100 | cf::register_exticmd w_e => sub { |
101 | cf::register_exticmd w_e => sub { |
101 | my ($ns, $pkt) = @_; |
102 | my ($ns, $id, $name, $args) = @_; |
102 | |
103 | |
103 | if (my $w = $ns->{widget}{$pkt->{id}}) { |
104 | if (my $w = $ns->{widget}{$id}) { |
104 | if (my $cb = $w->{ev}{$pkt->{name}}) { |
105 | if (my $cb = $w->{ev}{$name}) { |
105 | $_->($w, @{ $pkt->{args} || [] }) |
106 | $_->($w, @$args) |
106 | for @$cb; |
107 | for @$cb; |
107 | } |
108 | } |
108 | } |
109 | } |
109 | |
110 | |
110 | () |
111 | () |
111 | }; |
112 | }; |
112 | |
113 | |
113 | cf::register_exticmd w_r => sub { |
114 | cf::register_exticmd w_r => sub { |
114 | my ($ns, $pkt) = @_; |
115 | my ($ns, $rid, $res) = @_; |
115 | |
116 | |
116 | if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) { |
117 | if (my $cb = delete $ns->{widget_return}{$rid}) { |
117 | $cb->(@{$pkt->{res} || [] }); |
118 | $cb->(@$res); |
118 | } |
119 | } |
119 | |
120 | |
120 | () |
121 | () |
121 | }; |
122 | }; |
122 | |
123 | |
… | |
… | |
126 | my $id = ++$self->{id}; |
127 | my $id = ++$self->{id}; |
127 | |
128 | |
128 | my $ws = bless { |
129 | my $ws = bless { |
129 | id => $id, |
130 | id => $id, |
130 | ns => $self, |
131 | ns => $self, |
131 | w => {}, |
132 | _w => {}, |
132 | }, "ext::widget::set"; |
133 | }, "ext::widget::set"; |
133 | |
134 | |
134 | $ws->msg (ws_n => id => $id); |
135 | $ws->msg (ws_n => $id); |
135 | |
136 | |
136 | $ws |
137 | $ws |
137 | } |
138 | } |
138 | |
139 | |
139 | ############################################################################# |
140 | ############################################################################# |
… | |
… | |
145 | } |
146 | } |
146 | |
147 | |
147 | sub destroy { |
148 | sub destroy { |
148 | my ($self) = @_; |
149 | my ($self) = @_; |
149 | |
150 | |
150 | $self->msg (ws_d => id => $self->{id}); |
151 | $self->msg (ws_d => $self->{id}); |
151 | delete $self->{ns}; |
152 | delete $self->{ns}; |
152 | $_->destroy |
153 | $_->destroy |
153 | for values %{ $self->{w} }; |
154 | for values %{ $self->{w} }; |
154 | } |
155 | } |
155 | |
156 | |
156 | sub msg { |
157 | sub msg { |
157 | my ($self, $type, %msg) = @_; |
158 | my ($self, @msg) = @_; |
158 | |
159 | |
159 | if (my $ns = shift->{ns}) { |
160 | if (my $ns = shift->{ns}) { |
160 | $msg{msgtype} = $type; |
161 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
161 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
162 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
162 | } |
163 | } |
163 | } |
164 | } |
164 | |
165 | |
165 | sub new { |
166 | sub new { |
166 | my ($self, $class, %args) = @_; |
167 | my ($self, $class, %args) = @_; |
167 | |
168 | |
168 | my $id = ++$self->{ns}{id}; |
169 | my $id = ++$self->{ns}{id}; |
169 | |
170 | |
170 | my $proxy = $self->{w}{$id} = bless { |
171 | my $proxy = bless { |
171 | id => $id, |
172 | id => $id, |
172 | }, "ext::widget::proxy"; |
173 | }, "ext::widget::proxy"; |
173 | |
174 | |
|
|
175 | Scalar::Util::weaken ($self->{_w}{$id} = $proxy); |
174 | Scalar::Util::weaken ($proxy->{ws} = $self); |
176 | Scalar::Util::weaken ($proxy->{ws} = $self); |
175 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
177 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
176 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
178 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
177 | |
179 | |
178 | for my $ev (grep /^on_/, keys %args) { |
180 | for my $ev (grep /^on_/, keys %args) { |
179 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
181 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
180 | $args{$ev} = 0; |
182 | $args{$ev} = 0; |
181 | } |
183 | } |
182 | |
184 | |
183 | $self->msg (ws_c => |
185 | $self->msg (ws_c => |
184 | ws => $self->{w}{id}, |
186 | $self->{id}, |
185 | id => $id, |
187 | $id, |
186 | class => $class, |
188 | $class, |
187 | args => \%args, |
189 | \%args, |
188 | ); |
190 | ); |
189 | |
191 | |
190 | $proxy |
192 | $proxy |
191 | } |
193 | } |
192 | |
194 | |
… | |
… | |
198 | my ($self) = @_; |
200 | my ($self) = @_; |
199 | |
201 | |
200 | delete $self->{ns}{widget}{$self->{id}}; |
202 | delete $self->{ns}{widget}{$self->{id}}; |
201 | |
203 | |
202 | if (my $ws = $self->{ws}) { |
204 | if (my $ws = $self->{ws}) { |
|
|
205 | $self->msg (w_c => 0, "destroy"); |
203 | delete $ws->{w}{$self->{id}}; |
206 | delete $ws->{_w}{$self->{id}}; |
204 | $self->msg (w_c => name => "destroy"); |
|
|
205 | } |
207 | } |
206 | } |
208 | } |
207 | |
209 | |
208 | sub msg { |
210 | sub msg { |
209 | my ($self, $type, %msg) = @_; |
211 | my ($self, $type, @arg) = @_; |
210 | |
212 | |
211 | if (my $ws = $self->{ws}) { |
213 | if (my $ws = $self->{ws}) { |
212 | $ws->msg ($type, |
214 | $ws->msg ($type, $self->{id}, @arg); |
213 | %msg, |
|
|
214 | id => $self->{id}, |
|
|
215 | ); |
|
|
216 | } |
215 | } |
217 | } |
216 | } |
218 | |
217 | |
219 | sub msg_cb { |
218 | sub msg_cb { |
220 | my ($self, $cb, $type, %msg) = @_; |
219 | my ($self, $cb, $type, @arg) = @_; |
221 | |
220 | |
222 | if (my $ws = $self->{ws}) { |
221 | if (my $ws = $self->{ws}) { |
223 | |
|
|
224 | my $rid = ++$ws->{ns}{id}; |
222 | my $rid = ++$ws->{ns}{id}; |
225 | |
223 | |
226 | $self->msg ($type, %msg, rid => $rid); |
224 | $self->msg ($type, $rid, @arg); |
227 | |
225 | |
228 | if ($cb) { |
226 | if ($cb) { |
229 | $ws->{ns}{widget_return}{$rid} = $cb; |
227 | $ws->{ns}{widget_return}{$rid} = $cb; |
230 | } else { |
228 | } else { |
231 | # synchronous case |
229 | # synchronous case |
… | |
… | |
244 | |
242 | |
245 | () |
243 | () |
246 | } |
244 | } |
247 | |
245 | |
248 | sub set { |
246 | sub set { |
249 | my ($self, $member, $value) = @_; |
247 | my ($self, @kv) = @_; |
250 | |
248 | |
251 | $self->msg (w_s => name => $member, value => $value); |
249 | $self->msg (w_s => \@kv); |
252 | } |
250 | } |
253 | |
251 | |
254 | sub get { |
252 | sub get { |
255 | my ($self, $member, $cb) = @_; |
253 | my ($self, $member, $cb) = @_; |
256 | |
254 | |
257 | $self->msg_cb ($cb, w_g => name => $member); |
255 | $self->msg_cb ($cb, w_g => [$member]); |
258 | } |
256 | } |
259 | |
257 | |
260 | sub TO_JSON { |
258 | sub TO_JSON { |
261 | { __widget_ref__ => $_[0]{id} } |
259 | { __w_ => $_[0]{id} } |
262 | } |
260 | } |
263 | |
261 | |
264 | our $AUTOLOAD; |
262 | our $AUTOLOAD; |
265 | |
263 | |
266 | sub AUTOLOAD { |
264 | sub AUTOLOAD { |
267 | $AUTOLOAD =~ s/^.*:// |
265 | $AUTOLOAD =~ s/^.*:// |
268 | or return; |
266 | or return; |
269 | |
267 | |
270 | my $self = shift; |
268 | my $self = shift; |
271 | |
269 | |
272 | $self->msg (w_c => name => $AUTOLOAD, args => \@_); |
270 | $self->msg (w_c => 0, $AUTOLOAD, \@_); |
273 | |
271 | |
274 | () |
272 | () |
275 | } |
273 | } |
276 | |
274 | |