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

UI: Ember deprecation - addObject, removeObject #25952

Merged
merged 19 commits into from
Mar 25, 2024

Conversation

hashishaw
Copy link
Contributor

@hashishaw hashishaw commented Mar 14, 2024

This PR manages the following deprecation, which is a subset of ember-data:deprecate-array-like:

DEPRECATION: The addObject method on the class ManyArray is deprecated. Use the native array method push instead. [deprecation id: ember-data:deprecate-array-like] This will be removed in ember-data 5.0.

This deprecation caused quite a bit of refactoring, because the deprecated methods would immediately notify Ember lifecycle of the change, but Array.push and Array.splice do not. Additionally, removeObject and addObject etc automatically handled making sure duplicate entries weren't added.

With the new changes, we now need to set the tracked value to the new value with items removed or added. I used a helper we already had to help simplify the changes. There are also a few straggling instances of addObject and removeObject if you search the codebase, but those are actually happening on items of type ArrayProxy so they are still valid. I tried to add comments everywhere that happens.

  • Ent tests pass locally

@hashishaw hashishaw added this to the 1.17.0-rc milestone Mar 14, 2024
@github-actions github-actions bot added the hashicorp-contributed-pr If the PR is HashiCorp (i.e. not-community) contributed label Mar 14, 2024
@@ -144,7 +144,7 @@ export default ApplicationAdapter.extend({
async _updateAllowedRoles(store, { role, backend, db, type = 'add' }) {
const connection = await store.queryRecord('database/connection', { backend, id: db });
const roles = [...(connection.allowed_roles || [])];
const allowedRoles = type === 'add' ? addToArray([roles, role]) : removeFromArray([roles, role]);
const allowedRoles = type === 'add' ? addToArray(roles, role) : removeFromArray(roles, role);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I simplified the interface for the exported methods so that we don't need to wrap the params in an array

Copy link

github-actions bot commented Mar 14, 2024

CI Results:
All Go tests succeeded! ✅

return this.events.some((e) => e.violatedDirective.startsWith('connect-src'));
}),
export default class CspEventService extends Service {
@tracked connectionViolations = [];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Full list of events didn't seem to be used anywhere, and the alias for the getter wasn't updating on the auth-form correctly so I refactored this service to track connectionViolations directly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This util was only used in one component, so I moved the logic back. See #16470

@@ -32,7 +32,6 @@ export default class RolesPageComponent extends Component {
try {
const message = `Successfully deleted role ${model.name}`;
await model.destroyRecord();
this.args.roles.removeObject(model);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Destroying record automatically removes it from the store, so no need to update this.args.roles list directly

@hashishaw hashishaw marked this pull request as ready for review March 19, 2024 21:53
Copy link

github-actions bot commented Mar 19, 2024

Build Results:
All builds succeeded! ✅

Copy link
Contributor

@hellobontempo hellobontempo left a comment

Choose a reason for hiding this comment

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

Nice work tackling this! Mostly just comments and cleanup questions 🏅

export function addToArray([array, string]) {
export function addManyToArray(array, otherArray) {
assert(`Both values must be an array`, Array.isArray(array) && Array.isArray(otherArray));
const newArray = [...array].concat(otherArray);
Copy link
Contributor

Choose a reason for hiding this comment

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

out of curiosity - is there a reason you used concat() here instead of spreading otherArray?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there was, I don't remember it 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TIL: concat will add otherArray as a single item if it isn't an array, which is nice. https://stackoverflow.com/questions/48865710/spread-operator-vs-array-concat

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh neat! 💡

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if these helpers should live in the addon engine, it seems like something we may want access to in the wider app? It also seems like this should be a util as I don't see it used in any templates, which would simplify the extra logic needed to build the helper, but that may be too much scope creep for this PR to relocate. 😶‍🌫️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good idea for a follow-on! I think it will be easier to track the changes outside of this PR

@@ -195,6 +195,7 @@ export default class SecretCreateOrUpdate extends Component {
if (isBlank(item.name)) {
return;
}
// secretData is a KVObject/ArrayProxy so removeObject is fine here
Copy link
Contributor

Choose a reason for hiding this comment

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

thank you for the comment!

} else {
updatedValue = removeFromArray(updatedValue, event.target.value);
}
this.args.model[this.valuePath] = updatedValue;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this.setAndBroadcast handles setting the model, so I don't think line 204 is necessary?

  @action
  setAndBroadcast(value) {
    this.args.model.set(this.valuePath, value);
    this.onChange(this.valuePath, value);
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm matching what the previous line 198 does (either valueArray.addObject(value) or valueArray.removeObject(value). It might be unnecessary but I'm hesitant to change such a far-reaching behavior here

Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 Ah, to me it looks like lines 200 and 202 are adding or removing the object. I don't think 198 valueArray[method](event.target.value); updated the original model value, that appeared left to line 199, which is why the refactor appeared to duplicate that process.

@hashishaw hashishaw enabled auto-merge (squash) March 25, 2024 18:21
@hashishaw hashishaw merged commit 5c18a4e into main Mar 25, 2024
26 checks passed
@hashishaw hashishaw deleted the ui/VAULT-24818/deprecate-addObject branch March 25, 2024 18:31
@hashishaw hashishaw mentioned this pull request May 6, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hashicorp-contributed-pr If the PR is HashiCorp (i.e. not-community) contributed pr/no-changelog ui
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants