canjs / can-component

Custom elements and widgets
https://canjs.com/doc/can-component.html
MIT License
8 stars 8 forks source link

Components should serialize to empty objects #321

Open phillipskevin opened 5 years ago

phillipskevin commented 5 years ago

We should add a Symbol so that component instances serialize to empty objects, this way if a ViewModel has component instances on it, the serialization will not be extremely slow.

Related to https://github.com/canjs/devtools/issues/41#issuecomment-445121130.

justinbmeyer commented 5 years ago

Could we make can-reflect avoid serializing elements all together?

phillipskevin commented 5 years ago

Serializing elements is not the issue.

canReflect.serialize does avoid serializing elements because built ins are directly returned.

The performance problem is specific to devtools because it doesn't always use serialize directly. It is using canReflect.getOwnKeys so that it can recursively get the names for objects.

I've fixed this in devtools.

phillipskevin commented 5 years ago

That being said, what should components serialize to?

Right now, serializing a component like:

const C = Component.extend({
  tag: "a-pp",
  view: `<p>Hello</p>`,
  ViewModel: {}
});

Reflect.serialize( new C() );

...gives something like:

{
  "_initialArgs": [
    null,
    null
  ],
  "element": {
    "canFri Dec 07 2018 15:21:07 GMT-0600 (Central Standard Time)": "2",
    "viewModel": {}
  },
  "viewModel": {},
  "nodeList": [
    {}
  ],
  "_torndown": false
}

Is this useful? It's not actually "serialized" since the element is an actual element.