Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Toolkit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
digital blueprint
Web Component Framework
Toolkit
Commits
ea755264
Commit
ea755264
authored
5 years ago
by
Reiter, Christoph
Browse files
Options
Downloads
Patches
Plain Diff
Add experimental events API plus tests
parent
868d6eea
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
packages/common/events.js
+149
-0
149 additions, 0 deletions
packages/common/events.js
packages/common/test/events.js
+76
-0
76 additions, 0 deletions
packages/common/test/events.js
with
225 additions
and
0 deletions
packages/common/events.js
0 → 100644
+
149
−
0
View file @
ea755264
export
class
EventSubscriber
{
/**
* @param {string} eventName The event name used for broadcasting the event
* @param {string} requestName The event name used for requesting the event
*/
constructor
(
eventName
,
requestName
)
{
this
.
_eventName
=
eventName
;
this
.
_requestName
=
requestName
;
this
.
_callbacks
=
[];
this
.
_eventHandler
=
this
.
_eventHandler
.
bind
(
this
);
}
/**
* Register a callback for the event
*
* @param {Function} callback
*/
subscribe
(
callback
)
{
const
first
=
(
this
.
_callbacks
.
length
===
0
);
this
.
_callbacks
.
push
(
callback
);
if
(
first
)
{
window
.
addEventListener
(
this
.
_eventName
,
this
.
_eventHandler
);
}
this
.
_requestUpdate
(
callback
);
}
/**
* Unregister a callback registered through subscribe()
*
* @param {Function} callback
*/
unsubscribe
(
callback
)
{
const
index
=
this
.
_callbacks
.
indexOf
(
callback
);
if
(
index
==
-
1
)
throw
new
Error
(
"
not subscribed
"
);
this
.
_callbacks
.
splice
(
index
,
1
);
const
last
=
(
this
.
_callbacks
.
length
===
0
);
if
(
last
)
{
window
.
removeEventListener
(
this
.
_eventName
,
this
.
_eventHandler
);
}
}
/**
* Request an event, might not do anything if there is no emitter.
*/
requestUpdate
()
{
this
.
_requestUpdate
((
data
)
=>
{
for
(
const
cb
of
this
.
_callbacks
)
{
cb
(
data
);
}
});
}
/**
* Returns the event data or throws an error if there is no emitter
*/
async
requestData
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
event
=
new
CustomEvent
(
this
.
_requestName
,
{
bubbles
:
true
,
composed
:
true
,
cancelable
:
true
,
detail
:
{
'
callback
'
:
resolve
}
});
if
(
window
.
dispatchEvent
(
event
))
{
reject
(
new
Error
(
"
other side not there
"
));
}
});
}
_requestUpdate
(
callback
)
{
const
event
=
new
CustomEvent
(
this
.
_requestName
,
{
bubbles
:
true
,
composed
:
true
,
detail
:
{
callback
:
callback
}
});
window
.
dispatchEvent
(
event
);
}
_eventHandler
(
event
)
{
const
data
=
event
.
detail
.
data
;
for
(
const
cb
of
this
.
_callbacks
)
{
cb
(
data
);
}
}
}
export
class
EventEmitter
{
/**
* @param {string} eventName The event name used for broadcasting the event
* @param {string} requestName The event name used for requesting the event
*/
constructor
(
eventName
,
requestName
)
{
this
.
_eventName
=
eventName
;
this
.
_requestName
=
requestName
;
}
/**
* Register a callback that will be called when a new event is generated.
* The callback needs to return an object that is send with the event.
*
* @param {Function} callback
*/
registerCallback
(
callback
)
{
if
(
this
.
_getData
!==
undefined
)
throw
new
Error
(
"
already registered
"
);
this
.
_getData
=
callback
;
this
.
_onRequest
=
(
event
)
=>
{
const
callback
=
event
.
detail
.
callback
;
if
(
callback
===
undefined
)
{
return
;
}
event
.
preventDefault
();
event
.
stopImmediatePropagation
();
callback
(
this
.
_getData
());
};
window
.
addEventListener
(
this
.
_requestName
,
this
.
_onRequest
);
this
.
emit
();
}
/**
* Unregister a callback that was passed to registerCallback()
*
* @param {Function} callback
*/
unregisterCallback
(
callback
)
{
if
(
this
.
_getData
!==
callback
)
throw
new
Error
(
"
not registered
"
);
window
.
removeEventListener
(
this
.
_requestName
,
this
.
_onRequest
);
delete
this
.
_getData
;
delete
this
.
_onRequest
;
}
/**
* Force emit an event (when something has changed and new data needs to be send out)
*/
emit
()
{
if
(
this
.
_getData
===
undefined
)
throw
new
Error
(
"
no callback registered
"
);
const
event
=
new
CustomEvent
(
this
.
_eventName
,
{
bubbles
:
true
,
composed
:
true
,
detail
:
{
data
:
this
.
_getData
()
}
});
window
.
dispatchEvent
(
event
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
packages/common/test/events.js
0 → 100644
+
76
−
0
View file @
ea755264
import
{
assert
}
from
'
chai
'
;
import
{
EventEmitter
,
EventSubscriber
}
from
'
../events.js
'
;
const
assertThrowsAsync
=
async
(
fn
,
...
args
)
=>
{
let
error
;
try
{
await
fn
();
}
catch
(
e
)
{
error
=
e
;
}
return
assert
.
throws
(()
=>
{
if
(
error
!==
undefined
)
throw
error
;
},
...
args
);
};
suite
(
'
events
'
,
()
=>
{
test
(
'
emitter basics
'
,
()
=>
{
const
ev
=
new
EventEmitter
(
"
foo
"
,
"
foo-req
"
);
assert
.
throws
(
ev
.
emit
);
const
cb
=
()
=>
{
return
42
;
};
ev
.
registerCallback
(
cb
);
ev
.
emit
();
ev
.
unregisterCallback
(
cb
);
assert
.
throws
(()
=>
{
ev
.
unregisterCallback
(
cb
);});
assert
.
throws
(
ev
.
emit
);
});
test
(
'
sub basics
'
,
async
()
=>
{
const
sub
=
new
EventSubscriber
(
"
foo
"
,
"
foo-req
"
);
await
assertThrowsAsync
(
async
()
=>
{
await
sub
.
requestData
();},
/not there/
);
const
buffer
=
[];
const
handler
=
(
data
)
=>
{
buffer
.
push
(
data
);
};
sub
.
subscribe
(
handler
);
sub
.
subscribe
(
handler
);
sub
.
requestUpdate
();
sub
.
unsubscribe
(
handler
);
sub
.
unsubscribe
(
handler
);
assert
.
throws
(()
=>
{
sub
.
unsubscribe
(
handler
);});
assert
.
deepEqual
(
buffer
,
[]);
});
test
(
'
both basics
'
,
async
()
=>
{
const
sub
=
new
EventSubscriber
(
"
foo
"
,
"
foo-req
"
);
const
ev
=
new
EventEmitter
(
"
foo
"
,
"
foo-req
"
);
let
i
=
0
;
const
cb
=
()
=>
{
return
i
++
;
};
ev
.
registerCallback
(
cb
);
const
buffer
=
[];
const
handler
=
(
data
)
=>
{
buffer
.
push
(
data
);
};
sub
.
subscribe
(
handler
);
assert
.
deepEqual
(
buffer
,
[
1
]);
sub
.
requestUpdate
();
assert
.
deepEqual
(
buffer
,
[
1
,
2
]);
sub
.
requestUpdate
();
assert
.
deepEqual
(
buffer
,
[
1
,
2
,
3
]);
ev
.
emit
();
assert
.
deepEqual
(
buffer
,
[
1
,
2
,
3
,
4
]);
sub
.
unsubscribe
(
handler
);
sub
.
requestUpdate
();
assert
.
deepEqual
(
buffer
,
[
1
,
2
,
3
,
4
]);
ev
.
unregisterCallback
(
cb
);
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment