Skip to content

Commit

Permalink
Convert tabs in lib/em/*.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm1 committed Mar 15, 2009
1 parent dac738f commit dc58c30
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 256 deletions.
2 changes: 1 addition & 1 deletion lib/em/buftok.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@ def flush
def empty?
@input.empty?
end
end
end
24 changes: 12 additions & 12 deletions lib/em/deferrable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,45 +153,45 @@ def set_deferred_status status, *args
#
#
def timeout seconds
cancel_timeout
me = self
@deferred_timeout = EventMachine::Timer.new(seconds) {me.fail}
cancel_timeout
me = self
@deferred_timeout = EventMachine::Timer.new(seconds) {me.fail}
end


# Cancels an outstanding timeout if any. Undoes the action of #timeout.
#
#
def cancel_timeout
if @deferred_timeout
@deferred_timeout.cancel
@deferred_timeout = nil
end
if @deferred_timeout
@deferred_timeout.cancel
@deferred_timeout = nil
end
end


# Equivalent to set_deferred_status(:succeeded, ...)
#
def set_deferred_success *args
set_deferred_status :succeeded, *args
set_deferred_status :succeeded, *args
end

# Equivalent to set_deferred_status(:failed, ...)
#
def set_deferred_failure *args
set_deferred_status :failed, *args
set_deferred_status :failed, *args
end

# And still more sugar
#
def succeed *args
set_deferred_success(*args)
set_deferred_success(*args)
end

# Can't get enough sugar
#
def fail *args
set_deferred_failure(*args)
set_deferred_failure(*args)
end
end

Expand All @@ -201,7 +201,7 @@ def fail *args
# as a way of communicating deferred status to some other part of a program.
#
class DefaultDeferrable
include Deferrable
include Deferrable
end

end
Expand Down
46 changes: 22 additions & 24 deletions lib/em/future.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,34 @@
# This defines EventMachine::Deferrable#future, which requires
# that the rest of EventMachine::Deferrable has already been seen.
# (It's in deferrable.rb.)
#
# A future is a sugaring of a typical deferrable usage.

module EventMachine
module Deferrable

class << self
# Evaluate arg (which may be an expression or a block).
# What's the class of arg?
# If arg is an ordinary expression, then return it.
# If arg is deferrable (responds to :set_deferred_status),
# then look at the arguments. If either callback or errback
# are defined, then use them. If neither are defined, then
# use the supplied block (if any) as the callback.
# Then return arg.
def future arg, cb=nil, eb=nil, &blk
arg = arg.call if arg.respond_to?(:call)
# A future is a sugaring of a typical deferrable usage.
#--
# Evaluate arg (which may be an expression or a block).
# What's the class of arg?
# If arg is an ordinary expression, then return it.
# If arg is deferrable (responds to :set_deferred_status),
# then look at the arguments. If either callback or errback
# are defined, then use them. If neither are defined, then
# use the supplied block (if any) as the callback.
# Then return arg.
def self.future arg, cb=nil, eb=nil, &blk
arg = arg.call if arg.respond_to?(:call)

if arg.respond_to?(:set_deferred_status)
if cb || eb
arg.callback(&cb) if cb
arg.errback(&eb) if eb
else
arg.callback(&blk) if blk
end
end
if arg.respond_to?(:set_deferred_status)
if cb || eb
arg.callback(&cb) if cb
arg.errback(&eb) if eb
else
arg.callback(&blk) if blk
end
end

arg
end
end
arg
end

end
end
Expand Down
70 changes: 34 additions & 36 deletions lib/em/processes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,43 @@

module EventMachine

# EM::DeferrableChildProcess is a sugaring of a common use-case
# involving EM::popen.
# Call the #open method on EM::DeferrableChildProcess, passing
# a command-string. #open immediately returns an EM::Deferrable
# object. It also schedules the forking of a child process, which
# will execute the command passed to #open.
# When the forked child terminates, the Deferrable will be signalled
# and execute its callbacks, passing the data that the child process
# wrote to stdout.
#
class DeferrableChildProcess < EventMachine::Connection
include EventMachine::Deferrable
# EM::DeferrableChildProcess is a sugaring of a common use-case
# involving EM::popen.
# Call the #open method on EM::DeferrableChildProcess, passing
# a command-string. #open immediately returns an EM::Deferrable
# object. It also schedules the forking of a child process, which
# will execute the command passed to #open.
# When the forked child terminates, the Deferrable will be signalled
# and execute its callbacks, passing the data that the child process
# wrote to stdout.
#
class DeferrableChildProcess < EventMachine::Connection
include EventMachine::Deferrable

# Sugars a common use-case involving forked child processes.
# #open takes a String argument containing an shell command
# string (including arguments if desired). #open immediately
# returns an EventMachine::Deferrable object, without blocking.
#
# It also invokes EventMachine#popen to run the passed-in
# command in a forked child process.
#
# When the forked child terminates, the Deferrable that
# #open calls its callbacks, passing the data returned
# from the child process.
#
def self.open cmd
EventMachine.popen( cmd, DeferrableChildProcess )
end
# Sugars a common use-case involving forked child processes.
# #open takes a String argument containing an shell command
# string (including arguments if desired). #open immediately
# returns an EventMachine::Deferrable object, without blocking.
#
# It also invokes EventMachine#popen to run the passed-in
# command in a forked child process.
#
# When the forked child terminates, the Deferrable that
# #open calls its callbacks, passing the data returned
# from the child process.
#
def self.open cmd
EventMachine.popen( cmd, DeferrableChildProcess )
end

def receive_data data
(@data ||= []) << data
end
def receive_data data
(@data ||= []) << data
end

def unbind
succeed( @data.join )
end
end
def unbind
succeed( @data.join )
end
end

class SystemCmd < EventMachine::Connection # :nodoc:
def initialize cb
Expand Down Expand Up @@ -109,5 +109,3 @@ def EventMachine::system cmd, *args, &cb
end
end
end


98 changes: 48 additions & 50 deletions lib/em/spawnable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,62 +26,60 @@
module EventMachine
# Support for Erlang-style processes.
#
class SpawnedProcess
#attr_accessor :receiver
def notify *x
me = self
EM.next_tick {
# A notification executes in the context of this
# SpawnedProcess object. That makes self and notify
# work as one would expect.
#
y = me.call(*x)
if y and y.respond_to?(:pull_out_yield_block)
a,b = y.pull_out_yield_block
set_receiver a
self.notify if b
end
}
end
alias_method :resume, :notify
alias_method :run, :notify # for formulations like (EM.spawn {xxx}).run
class SpawnedProcess
# Send a message to the spawned process
def notify *x
me = self
EM.next_tick {
# A notification executes in the context of this
# SpawnedProcess object. That makes self and notify
# work as one would expect.
#
y = me.call(*x)
if y and y.respond_to?(:pull_out_yield_block)
a,b = y.pull_out_yield_block
set_receiver a
self.notify if b
end
}
end
alias_method :resume, :notify
alias_method :run, :notify # for formulations like (EM.spawn {xxx}).run
#attr_accessor :receiver

#--
# I know I'm missing something stupid, but the inside of class << s
# can't see locally-bound values. It can see globals, though.
def set_receiver blk
$em______tmpglobal = blk
class << self
define_method :call, $em______tmpglobal.dup
end
end
# I know I'm missing something stupid, but the inside of class << s
# can't see locally-bound values. It can see globals, though.
def set_receiver blk
$em______tmpglobal = blk
class << self
define_method :call, $em______tmpglobal.dup
end
end

end
end

class YieldBlockFromSpawnedProcess # :nodoc:
def initialize block, notify
@block = [block,notify]
end
def pull_out_yield_block
@block
end
end
class YieldBlockFromSpawnedProcess # :nodoc:
def initialize block, notify
@block = [block,notify]
end
def pull_out_yield_block
@block
end
end

# Spawn an erlang-style process
def EventMachine.spawn &block
s = SpawnedProcess.new
s.set_receiver block
s
end
def self.spawn &block
s = SpawnedProcess.new
s.set_receiver block
s
end

def EventMachine.yield &block # :nodoc:
return YieldBlockFromSpawnedProcess.new( block, false )
end
def self.yield &block # :nodoc:
return YieldBlockFromSpawnedProcess.new( block, false )
end

def EventMachine.yield_and_notify &block # :nodoc:
return YieldBlockFromSpawnedProcess.new( block, true )
end
def self.yield_and_notify &block # :nodoc:
return YieldBlockFromSpawnedProcess.new( block, true )
end
end



2 changes: 1 addition & 1 deletion lib/evma.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# $Id$
#--
#
# Author:: Francis Cianfrocca (gmail: blackhedd)
# Homepage:: http://rubyeventmachine.com
Expand Down
Loading

0 comments on commit dc58c30

Please sign in to comment.