Class: React::ComponentFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/react/opal/component_factory.rb

Constant Summary collapse

@@component_classes =
{}

Class Method Summary collapse

Class Method Details

.clear_component_class_cacheObject



5
6
7
# File 'lib/react/opal/component_factory.rb', line 5

def self.clear_component_class_cache
  @@component_classes = {}
end

.native_component_class(klass) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/react/opal/component_factory.rb', line 9

def self.native_component_class(klass)
  @@component_classes[klass.to_s] ||= begin
    klass.class_eval do
      include(React::Component::API)
      # In Opal 0.8, native_alias fails if the method isn't there but we don't want to force all of these to be implemented
      optional_native_alias = lambda do |js, ruby|
        not_there = `!(#{self}.$$proto['$' + #{ruby}])`
        native_alias js, ruby unless not_there
      end
      optional_native_alias[:componentWillMount, :component_will_mount]
      optional_native_alias[:componentDidMount, :component_did_mount]
      optional_native_alias[:componentWillReceiveProps, :component_will_receive_props]
      optional_native_alias[:shouldComponentUpdate, :should_component_update?]
      optional_native_alias[:componentWillUpdate, :component_will_update]
      optional_native_alias[:componentDidUpdate, :component_did_update]
      optional_native_alias[:componentWillUnmount, :component_will_unmount]
      optional_native_alias[:getChildContext, :get_child_context]
      native_alias :render, :render
    end
    %x{
      if (!Object.assign) {
        Object.defineProperty(Object, 'assign', {
          enumerable: false,
          configurable: true,
          writable: true,
          value: function(target, firstSource) {
            'use strict';
            if (target === undefined || target === null) {
              throw new TypeError('Cannot convert first argument to object');
            }

            var to = Object(target);
            for (var i = 1; i < arguments.length; i++) {
              var nextSource = arguments[i];
              if (nextSource === undefined || nextSource === null) {
                continue;
              }

              var keysArray = Object.keys(Object(nextSource));
              for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
                var nextKey = keysArray[nextIndex];
                var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
                if (desc !== undefined && desc.enumerable) {
                  to[nextKey] = nextSource[nextKey];
                }
              }
            }
            return to;
          }
        });
      }
      function ctor(props){
        this.constructor = ctor;
        this.state = #{klass.respond_to?(:initial_state) ? klass.initial_state.to_n : `{}`};
        React.Component.apply(this, arguments);
        #{klass}.$$alloc.prototype.$initialize.call(this, Opal.Hash.$new(props));
      };
      ctor.prototype = klass.$$proto;
      Object.assign(ctor.prototype, React.Component.prototype);
      ctor.propTypes = #{klass.respond_to?(:prop_types) ? klass.prop_types.to_n : `{}`};
      ctor.contextTypes = #{klass.respond_to?(:context_types) ? klass.context_types.to_n : `{}`};
      ctor.childContextTypes = #{klass.respond_to?(:child_context_types) ? klass.child_context_types.to_n : `{}`};
      ctor.defaultProps = #{klass.respond_to?(:default_props) ? klass.default_props.to_n : `{}`};
      ctor.displayName = #{klass.to_s};
    }
    `ctor`
  end
end