Skip to content

Commit 00b66fe

Browse files
committed
feat(pat inject): Dispatch a patterns-injected-delayed event 10ms after the injection has been done and pass the injected content with it. This allows to re-scan the injected content in cases where a pattern wasn't registered at injection time.
1 parent e740a88 commit 00b66fe

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/pat/inject/inject.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ const inject = {
10891089
},
10901090
};
10911091

1092-
$(document).on("patterns-injected.inject", (ev, cfg, trigger, injected) => {
1092+
$(document).on("patterns-injected.inject", async (ev, cfg, trigger, injected) => {
10931093
/* Listen for the patterns-injected event.
10941094
*
10951095
* Remove the "loading-class" classes from all injection targets and
@@ -1108,6 +1108,17 @@ $(document).on("patterns-injected.inject", (ev, cfg, trigger, injected) => {
11081108
if (injected.nodeType !== TEXT_NODE && injected !== COMMENT_NODE) {
11091109
registry.scan(injected, null, { type: "injection", element: trigger });
11101110
$(injected).trigger("patterns-injected-scanned");
1111+
1112+
await utils.timeout(10); // Wait a bit before dispatching next event.
1113+
injected.dispatchEvent(
1114+
new CustomEvent("patterns-injected-delayed", {
1115+
bubbles: true,
1116+
cancelable: true,
1117+
detail: {
1118+
injected: injected,
1119+
},
1120+
})
1121+
);
11111122
}
11121123
});
11131124

src/pat/inject/inject.test.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,11 +559,45 @@ describe("pat-inject", function () {
559559
var $div = $('<div id="someid" />');
560560
$("#lab").empty().append($a).append($div);
561561
var callback = jest.fn();
562-
$(document).on("patterns-injected", callback);
562+
$(document).on("patterns-injected-scanned", callback);
563563
pattern.init($a);
564564
$a.trigger("click");
565-
answer("<html><body>" + '<div id="someid">repl</div>' + "</body></html>");
565+
answer(`
566+
<html>
567+
<body>
568+
<div id="someid">
569+
<div>repl</div>
570+
</div>
571+
</body>
572+
</html>
573+
`);
574+
await utils.timeout(1); // wait a tick for async to settle.
575+
expect(callback).toHaveBeenCalled();
576+
577+
spy_ajax.mockRestore();
578+
});
579+
580+
it("triggers the patterns-injected-delayed event 10ms after the injected content has been scanned", async function () {
581+
const spy_ajax = jest.spyOn($, "ajax").mockImplementation(() => deferred);
582+
var $a = $('<a class="pat-inject" href="test.html#someid">link</a>');
583+
var $div = $('<div id="someid" />');
584+
$("#lab").empty().append($a).append($div);
585+
var callback = jest.fn();
586+
$(document.body).on("patterns-injected-delayed", callback);
587+
pattern.init($a);
588+
$a.trigger("click");
589+
answer(`
590+
<html>
591+
<body>
592+
<div id="someid">
593+
<div>repl</div>
594+
</div>
595+
</body>
596+
</html>
597+
`);
566598
await utils.timeout(1); // wait a tick for async to settle.
599+
expect(callback).not.toHaveBeenCalled();
600+
await utils.timeout(10); // wait a tick for async to settle.
567601
expect(callback).toHaveBeenCalled();
568602

569603
spy_ajax.mockRestore();

0 commit comments

Comments
 (0)