Skip to content

Commit a532ebf

Browse files
committed
fix(core events): await_pattern_init - check for event coming from correct element.
Check if the init/not-init events were thrown from the Pattern's own element. When a child element did an unsuccessful Pattern init (rejected because already initialized) and at the same time the parent element also tried to initialized the same Pattern await_pattern_init could fail. The not-init event bubbled up which was incorrectly catched by await_pattern_init on the parent element.
1 parent 0c3dea7 commit a532ebf

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,35 @@ describe("core.events tests", () => {
133133
// catched.
134134
expect(true).toBe(true);
135135
});
136+
137+
it("Allows to initialize the same pattern in a nested structure.", async () => {
138+
class Pat extends BasePattern {
139+
static name = "tmp";
140+
static trigger = ".pat-tmp";
141+
init() {}
142+
}
143+
144+
const div = document.createElement("div");
145+
const span = document.createElement("span");
146+
div.append(span);
147+
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);
161+
162+
// If test reaches this expect statement, all is fine.
163+
expect(true).toBe(true);
164+
});
136165
});
137166

138167
describe("2 - event factories", () => {

0 commit comments

Comments
 (0)