Skip to content

Commit 9c401fc

Browse files
authored
Merge pull request #1136 from Patternslib/fix-await-pattern-init
Fix await pattern init
2 parents 09e99cd + a532ebf commit 9c401fc

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/core/events.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ const await_pattern_init = (pattern) => {
102102
// See: https://stackoverflow.com/a/44746691/1337474
103103
return new Promise((resolve, reject) => {
104104
// Case initialized
105-
pattern.one("init", () => {
105+
pattern.one("init", (e) => {
106+
if (e.target !== pattern.el) {
107+
// Don't handle bubbling init events from child elements. We
108+
// want to check on init events coming directly from this
109+
// Pattern's element.
110+
return;
111+
}
106112
// Resolve promise and unregister the not-init event handler.
107113
remove_event_listener(
108114
pattern.el,
@@ -112,7 +118,13 @@ const await_pattern_init = (pattern) => {
112118
});
113119

114120
// Case not initialized
115-
pattern.one("not-init", () => {
121+
pattern.one("not-init", (e) => {
122+
if (e.target !== pattern.el) {
123+
// Don't handle bubbling not-init events from child elements.
124+
// We want to check on not-init events coming directly from
125+
// this Pattern's element.
126+
return;
127+
}
116128
// Reject promise and unregister the init event handler.
117129
remove_event_listener(
118130
pattern.el,

src/core/events.test.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,30 @@ describe("core.events tests", () => {
134134
expect(true).toBe(true);
135135
});
136136

137-
it("Handles double-registration attempts by rejecting the await_pattern_init promise.", async () => {
137+
it("Allows to initialize the same pattern in a nested structure.", async () => {
138138
class Pat extends BasePattern {
139139
static name = "tmp";
140140
static trigger = ".pat-tmp";
141141
init() {}
142142
}
143143

144-
const el = document.createElement("div");
145-
const instance = new Pat(el);
144+
const div = document.createElement("div");
145+
const span = document.createElement("span");
146+
div.append(span);
146147

147-
await events.await_pattern_init(instance);
148+
new Pat(span);
149+
// need to wait a tick as basepattern initializes also with a
150+
// tick delay.
151+
await utils.timeout(1);
152+
153+
// Next one isn't initialized and throws an bubbling not-init error.
154+
new Pat(span);
155+
const instance_div = new Pat(div);
156+
157+
// The bubbling not-init error would be catched if there wasn't a
158+
// check for the origin of the error which has to be the same as
159+
// the Pattern element.
160+
await events.await_pattern_init(instance_div);
148161

149162
// If test reaches this expect statement, all is fine.
150163
expect(true).toBe(true);

src/core/utils.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,9 @@ describe("debounce ...", function () {
625625
debouncer();
626626
expect(test_func).not.toHaveBeenCalled();
627627
await utils.timeout(1);
628+
await utils.timeout(1);
629+
await utils.timeout(1);
630+
await utils.timeout(1);
628631
expect(test_func).toHaveBeenCalledTimes(1);
629632
});
630633
it("incorrect usage by multi instantiation won't cancel previous runs", async () => {

0 commit comments

Comments
 (0)