Skip to content

Commit

Permalink
Cleanup Enumerator
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Nov 8, 2013
1 parent eef232c commit 82fd939
Showing 1 changed file with 38 additions and 37 deletions.
75 changes: 38 additions & 37 deletions opal/core/enumerator.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,6 @@
class Enumerator
include Enumerable

class Yielder
def initialize(enumerator, block, to)
@enumerator = enumerator
@block = block
@to = to
end

def yield(*values)
%x{
if ($opal.$yieldX(#@to, values) === $breaker) {
throw $breaker;
}
}

self
end

alias << yield

def call
%x{
try {
#@block(self)
}
catch (e) {
if (e === $breaker) {
return $breaker.$v;
}
else {
throw e;
}
}
}
end
end

def initialize(obj = nil, method = :each, *args, &block)
if block
@block = block
Expand All @@ -51,7 +15,7 @@ def each(&block)
return enum_for :each unless block_given?

if @block
Yielder.new(self, @block, block).call
Yielder.new(self, @block).each(&block)
else
@object.__send__(@method, *@args, &block)
end
Expand All @@ -70,4 +34,41 @@ def rewind

self
end

class Yielder
def initialize(enumerator, block)
@enumerator = enumerator
@block = block
end

def yield(*values)
%x{
if ($opal.$yieldX(#@to, values) === $breaker) {
throw $breaker;
}
}

self
end

alias << yield

def each(&block)
@to = block

%x{
try {
#@block(self)
}
catch (e) {
if (e === $breaker) {
return $breaker.$v;
}
else {
throw e;
}
}
}
end
end
end

0 comments on commit 82fd939

Please sign in to comment.