Skip to content

Commit 0f70c4f

Browse files
committed
feat(core basepattern): Lazy loading patterns.
Support for lazy initialization of Patterns. The import is done eagerly, but the initialization only when an intersection observer detects that a pattern is being shown.
1 parent dcaa286 commit 0f70c4f

1 file changed

Lines changed: 51 additions & 33 deletions

File tree

src/core/basepattern.js

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class BasePattern {
2121
parser_group_options = true;
2222
parser_multiple = undefined;
2323
parser_inherit = true;
24+
init_lazy = false;
2425

2526
constructor(el, options = {}) {
2627
// Make static variables available on instance.
@@ -59,45 +60,62 @@ class BasePattern {
5960
// Both limitations are gone in next tick.
6061
//
6162
window.setTimeout(async () => {
62-
if (typeof this.el[`pattern-${this.name}`] !== "undefined") {
63-
// Do not reinstantiate
64-
log.debug(`Not reinstatiating the pattern ${this.name}.`, this.el);
65-
66-
// Notify that not instantiated
67-
this.el.dispatchEvent(
68-
new Event(`not-init.${this.name}.patterns`, {
69-
bubbles: true,
70-
cancelable: false,
71-
})
72-
);
73-
return;
63+
if (this.init_lazy === true) {
64+
const observer = new IntersectionObserver(async (entries) => {
65+
for (const entry of entries) {
66+
if (entry.isIntersecting) {
67+
await this.pattern_init(options);
68+
observer.unobserve(entry.target);
69+
}
70+
}
71+
});
72+
73+
observer.observe(this.el);
74+
} else {
75+
await this.pattern_init(options);
7476
}
77+
}, 0);
78+
}
7579

76-
// Create the options object by parsing the element and using the
77-
// optional options as default.
78-
this.options =
79-
this.parser?.parse(
80-
this.el,
81-
options,
82-
this.parser_multiple,
83-
this.parser_inherit,
84-
this.parser_group_options
85-
) ?? options;
86-
87-
// Store pattern instance on element
88-
this.el[`pattern-${this.name}`] = this;
89-
90-
// Initialize the pattern
91-
await this.init();
80+
async pattern_init(options) {
81+
if (typeof this.el[`pattern-${this.name}`] !== "undefined") {
82+
// Do not reinstantiate
83+
log.debug(`Not reinstatiating the pattern ${this.name}.`, this.el);
9284

93-
// Notify that now ready
85+
// Notify that not instantiated
9486
this.el.dispatchEvent(
95-
new Event(`init.${this.name}.patterns`, {
87+
new Event(`not-init.${this.name}.patterns`, {
9688
bubbles: true,
97-
cancelable: true,
89+
cancelable: false,
9890
})
9991
);
100-
}, 0);
92+
return;
93+
}
94+
95+
// Create the options object by parsing the element and using the
96+
// optional options as default.
97+
this.options =
98+
this.parser?.parse(
99+
this.el,
100+
options,
101+
this.parser_multiple,
102+
this.parser_inherit,
103+
this.parser_group_options
104+
) ?? options;
105+
106+
// Store pattern instance on element
107+
this.el[`pattern-${this.name}`] = this;
108+
109+
// Initialize the pattern
110+
await this.init();
111+
112+
// Notify that now ready
113+
this.el.dispatchEvent(
114+
new Event(`init.${this.name}.patterns`, {
115+
bubbles: true,
116+
cancelable: true,
117+
})
118+
);
101119
}
102120

103121
async init() {
@@ -110,7 +128,7 @@ class BasePattern {
110128
dom: this.el,
111129
action: action,
112130
...options,
113-
}
131+
};
114132
this.el.dispatchEvent(events.update_event(options));
115133
}
116134

0 commit comments

Comments
 (0)