Skip to content

Commit 0f18f13

Browse files
committed
Event handler improvements.
- Add basic event handlers (can be used to construct more complex handlers) - log all events and continue - fail on unhandled events - fail on unacceptable events - log warnings and continue - strict mode to fail on all current warnings - Add default event handler feature.
1 parent d3899b6 commit 0f18f13

File tree

3 files changed

+121
-11
lines changed

3 files changed

+121
-11
lines changed

lib/events.js

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const {
1616
const api = {};
1717
module.exports = api;
1818

19+
let _defaultEventHandler = [];
20+
1921
/**
2022
* Handle an event.
2123
*
@@ -42,7 +44,7 @@ api.handleEvent = ({
4244
}) => {
4345
const handlers = [].concat(
4446
options.eventHandler ? _asArray(options.eventHandler) : [],
45-
_defaultHandler
47+
..._defaultEventHandler
4648
);
4749
_handle({event, handlers});
4850
};
@@ -76,17 +78,76 @@ function _handle({event, handlers}) {
7678
return doNext;
7779
}
7880

79-
function _defaultHandler({event}) {
81+
// logs all events and continues
82+
api.logEventHandler = function({event, next}) {
83+
console.warn(`EVENT: ${event.message}`, {
84+
code: event.code,
85+
level: event.level,
86+
details: event.details
87+
});
88+
next();
89+
};
90+
91+
// fallback to throw errors for any unhandled events
92+
api.unhandledEventHandler = function({event}) {
93+
throw new JsonLdError(
94+
'No handler for event.',
95+
'jsonld.UnhandledEvent',
96+
{event});
97+
};
98+
99+
// throw with event details
100+
api.throwUnacceptableEventHandler = function({event}) {
101+
throw new JsonLdError(
102+
event.message,
103+
'jsonld.UnacceptableEvent',
104+
{event});
105+
};
106+
107+
// log 'warning' level events
108+
api.logWarningEventHandler = function({event}) {
80109
if(event.level === 'warning') {
81110
console.warn(`WARNING: ${event.message}`, {
82111
code: event.code,
83112
details: event.details
84113
});
85-
return;
86114
}
87-
// fallback to ensure events are handled somehow
88-
throw new JsonLdError(
89-
'No handler for event.',
90-
'jsonld.UnhandledEvent',
91-
{event});
92-
}
115+
};
116+
117+
// strict handler that rejects warning conditions
118+
api.strictModeEventHandler = [
119+
function({event, next}) {
120+
// fail on all warnings
121+
if(event.level === 'warning') {
122+
throw new JsonLdError(
123+
event.message,
124+
'jsonld.StrictModeViolationEvent',
125+
{event});
126+
}
127+
next();
128+
},
129+
// fail on unhandled events
130+
// TODO: update when events are added that are acceptable in strict mode
131+
api.unhandledEventHandler
132+
];
133+
134+
// log warnings to console or fail
135+
api.basicEventHandler = [
136+
api.logWarningEventHandler,
137+
api.unhandledEventHandler
138+
];
139+
140+
/**
141+
* Set default event handler.
142+
*
143+
* By default, all event are unhandled. It is recommended to pass in an
144+
* eventHandler into each call. However, this call allows using a default
145+
* eventHandler when one is not otherwise provided.
146+
*
147+
* @param {object} options - default handler options:
148+
* {function|object|array} eventHandler - a default event handler.
149+
* falsey to unset.
150+
*/
151+
api.setDefaultEventHandler = function({eventHandler} = {}) {
152+
_defaultEventHandler = eventHandler ? _asArray(eventHandler) : [];
153+
};

lib/jsonld.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ const {
8080
mergeNodeMaps: _mergeNodeMaps
8181
} = require('./nodeMap');
8282

83+
const {
84+
basicEventHandler: _basicEventHandler,
85+
logEventHandler: _logEventHandler,
86+
logWarningEventHandler: _logWarningEventHandler,
87+
setDefaultEventHandler: _setDefaultEventHandler,
88+
strictModeEventHandler: _strictModeEventHandler,
89+
throwUnacceptableEventHandler: _throwUnacceptableEventHandler,
90+
unhandledEventHandler: _unhandledEventHandler
91+
} = require('./events');
92+
8393
/* eslint-disable indent */
8494
// attaches jsonld API to the given object
8595
const wrapper = function(jsonld) {
@@ -997,6 +1007,16 @@ jsonld.registerRDFParser('application/nquads', NQuads.parse);
9971007
/* URL API */
9981008
jsonld.url = require('./url');
9991009

1010+
/* Events API and handlers */
1011+
jsonld.setDefaultEventHandler = _setDefaultEventHandler;
1012+
jsonld.basicEventHandler = _basicEventHandler;
1013+
jsonld.logEventHandler = _logEventHandler;
1014+
jsonld.logWarningEventHandler = _logWarningEventHandler;
1015+
jsonld.setDefaultEventHandler = _setDefaultEventHandler;
1016+
jsonld.strictModeEventHandler = _strictModeEventHandler;
1017+
jsonld.throwUnacceptableEventHandler = _throwUnacceptableEventHandler;
1018+
jsonld.unhandledEventHandler = _unhandledEventHandler;
1019+
10001020
/* Utility API */
10011021
jsonld.util = util;
10021022
// backwards compatibility

tests/misc.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,37 @@ function addEventCounts(counts, event) {
493493
}
494494

495495
describe.only('events', () => {
496-
// FIXME add default handler tests
497-
// FIXME add object '*' handler tests
496+
// FIXME/TODO add object '*' handler and tests?
497+
498+
it('check default handler called', async () => {
499+
const d =
500+
{
501+
"relative": "test"
502+
}
503+
;
504+
const ex = [];
505+
506+
const counts = {};
507+
const eventHandler = ({event}) => {
508+
addEventCounts(counts, event);
509+
};
510+
511+
jsonld.setDefaultEventHandler({eventHandler});
512+
513+
const e = await jsonld.expand(d);
514+
515+
assert.deepStrictEqual(e, ex);
516+
assert.deepStrictEqual(counts, {
517+
codes: {
518+
'invalid property expansion': 1,
519+
'relative IRI after expansion': 2
520+
},
521+
events: 3
522+
});
523+
524+
// reset default
525+
jsonld.setDefaultEventHandler();
526+
});
498527

499528
it('handle warning event with function', async () => {
500529
const d =

0 commit comments

Comments
 (0)