Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 52c7422

Browse files
committed
added Hyperloop::Context.reset handling
1 parent 150a7b8 commit 52c7422

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

hyper-react.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
2121
s.add_dependency 'opal', '>= 0.8.0'
2222
s.add_dependency 'opal-activesupport', '>= 0.2.0'
2323
s.add_dependency 'hyper-store', '>= 0.2.1'
24-
s.add_dependency 'hyperloop-config', '>= 0.9.2'
24+
s.add_dependency 'hyperloop-config', '>= 0.9.7'
2525
s.add_development_dependency 'rake', '< 11.0'
2626
s.add_development_dependency 'rspec-rails', '3.3.3'
2727
s.add_development_dependency 'timecop'

lib/react/callbacks.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
require 'active_support/core_ext/class/attribute'
2-
1+
require 'hyperloop-config'
2+
Hyperloop::Context
33
module React
44
module Callbacks
55
def self.included(base)
@@ -18,24 +18,23 @@ def run_callback(name, *args)
1818

1919
module ClassMethods
2020
def define_callback(callback_name)
21-
attribute_name = "_#{callback_name}_callbacks"
22-
class_attribute(attribute_name)
23-
self.send("#{attribute_name}=", [])
21+
wrapper_name = "_#{callback_name}_callbacks"
22+
define_singleton_method(wrapper_name) do
23+
Hyperloop::Context.set_var(self, "@#{wrapper_name}", force: true) { [] }
24+
end
2425
define_singleton_method(callback_name) do |*args, &block|
25-
callbacks = self.send(attribute_name)
26-
callbacks.concat(args)
27-
callbacks.push(block) if block_given?
28-
self.send("#{attribute_name}=", callbacks)
26+
send(wrapper_name).concat(args)
27+
send(wrapper_name).push(block) if block_given?
2928
end
3029
end
3130

3231
def callbacks_for(callback_name)
33-
attribute_name = "_#{callback_name}_callbacks"
32+
wrapper_name = "_#{callback_name}_callbacks"
3433
if superclass.respond_to? :callbacks_for
3534
superclass.callbacks_for(callback_name)
3635
else
3736
[]
38-
end + self.send(attribute_name)
37+
end + send(wrapper_name)
3938
end
4039
end
4140
end

spec/react/callbacks_spec.rb

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,50 @@ def wash_hand
2424
include React::Callbacks
2525
define_callback :before_dinner
2626

27-
before_dinner :wash_hand, :turn_of_laptop
27+
before_dinner :wash_hand, :turn_off_laptop
2828

2929
def wash_hand;end
3030

31-
def turn_of_laptop;end
31+
def turn_off_laptop;end
3232
end
3333

3434
instance = Foo.new
3535
expect(instance).to receive(:wash_hand)
36-
expect(instance).to receive(:turn_of_laptop)
36+
expect(instance).to receive(:turn_off_laptop)
3737
instance.run_callback(:before_dinner)
3838
end
3939

40+
context 'using Hyperloop::Context.reset!' do
41+
after(:all) do
42+
Hyperloop::Context.instance_variable_set(:@context, nil)
43+
end
44+
it 'clears callbacks on Hyperloop::Context.reset!' do
45+
Hyperloop::Context.reset!
46+
stub_const 'Foo', Class.new
47+
Foo.class_eval do
48+
include React::Callbacks
49+
define_callback :before_dinner
50+
51+
before_dinner :wash_hand, :turn_off_laptop
52+
53+
def wash_hands;end
54+
55+
def turn_off_laptop;end
56+
end
57+
instance = Foo.new
58+
expect(instance).to receive(:wash_hand).once
59+
expect(instance).not_to receive(:turn_off_laptop)
60+
61+
Hyperloop::Context.reset!
62+
63+
instance.run_callback(:before_dinner)
64+
Foo.class_eval do
65+
before_dinner :wash_hand
66+
end
67+
instance.run_callback(:before_dinner)
68+
end
69+
end # moved elswhere cause its just hard to get anything to work in this environment
70+
4071
it 'defines block callback' do
4172
stub_const 'Foo', Class.new
4273
Foo.class_eval do

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def ruby?
1515

1616
if RUBY_ENGINE == 'opal'
1717
require File.expand_path('../vendor/jquery-2.2.4.min', __FILE__)
18+
require 'hyperloop-config'
19+
Hyperloop::Context
1820
require 'react/react-source-browser'
1921
require 'react/react-source-server'
2022
require 'hyper-react'

0 commit comments

Comments
 (0)