source: main/trunk/greenstone2/perllib/cpan/Mojo/EventEmitter.pm

Last change on this file was 32205, checked in by ak19, 6 years ago

First set of commits to do with implementing the new 'paged_html' output option of PDFPlugin that uses using xpdftools' new pdftohtml. So far tested only on Linux (64 bit), but things work there so I'm optimistically committing the changes since they work. 2. Committing the pre-built Linux binaries of XPDFtools for both 32 and 64 bit built by the XPDF group. 2. To use the correct bitness variant of xpdftools, setup.bash now exports the BITNESS env var, consulted by gsConvert.pl. 3. All the perl code changes to do with using xpdf tools' pdftohtml to generate paged_html and feed it in the desired form into GS(3): gsConvert.pl, PDFPlugin.pm and its parent ConvertBinaryPFile.pm have been modified to make it all work. xpdftools' pdftohtml generates a folder containing an html file and a screenshot for each page in a PDF (as well as an index.html linking to each page's html). However, we want a single html file that contains each individual 'page' html's content in a div, and need to do some further HTML style, attribute and structure modifications to massage the xpdftool output to what we want for GS. In order to parse and manipulate the HTML 'DOM' to do this, we're using the Mojo::DOM package that Dr Bainbridge found and which he's compiled up. Mojo::DOM is therefore also committed in this revision. Some further changes and some display fixes are required, but need to check with the others about that.

File size: 3.5 KB
Line 
1package Mojo::EventEmitter;
2use Mojo::Base -base;
3
4use Scalar::Util qw(blessed weaken);
5
6use constant DEBUG => $ENV{MOJO_EVENTEMITTER_DEBUG} || 0;
7
8sub catch { $_[0]->on(error => $_[1]) and return $_[0] }
9
10sub emit {
11 my ($self, $name) = (shift, shift);
12
13 if (my $s = $self->{events}{$name}) {
14 warn "-- Emit $name in @{[blessed $self]} (@{[scalar @$s]})\n" if DEBUG;
15 for my $cb (@$s) { $self->$cb(@_) }
16 }
17 else {
18 warn "-- Emit $name in @{[blessed $self]} (0)\n" if DEBUG;
19 die "@{[blessed $self]}: $_[0]" if $name eq 'error';
20 }
21
22 return $self;
23}
24
25sub has_subscribers { !!shift->{events}{shift()} }
26
27sub on { push @{$_[0]{events}{$_[1]}}, $_[2] and return $_[2] }
28
29sub once {
30 my ($self, $name, $cb) = @_;
31
32 weaken $self;
33 my $wrapper;
34 $wrapper = sub {
35 $self->unsubscribe($name => $wrapper);
36 $cb->(@_);
37 };
38 $self->on($name => $wrapper);
39 weaken $wrapper;
40
41 return $wrapper;
42}
43
44sub subscribers { shift->{events}{shift()} ||= [] }
45
46sub unsubscribe {
47 my ($self, $name, $cb) = @_;
48
49 # One
50 if ($cb) {
51 $self->{events}{$name} = [grep { $cb ne $_ } @{$self->{events}{$name}}];
52 delete $self->{events}{$name} unless @{$self->{events}{$name}};
53 }
54
55 # All
56 else { delete $self->{events}{$name} }
57
58 return $self;
59}
60
611;
62
63=encoding utf8
64
65=head1 NAME
66
67Mojo::EventEmitter - Event emitter base class
68
69=head1 SYNOPSIS
70
71 package Cat;
72 use Mojo::Base 'Mojo::EventEmitter';
73
74 # Emit events
75 sub poke {
76 my $self = shift;
77 $self->emit(roar => 3);
78 }
79
80 package main;
81
82 # Subscribe to events
83 my $tiger = Cat->new;
84 $tiger->on(roar => sub {
85 my ($tiger, $times) = @_;
86 say 'RAWR!' for 1 .. $times;
87 });
88 $tiger->poke;
89
90=head1 DESCRIPTION
91
92L<Mojo::EventEmitter> is a simple base class for event emitting objects.
93
94=head1 EVENTS
95
96L<Mojo::EventEmitter> can emit the following events.
97
98=head2 error
99
100 $e->on(error => sub {
101 my ($e, $err) = @_;
102 ...
103 });
104
105This is a special event for errors, it will not be emitted directly by this
106class, but is fatal if unhandled. Subclasses may choose to emit it, but are not
107required to do so.
108
109 $e->on(error => sub {
110 my ($e, $err) = @_;
111 say "This looks bad: $err";
112 });
113
114=head1 METHODS
115
116L<Mojo::EventEmitter> inherits all methods from L<Mojo::Base> and implements
117the following new ones.
118
119=head2 catch
120
121 $e = $e->catch(sub {...});
122
123Subscribe to L</"error"> event.
124
125 # Longer version
126 $e->on(error => sub {...});
127
128=head2 emit
129
130 $e = $e->emit('foo');
131 $e = $e->emit('foo', 123);
132
133Emit event.
134
135=head2 has_subscribers
136
137 my $bool = $e->has_subscribers('foo');
138
139Check if event has subscribers.
140
141=head2 on
142
143 my $cb = $e->on(foo => sub {...});
144
145Subscribe to event.
146
147 $e->on(foo => sub {
148 my ($e, @args) = @_;
149 ...
150 });
151
152=head2 once
153
154 my $cb = $e->once(foo => sub {...});
155
156Subscribe to event and unsubscribe again after it has been emitted once.
157
158 $e->once(foo => sub {
159 my ($e, @args) = @_;
160 ...
161 });
162
163=head2 subscribers
164
165 my $subscribers = $e->subscribers('foo');
166
167All subscribers for event.
168
169 # Unsubscribe last subscriber
170 $e->unsubscribe(foo => $e->subscribers('foo')->[-1]);
171
172 # Change order of subscribers
173 @{$e->subscribers('foo')} = reverse @{$e->subscribers('foo')};
174
175=head2 unsubscribe
176
177 $e = $e->unsubscribe('foo');
178 $e = $e->unsubscribe(foo => $cb);
179
180Unsubscribe from event.
181
182=head1 DEBUGGING
183
184You can set the C<MOJO_EVENTEMITTER_DEBUG> environment variable to get some
185advanced diagnostics information printed to C<STDERR>.
186
187 MOJO_EVENTEMITTER_DEBUG=1
188
189=head1 SEE ALSO
190
191L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
192
193=cut
Note: See TracBrowser for help on using the repository browser.