Skip to content

Commit 7c85ee5

Browse files
committed
core base constructor: Await for initalization.
Core Base: ``await`` for initalization in the base class constructor, so that the ``init`` event is really thrown after initialization is done.
1 parent af8e62f commit 7c85ee5

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272

7373
### Technical
7474

75+
- Core Base: ``await`` for initalization in the base class constructor, so that the ``init`` event is really thrown after initialization is done.
7576
- pat calendar: Explicitly import JavaScript language files to avoid missing Webpack TypeScript loader errors.
7677
- Use Babel for all files, allowing latest JavaScript features everywhere.
7778
- Add example `minimalpattern`.

src/core/base.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Older Patternslib patterns on the other hand have a single global scope for
1212
* all DOM elements.
1313
*/
14-
14+
import "regenerator-runtime/runtime"; // needed for ``await`` support
1515
import $ from "jquery";
1616
import Registry from "./registry";
1717
import logging from "./logging";
@@ -41,14 +41,14 @@ const initBasePattern = function ($el, options, trigger) {
4141
return pattern;
4242
};
4343

44-
const Base = function ($el, options, trigger) {
44+
const Base = async function ($el, options, trigger) {
4545
if (!$el.jquery) {
4646
$el = $($el);
4747
}
4848
this.$el = $el;
4949
this.el = $el[0];
5050
this.options = $.extend(true, {}, this.defaults || {}, options || {});
51-
this.init($el, options, trigger);
51+
await this.init($el, options, trigger);
5252
this.emit("init");
5353
};
5454

src/core/base.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import registry from "./registry";
22
import $ from "jquery";
33
import Base from "./base";
4+
import utils from "./utils";
45
import _ from "underscore";
56

67
describe("pat-base: The Base class for patterns", function () {
@@ -157,4 +158,31 @@ describe("pat-base: The Base class for patterns", function () {
157158
})
158159
);
159160
});
161+
162+
it("triggers the init event after init has finished.", async function (done) {
163+
const Tmp = Base.extend({
164+
name: "example",
165+
trigger: "pat-example",
166+
init: async function () {
167+
// await to actually give the Base constructor a chance to
168+
// throw it's event before we throw it here.
169+
await utils.timeout(1);
170+
this.el.dispatchEvent(new Event("init_done"));
171+
},
172+
});
173+
const node = document.createElement("div");
174+
node.setAttribute("class", "pat-example");
175+
const event_list = [];
176+
node.addEventListener("init_done", () => event_list.push("pat init"));
177+
$(node).on("init.example.patterns", () => event_list.push("base init"));
178+
new Tmp(node);
179+
180+
// await until all asyncs are settled. 1 event loop should be enough.
181+
await utils.timeout(1);
182+
183+
expect(event_list[0]).toBe("pat init");
184+
expect(event_list[1]).toBe("base init");
185+
186+
done();
187+
});
160188
});

0 commit comments

Comments
 (0)