Skip to content
This repository was archived by the owner on Mar 12, 2023. It is now read-only.

Commit 899c771

Browse files
committed
Move state out of manager
1 parent 903ce52 commit 899c771

File tree

3 files changed

+76
-60
lines changed

3 files changed

+76
-60
lines changed

lib/ruby_jard.rb

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require 'ruby_jard/key_bindings'
1616
require 'ruby_jard/repl_processor'
1717
require 'ruby_jard/repl_manager'
18+
require 'ruby_jard/repl_state'
1819
require 'ruby_jard/screen_manager'
1920
require 'ruby_jard/reflection'
2021

lib/ruby_jard/repl_manager.rb

-60
Original file line numberDiff line numberDiff line change
@@ -56,68 +56,8 @@ class ReplManager
5656
KEY_READ_TIMEOUT = 0.2 # 200ms
5757
PTY_OUTPUT_TIMEOUT = 1.to_f / 60 # 60hz
5858

59-
# A class to store the state with multi-thread guarding
60-
# Ready => Processing/Exiting
61-
# Processing => Ready again
62-
# Exiting => Exited
63-
# Exited => Ready
64-
class ReplState
65-
STATES = [
66-
STATE_READY = 0,
67-
STATE_EXITING = 1,
68-
STATE_PROCESSING = 2,
69-
STATE_EXITED = 3
70-
].freeze
71-
def initialize
72-
@state = STATE_EXITED
73-
@mutex = Mutex.new
74-
end
75-
76-
def check(method_name)
77-
@mutex.synchronize { yield if send(method_name) }
78-
end
79-
80-
def ready?
81-
@state == STATE_READY
82-
end
83-
84-
def ready!
85-
if ready? || processing? || exited?
86-
@mutex.synchronize { @state = STATE_READY }
87-
end
88-
end
89-
90-
def processing?
91-
@state == STATE_PROCESSING
92-
end
93-
94-
def processing!
95-
return unless ready?
96-
97-
@mutex.synchronize { @state = STATE_PROCESSING }
98-
end
99-
100-
def exiting?
101-
@state == STATE_EXITING
102-
end
103-
104-
def exiting!
105-
@mutex.synchronize { @state = STATE_EXITING }
106-
end
107-
108-
def exited?
109-
@state == STATE_EXITED
110-
end
111-
112-
def exited!
113-
@mutex.synchronize { @state = STATE_EXITED }
114-
end
115-
end
116-
11759
def initialize(console:, key_bindings: nil)
11860
@console = console
119-
@state = ReplState.new
120-
12161
@pry_input_pipe_read, @pry_input_pipe_write = IO.pipe
12262
@pry_output_pty_read, @pry_output_pty_write = PTY.open
12363

lib/ruby_jard/repl_state.rb

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# frozen_string_literal: true
2+
3+
module RubyJard
4+
# A class to store the state with multi-thread guarding
5+
# Ready => Processing/Exiting
6+
# Processing => Ready again
7+
# Exiting => Exited
8+
# Exited => Ready
9+
class ReplState
10+
STATES = [
11+
STATE_READY = 0,
12+
STATE_EXITING = 1,
13+
STATE_PROCESSING = 2,
14+
STATE_EXITED = 3
15+
].freeze
16+
17+
def initialize
18+
@state = STATE_EXITED
19+
@mutex = Mutex.new
20+
@pager = false
21+
end
22+
23+
def check(method_name)
24+
@mutex.synchronize { yield if send(method_name) }
25+
end
26+
27+
def pager?
28+
@pager == true
29+
end
30+
31+
def set_pager!
32+
@pager = true
33+
end
34+
35+
def clear_pager!
36+
@pager = false
37+
end
38+
39+
def ready?
40+
@state == STATE_READY
41+
end
42+
43+
def ready!
44+
if ready? || processing? || exited?
45+
@mutex.synchronize { @state = STATE_READY }
46+
end
47+
end
48+
49+
def processing?
50+
@state == STATE_PROCESSING
51+
end
52+
53+
def processing!
54+
return unless ready?
55+
56+
@mutex.synchronize { @state = STATE_PROCESSING }
57+
end
58+
59+
def exiting?
60+
@state == STATE_EXITING
61+
end
62+
63+
def exiting!
64+
@mutex.synchronize { @state = STATE_EXITING }
65+
end
66+
67+
def exited?
68+
@state == STATE_EXITED
69+
end
70+
71+
def exited!
72+
@mutex.synchronize { @state = STATE_EXITED }
73+
end
74+
end
75+
end

0 commit comments

Comments
 (0)