Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugins helpers #4274

Merged
merged 13 commits into from
Nov 23, 2023
Merged

Plugins helpers #4274

merged 13 commits into from
Nov 23, 2023

Conversation

dbatiste
Copy link
Contributor

@dbatiste dbatiste commented Nov 22, 2023

US160427

This PR adds plugins helpers - registerPlugin, getPlugins, and getPlugin to enable implementors to define plugins without consumers of those plugins being required to import them directly. BSI or a higher level module is responsible for importing the registrations.

Other options were considered, including deferring the registrations or by accepting an async provider method for the plugin, however we do not want plugin implementors assuming that if the registration is deferred, that it would be safe to synchronously import dependencies within the registration module. Typically, we expect projects registrations to be aggregated into a single public export, which means that unrelated plugins could be registered (and synchronous imports loaded) as soon as the registration module is requested. The implementor mindset should be geared towards deferring its dependencies until needed, as shown in the readme.

Required for: https://github.com/BrightspaceUI/lms-core/pull/61

@dbatiste dbatiste requested a review from dlockhart November 22, 2023 19:30
@dbatiste dbatiste requested a review from a team as a code owner November 22, 2023 19:30
Copy link
Contributor

Thanks for the PR! 🎉

We've deployed an automatic preview for this PR - you can see your changes here:

URL https://live.d2l.dev/prs/BrightspaceUI/core/pr-4274/

Note

The build needs to finish before your changes are deployed.
Changes to the PR will automatically update the instance.

helpers/plugins.js Outdated Show resolved Hide resolved
helpers/plugins.js Outdated Show resolved Hide resolved
helpers/README.md Outdated Show resolved Hide resolved
helpers/README.md Outdated Show resolved Hide resolved
helpers/README.md Outdated Show resolved Hide resolved
helpers/plugins.js Outdated Show resolved Hide resolved
helpers/plugins.js Outdated Show resolved Hide resolved
if (!pluginSet) {
pluginSet = { plugins: [], requiresSorting: false };
pluginSets.set(setKey, pluginSet);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: not sure if this is better or not, but another option that's 1 less line!

if (!pluginSets.has(setKey)) {
  pluginSets.set({ plugins: [], requiresSorting: false });
}
const pluginSet = pluginSets.get(setKey);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah... I think I might had that initially, but then switched it to only do the single look-up. The difference is probably negligible for the number of plugin sets we'll have though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be O(1) lookup on the Map anyway.

helpers/plugins.js Show resolved Hide resolved
helpers/test/plugins.test.js Show resolved Hide resolved
helpers/test/plugins.test.js Outdated Show resolved Hide resolved
helpers/test/plugins.test.js Show resolved Hide resolved
helpers/test/plugins.test.js Outdated Show resolved Hide resolved

export function getPlugins(setKey) {
if (!pluginsRequested.has(setKey)) pluginsRequested.add(setKey);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be to set something like a requested = true on the set, similar to requiresSorting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I originally considered that but felt wrong because getPlugins would need to add to the pluginSets for the case where there are no registrations. But now that I think about it more, tracking the bit separately isn't really saving anything, whether the plugin Set key is valid or not. I think I will switch it.

@dbatiste dbatiste merged commit d7d018e into main Nov 23, 2023
5 checks passed
@dbatiste dbatiste deleted the dbatiste/plugins branch November 23, 2023 19:25
Copy link

🎉 This PR is included in version 2.162.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants