Class: Markedly::App

Inherits:
Object
  • Object
show all
Defined in:
lib/markedly/app.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ App

Returns a new instance of App.



12
13
14
15
# File 'lib/markedly/app.rb', line 12

def initialize(source, options = {})
  @document = Document.new(source, options)
  @options = options
end

Class Method Details

.run!(source, options = {}) ⇒ Object



8
9
10
# File 'lib/markedly/app.rb', line 8

def self.run!(source, options = {})
  App.new(source, options).run!
end

Instance Method Details

#run!Object



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
# File 'lib/markedly/app.rb', line 17

def run!
  @document.convert
  puts "Open '%s'" % @document.uri
  Launchy.open(@document.uri)

  EM.run do

    @channel = EM::Channel.new

    debug "Start WebSocket Server (ws://localhost:#{@document.port})"
    EM::WebSocket.start(host: '0.0.0.0', port: @document.port) do |ws|
      ws.onopen do
        subscriber = @channel.subscribe do |message|
          ws.send(message)
        end

        debug "client #{subscriber} connected"

        ws.onclose do
          @channel.unsubscribe(subscriber)
          debug "client #{subscriber} closed"
        end
      end
    end

    EM.defer do
      mtime = last_mtime = File.mtime(@document.source)
      loop do
        mtime = File.mtime(@document.source)
        if mtime > last_mtime
          puts "file modified"
          @document.convert
          @channel.push 'updated'
        end
        last_mtime = mtime
        sleep @options[:interval]
      end
    end
  end
end