Page MenuHomePhabricator

Initialize immediately, throw when accessing node data before merge.
Needs RevisionPublic

Authored by leebyron on Nov 11 2011, 7:01 PM.
Tags
None
Referenced Files
Unknown Object (File)
Fri, Jan 3, 2:58 AM
Unknown Object (File)
Tue, Dec 24, 3:24 AM
Unknown Object (File)
Fri, Dec 20, 5:40 PM
Unknown Object (File)
Nov 28 2024, 5:48 PM
Unknown Object (File)
Nov 27 2024, 8:31 AM
Unknown Object (File)
Nov 27 2024, 7:33 AM
Unknown Object (File)
Nov 27 2024, 4:22 AM
Unknown Object (File)
Nov 27 2024, 4:21 AM

Details

Summary

Rather than treating class initialize method like a behavior in that it waits for the page to be complete, this calls it immediately upon install. This changes the rules of initialize slightly in that you can no longer access Stratcom.getData since the data has not yet merged. This makes sense to me since class installation shouldn't have side effects nor should it rely on a dom being available to reference (eg, class could be loaded in <head>). If you want to look for a node or a nodes data you're better off using a behavior, or at least JX.onload.

To ensure that this rule is abided, it throws an exception if you attempt to call Stratcom.getData before data has been merged.

Test Plan

Loaded up example pages. Checked against tools/m sites.

Diff Detail

Branch
fastinit
Lint
Lint Passed
Unit
No Test Coverage

Event Timeline

leebyron updated this revision to Unknown Object (????).Nov 11 2011, 7:02 PM

.

Is the objective here improved performance?

The direct goal is flexibility, the indirect goal is performance.

Basically I'd like to change the contract of JX.install to say that when a class has been installed, it's ready to be used. The initialize method is the holdout from that contract being true, since it waits to be executed until mergeData.

If this contract is true, it provides the (future) flexibility to use a class before mergeData is called, which can be desirable when you wish to do something as soon as possible, eg "when the class is available" instead of "as soon as DOMContentLoaded fires".

+@cpojer

JX.Vector and JX.Resource have initialize() methods which access the DOM. I'm pretty sure this breaks JX.Resource straight-up if it gets unlucky with where/when it loads.

(For JX.Resource, we could change the implicit scan to an explicit one as discussed in D794. For JX.Vector, maybe the initialize contract is "the document will exist but may not be ready so you can sort of muck with it as long as you don't poke too hard"?)

If you explicitify or otherwise resolve JX.Resource and document the expectations around what initialize() should and shouldn't do in JX.install(), I'll accept this.

cpojer requested changes to this revision.Dec 13 2011, 10:37 PM

Given that JX.Resource only has a single entry point the part that uses the DOM can be moved to JX.Resource.load (call JX.Resource._setup or _initialize on the first run). JX.Vector._viewport can be changed to JX.Vector._getViewport();

I like this change although I'm worried about random issues by accessing the DOM early. What kind of use cases do you intend to cover from running code before dom ready?

chad changed the visibility from "All Users" to "Public (No Login Required)".Jan 11 2017, 3:33 PM