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

Suggestion - add a property initialized (Promise object) #126

Open
cyfung1031 opened this issue Aug 25, 2023 · 1 comment
Open

Suggestion - add a property initialized (Promise object) #126

cyfung1031 opened this issue Aug 25, 2023 · 1 comment

Comments

@cyfung1031
Copy link

cyfung1031 commented Aug 25, 2023

I am recently upgrading a UserScript to the latest version and found that the get value issue before init.
Probably you can make a new property in the gmc object, so that it can be much easier to do coding. just like GM.getValue vs GM_getValue.

All I wrote is referring to the following wiki page

https://github.com/sizzlemctwizzle/GM_config/wiki/Asynchronous-read-and-write-migration

I found it mentions multiple ways but none of them is simple or easy to use, when the coder don't want to change so much.

Implementation

const gmc = new GM_config(
{
  'id': 'ThisConfig',
  'fields': {
    'val': {
      'label': 'Value',
      'type': 'text'
    }
  },
  'events': {
    'init': () => {
      // initialization complete
      // value is now available
      gmc.initializedResolve && gmc.initializedResolve();
      gmc.initializedResolve = null;
    }
  }
});
gmc.initialized = new Promise(r=>(gmc.initializedResolve=r));
await gmc.initialized.then();
console.log(gmc.get('val'));

This shall be added to documentation for reference.

OR you can directly add it to the code internally.

  • initializedResolve is stored in the nested scope in internal coding.
const gmc = new GM_config(
{
  'id': 'ThisConfig',
  'fields': {
    'val': {
      'label': 'Value',
      'type': 'text'
    }
  },
  'events': {
    'init': () => {
      // initialization complete
      // value is now available
    }
  }
});
await gmc.initialized.then();
console.log(gmc.get('val'));

Benefit

To upgrade from old version to latest version, just need to add one line.

Before:

let gmc = new GM_config(
{
  'id': 'ThisConfig',
  'fields': {
    'val': {
      'label': 'Value',
      'type': 'text'
    }
  }
});
let val = gmc.get('val');

After:

let gmc = new GM_config(
{
  'id': 'ThisConfig',
  'fields': {
    'val': {
      'label': 'Value',
      'type': 'text'
    }
  }
});
await gmc.initialized.then();
let val = gmc.get('val');

Minor Remarks

await gmc.initialized.then(); syntax is to fulfill some dumb script checkers don't believe await gmc.initialized; is correct.

@cyfung1031 cyfung1031 changed the title Suggestion - add a property on the gmc object Suggestion - add a property initialized (Promise object) Aug 25, 2023
@mrummuka
Copy link

I am recently upgrading a UserScript to the latest version and found that the get value issue before init. Probably you can make a new property in the gmc object, so that it can be much easier to do coding. just like GM.getValue vs GM_getValue.

All I wrote is referring to the following wiki page

https://github.com/sizzlemctwizzle/GM_config/wiki/Asynchronous-read-and-write-migration

I found it mentions multiple ways but none of them is simple or easy to use, when the coder don't want to change so much.

Implementation

const gmc = new GM_config(
{
  'id': 'ThisConfig',
  'fields': {
    'val': {
      'label': 'Value',
      'type': 'text'
    }
  },
  'events': {
    'init': () => {
      // initialization complete
      // value is now available
      gmc.initializedResolve && gmc.initializedResolve();
      gmc.initializedResolve = null;
    }
  }
});
gmc.initialized = new Promise(r=>(gmc.initializedResolve=r));
await gmc.initialized.then();
console.log(gmc.get('val'));

This shall be added to documentation for reference.

OR you can directly add it to the code internally.

* `initializedResolve` is stored in the nested scope in internal coding.
const gmc = new GM_config(
{
  'id': 'ThisConfig',
  'fields': {
    'val': {
      'label': 'Value',
      'type': 'text'
    }
  },
  'events': {
    'init': () => {
      // initialization complete
      // value is now available
    }
  }
});
await gmc.initialized.then();
console.log(gmc.get('val'));

Benefit

To upgrade from old version to latest version, just need to add one line.

Before:

let gmc = new GM_config(
{
  'id': 'ThisConfig',
  'fields': {
    'val': {
      'label': 'Value',
      'type': 'text'
    }
  }
});
let val = gmc.get('val');

After:

let gmc = new GM_config(
{
  'id': 'ThisConfig',
  'fields': {
    'val': {
      'label': 'Value',
      'type': 'text'
    }
  }
});
await gmc.initialized.then();
let val = gmc.get('val');

Minor Remarks

await gmc.initialized.then(); syntax is to fulfill some dumb script checkers don't believe await gmc.initialized; is correct.

+1 for this example implementation ("This shall be added to documentation for reference.") - I finally got my userscript back functioning thanks to this clear and simple example.

Of course, additional things I had to do were also to convert practically all functions to async/await and add this await
to
await gmc.initialized.then();
the beginning of functions were GM_config was used (as I used this all over the place)

Please, do add this to the list of examples.

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

No branches or pull requests

2 participants