Skip to content

Commit

Permalink
feat(core registry): Implement sort_early Pattern option.
Browse files Browse the repository at this point in the history
When scanning a document for patterns, resort patterns and set those with a
sort_early property to the beginning of the initialization chain.

NOTE: Only use when necessary. It's not guaranteed that a pattern with
sort_early is set before another pattern with sort_early. Patterns which
are registered later will have a higher chance to be sorted before
others. Last come, first serve.
  • Loading branch information
thet committed Apr 26, 2023
1 parent 61953d8 commit c357b23
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/core/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,24 @@ const registry = {
},

orderPatterns(patterns) {
// Resort patterns and set those with `sort_early` to the beginning.
// NOTE: Only use when necessary and it's not guaranteed that a pattern
// with `sort_early` is set to the beginning. Last come, first serve.
for (const name of [...patterns]) {
if (registry[name]?.sort_early) {
patterns.splice(patterns.indexOf(name), 1);
patterns.unshift(name);
}
}

// Always add pat-validation as first pattern, so that it can prevent
// other patterns from reacting to submit events if form validation
// fails.
if (patterns.includes("validation")) {
patterns.splice(patterns.indexOf("validation"), 1);
patterns.unshift("validation");
}

// Add clone-code to the very beginning - we want to copy the markup
// before any other patterns changed the markup.
if (patterns.includes("clone-code")) {
Expand Down

0 comments on commit c357b23

Please sign in to comment.