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

Added a reinitialize method to Fides.js #4812

Merged
merged 23 commits into from
Apr 21, 2024
Merged

Added a reinitialize method to Fides.js #4812

merged 23 commits into from
Apr 21, 2024

Conversation

guncha
Copy link
Contributor

@guncha guncha commented Apr 17, 2024

Closes https://ethyca.atlassian.net/browse/PROD-1967

Description Of Changes

Adds a new reinit() method to FideJS global as well as a prevConfig property that stores the configuration that was used to call init. There are also minor changes to the init functions to treat them like instance methods, which they actually are, instead of programming with globals.

Opening this now to get some early feedback. This is being used in my WT branch to "move" the embed to the slideout and back. It works fine so I don't expect any big changes though perhaps we want to consider supporting multiple embeds per page (this already works basically, but would need to be more carefully considered). We can discuss in the forthcoming PR.

Pre-Merge Checklist

  • All CI Pipelines Succeeded
  • Update CHANGELOG.md

@guncha guncha requested a review from NevilleS April 17, 2024 19:57
Copy link

vercel bot commented Apr 17, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
fides-plus-nightly ⬜️ Ignored (Inspect) Visit Preview Apr 21, 2024 8:12pm

Copy link

cypress bot commented Apr 17, 2024

Passing run #7368 ↗︎

0 4 0 0 Flakiness 0
⚠️ You've recorded test results over your free plan limit.
Upgrade your plan to view test results.

Details:

Merge 9707fc2 into 0d30590...
Project: fides Commit: 617bf4db0a ℹ️
Status: Passed Duration: 00:33 💡
Started: Apr 21, 2024 8:23 PM Ended: Apr 21, 2024 8:24 PM

Review all test suite changes for PR #4812 ↗︎

Copy link
Contributor

@NevilleS NevilleS left a comment

Choose a reason for hiding this comment

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

This seems pretty reasonable so far - left some questions and nits.

clients/fides-js/src/fides-tcf.ts Show resolved Hide resolved
@@ -172,7 +172,7 @@ const init = async (config: FidesConfig) => {
// Initialize the CMP API early so that listeners are established
initializeTcfCmpApi();
if (initialFides) {
Object.assign(_Fides, initialFides);
Object.assign(this, initialFides);
Copy link
Contributor

Choose a reason for hiding this comment

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

Explain why you think this is preferable - and are we sure it's safe to refactor like this?

Copy link
Contributor Author

@guncha guncha Apr 18, 2024

Choose a reason for hiding this comment

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

This is preferable because it's closer to functional style which is preferable because it makes code easier to reason about and maintain. By functional I mean functions that take all the data they operate on as arguments and just return values, without modifying any external state (including the arguments). Pure idempotent functions are the best, which init() certainly isn't, but it's a matter of degree. In JS as well as C++ and any other OOP language I know, this is just another argument to the function so Fides.init() is syntax sugar for init(Fides).

It also makes it easier to unit test when you're not relying on some captured state. We could import the init function in a test file and call it repeatedly on different objects to test its behavior.

As for safety, I think that's a very relevant question. If you're working with global state, you have to consider any other code that might be using and changing it. The more visible some state is, the more code you have to audit, with the global state being the most visible of all. Luckily, _Fides is only visible to this module so it's not too bad. The only possible unsafety I can think of with this change is if someone is doing something like this:

const { init } = Fides;
init();

But they really shouldn't be doing that in the first place 😄

clients/fides-js/src/fides.ts Show resolved Hide resolved
clients/fides-js/src/lib/consent-types.ts Outdated Show resolved Hide resolved
@guncha guncha changed the title Add a method to reinitialize Fides.js Add a reinitialize method to Fides.js Apr 19, 2024
@guncha guncha changed the title Add a reinitialize method to Fides.js Added a reinitialize method to Fides.js Apr 19, 2024
if (!this.prevConfig || !this.initialized) {
throw new Error("Fides must be initialized before reinitializing");
}
return this.init(this.prevConfig);
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this is maybe really Hard™️, but I think we need to ensure that Fides.reinitialize cleans up our existing rendered DOM elements automaticaly... 😄

I wrote some quick Cypress specs to exercise this and toggled fides_embed off and on. Since reinitialize doesn't wipe previously rendered elements, I could get into a fun state where I had both an embedded and overlay banner running simultaneously:
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you mean, that's a FEATURE!

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, I added some code to initOverlay to unmount previously rendered instances, which makes this work swimmingly.

@NevilleS
Copy link
Contributor

OK @guncha, this is ready for your re-review. I think it looks pretty good...

Copy link
Contributor

@NevilleS NevilleS left a comment

Choose a reason for hiding this comment

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

This looks solid to me... if @guncha is OK with the edits I made, I think it's ready to ship

Copy link
Contributor Author

@guncha guncha left a comment

Choose a reason for hiding this comment

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

LGTM!

clients/fides-js/src/lib/consent-types.ts Show resolved Hide resolved
clients/fides-js/src/lib/initOverlay.ts Show resolved Hide resolved
@NevilleS NevilleS merged commit 1bc52de into main Apr 21, 2024
13 checks passed
@NevilleS NevilleS deleted the ga-reinit branch April 21, 2024 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants