Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
15e38a13
Commit
15e38a13
authored
Apr 20, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Apr 20, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate emitter module to TS
parent
21ae258e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
23 deletions
+19
-23
emitter.ts
src/annotator/util/emitter.ts
+19
-23
No files found.
src/annotator/util/emitter.
j
s
→
src/annotator/util/emitter.
t
s
View file @
15e38a13
/* eslint-disable @typescript-eslint/ban-types */
/*
* Disable @typescript-eslint/ban-types for the whole file, as changing the
* event's callback type away from `Function` has multiple implications that
* should be addressed separately
*/
import
{
TinyEmitter
}
from
'tiny-emitter'
;
import
{
TinyEmitter
}
from
'tiny-emitter'
;
/** @typedef {import('../../types/annotator').Destroyable} Destroyable */
import
type
{
Destroyable
}
from
'../../types/annotator'
;
/**
/**
* Emitter is a communication class that implements the publisher/subscriber
* Emitter is a communication class that implements the publisher/subscriber
* pattern. It allows sending and listening events through a shared EventBus.
* pattern. It allows sending and listening events through a shared EventBus.
* The different elements of the application can communicate with each other
* The different elements of the application can communicate with each other
* without being tightly coupled.
* without being tightly coupled.
*
* @implements {Destroyable}
*/
*/
export
class
Emitter
{
export
class
Emitter
implements
Destroyable
{
/**
private
_emitter
:
TinyEmitter
;
* @param {TinyEmitter} emitter
private
_subscriptions
:
[
event
:
string
,
callback
:
Function
][];
*/
constructor
(
emitter
)
{
this
.
_emitter
=
emitter
;
/** @type {[event: string, callback: Function][]} */
constructor
(
emitter
:
TinyEmitter
)
{
this
.
_emitter
=
emitter
;
this
.
_subscriptions
=
[];
this
.
_subscriptions
=
[];
}
}
/**
/**
* Fire an event.
* Fire an event.
*
* @param {string} event
* @param {unknown[]} args
*/
*/
publish
(
event
,
...
args
)
{
publish
(
event
:
string
,
...
args
:
unknown
[]
)
{
this
.
_emitter
.
emit
(
event
,
...
args
);
this
.
_emitter
.
emit
(
event
,
...
args
);
}
}
/**
/**
* Register an event listener.
* Register an event listener.
*
* @param {string} event
* @param {Function} callback
*/
*/
subscribe
(
event
,
callback
)
{
subscribe
(
event
:
string
,
callback
:
Function
)
{
this
.
_emitter
.
on
(
event
,
callback
);
this
.
_emitter
.
on
(
event
,
callback
);
this
.
_subscriptions
.
push
([
event
,
callback
]);
this
.
_subscriptions
.
push
([
event
,
callback
]);
}
}
/**
/**
* Remove an event listener.
* Remove an event listener.
*
* @param {string} event
* @param {Function} callback
*/
*/
unsubscribe
(
event
,
callback
)
{
unsubscribe
(
event
:
string
,
callback
:
Function
)
{
this
.
_emitter
.
off
(
event
,
callback
);
this
.
_emitter
.
off
(
event
,
callback
);
this
.
_subscriptions
=
this
.
_subscriptions
.
filter
(
this
.
_subscriptions
=
this
.
_subscriptions
.
filter
(
([
subEvent
,
subCallback
])
=>
([
subEvent
,
subCallback
])
=>
...
@@ -60,7 +54,7 @@ export class Emitter {
...
@@ -60,7 +54,7 @@ export class Emitter {
* Remove all event listeners.
* Remove all event listeners.
*/
*/
destroy
()
{
destroy
()
{
for
(
le
t
[
event
,
callback
]
of
this
.
_subscriptions
)
{
for
(
cons
t
[
event
,
callback
]
of
this
.
_subscriptions
)
{
this
.
_emitter
.
off
(
event
,
callback
);
this
.
_emitter
.
off
(
event
,
callback
);
}
}
this
.
_subscriptions
=
[];
this
.
_subscriptions
=
[];
...
@@ -68,6 +62,8 @@ export class Emitter {
...
@@ -68,6 +62,8 @@ export class Emitter {
}
}
export
class
EventBus
{
export
class
EventBus
{
private
_emitter
:
TinyEmitter
;
constructor
()
{
constructor
()
{
this
.
_emitter
=
new
TinyEmitter
();
this
.
_emitter
=
new
TinyEmitter
();
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment