Skip to content

Meta getProperty mutates underlying storage #40

@danielgtaylor

Description

@danielgtaylor

This leads to non-obvious behavior which I think we need to properly define or change. Here is the implementation as it stands:

pimitives.es6#34-40

class Meta {
  getProperty(name, value) {
    if (!this.meta[name]) {
      this.meta[name] = registry.toType(value);
    }

    return this.meta[name];
  }
}

Here is the surprising part:

const element = new ElementType();

console.log(element.toCompactRefract());
// => ['element', {}, {}, null]

// Access the non-existing name property
element.name;

// What? Now there is a meta name for some reason!?
console.log(element.toCompactRefract());
// => ['element', {name: ''}, {}, null]

// Try to get something from meta with a default if not found
element.getProperty('foo', 'bar');

// What? Now my default value is getting serialized!
console.log(element.toCompactRefract());
// => ['element', {name: '', foo: 'bar'}, {}, null]

I'm sorry I glossed over this in the initial review! I believe the code is written this way to cache the conversion to refract element classes for the default value, but I think we need to either do that conversion each time or keep a separate cache that doesn't mutate the original meta values..

Thoughts @smizell?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions