Class: Hakuban::Stream::StreamEnumerator

Inherits:
Object
  • Object
show all
Defined in:
lib/hakuban/stream.rb

Overview

TODO: meh

Instance Method Summary collapse

Constructor Details

#initialize(stream, without_implicit_drop, async, kill_on_next, args, kwargs) ⇒ StreamEnumerator

Returns a new instance of StreamEnumerator.



15
16
17
# File 'lib/hakuban/stream.rb', line 15

def initialize(stream, without_implicit_drop, async, kill_on_next, args, kwargs)
	@stream, @without_implicit_drop, @async, @kill_on_next, @args, @kwargs = stream, without_implicit_drop, async, kill_on_next, args, kwargs
end

Instance Method Details

#async(*args, **kwargs, &block) ⇒ Object



19
20
21
# File 'lib/hakuban/stream.rb', line 19

def async(*args,**kwargs,&block)
	StreamEnumerator.new(@stream, @without_implicit_drop, :async, @kill_on_next, @args+args, @kwargs.merge(kwargs)).call_or_return(&block)
end

#call_or_return(&block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hakuban/stream.rb', line 35

def call_or_return(&block)
	if block
		Thread.handle_interrupt(Object => :never) {
			begin
				to_kill = nil
				Thread.handle_interrupt(Object => :immediate) {
					while new_item = @stream.next
						case @async
						when false
							sync_call(new_item, block)
						when :async
							if @kill_on_next
								if to_kill
									to_kill.stop
									to_kill.wait
								end
							end
							to_kill = Async(new_item) { |_task, item|
								sync_call(item, block)
							}						
						when :thread
							if @kill_on_next
								if to_kill
									to_kill.raise KilledOnNext.new
									to_kill.value 
								end
							end
							to_kill = Thread.new(new_item) { |item|
								sync_call(item, block)
							}						
						end
					end

					case @async
					when false
					when :async
						if @kill_on_next
							if to_kill
								to_kill.stop
								to_kill.wait
							end
						end
					when :thread
						if @kill_on_next
							if to_kill
								to_kill.raise KilledOnNext.new
								to_kill.value 
							end
						end
					end
				}
			ensure
				#TODO: should we kill still-running sub-tasks here, or await? or maybe kill on exception, and await otherwise?
			end
		}
	else
		self
	end
end

#kill_on_next(*args, **kwargs, &block) ⇒ Object



31
32
33
# File 'lib/hakuban/stream.rb', line 31

def kill_on_next(*args,**kwargs,&block)
	StreamEnumerator.new(@stream, @without_implicit_drop, @async, true, @args+args, @kwargs.merge(kwargs)).call_or_return(&block)
end

#thread(*args, **kwargs, &block) ⇒ Object



23
24
25
# File 'lib/hakuban/stream.rb', line 23

def thread(*args,**kwargs,&block)
	StreamEnumerator.new(@stream, @without_implicit_drop, :thread, @kill_on_next, @args+args, @kwargs.merge(kwargs)).call_or_return(&block)
end

#without_implicit_drop(*args, **kwargs, &block) ⇒ Object



27
28
29
# File 'lib/hakuban/stream.rb', line 27

def without_implicit_drop(*args,**kwargs,&block)
	StreamEnumerator.new(@stream, true, @async, @kill_on_next, @args+args, @kwargs.merge(kwargs)).call_or_return(&block)
end