Module: LucidQuery::Mixin

Defined in:
lib/isomorfeus_data/lucid_query/mixin.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
# File 'lib/isomorfeus_data/lucid_query/mixin.rb', line 3

def self.included(base)
  base.extend(LucidPropDeclaration::Mixin)

  if RUBY_ENGINE == 'opal'
    base.instance_exec do
      def execute(**props)
        props[:query_result_instance] = LucidQueryResult.new
        promise_execute(props) unless props[:query_result_instance].loaded?
        props[:query_result_instance]
      end

      def promise_execute(**props)
        query_result_instance = props.delete(:query_result_instance)
        query_result_instance = LucidQueryResult.new unless query_result_instance
        props.each_key do |prop_name|
          Isomorfeus.raise_error(message: "#{self.to_s} No such query prop declared: '#{prop_name}'!") unless declared_props.key?(prop_name)
        end
        props = validated_props(props)
        props[:query_result_instance_key] = query_result_instance.key
        Isomorfeus::Transport.promise_send_path( 'Isomorfeus::Data::Handler::Generic', self.name, :query, props).then do |agent|
          if agent.processed
            agent.result
          else
            agent.processed = true
            if agent.response.key?(:error)
              Isomorfeus.raise_error(message: agent.response[:error])
            end
            query_result_instance._load_from_store!
            Isomorfeus.store.dispatch(type: 'DATA_LOAD', data: agent.full_response[:data])
            agent.result = query_result_instance
          end
        end
      end

      def execute_query(_); end
    end
  else
    unless base == LucidQuery::Base
      Isomorfeus.add_valid_data_class(base)
    end

    base.instance_exec do
      def promise_execute(**props)
        instance = self.execute(**props)
        result_promise = Promise.new
        result_promise.resolve(instance)
        result_promise
      end

      def execute(**props)
        query_result_instance_key = props.delete(:query_result_instance_key)
        query_result = LucidQueryResult.new(key: query_result_instance_key)
        query_result.result_set = self.new(**props).instance_exec(&@_query_block)
        query_result
      end

      def execute_query(&block)
        @_query_block = block
      end
    end

    attr_reader :props

    def initialize(**props_hash)
      props_hash = self.class.validated_props(props_hash)
      @props = LucidProps.new(props_hash)
    end

    def current_user
      Isomorfeus.current_user
    end

    def pub_sub_client
      Isomorfeus.pub_sub_client
    end
  end
end