Class: Lolex
- Inherits:
-
Object
- Object
- Lolex
- Defined in:
- lib/hyper-spec/time_cop.rb,
lib/hyper-spec/time_cop.rb
Overview
Interface to the Lolex package running on the client side Below we will monkey patch Timecop to call these methods
Class Method Summary collapse
- .create_ticker ⇒ Object
- .init(page, client_time_zone, resolution) ⇒ Object
- .initialized? ⇒ Boolean
-
.parse_time(str) ⇒ Object
to avoid forcing us to require time we make our own Time.parse method copied from opal.rb master branch.
- .pop ⇒ Object
- .push(mock_type, *args) ⇒ Object
- .restore ⇒ Object
- .stack ⇒ Object
- .tick ⇒ Object
- .unmock ⇒ Object
- .update_lolex(time, scale, resolution) ⇒ Object
Class Method Details
.create_ticker ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/hyper-spec/time_cop.rb', line 50 def create_ticker return unless @scale && @scale > 0 ticker = %x{ #{@lolex}['_setInterval'].call( window, function() { #{tick} }, #{@resolution} ) } ticker end |
.init(page, client_time_zone, resolution) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/hyper-spec/time_cop.rb', line 90 def init(page, client_time_zone, resolution) = page @resolution = resolution || 10 @client_time_zone = client_time_zone run_pending_evaluations @initialized = true end |
.initialized? ⇒ Boolean
98 99 100 |
# File 'lib/hyper-spec/time_cop.rb', line 98 def initialized? @initialized end |
.parse_time(str) ⇒ Object
to avoid forcing us to require time we make our own Time.parse method copied from opal.rb master branch
9 10 11 |
# File 'lib/hyper-spec/time_cop.rb', line 9 def parse_time(str) `new Date(Date.parse(str))` end |
.pop ⇒ Object
23 24 25 |
# File 'lib/hyper-spec/time_cop.rb', line 23 def pop update_lolex(*stack.pop) unless stack.empty? end |
.push(mock_type, *args) ⇒ Object
17 18 19 20 21 |
# File 'lib/hyper-spec/time_cop.rb', line 17 def push(time, scale = 1, resolution = 10) time = parse_time(time) if time.is_a? String stack << [Time.now, @scale, @resolution] update_lolex(time, scale, resolution) end |
.restore ⇒ Object
33 34 35 36 |
# File 'lib/hyper-spec/time_cop.rb', line 33 def restore @stack = @backup_stack pop end |
.stack ⇒ Object
13 14 15 |
# File 'lib/hyper-spec/time_cop.rb', line 13 def stack @stack ||= [] end |
.tick ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/hyper-spec/time_cop.rb', line 38 def tick real_clock = `(new #{@lolex}['_Date']).getTime()` mock_clock = Time.now.to_f * 1000 real_elapsed_time = real_clock - @real_start_time mock_elapsed_time = mock_clock - @mock_start_time ticks = real_elapsed_time * @scale - mock_elapsed_time `#{@lolex}.tick(#{ticks.to_i})` nil end |
.unmock ⇒ Object
27 28 29 30 31 |
# File 'lib/hyper-spec/time_cop.rb', line 27 def unmock(time, resolution) push(time, 1, resolution) @backup_stack = stack @stack = [] end |
.update_lolex(time, scale, resolution) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/hyper-spec/time_cop.rb', line 62 def update_lolex(time, scale, resolution) `#{@lolex}.uninstall()` && return if scale.nil? @mock_start_time = time.to_f * 1000 if @lolex `#{@lolex}['_clearInterval'].call(window, #{@ticker})` if @ticker @real_start_time = `(new #{@lolex}['_Date']).getTime()` `#{@lolex}.tick(#{@mock_start_time - Time.now.to_f * 1000})` else @real_start_time = Time.now.to_f * 1000 @lolex = `lolex.install({ now: #{@mock_start_time} })` end @scale = scale @resolution = resolution @ticker = create_ticker nil # must return nil otherwise we try to return a timer to server! end |