Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Let packages define deserializers & view providers as main module methods - #10764

Merged
maxbrunsfeld merged 2 commits into
masterfrom
mb-tweak-new-package-json-fields
Feb 11, 2016
Merged

maxbrunsfeld merged 2 commits into
masterfrom
mb-tweak-new-package-json-fields

Conversation

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

This makes some further changes to the new package.json-based deserializer and view-provider APIs introduced in #9687. After updating some packages (see #9974) to use these new fields, I realized that it was somewhat awkward to specify deserializers and view providers in separate files from the packages's main module. I think it is better if these fields work similarly to consumedServices and providedServices, by specifying names of methods on the package's main module.

Example

package.json

{
  "name": "my-package",
  "main": "index",
  "deserializers": {
    "MyObject": "deserializeMyObject"
  },
  "viewProviders": [
    "buildMyObjectView"
  ]
}

index.js

const MyObject = require('./my-object')

module.exports = {
  activate () {
    // ...
  },

  deserializeMyObject (state) {
    return new MyObject(state)
  },

  buildMyObjectView (model) {
    if (model instanceof MyObject) {
      return new MyObjectView
    }
  }
}

Advantages

  • This approach is more consistent with consumedServices and providedServices, which seem to be working well for a variety of use cases.
  • Many packages currently maintain singleton instances of some view or model object, and keep a reference to it in their main module. This makes it easier for view providers and deserializers to create or update that singleton instance. The alternative would be for the instance to be stored as a global variable in the deserializer file, or in some third file that is required by both the main module and the deserializer.
  • Similarly, this allows the deserializer to interact with any of the package's consumed or provided services with less boilerplate.
  • Overall, I like the idea of the package's main module being the only file that needs global state, and all other files in the package exporting classes or functions.

Drawbacks

  • The main module will be required earlier now, for packages that either 1) have view providers, or 2) have deserializers that are needed immediately. But some code file would have to be required under these conditions regardless, so I think as long as the main module defers its requires effectively, it shouldn't make any difference in terms of load time.

/cc @atom/core

@izuzak

izuzak commented Feb 13, 2016

Copy link
Copy Markdown
Contributor

@maxbrunsfeld Wondering if you can help me figure out something. I'm on 1.7.0-dev-9720541 (latest master version) and doing this:

  1. start Atom
  2. open Settings View
  3. run "Window: reload" from command palette
  4. observe that Settings View is not shown (this is unexpected behavior -- I expected Settings View to be shown)
  5. open devtools console
  6. observe a "No deserializer found" warning in console for the settings view state

Same thing happens for Archive View as well.

Any chance this PR caused this behavior to change/break?

@maxbrunsfeld

Copy link
Copy Markdown
Contributor Author

Thanks for the heads up @izuzak! Looking into this now.

@maxbrunsfeld

Copy link
Copy Markdown
Contributor Author

@izuzak @joshaber Actually, I think this was caused by #10743. We need to load packages before deserializing our state, because packages provide deserializers that are used during state loading. Looking into how we can restore that ordering while addressing the window cascade problem....

@joshaber

Copy link
Copy Markdown
Contributor

Ugh, yeah, good catch. Could we make loading @state separate from deserializing?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants