Skip to content

Commit 5dd0bb7

Browse files
committed
fixed partials and async defer
1 parent 1d9fdac commit 5dd0bb7

File tree

7 files changed

+239
-29
lines changed

7 files changed

+239
-29
lines changed

app/concepts/matestack/ui/core/async/async.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,16 @@ const componentDef = {
1818
methods: {
1919
show: function(event_data){
2020
const self = this
21+
if (this.showing === true){
22+
return
23+
}
2124
this.showing = true
2225
this.event.data = event_data
26+
if(this.componentConfig["defer"] != undefined){
27+
if(!isNaN(this.componentConfig["defer"])){
28+
this.startDefer()
29+
}
30+
}
2331
if(this.componentConfig["hide_after"] != undefined){
2432
self.hide_after_timeout = setTimeout(function () {
2533
self.hide()
@@ -29,6 +37,12 @@ const componentDef = {
2937
hide: function(){
3038
this.showing = false
3139
this.event.data = {}
40+
},
41+
startDefer: function(){
42+
const self = this
43+
setTimeout(function () {
44+
self.rerender()
45+
}, parseInt(this.componentConfig["defer"]));
3246
}
3347
},
3448
created: function () {
@@ -39,14 +53,11 @@ const componentDef = {
3953
if(this.componentConfig["show_on"] != undefined){
4054
this.showing = false
4155
}
42-
if(this.componentConfig["hide_on"] != undefined){
43-
this.showing = true
44-
}
4556
if(this.componentConfig["defer"] != undefined){
4657
if(!isNaN(this.componentConfig["defer"])){
47-
setTimeout(function () {
48-
self.rerender()
49-
}, parseInt(this.componentConfig["defer"]));
58+
if (this.componentConfig["show_on"] == undefined){
59+
this.startDefer()
60+
}
5061
}
5162
}
5263
},

app/concepts/matestack/ui/core/page/page.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def prepare
4848
end
4949

5050
def components(&block)
51-
@nodes = Matestack::Ui::Core::PageNode.build(self, nil, &block)
51+
@nodes = Matestack::Ui::Core::PageNode.build(self, nil, context[:params], &block)
5252
end
5353

5454
def nodes_to_cell
@@ -59,12 +59,10 @@ def nodes_to_cell
5959

6060
def partial(&block)
6161
return block
62-
# Matestack::Ui::Core::PageNode.build(self, included, &block)
6362
end
6463

6564
def slot(&block)
66-
# return block
67-
Matestack::Ui::Core::PageNode.build(self, nil, &block)
65+
Matestack::Ui::Core::PageNode.build(self, nil, context[:params], &block)
6866
end
6967

7068

app/lib/matestack/ui/core/page_node.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
module Matestack::Ui::Core
22
class PageNode
33

4-
def self.build(page_instance, included_config, &block)
5-
node = PageNode.new(page_instance, included_config)
4+
def self.build(page_instance, included_config, url_params, &block)
5+
node = PageNode.new(page_instance, included_config, url_params)
66
node.instance_eval(&block)
77
node.hash
88
end
99

1010
attr_reader :hash
1111

12-
def initialize(page_instance, included_config)
12+
def initialize(page_instance, included_config, url_params)
1313
@hash = {}
1414
@node_start_id = 0
1515
@included_config = included_config
16+
@url_params = url_params
1617
@page_instance = page_instance
1718
page_instance.instance_variables.each do |page_instance_var_key|
1819
self.instance_variable_set(page_instance_var_key, page_instance.instance_variable_get(page_instance_var_key))
@@ -45,7 +46,7 @@ def method_missing meth, *args, &block
4546
if meth == :partial
4647
partial_block = @page_instance.send(args.first, *args.drop(1))
4748
@hash[current_node]["components"] = PageNode.build(
48-
@page_instance, included, &partial_block
49+
@page_instance, included, @url_params, &partial_block
4950
)
5051
else
5152
if args.first.is_a?(Hash)
@@ -56,13 +57,13 @@ def method_missing meth, *args, &block
5657

5758
if block_given?
5859
if args.first.is_a?(Hash) && args.first[:defer].present?
59-
if args.first[:url_params].present? && args.first[:url_params][:component_key].present?
60-
@hash[current_node]["components"] = PageNode.build(@page_instance, included, &block)
60+
if @url_params.present? && @url_params[:component_key].present?
61+
@hash[current_node]["components"] = PageNode.build(@page_instance, included, @url_params, &block)
6162
else
6263
return
6364
end
6465
else
65-
@hash[current_node]["components"] = PageNode.build(@page_instance, included, &block)
66+
@hash[current_node]["components"] = PageNode.build(@page_instance, included, @url_params, &block)
6667
end
6768
end
6869
end

spec/dummy/app/matestack/pages/my_app/my_first_page.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
class Pages::MyApp::MyFirstPage < Matestack::Ui::Page
2-
#
3-
# def prepare
4-
# @count = DummyModel.all.count
5-
# end
62

73
def response
84
components {
@@ -14,7 +10,7 @@ def response
1410
onclick emit: "test" do
1511
button text: "rerender"
1612
end
17-
async defer: true, url_params: context[:params] do
13+
async defer: 3000 do
1814
partial :my_deferred_scope
1915
end
2016
}

spec/usage/components/async_spec.rb

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,197 @@ def response
193193

194194
end
195195

196+
describe "defer" do
197+
198+
it "Example 5: deferred loading without any timeout, deferred request right after component mounting" do
199+
200+
class ExamplePage < Matestack::Ui::Page
201+
202+
def prepare
203+
@current_time = DateTime.now.strftime('%Q')
204+
end
205+
206+
def response
207+
components {
208+
div id: "my-reference-div" do
209+
plain "#{@current_time}"
210+
end
211+
async do
212+
div id: "my-not-deferred-div" do
213+
plain "#{@current_time}"
214+
end
215+
end
216+
async defer: true do
217+
div id: "my-deferred-div" do
218+
plain "#{@current_time}"
219+
end
220+
end
221+
}
222+
end
223+
224+
end
225+
226+
visit "/example"
227+
228+
initial_timestamp = page.find("#my-reference-div").text #initial page load
229+
non_deferred_timestamp = page.find("#my-not-deferred-div").text #initial page load
230+
231+
deferred_timestamp = page.find("#my-deferred-div").text #deferred loading
232+
233+
expect(non_deferred_timestamp).to eq initial_timestamp
234+
expect(deferred_timestamp).to be > initial_timestamp
235+
236+
expect(deferred_timestamp.to_i - initial_timestamp.to_i).to be_between(0, 2000).inclusive
237+
238+
end
239+
240+
it "Example 6: deferred loading with a specific timeout" do
241+
242+
class ExamplePage < Matestack::Ui::Page
243+
244+
def prepare
245+
@current_time = DateTime.now.strftime('%Q')
246+
end
247+
248+
def response
249+
components {
250+
div id: "my-reference-div" do
251+
plain "#{@current_time}"
252+
end
253+
async do
254+
div id: "my-not-deferred-div" do
255+
plain "#{@current_time}"
256+
end
257+
end
258+
async defer: 1000 do
259+
div id: "my-deferred-div" do
260+
plain "#{@current_time}"
261+
end
262+
end
263+
}
264+
end
265+
266+
end
267+
268+
visit "/example"
269+
270+
initial_timestamp = page.find("#my-reference-div").text #initial page load
271+
non_deferred_timestamp = page.find("#my-not-deferred-div").text #initial page load
272+
273+
deferred_timestamp = page.find("#my-deferred-div").text #deferred loading
274+
275+
expect(non_deferred_timestamp).to eq initial_timestamp
276+
expect(deferred_timestamp).to be > initial_timestamp
277+
expect(deferred_timestamp.to_i - initial_timestamp.to_i).to be_between(1000, 3000).inclusive
278+
279+
end
280+
281+
it "Example 7: multiple deferred loadings with a specific timeout" do
282+
283+
class ExamplePage < Matestack::Ui::Page
284+
285+
def prepare
286+
@current_time = DateTime.now.strftime('%Q')
287+
end
288+
289+
def response
290+
components {
291+
div id: "my-reference-div" do
292+
plain "#{@current_time}"
293+
end
294+
async do
295+
div id: "my-not-deferred-div" do
296+
plain "#{@current_time}"
297+
end
298+
end
299+
async defer: 1000 do
300+
div id: "my-deferred-div" do
301+
plain "#{@current_time}"
302+
end
303+
end
304+
async defer: 2000 do
305+
div id: "my-second-deferred-div" do
306+
plain "#{@current_time}"
307+
end
308+
end
309+
}
310+
end
311+
312+
end
313+
314+
visit "/example"
315+
316+
initial_timestamp = page.find("#my-reference-div").text #initial page load
317+
non_deferred_timestamp = page.find("#my-not-deferred-div").text #initial page load
318+
319+
deferred_timestamp = page.find("#my-deferred-div").text #deferred loading
320+
second_deferred_timestamp = page.find("#my-second-deferred-div").text #deferred loading
321+
322+
expect(non_deferred_timestamp).to eq initial_timestamp
323+
expect(deferred_timestamp).to be > initial_timestamp
324+
expect(second_deferred_timestamp).to be > initial_timestamp
325+
expect(deferred_timestamp.to_i - initial_timestamp.to_i).to be_between(1000, 3000).inclusive
326+
expect(second_deferred_timestamp.to_i - initial_timestamp.to_i).to be_between(2000, 4000).inclusive
327+
328+
end
329+
330+
it "Example 8: deferred loading without any timeout, triggered by on_show event" do
331+
332+
class ExamplePage < Matestack::Ui::Page
333+
334+
def prepare
335+
@current_time = DateTime.now.strftime('%Q')
336+
end
337+
338+
def response
339+
components {
340+
div id: "my-reference-div" do
341+
plain "#{@current_time}"
342+
end
343+
onclick emit: "my_event" do
344+
button text: "show"
345+
end
346+
onclick emit: "my_other_event" do
347+
button text: "hide"
348+
end
349+
async defer: true, show_on: "my_event", hide_on: "my_other_event" do
350+
plain "waited for 'my_event'"
351+
div id: "my-deferred-div" do
352+
plain "#{@current_time}"
353+
end
354+
end
355+
}
356+
end
357+
358+
end
359+
360+
visit "/example"
361+
362+
initial_timestamp = page.find("#my-reference-div").text #initial page load
363+
364+
expect(page).not_to have_content("waited for 'my_event'")
365+
366+
sleep 2
367+
368+
click_button "show"
369+
370+
expect(page).to have_content("waited for 'my_event'")
371+
372+
deferred_timestamp = page.find("#my-deferred-div").text #deferred loading after click
373+
374+
expect(deferred_timestamp.to_i - initial_timestamp.to_i).to be_between(2000, 4000).inclusive
375+
376+
sleep 1
377+
378+
click_button "hide"
379+
click_button "show"
380+
381+
new_deferred_timestamp = page.find("#my-deferred-div").text #deferred loading after another click
382+
383+
expect(new_deferred_timestamp.to_i - deferred_timestamp.to_i).to be_between(1000, 2000).inclusive
384+
385+
end
386+
387+
end
388+
196389
end

vendor/assets/javascripts/matestack-ui-core.js

Lines changed: 17 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/assets/javascripts/matestack-ui-core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)