class BullClientController

Attributes

app_rendered[RW]

include MRelogin

connection[R]
ws[RW]

Public Class Methods

new() click to toggle source
# File lib/bull/client.rb, line 24
def initialize
    @user_id = nil
    @password = nil
    @watch = {}
    @promises = {}
    @ws = nil
    @app_rendered = false
    @files = {}

    @connection = RVar.new 'disconnected'
    reactive(@connection) do
        if @connection.value == 'disconnected'
            notify_error 'disconnected', 1
        else
            notify_ok 'connected', 1
        end
    end
end

Public Instance Methods

delete(table, id) click to toggle source
# File lib/bull/client.rb, line 108
def delete(table, id)
    #prom = rpc('delete', table, id).then do |count|
    rpc('delete', table, id).then do |count|
        if count == 0
            #$notifications.add ['error', 'data not deleted', 0] if $notifications
            notify_error 'data not deleted', 0
        elsif count == 1
            #$notifications.add ['ok', 'data deleted', 0] if $notifications
            notify_ok 'data deleted', 0
        end
        #count
    end
    #prom
end
file(command, *args) click to toggle source
# File lib/bull/client.rb, line 71
def file(command, *args)
    id = get_ticket
    promise = Promise.new
    send 'file_'+command, id, *args
    @promises[id] = promise
    @files[id] = ""
    promise
end
get_watch() click to toggle source
# File lib/bull/client.rb, line 54
def get_watch
    @watch
end
insert(table, hsh) click to toggle source
# File lib/bull/client.rb, line 80
def insert(table, hsh)
    prom = rpc('insert', table, value: hsh).then do |response|
        if response.nil?
            #$notifications.add ['error', 'data not inserted', 0] if $notifications
            notify_error 'data not inserted', 0
        else
            #$notifications.add ['ok', 'data inserted', 0] if $notifications
          notify_ok 'data inserted', 0
        end
        response
    end
    prom
end
login(user, password) click to toggle source
# File lib/bull/client.rb, line 123
def login user, password
    rpc('login', user, password).then do |response|
        if response
            @user_id = user
            @password = password
            @roles = response
            notify_ok 'logged', 0
        else
            notify_error 'incorrect user or password', 0
        end
        response
    end
end
logout() click to toggle source
# File lib/bull/client.rb, line 155
def logout
    rpc('logout')
    clear
end
notify(msg) click to toggle source
# File lib/bull/client.rb, line 160
def notify msg
  begin
    msg = JSON.parse(msg)
    data = msg['data'] || msg['result']
    if data.instance_of?(String) && msg['times'] && msg['times'][0] == 'result'
        data = Time.parse data
    else
        resolve_times data, msg['times']
    end
    if msg['response'] == 'watch'
        handle_watch_data msg['id'], data
    elsif msg['response'] == 'rpc'
        handle_rpc_result msg['id'], data
    elsif msg['response'] == 'file'
        handle_file_data msg['id'], data, msg['end']
    end
  rescue Exception => e
      #print e.message
      #print e.backtrace.inspect
  end
end
relogin() click to toggle source
# File lib/bull/client.rb, line 143
def relogin #password
    login(@user_id, @password).then do |response|
        if response
            #show_relogin false
            notify_ok 'relogged', 0
            rewatch
        else
            notify_error 'password incorrect', 0
        end
    end
end
rewatch() click to toggle source
# File lib/bull/client.rb, line 137
def rewatch
    @watch.each do |id, value|
        send 'watch_' + value[:name], id, *value[:args]
    end
end
rpc(command, *args) click to toggle source
# File lib/bull/client.rb, line 63
def rpc(command, *args)
    id = get_ticket
    promise = Promise.new
    send 'rpc_'+command, id, *args
    @promises[id] = promise
    promise
end
start(app) click to toggle source
# File lib/bull/client.rb, line 182
def start(app)
    begin
        controller = self
        url = 'wss://' + `document.location.hostname` + ':3000'
        @ws = Browser::Socket.new url do
            on :open do |e|
                controller.connection.value = 'connected'
                if !controller.app_rendered
                    $document.ready do
                      React.render(React.create_element(app), `document.getElementById('container')`)
                      controller.app_rendered = true
                    end
                else
                    if @user_id
                        #controller.show_relogin true
                        controller.relogin
                    else
                        controller.rewatch
                    end
                end
            end
            on :message do |e|
                begin
                    controller.notify e.data
                rescue Exception => e
                    #print e.message
                    #print e.backtrace.inspect
                end
            end
            on :close do |e|
                #print e.message
                #print e.backtrace.inspect
                controller.connection.value = 'disconnected'
                $window.after(5) {controller.reset}

            end
        end            
    rescue Exception => e  
        #print e.message
        #print e.backtrace.inspect
        $window.after(5) {reset}
    end
end
stop_watch(id) click to toggle source
# File lib/bull/client.rb, line 58
def stop_watch(id)
    @watch.delete(id)
    send('stop_watch', id)
end
task(name, *args) click to toggle source
# File lib/bull/client.rb, line 50
def task(name, *args)
    send 'task_' + name, -1, *args
end
update(table, id, hsh) click to toggle source
# File lib/bull/client.rb, line 94
def update(table, id, hsh)
    prom = rpc('update', table, id, value: hsh).then do |count|
        if count == 0
            #$notifications.add ['error', 'data not updated', 0] if $notifications
            notify_error 'data not updated', 0
        elsif count == 1
            #$notifications.add ['ok', 'data updated', 0] if $notifications
            notify_ok 'data updated', 0
        end
        count
    end
    prom
end
watch(name, *args, &block) click to toggle source
# File lib/bull/client.rb, line 43
def watch(name, *args, &block)
    id = get_ticket
    @watch[id] = {who: block, name: name, args: args}
    send('watch_' + name, id, *args)
    id
end

Private Instance Methods

clear() click to toggle source
# File lib/bull/client.rb, line 261
def clear
    @watch.each_value do |value|
        value[:who].call nil
    end
    @promises = {}
    @user_id = nil
    @password = nil
    @watch = {} ###
end
get_ticket() click to toggle source
# File lib/bull/client.rb, line 228
def get_ticket
    id = @@ticket
    @@ticket += 1
    id
end
handle_file_data(id, data, end_) click to toggle source
# File lib/bull/client.rb, line 252
def handle_file_data(id, data, end_)
    @files[id] << data
    if end_
        @promises[id].resolve @files[id]
        @promises.delete id
        @files.delete id
    end
end
handle_rpc_result(id, result) click to toggle source
# File lib/bull/client.rb, line 247
def handle_rpc_result(id, result)
    @promises[id].resolve result
    @promises.delete id
end
handle_watch_data(id, data) click to toggle source
# File lib/bull/client.rb, line 240
def handle_watch_data(id, data)
    w = @watch[id]
    if w
        w[:who].call data
    end
end
reset() click to toggle source
# File lib/bull/client.rb, line 271
def reset
    #clear
    @watch.each_value do |value|
        value[:who].call nil
    end
    @promises = {}
    #@watch = {}
    start nil
end
send(command, id, *args, **kwargs) click to toggle source
# File lib/bull/client.rb, line 234
def send(command, id, *args, **kwargs)
    times = encode_times(kwargs)
    msg = {command: command, id: id, args: args, kwargs: kwargs, times: times}.to_json
    @ws.send(msg)
end