Module: Preact::Component::Callbacks

Defined in:
lib/preact/component/callbacks.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
7
8
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/preact/component/callbacks.rb', line 2

def self.included(base)
  base.instance_exec do
    def component_did_catch(&block)
      # TODO convert error
      %x{
        var fun = function(error, newVNode, oldVNode, errorInfo) {
          const oper = Opal.Preact;
          oper.register_active_component(this);
          try {
            #{`this.__ruby_instance`.instance_exec(`error`, `newVNode`, `oldVNode`, `errorInfo`, &block)};
          } catch (e) { console.error(e.message === Opal.nil ? 'error at' : e.message, e.stack); }
          oper.unregister_active_component(this);
        }
        if (self.lucid_preact_component) { self.lucid_preact_component.prototype.componentDidCatch = fun; }
        else { self.preact_component.prototype.componentDidCatch = fun; }
        Opal.Preact.using_did_catch = true;
      }
    end

    def component_did_mount(&block)
      %x{
        let fun = function() {
          const oper = Opal.Preact;
          oper.register_active_component(this);
          try { #{`this.__ruby_instance`.instance_exec(&block)}; }
          catch (e) { console.error(e.message === Opal.nil ? 'error at' : e.message, e.stack); }
          if (self.preload_did_mount_proc) { #{`this.__ruby_instance`.instance_exec { self.class.JS[:preload_did_mount_proc].call } } }
          oper.unregister_active_component(this);
        }
        if (self.lucid_preact_component) {
          self.lucid_preact_component.prototype.componentDidMount = fun;
        } else {
          self.preact_component.prototype.componentDidMount = fun;
        }
      }
    end

    def component_did_update(&block)
      %x{
        var fun = function(prev_props, prev_state, snapshot) {
          const oper = Opal.Preact;
          oper.register_active_component(this);
          try {
            #{`this.__ruby_instance`.instance_exec(`oper.Props.$new({props: prev_props})`,
                                                   `oper.State.$new({state: prev_state})`,
                                                   `snapshot`, &block)};
          } catch (e) { console.error(e.message === Opal.nil ? 'error at' : e.message, e.stack); }
          oper.unregister_active_component(this);
        }
        if (self.lucid_preact_component) { self.lucid_preact_component.prototype.componentDidUpdate = fun; }
        else { self.preact_component.prototype.componentDidUpdate = fun; }
      }
    end

    def component_will_unmount(&block)
      %x{
        var fun = function() {
          const oper = Opal.Preact;
          if (typeof this.unsubscriber === "function") { this.unsubscriber(); };
          oper.register_active_component(this);
          try {
            #{`this.__ruby_instance`.instance_exec(&block)};
          } catch (e) { console.error(e.message === Opal.nil ? 'error at' : e.message, e.stack); }
          oper.unregister_active_component(this);
        }
        if (self.lucid_preact_component) { self.lucid_preact_component.prototype.componentWillUnmount = fun; }
        else { self.preact_component.prototype.componentWillUnmount = fun; }
      }
    end


    def get_derived_state_from_props(&block)
      %x{
        var fun = function(props, state) {
          const oper = Opal.Preact;
          oper.register_active_component(this);
          try {
            var result = #{`this.__ruby_instance`.instance_exec(`oper.Props.$new({props: props})`,
                                                                `oper.State.$new({state: state})`, &block)};
          } catch (e) { console.error(e.message === Opal.nil ? 'error at' : e.message, e.stack); }
          oper.unregister_active_component(this);
          if (typeof result.$to_n === 'function') { result = result.$to_n() }
          if (result === nil) { return null; }
          return result;
        }
        if (self.lucid_preact_component) { self.lucid_preact_component.prototype.getDerivedStateFromProps = fun; }
        else { self.preact_component.prototype.getDerivedStateFromProps = fun; }
      }
    end

    def get_snapshot_before_update(&block)
      %x{
        var fun = function(prev_props, prev_state) {
          const oper = Opal.Preact;
          oper.register_active_component(this);
          try {
            var result = #{`this.__ruby_instance`.instance_exec(`oper.Props.$new({props: prev_props})`,
                                                                `oper.State.$new({state: prev_state})`, &block)};
          } catch (e) { console.error(e.message === Opal.nil ? 'error at' : e.message, e.stack); }
          oper.unregister_active_component(this);
          if (result === nil) { return null; }
          return result;
        }
        if (self.lucid_preact_component) { self.lucid_preact_component.prototype.getSnapshotBeforeUpdate = fun; }
        else { self.preact_component.prototype.getSnapshotBeforeUpdate = fun; }
      }
    end
  end
end