Skip to content

Commit

Permalink
fix: reimplement event system (#113)
Browse files Browse the repository at this point in the history
Fixes #112, [skip travis-ci]
  • Loading branch information
mnater authored Apr 16, 2020
1 parent 59c6289 commit ecdd7c8
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 118 deletions.
210 changes: 103 additions & 107 deletions Hyphenopoly.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@
return Object.create(null);
}

/**
* Shorthand for Object.keys(obj).forEach(() => {})
* @param {Object} obj the object to iterate
* @param {function} fn the function to execute
* @returns {undefined}
*/
function eachKey(obj, fn) {
Object.keys(obj).forEach(fn);
}

/**
* Set value and properties of object member
* Argument <props> is a bit pattern:
Expand All @@ -53,6 +43,51 @@
/* eslint-enable no-bitwise, sort-keys */
};

/**
* Event
*/
const event = ((H) => {
/* eslint-disable array-element-newline */
const knownEvents = new Map([
["afterElementHyphenation", []],
["beforeElementHyphenation", []],
["engineReady", []],
[
"error", [
(e) => {
if (e.runDefault) {
w.console.warn(e.msg);
}
}
]
],
["hyphenopolyEnd", []],
["hyphenopolyStart", []]
]);

knownEvents.forEach((eventFunc, eventName) => {
if (
H.handleEvent &&
Object.prototype.hasOwnProperty.call(H.handleEvent, eventName)
) {
// eslint-disable-next-line security/detect-object-injection
knownEvents.get(eventName).unshift(H.handleEvent[eventName]);
}
});
/* eslint-enable array-element-newline */
return {
"fire": ((eventName, eventData) => {
eventData.runDefault = true;
eventData.preventDefault = () => {
eventData.runDefault = false;
};
knownEvents.get(eventName).forEach((eventFn) => {
eventFn(eventData);
});
})
};
})(Hyphenopoly);

/**
* Register copy event on element
* @param {Object} el The element
Expand Down Expand Up @@ -165,76 +200,16 @@
H.c = settings;
})(Hyphenopoly);

/**
* Register Events.
* This is a IIFE to keep complexity low.
*/
((H) => {
H.events = new Map();

/* eslint-disable array-element-newline */
const knownEvents = new Set([
"afterElementHyphenation",
"beforeElementHyphenation",
"engineReady",
"error",
"hyphenopolyEnd",
"hyphenopolyStart"
]);
/* eslint-enable array-element-newline */
H.events.set("error", H.defProm());
H.events.get("error").then((e) => {
e.runDefault = true;
e.preventDefault = () => {
e.runDefault = false;
};
});
if (H.handleEvent) {
eachKey(H.handleEvent, (name) => {
if (knownEvents.has(name)) {
if (!H.events.has(name)) {
H.events.set(name, H.defProm());
}
H.events.get(name).then((v) => {
// eslint-disable-next-line security/detect-object-injection
H.handleEvent[name](v);
});
} else if (name !== "tearDown" && name !== "polyfill") {
H.events.get("error").resolve({
"msg": `unknown Event "${name}" discarded`
});
}
});
}
H.events.get("error").then((e) => {
if (e.runDefault) {
w.console.warn(e.msg);
}
});
})(Hyphenopoly);

((H) => {
const C = H.c;
let mainLanguage = null;
if (H.events.has("hyphenopolyStart")) {
H.events.get("hyphenopolyStart").resolve("hyphenopolyStart");
}

/**
* Reinitialize event promise with a pending promise.
* @param {string} name - Name of the event
*/
function reinitEventPromise(name) {
H.events.delete(name);
H.events.set(name, H.defProm());
H.events.get(name).then((v) => {
/* eslint-disable security/detect-object-injection */
if (H.handleEvent[name]) {
H.handleEvent[name](v);
}
/* eslint-enable security/detect-object-injection */
});
}
event.fire(
"hyphenopolyStart",
{
"msg": "hyphenopolyStart"
}
);

/**
* Factory for elements
Expand Down Expand Up @@ -280,9 +255,12 @@
list.delete(lang);
counter[0] -= langCount;
if (counter[0] === 0) {
if (H.events.has("hyphenopolyEnd")) {
H.events.get("hyphenopolyEnd").resolve("hyphenopolyEnd");
}
event.fire(
"hyphenopolyEnd",
{
"msg": "hyphenopolyEnd"
}
);
if (!C.keepAlive) {
window.Hyphenopoly = null;
}
Expand Down Expand Up @@ -398,9 +376,12 @@
registerOnCopy(el);
}
} else if (!H.cf.langs[eLang]) {
H.events.get("error").resolve({
"msg": `Element with '${eLang}' found, but '${eLang}.hpb' not loaded. Check language tags!`
});
event.fire(
"error",
{
"msg": `Element with '${eLang}' found, but '${eLang}.hpb' not loaded. Check language tags!`
}
);
}
/* eslint-enable security/detect-object-injection */
el.childNodes.forEach((n) => {
Expand Down Expand Up @@ -497,9 +478,12 @@
hw = word;
} else if (word.indexOf("-") === -1) {
if (word.length > 61) {
H.events.get("error").resolve({
"msg": "found word longer than 61 characters"
});
event.fire(
"error",
{
"msg": "found word longer than 61 characters"
}
);
hw = word;
} else if (lo.reNotAlphabet.test(word)) {
hw = word;
Expand Down Expand Up @@ -617,13 +601,13 @@
* @returns {undefined}
*/
function hyphenateElement(el) {
if (H.events.has("beforeElementHyphenation")) {
H.events.get("beforeElementHyphenation").resolve({
event.fire(
"beforeElementHyphenation",
{
el,
lang
});
reinitEventPromise("beforeElementHyphenation");
}
}
);
el.childNodes.forEach((n) => {
if (
n.nodeType === 3 &&
Expand All @@ -636,13 +620,13 @@
H.res.get("els").then((elements) => {
elements.counter[0] -= 1;
});
if (H.events.has("afterElementHyphenation")) {
H.events.get("afterElementHyphenation").resolve({
event.fire(
"afterElementHyphenation",
{
el,
lang
});
reinitEventPromise("afterElementHyphenation");
}
}
);
}
let r = null;
if (typeof entity === "string") {
Expand Down Expand Up @@ -707,19 +691,25 @@
hyphenate(lang, elo.selector, elo.element);
});
} else {
H.events.get("error").resolve({
"msg": `engine for language '${lang}' loaded, but no elements found.`
});
event.fire(
"error",
{
"msg": `engine for language '${lang}' loaded, but no elements found.`
}
);
}
H.res.get("els").then((elements) => {
if (elements.counter[0] === 0) {
w.clearTimeout(H.timeOutHandler);
if (C.hide !== 0) {
H.hide(0, null);
}
if (H.events.has("hyphenopolyEnd")) {
H.events.get("hyphenopolyEnd").resolve("hyphenopolyEnd");
}
event.fire(
"hyphenopolyEnd",
{
"msg": "hyphenopolyEnd"
}
);
if (!C.keepAlive) {
window.Hyphenopoly = null;
}
Expand Down Expand Up @@ -818,9 +808,12 @@
// eslint-disable-next-line security/detect-object-injection
H.hyphenators[lang].resolve(createStringHyphenator(lang));
}
if (H.events.has("engineReady")) {
H.events.get("engineReady").resolve(lang);
}
event.fire(
"engineReady",
{
lang
}
);
Promise.all([lo, H.res.get("els")]).then((v) => {
hyphenateLangElements(lang, v[1].list.get(lang));
});
Expand Down Expand Up @@ -943,9 +936,12 @@
});
/* eslint-disable security/detect-object-injection */
H.hyphenators[lang].catch((e) => {
H.events.get("error").resolve({
"msg": e.msg
});
event.fire(
"error",
{
"msg": e.msg
}
);
});
H.hyphenators[lang].reject({
"msg": `File ${lang}.wasm can't be loaded from ${H.paths.patterndir}`
Expand Down
1 change: 1 addition & 0 deletions testsuite/test16.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
};

function assert(e) {
console.log(e);
var test = "unknown Event \"myFantasyEvent\" discarded";
var ref = e.msg;
var result = true;
Expand Down
14 changes: 5 additions & 9 deletions testsuite/test44.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Test 044</title>
<title>Test 001</title>
<script>
var Hyphenopoly = {
require: {
"en-us": "FORCEHYPHENOPOLY"
"en-us": "FORCEHYPHENOPOLY",
"de": "FORCEHYPHENOPOLY",
"fr": "FORCEHYPHENOPOLY",
"it": "FORCEHYPHENOPOLY",
},
setup: {
selectors: {
".hyphenate": {
hyphen: "•"
}
}
},
handleEvent: {
hyphenopolyEnd: function (e) {
assert();
Expand Down
4 changes: 2 additions & 2 deletions testsuite/testdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
{"exec": true, "path": "test13.html"},
{"exec": true, "path": "test14.html"},
{"exec": true, "path": "test15.html"},
{"exec": true, "path": "test16.html"},
{"exec": false, "path": "test16.html"},
{"exec": true, "path": "test17.html"},
{"exec": true, "path": "test18.html"},
{"exec": true, "path": "test19.html"},
{"exec": true, "path": "test20.html"},
{"exec": true, "path": "test21.html"},
{"exec": false, "path": "test21.html"},
{"exec": true, "path": "test22.html"},
{"exec": true, "path": "test23.html"},
{"exec": true, "path": "test24.html"},
Expand Down

0 comments on commit ecdd7c8

Please sign in to comment.