Use Singleton
This commit is contained in:
parent
0034417bdf
commit
3aeba166b2
2 changed files with 4 additions and 11 deletions
|
@ -8,4 +8,4 @@ $LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
|
|||
|
||||
require 'main'
|
||||
|
||||
Main.new
|
||||
Main.instance
|
||||
|
|
13
lib/main.rb
13
lib/main.rb
|
@ -2,7 +2,7 @@
|
|||
|
||||
require 'tox'
|
||||
|
||||
require 'thread'
|
||||
require 'singleton'
|
||||
|
||||
require 'helpers'
|
||||
require 'actions'
|
||||
|
@ -10,18 +10,11 @@ require 'reducer'
|
|||
require 'screen'
|
||||
|
||||
class Main
|
||||
include Singleton
|
||||
|
||||
SAVEDATA_FILENAME = File.expand_path '../savedata', __dir__
|
||||
|
||||
def self.inherited(_base)
|
||||
raise "#{self} is final"
|
||||
end
|
||||
|
||||
def self.mutex
|
||||
(@mutex ||= Mutex.new).tap { freeze }
|
||||
end
|
||||
|
||||
def initialize
|
||||
raise "#{self.class} is singleton" unless self.class.mutex.try_lock
|
||||
call
|
||||
end
|
||||
|
||||
|
|
Reference in a new issue