question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What's the best way to get primitive properties from child/parent?

See original GitHub issue

It seems like anything you want to pass over the postmessage, you just put in methods. But what if you want to pass over static properties, like 'max': 5000. Obviously you could use a getter-ish method and slap it in methods, but I’m wondering if that’s really the best way, or if there could be some way to access properties immediately from the connection.promise’s resonse? eg

connection.promise.then(child =>  console.log(child.max));

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Aaroniuscommented, Apr 18, 2017

You have to go through methods like you’re talking about. So in your child iframe you might have something like this:

const childModel = {
  max: 5000
};

const connection = Penpal.connectToParent({
  methods: {
    get(name) {
      return childModel[name];
    }
});

And then from the parent:

connection.promise.then(child => child.get('max').then(max => console.log(max)));
1reaction
loganvolkerscommented, May 5, 2017

I like the configuration use case, such as using debug to tell a child to operate in debug mode.

IMHO the most common mistake with @gajewsk2’s proposal would be someone expects Penpal to create a proxy object instead of freezing the property.

var object = {};
object.debug: true;
object.getDebug: function(){ return object.debug; };

Penpal.connectToParent(object);

object.debug = false;

// Later
child.getDebug().then(debug => debug === child.debug) // Why aren't they equal?

child.debug.get().then(...) // OR! should we provide async getters for props so that mutations can go through

A couple suggestions here:

  • Separate methods from metadata e.g Penpal.connectToParent(methods,metadata). Would help clarify the difference between the static frozen properties and the dynamic methods. Access via parent._metadata.debug.
  • Use Object.freeze to freeze the properties.

** Aside **

For Parent->Child communication the fastest approach wouldn’t used PostMessage at all, it would use query params or hash params. That way if you were trying to get the Client to debug, it would happen immediately instead of only after the handshake is complete. I’m not suggesting we do this, just reminding about speediness of passing configuration details.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Most elegant way to save only primitive properties of a ...
I need to make a simple snapshot of it with only primitive properties. What's the most elegant way how to do it? Javascript...
Read more >
How do primitive values get their properties? - 2ality
We'll look at: Getting property values; Invoking property values (a.k.a. method calls); Setting property values.
Read more >
3.4.1.1 Constructor-based dependency injection - Spring
The Spring team generally advocates setter injection, because large numbers of constructor arguments can get unwieldy, especially when properties are optional.
Read more >
How to add child list items to parent level list properties in C# ...
Go bottom up: C#. for each ( var child in children ){ var childParent = childParents.Single( cp => cp.id == child.id); childParent.Children.
Read more >
Methods of primitives - The Modern JavaScript Tutorial
There are 7 primitive types: string , number , bigint , boolean , symbol , null and undefined . An object. Is capable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found