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

Multi-value support for GM_getValues, GM_setValues, GM_deleteValues? #2045

Closed
tophf opened this issue May 1, 2024 · 6 comments
Closed

Multi-value support for GM_getValues, GM_setValues, GM_deleteValues? #2045

tophf opened this issue May 1, 2024 · 6 comments

Comments

@tophf
Copy link

tophf commented May 1, 2024

Maybe something like this?

  • GM_getValues(['foo', 'bar']) -> {foo: 1, bar: 2} or {foo: 1} if bar is missing
  • GM_getValues({foo: 1, bar: 2}) -> providing the default values.
  • GM_getValues() or GM_getValues(null) or GM_getValues(undefined) -> read the entire storage

  • GM_setValues({foo: 1, bar: 2})
  • GM_deleteValues(['foo', 'bar'])

Ideally I'd like Violentmonkey/Tampermonkey to avoid preloading all storage data at script start when the script doesn't ask for the legacy GM_xxxValue, in which case multi-value support becomes important because of the overhead to process each value individually. When GM.xxxValue functions are implemented as a native asynchronous call it makes sense from a performance viewpoint to allow specifying multiple values in one call, just like it's done in chrome.storage API.

@derjanb
Copy link
Member

derjanb commented May 1, 2024

Makes sense. I'll add support for this in the next BETA version.

@cyfung1031
Copy link

cyfung1031 commented May 6, 2024

Could there be something "Proxy" ? ( the usage is like localStorage )
So developers do not need to think too much about sync the values along tabs.
and don't need getValue and setValue

Option 1

// only one argument. object. the value is the default value if not exist (so this can make sure there is always a default value)
const gmValues = GM_values({
foo: 1,
bar: 2
});

// gmValues is a Proxy
// the values are obtained at each proxy's get

console.log(await gmValues.foo) // 1
gmValues.foo = 3
console.log(await gmValues.foo) // 3

Option 2

// only one argument. object. the value is the default value if not exist (so this can make sure there is always a default value)
const gmValues = await GM.values({
foo: 1,
bar: 2
});
// values are all cached in gmValues.

// gmValues is a Proxy. All values inside will have GM_addEvenListener. 
// the values will be automatically updated, so getter just read the stored cache value.

console.log(gmValues.foo) // 1 (just the cache)
gmValues.foo = 3
console.log(gmValues.foo) // 3 (the GM.setValue might be not completed at this time, but the setter updated the cache value already)

// if it is changed externally, the gmValues.foo will be also updated.

@tophf
Copy link
Author

tophf commented May 6, 2024

With proxy you can't await the result of setting the value. Either way, it's not related to the idea in this issue, so you should open a new one.

@erosman
Copy link

erosman commented May 6, 2024

If I may add a word, FM introduced multi-value GM storage option in erosman/support#545
Initially, using a new API but changing to an option for the existing API, which mimics the browser.storage.local.get().

@tophf
Copy link
Author

tophf commented May 6, 2024

I thought about extending the existing singular functions with the new syntax, but I decided it would encourage authors to use it directly without first checking for typeof GM.getValues === 'function'.

@derjanb derjanb modified the milestones: 5.2, 5.3 Jul 11, 2024
@derjanb
Copy link
Member

derjanb commented Aug 8, 2024

Fixed at 5.3.6207 (crx|xpi in review)

I've decided against overloading the old API methods, because it would be difficult to detect whether multi-values are supported or not.

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

No branches or pull requests

4 participants