lib/react/state.rb in hyper-store-0.2.3 vs lib/react/state.rb in hyper-store-0.99.0
- old
+ new
@@ -20,11 +20,11 @@
end
def set_state2(object, name, value, updates, exclusions = nil)
# set object's name state to value, tell all observers it has changed.
# Observers must implement update_react_js_state
- object_needs_notification = object.respond_to? :update_react_js_state
+ object_needs_notification = object.respond_to?(:update_react_js_state)
observers_by_name[object][name].dup.each do |observer|
next if exclusions && exclusions.include?(observer)
updates[observer] += [object, name, value]
object_needs_notification = false if object == observer
end
@@ -45,12 +45,22 @@
@delayed_updates[object][name][1] << current_observer
end
states[object][name]
end
+ # ReactDOM.unstable_batchedUpdates does not seem to improve things here, ill leave it here commented for reference
+ # and later investigation
+ #if `ReactDOM.unstable_batchedUpdates !== undefined`
+ # %x{
+ # ReactDOM.unstable_batchedUpdates(function(){
+ # #{updates.each { |observer, args| observer.update_react_js_state(*args) }}
+ # });
+ # }
+ #else # run the other one
def set_state(object, name, value, delay=ALWAYS_UPDATE_STATE_AFTER_RENDER)
states[object][name] = value
+ delay = false if object.respond_to?(:set_state_synchronously?) && object.set_state_synchronously?
if delay || @bulk_update_flag
@delayed_updates ||= Hash.new { |h, k| h[k] = {} }
@delayed_updates[object][name] = [value, Set.new]
@delayed_updater ||= after(0.001) do
delayed_updates = @delayed_updates
@@ -70,29 +80,10 @@
updates.each { |observer, args| observer.update_react_js_state(*args) }
end
value
end
- def notify_observers(object, name, value)
- object_needs_notification = object.respond_to? :update_react_js_state
- observers_by_name[object][name].dup.each do |observer|
- observer.update_react_js_state(object, name, value)
- object_needs_notification = false if object == observer
- end
- object.update_react_js_state(nil, name, value) if object_needs_notification
- end
-
- def notify_observers_after_thread_completes(object, name, value)
- (@delayed_updates ||= []) << [object, name, value]
- @delayed_updater ||= after(0) do
- delayed_updates = @delayed_updates
- @delayed_updates = []
- @delayed_updater = nil
- delayed_updates.each { |args| notify_observers(*args) }
- end
- end
-
def will_be_observing?(object, name, current_observer)
current_observer && new_observers[current_observer][object].include?(name)
end
def is_observing?(object, name, current_observer)
@@ -124,23 +115,17 @@
end
current_observers.delete(@current_observer)
end
def set_state_context_to(observer, rendering = nil) # wrap all execution that may set or get states in a block so we know which observer is executing
- if `typeof Opal.global.reactive_ruby_timing !== 'undefined'`
- @nesting_level = (@nesting_level || 0) + 1
- start_time = Time.now.to_f
- observer_name = (observer.class.respond_to?(:name) ? observer.class.name : observer.to_s) rescue "object:#{observer.object_id}"
- end
saved_current_observer = @current_observer
@current_observer = observer
@rendering_level += 1 if rendering
return_value = yield
return_value
ensure
@current_observer = saved_current_observer
@rendering_level -= 1 if rendering
- @nesting_level = [0, @nesting_level - 1].max if `typeof Opal.global.reactive_ruby_timing !== 'undefined'`
return_value
end
def states
@states ||= Hash.new { |h, k| h[k] = {} }