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

Rewrite 'Preferences' to make it async #4482

Merged
merged 3 commits into from
Apr 3, 2014
Merged

Rewrite 'Preferences' to make it async #4482

merged 3 commits into from
Apr 3, 2014

Conversation

Snuffleupagus
Copy link
Collaborator

/cc @brendandahl Please look at this, and see if I'm on the right track with the rewrite.

This PR makes a number of changes to Preferences:

  • Remove the class-like implementation, so that it's instead invoked once when the viewer loads.

    This change is necessary if we want to support preferences for the viewer itself, and not just when opening documents. (The second commit makes use of this to add a pref that enables the hand tool by default.)
  • Changes all functions to be asynchronous.
  • Adds docs. Some of it might be unnecessary or too verbose, so feedback is welcome on this.

Fixes #4459.

@brendandahl
Copy link
Contributor

Getting better, but it can be made simpler if you use promises to their fullest and remember that .then creates a promise and is resolved with whatever is returned in that function(or if you return a promise, that promise's resolved value). For example the set function:

  set: function preferencesSet(name, value) {
    return this.initializedPromise.then(function () {
      if (DEFAULT_PREFERENCES[name] === undefined) {
        throw new Error('preferencesSet: \'' + name + '\' is undefined.');
      } else if (value === undefined) {
        throw new Error('preferencesSet: no value is specified.');
      }
      var valueType = typeof value;
      var defaultType = typeof DEFAULT_PREFERENCES[name];

      if (valueType !== defaultType) {
        if (valueType === 'number' && defaultType === 'string') {
          value = value.toString();
        } else {
          throw new Error('Preferences_set: \'' + value + '\' is a \"' +
                           valueType + '\", expected \"' + defaultType + '\".');
        }
      } else {
        if (valueType === 'number' && (value | 0) !== value) {
          throw new Error('Preferences_set: \'' + value +
                           '\' must be an \"integer\".');
        }
      }
      this.prefs[name] = value;
      return this._writeToStorage(this.prefs); // <<!! should return promise
    }.bind(this));
  },

@brendandahl
Copy link
Contributor

The above is also nice since you don't have to worry about rejecting the promise and can use regular exceptions.

@Snuffleupagus
Copy link
Collaborator Author

@brendandahl Good points, thanks for the quick feedback!
I've pushed an updated version, that I hope should address your comments.

FirefoxCom.requestSync('setPreferences', prefObj);
Preferences._writeToStorage = function (prefObj) {
return new Promise(function (resolve) {
FirefoxCom.requestSync('setPreferences', prefObj);
Copy link
Contributor

Choose a reason for hiding this comment

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

can you make it with async "request" call?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There seems to be something wrong with the request method, I'll check if it's easy to address.

Edit: Ignore the above, I apparently missed that you have to change the methods in PdfStreamConverter.jsm for this to work properly. Revised patch coming up.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done, thanks for catching this!

@Snuffleupagus
Copy link
Collaborator Author

@brendandahl Could you please look at this again, and see if I have managed to address all of your comments.

Slightly off-topic: The main reason that prompted me to rewrite this, was actually to make it possible to easily add a pref to enable/disable WebGL (once #4286 lands).

@yurydelendik
Copy link
Contributor

/botio-linux preview

@pdfjsbot
Copy link

pdfjsbot commented Apr 2, 2014

From: Bot.io (Linux)


Received

Command cmd_preview from @yurydelendik received. Current queue size: 3

Live output at: http://107.21.233.14:8877/31d10bc354d18c5/output.txt

* or every time the viewer is loaded.
*/
var Preferences = {
prefs: {},
Copy link
Contributor

Choose a reason for hiding this comment

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

Will prefs: Object.create(DEFAULT_PREFERENCES), be better?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't know, what's the advantage of doing that?

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking you would want to populate prefs with default values

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The way things currently are set up (this wasn't changed by this PR), is that if/when a pref doesn't exist in prefs, it's just fetched from DEFAULT_PREFERENCES instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, I was just wondering just that, {} has meaning of the new object -- I would expect null in this case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I would expect null in this case.

That would complicate the code more, and after thinking about this I've reached the conclusion that it's probably nicer to do what you suggested initially anyway.

@yurydelendik
Copy link
Contributor

Looks good after the comments above addressed (and removing [wip] from the title will help a little :)

@yurydelendik
Copy link
Contributor

/botio-linux preview

@pdfjsbot
Copy link

pdfjsbot commented Apr 2, 2014

From: Bot.io (Linux)


Received

Command cmd_preview from @yurydelendik received. Current queue size: 0

Live output at: http://107.21.233.14:8877/86e4ded51fd7f81/output.txt

@Snuffleupagus Snuffleupagus changed the title [WIP] Rewrite 'Preferences' to make it async Rewrite 'Preferences' to make it async Apr 3, 2014
@yurydelendik
Copy link
Contributor

/botio-windows preview

@pdfjsbot
Copy link

pdfjsbot commented Apr 3, 2014

From: Bot.io (Windows)


Received

Command cmd_preview from @yurydelendik received. Current queue size: 0

Live output at: http://107.22.172.223:8877/4da7afe83358619/output.txt

@yurydelendik
Copy link
Contributor

Looks good, thank you

yurydelendik added a commit that referenced this pull request Apr 3, 2014
Rewrite 'Preferences' to make it async
@yurydelendik yurydelendik merged commit 3e17021 into mozilla:master Apr 3, 2014
@Snuffleupagus Snuffleupagus deleted the prefs-async-v2 branch April 3, 2014 14:29
@Snuffleupagus
Copy link
Collaborator Author

@brendandahl and @yurydelendik Thanks to the both of you for your help with this PR!

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.

Rewrite Preferences to make them properly async
4 participants