Skip to content

Commit

Permalink
Adding updated files for Rx.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpodwysocki committed Sep 26, 2013
1 parent d57aa52 commit eb229bd
Show file tree
Hide file tree
Showing 22 changed files with 867 additions and 683 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -1,3 +1,9 @@
#################
## Ruby
#################

pkg/

#################
## Eclipse
#################
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in Rx.rb.gemspec
gemspec
26 changes: 26 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,26 @@
PATH
remote: .
specs:
Rx.rb (0.0.1)

GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.1.3)
rake (10.0.3)
rspec (2.12.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
rspec-core (2.12.2)
rspec-expectations (2.12.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.12.2)

PLATFORMS
ruby

DEPENDENCIES
Rx.rb!
rake
rspec
5 changes: 5 additions & 0 deletions Rakefile
@@ -0,0 +1,5 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
17 changes: 17 additions & 0 deletions Rx.rb.gemspec
@@ -0,0 +1,17 @@
# -*- encoding: utf-8 -*-

Gem::Specification.new do |gem|
gem.authors = ["Microsoft Corporation"]
gem.description = %q{Reactive Extensions for Ruby}
gem.summary = %q{This is an implementation of the Reactive Extensions for Ruby. Note that this is an early prototype, but contributions are welcome.}
gem.homepage = "https://github.com/Reactive-Extensions/Rx.rb"

gem.files = `git ls-files`.split($\)
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "Rx.rb"
gem.require_paths = ["lib"]
gem.version = "0.0.1"

gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
end
35 changes: 35 additions & 0 deletions lib/RX/abstract_observer.rb
@@ -0,0 +1,35 @@
# Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root

module RX
class AbstractObserver
def initialize
@stopped = false
end

def on_completed
unless @stopped
@stopped = true
self.completed
end
end

def on_error(exception)
# TODO: Error checking

unless @stopped
@stopped = true
self.error(exception)
end
end

def on_next(value)
unless @stopped
self.next(value)
end
end

def stop
@stopped = true
end
end
end
41 changes: 41 additions & 0 deletions lib/RX/auto_detach_disposable.rb
@@ -0,0 +1,41 @@
# Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root

module RX
class AutoDetachDisposable
def initialize(observer)
@gate = Mutex.new
@observer = observer
@disposed = false
@disposable = nil
end

def dispose
disposable = nil
@observer.stop
@gate.synchronize do
unless @disposed
@disposed = true
disposable = @disposable
end
end

unless disposable.nil?
disposable.dispose
end
end

def set(disposable)
flag = false
@gate.synchronize do
unless @disposed
@disposable = disposable
else
flag = true
end
end
if flag
disposable.dispose
end
end
end
end
31 changes: 31 additions & 0 deletions lib/RX/auto_detach_observer.rb
@@ -0,0 +1,31 @@
# Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root

require 'rx/abstract_observer'
require 'rx/composite_disposable'

module RX
class AutoDetachObserver < RX::AbstractObserver
def initialize(observer)
@group = RX::CompositeDisposable.new
@observer = observer
end

def add(disposable)
@group.add(disposable)
end

def completed
@observer.on_completed
@group.dispose
end

def error(exception)
@observer.on_error(exception)
@group.dispose
end

def next(value)
@observer.on_next(value)
end
end
end
18 changes: 18 additions & 0 deletions lib/RX/boolean_disposable.rb
@@ -0,0 +1,18 @@
# Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root

module RX
class BooleanDisposable

def initialize
@is_disposed = false
end

def disposed?
@is_disposed
end

def dispose
@is_disposed = true
end
end
end
63 changes: 63 additions & 0 deletions lib/RX/composite_disposable.rb
@@ -0,0 +1,63 @@
# Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root

module RX
class CompositeDisposable

def initialize(disposables = [])
@disposables = disposables
@disposed = false
@gate = Mutex.new
end

def dispose
currentDisposables = nil
@gate.synchronize do
unless @disposed
@disposed = true
currentDisposables = @disposables
@disposables = []
end
end
unless currentDisposables.nil?
currentDisposables.each {|disposable| disposable.dispose}
end
end

def add(disposable)
shouldDispose = false
@gate.synchronize do
shouldDispose = @disposed
unless @disposed
@disposables.push(disposable)
end
end
if shouldDispose
disposable.dispose
end
end

def clear
currentDisposables = nil
@gate.synchronize do
currentDisposables = @disposables
@disposables = []
end
currentDisposables.each {|disposable| disposable.dispose}
end

def count
@disposables.length
end

def remove(disposable)
should_dispose = false
@gate.synchronize do
should_dispose = @disposables.delete(disposable).dispose.nil?
end
if should_dispose
disposable.dispose
end
should_dispose
end
end
end
22 changes: 22 additions & 0 deletions lib/RX/disposable.rb
@@ -0,0 +1,22 @@
# Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root

module RX
class Disposable

def initialize(&disposable_action)
@disposable_action = disposable_action
@gate = Mutex.new
@disposed = false
end

def dispose
should_dispose = false
@gate.synchronize do
should_dispose = !@disposed
end
if should_dispose
@disposable_action.call
end
end
end
end
9 changes: 9 additions & 0 deletions lib/RX/empty_disposable.rb
@@ -0,0 +1,9 @@
# Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root

module RX
class EmptyDisposable
def dispose

end
end
end
40 changes: 40 additions & 0 deletions lib/RX/mutable_disposable.rb
@@ -0,0 +1,40 @@
# Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root

module RX
class MutableDisposable

def initialize
@current = nil
@disposed = false
@gate = Mutex.new
end

def replace(disposable)
shouldDispose = false
@gate.synchronize do
shouldDispose = @disposed
unless shouldDispose
unless @current.nil?
@current.dispose
end
@current = disposable
end
end
if shouldDispose && !disposable.nil?
disposable.dispose
end
end

def dispose
@gate.synchronize do
unless @disposed
@disposed = true
unless @current.nil?
@current.dispose
@current = nil
end
end
end
end
end
end

0 comments on commit eb229bd

Please sign in to comment.