-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix window bindings to store #3835
Merged
Conduitry
merged 3 commits into
sveltejs:master
from
tanhauhau:tanhauhau/fix-window-bind-to-store
Nov 3, 2019
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
test/js/samples/window-binding-scroll-store/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { | ||
SvelteComponent, | ||
add_render_callback, | ||
append, | ||
component_subscribe, | ||
detach, | ||
element, | ||
init, | ||
insert, | ||
listen, | ||
noop, | ||
safe_not_equal, | ||
set_data, | ||
set_style, | ||
space, | ||
text | ||
} from "svelte/internal"; | ||
|
||
import { writable, derived } from "svelte/store"; | ||
|
||
function create_fragment(ctx) { | ||
let scrolling = false; | ||
|
||
let clear_scrolling = () => { | ||
scrolling = false; | ||
}; | ||
|
||
let scrolling_timeout; | ||
let p; | ||
let t0; | ||
let t1; | ||
let t2; | ||
let t3; | ||
let t4; | ||
let t5; | ||
let t6; | ||
let t7; | ||
let t8; | ||
let div; | ||
let dispose; | ||
add_render_callback(ctx.onwindowscroll); | ||
|
||
return { | ||
c() { | ||
p = element("p"); | ||
t0 = text("scroll y is "); | ||
t1 = text(ctx.$y); | ||
t2 = text(". "); | ||
t3 = text(ctx.$y); | ||
t4 = text(" * "); | ||
t5 = text(ctx.$y); | ||
t6 = text(" = "); | ||
t7 = text(ctx.$y_squared); | ||
t8 = space(); | ||
div = element("div"); | ||
set_style(p, "position", "fixed"); | ||
set_style(p, "top", "1em"); | ||
set_style(p, "left", "1em"); | ||
set_style(div, "height", "9999px"); | ||
|
||
dispose = listen(window, "scroll", () => { | ||
scrolling = true; | ||
clearTimeout(scrolling_timeout); | ||
scrolling_timeout = setTimeout(clear_scrolling, 100); | ||
ctx.onwindowscroll(); | ||
}); | ||
}, | ||
m(target, anchor) { | ||
insert(target, p, anchor); | ||
append(p, t0); | ||
append(p, t1); | ||
append(p, t2); | ||
append(p, t3); | ||
append(p, t4); | ||
append(p, t5); | ||
append(p, t6); | ||
append(p, t7); | ||
insert(target, t8, anchor); | ||
insert(target, div, anchor); | ||
}, | ||
p(changed, ctx) { | ||
if (changed.$y && !scrolling) { | ||
scrolling = true; | ||
clearTimeout(scrolling_timeout); | ||
scrollTo(window.pageXOffset, ctx.$y); | ||
scrolling_timeout = setTimeout(clear_scrolling, 100); | ||
} | ||
|
||
if (changed.$y) set_data(t1, ctx.$y); | ||
if (changed.$y) set_data(t3, ctx.$y); | ||
if (changed.$y) set_data(t5, ctx.$y); | ||
if (changed.$y_squared) set_data(t7, ctx.$y_squared); | ||
}, | ||
i: noop, | ||
o: noop, | ||
d(detaching) { | ||
if (detaching) detach(p); | ||
if (detaching) detach(t8); | ||
if (detaching) detach(div); | ||
dispose(); | ||
} | ||
}; | ||
} | ||
|
||
function instance($$self, $$props, $$invalidate) { | ||
let $y; | ||
let $y_squared; | ||
const y = writable(0); | ||
component_subscribe($$self, y, value => $$invalidate("$y", $y = value)); | ||
const y_squared = derived(y, $y => $y * $y); | ||
component_subscribe($$self, y_squared, value => $$invalidate("$y_squared", $y_squared = value)); | ||
|
||
function onwindowscroll() { | ||
y.set($y = window.pageYOffset) | ||
} | ||
|
||
return { | ||
y, | ||
y_squared, | ||
$y, | ||
$y_squared, | ||
onwindowscroll | ||
}; | ||
} | ||
|
||
class Component extends SvelteComponent { | ||
constructor(options) { | ||
super(); | ||
init(this, options, instance, create_fragment, safe_not_equal, {}); | ||
} | ||
} | ||
|
||
export default Component; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<script> | ||
import { writable, derived } from 'svelte/store'; | ||
const y = writable(0); | ||
const y_squared = derived(y, $y => $y * $y); | ||
</script> | ||
|
||
<svelte:window bind:scrollY={$y}/> | ||
|
||
<p style="position: fixed; top: 1em; left: 1em;"> | ||
scroll y is {$y}. {$y} * {$y} = {$y_squared} | ||
</p> | ||
|
||
<div style="height: 9999px"> | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
test/runtime/samples/window-binding-scroll-store/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export default { | ||
skip_if_ssr: true, | ||
|
||
async test({ assert, component, target, window }) { | ||
assert.equal(window.pageYOffset, 0); | ||
|
||
const event = new window.Event('scroll'); | ||
Object.defineProperties(window, { | ||
pageYOffset: { | ||
value: 234, | ||
configurable: true, | ||
}, | ||
}); | ||
|
||
await window.dispatchEvent(event); | ||
|
||
assert.htmlEqual( | ||
target.innerHTML, | ||
`<p style="position: fixed; top: 1em; left: 1em;">scroll\ny\nis\n234.\n234\n*\n234\n=\n54756</p><div style="height: 9999px;"></div>` | ||
); | ||
}, | ||
|
||
after_test() { | ||
Object.defineProperties(window, { | ||
pageYOffset: { | ||
value: 0, | ||
configurable: true, | ||
}, | ||
}); | ||
}, | ||
}; |
15 changes: 15 additions & 0 deletions
15
test/runtime/samples/window-binding-scroll-store/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<script> | ||
import { writable, derived } from 'svelte/store'; | ||
const y = writable(0); | ||
const y_squared = derived(y, $y => $y * $y); | ||
</script> | ||
|
||
<svelte:window bind:scrollY={$y}/> | ||
|
||
<p style="position: fixed; top: 1em; left: 1em;"> | ||
scroll y is {$y}. {$y} * {$y} = {$y_squared} | ||
</p> | ||
|
||
<div style="height: 9999px"> | ||
|
||
</div> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this
js
test testing for anything in particular that the otherruntime
test doesn't cover? In general, we like to avoid having tests for particular js output if possible, because they need to be updated when there are changes to the generated code, and runtime tests continue to just work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to take snapshot of the generated output?
I thought it would be great to have both snapshot test and behaviour test?