Sha256: 5057c19a680f9d465f2d1c2d9c4cc581f19aa2583cf9b40b3fbe7681c46b49b6

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

import Resolver from './resolver';

export default class ObjectResolver extends Resolver {
  constructor(fragment, callback, propertyName) {
    super(callback);
    this._fragment = fragment;
    this._resolvers = {};
    this._propertyName = propertyName;
  }

  get(props, seed) {
    this._updateResolvers();

    if (this._propertyName) {
      props = props[this._propertyName];
    }

    if (!props) {
      return null;
    }

    return Object.keys(this._resolvers).reduce((result, key) => {
      const resolver = this._resolvers[key];

      result[key] = resolver.get(props, seed);
      return result;
    }, {...props});
  }

  dispose() {
    Object.keys(this._resolvers).forEach((key) => {
      this._resolvers[key].dispose();
    });
  }

  _updateResolvers() {
    var resolvers = this._resolvers;

    Object.keys(this._fragment || {}).forEach((key) => {
      const resolverProvider = this._fragment[key];

      if (!resolvers[key]) {
        resolvers[key] = this._createResolver(resolverProvider, key);
      }
    });
  }

  _createResolver(provider, key) {
    var handleChange = this._handleChange.bind(this);

    if (typeof provider === 'object') {
      return new ObjectResolver(provider, handleChange, key);
    }
    else {
      return provider(handleChange);
    }
  }
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pageflow-react-0.1.1 js/src/resolvers/object_resolver.js
pageflow-react-0.1.0 js/src/resolvers/object_resolver.js