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

Debug Render Tree (for Ember Inspector) #18372

Merged
merged 2 commits into from
Sep 16, 2019
Merged

Debug Render Tree (for Ember Inspector) #18372

merged 2 commits into from
Sep 16, 2019

Conversation

chancancode
Copy link
Member

@chancancode chancancode commented Sep 10, 2019

Introduce debug render tree

  1. Before loading Ember, set ENV._DEBUG_RENDER_TREE = true;

    This controls whether to perform extra bookkeeping needed to make the captureRenderTree API work.

    This has to be set before the ember JavaScript code is evaluated.

    This is usually done by setting one of these...

    window.EmberENV = { _DEBUG_RENDER_TREE: true };
    
    window.ENV = { _DEBUG_RENDER_TREE: true };
    

    ...before the "vendor" <script> tag in index.html.

    Setting the flag after Ember is already loaded will not work correctly. It may appear to work somewhat, but fundamentally broken.

    This is not intended to be set directly. Ember Inspector will enable the flag on behalf of the user as needed.

    This flag is always on in development mode.

    The flag is off by default in production mode, due to the cost associated with the the bookkeeping work.

    The expected flow is that Ember Inspector will ask the user to refresh the page after enabling the feature. It could also offer a feature where the user add some domains to the "always on" list. In either case, Ember Inspector will inject the code on the page to set the flag if needed.

  2. With the flag on, Ember._captureRenderTree() is available.

    It takes the application instance as an argument and returns an array of CapturedRenderNode:

    interface CapturedRenderNode {
      type: 'outlet' | 'engine' | 'route-template' | 'component';
      name: string;
      args: {
        positional: unknown[];
        named: Dict<unknown>;
      };
      instance: unknown;
      bounds: Option<{
        parentElement: Simple.Element;
        firstNode: Simple.Node;
        lastNode: Simple.Node;
      }>;
      children: CapturedRenderNode[];
    }

@chancancode chancancode changed the title Most of the way through the baseline tree stuff Debug Render Tree (for Ember Inspector) Sep 10, 2019
@chancancode chancancode force-pushed the render-tree branch 3 times, most recently from b11090b to f62d281 Compare September 14, 2019 11:43
@chancancode chancancode marked this pull request as ready for review September 14, 2019 15:04
@chancancode chancancode force-pushed the render-tree branch 3 times, most recently from 7435de8 to 1ab8536 Compare September 14, 2019 19:41
@chancancode chancancode force-pushed the render-tree branch 3 times, most recently from 6f8b85c to 33b0e6b Compare September 14, 2019 20:17
@rwjblue

This comment has been minimized.

@RobbieTheWagner
Copy link
Member

@chancancode is it better to use window.EmberENV or window.ENV?

chancancode and others added 2 commits September 16, 2019 08:25
1. Before loading Ember, set `ENV._DEBUG_RENDER_TREE = true;`

   This controls whether to perform extra bookkeeping needed to make
   the `captureRenderTree` API work.

   This has to be set before the ember JavaScript code is evaluated.
   This is usually done by setting one of these...

   ```
   window.EmberENV = { _DEBUG_RENDER_TREE: true };
   ```

   ```
   window.ENV = { _DEBUG_RENDER_TREE: true };
   ```

   ...before the "vendor" `<script>` tag in `index.html`.

   Setting the flag after Ember is already loaded will not work
   correctly. It may appear to work somewhat, but fundamentally broken.

   This is not intended to be set directly. Ember Inspector will enable
   the flag on behalf of the user as needed.

   This flag is always on in development mode.

   The flag is off by default in production mode, due to the cost
   associated with the the bookkeeping work.

   The expected flow is that Ember Inspector will ask the user to
   refresh the page after enabling the feature. It could also offer a
   feature where the user add some domains to the "always on" list. In
   either case, Ember Inspector will inject the code on the page to set
   the flag if needed.

2. With the flag on, `Ember._captureRenderTree()` is available.

   It takes the *application instance* as an argument and returns an
   array of `CapturedRenderNode`:

   ```typescript
   interface CapturedRenderNode {
     type: 'outlet' | 'engine' | 'route-template' | 'component';
     name: string;
     args: {
       positional: unknown[];
       named: Dict<unknown>;
     };
     instance: unknown;
     bounds: Option<{
       parentElement: Simple.Element;
       firstNode: Simple.Node;
       lastNode: Simple.Node;
     }>;
     children: CapturedRenderNode[];
   }
   ```

Co-authored-by: Yehuda Katz <[email protected]>
@chancancode
Copy link
Member Author

@rwwagner90 it is used like this: https://github.com/emberjs/ember.js/blob/master/packages/@ember/-internals/environment/lib/env.ts#L206

Do you have a way to inject the script just before the vendor script tag? If so, you should probably try to detect if the app has already tried to set either of those and then go with it.

If you have to inject at the very beginning of the page.. maybe you want to set window.ENV = window.EmberENV = {}? That way if the app tries to add more things to either, it would still work, as long as they check first and not override it with a new object.

@rwjblue
Copy link
Member

rwjblue commented Sep 16, 2019

We should only use window.EmberENV (usage of window.ENV is discouraged, and only exists as a form of legacy support). I’ll do a separate PR to add a deprecation if we find and use window.ENV.

*/
export default function captureRenderTree(app: Owner): CapturedRenderNode[] {
let env = expect(
app.lookup<Environment>('service:-glimmer-environment'),
Copy link
Member

Choose a reason for hiding this comment

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

Do we have stripping for this in Ember's own packages?

packages/ember/index.js Show resolved Hide resolved
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.

3 participants