Skip to content

Perform JSON conversion inside getLocal and setLocal#5328

Open
GogoVega wants to merge 2 commits into
node-red:devfrom
GogoVega:local-settings
Open

Perform JSON conversion inside getLocal and setLocal#5328
GogoVega wants to merge 2 commits into
node-red:devfrom
GogoVega:local-settings

Conversation

@GogoVega

Copy link
Copy Markdown
Contributor

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Proposed changes

Discussed in #5322.

  • Perform JSON conversion inside RED.settings.getLocal and RED.settings.setLocal
  • Update existing occurrences
  • Add docs (because I know you like it 😁)

Checklist

  • I have read the contribution guidelines
  • For non-bugfix PRs, I have discussed this change on the forum/slack team.
  • I have run npm run test to verify the unit tests pass
  • I have added suitable unit tests to cover the new/changed functionality

}
if (RED.workspaces.count() > 0) {
const hiddenTabs = JSON.parse(RED.settings.getLocal("hiddenTabs")||"{}");
const hiddenTabs = RED.settings.getLocal("hiddenTabs", {});

@AllanOricil AllanOricil Oct 26, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This was not what I meant. My suggestion was to stop using JSON.parse when not needed

Instead of

hiddenTabs = JSON.parse(RED.settings.getLocal("hiddenTabs") || "[]")

you could do something like

const serializedHiddenTabs = RED.settings.getLocal("hiddenTabs");
parsedHiddenTabs = serializedHiddenTabs? JSON.parse(serializedHiddenTabs) ?? undefined;

I know the amount of time spent parsing "[]" is irrelavant, and your solution works, but I thought it would be good if we never use JSON.parse unless really necessary. Please, don't think I'm being pedantic with this suggestion. I know targeted node-red hardware have a ton of memory, but because I know that parsing json is quite expensive in memory constrained hardware I learned to avoid using it whenever possible.

Comment thread packages/node_modules/@node-red/editor-client/src/js/settings.js
@AllanOricil

AllanOricil commented Oct 29, 2025

Copy link
Copy Markdown
Contributor

Changing getLocal could potentially break nodes that are using it since they will be expecting a string, not an object. If this method was not exposed, then we could do whatever we wanted.

This must be marked as a breaking change

@GogoVega

@knolleary

Copy link
Copy Markdown
Member

We shouldn't be making any changes to this API. It will break existing users.

@GogoVega

Copy link
Copy Markdown
Contributor Author

We can make it non-breaking based on the number of arguments. Agree?

Note: this API is not documented, therefore it's not public...

@AllanOricil

AllanOricil commented Oct 29, 2025

Copy link
Copy Markdown
Contributor

We can make it non-breaking based on the number of arguments. Agree?

Yes, that could work.

function setLocal(key: string, data: any, parse = false): void;
function getLocal(key: string, parse = false): string | object | null;

However, my preference would be creating new methods and leave the existing ones as is.

function setLocalData(key: string, data: unknown): void;
function getLocalData<T = unknown>(key: string): T | null;

Note: this API is not documented, therefore it's not public

No documentation doesn't mean that it isn't used. For example, I'm using undocumented parameters when registering a new history event. I learned about them because I read the code.

@GogoVega

Copy link
Copy Markdown
Contributor Author

I also use undocumented mechanisms and some internal ones 🫣🤫

By that I mean that we do not guarantee the backward compatibility of everything that is internal.

If Nick considers this API to be public (simply not documented), then we will apply backward compatibility.

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