Skip to content

Commit 297ea67

Browse files
committed
lock scroll bindings to allow tweening - fixes #1071
1 parent 70ce51d commit 297ea67

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/generators/nodes/Window.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import CodeBuilder from '../../utils/CodeBuilder';
12
import deindent from '../../utils/deindent';
23
import { stringify } from '../../utils/stringify';
34
import flattenReference from '../../utils/flattenReference';
@@ -106,6 +107,7 @@ export default class Window extends Node {
106107
});
107108

108109
const lock = block.getUniqueName(`window_updating`);
110+
const timeout = block.getUniqueName(`window_updating_timeout`);
109111

110112
Object.keys(events).forEach(event => {
111113
const handlerName = block.getUniqueName(`onwindow${event}`);
@@ -114,10 +116,14 @@ export default class Window extends Node {
114116
if (event === 'scroll') {
115117
// TODO other bidirectional bindings...
116118
block.addVariable(lock, 'false');
119+
block.addVariable(timeout,);
117120
}
118121

119122
const handlerBody = deindent`
120-
${event === 'scroll' && `${lock} = true;`}
123+
${event === 'scroll' && deindent`
124+
if (${lock}) return;
125+
${lock} = true;
126+
`}
121127
${generator.options.dev && `component._updatingReadonlyProperty = true;`}
122128
123129
#component.set({
@@ -146,14 +152,16 @@ export default class Window extends Node {
146152

147153
block.builders.init.addBlock(deindent`
148154
function ${observerCallback}() {
149-
if (${lock}) return;
155+
${lock} = true;
156+
clearTimeout(${timeout});
150157
var x = ${bindings.scrollX
151158
? `#component.get("${bindings.scrollX}")`
152159
: `window.scrollX`};
153160
var y = ${bindings.scrollY
154161
? `#component.get("${bindings.scrollY}")`
155162
: `window.scrollY`};
156163
window.scrollTo(x, y);
164+
${timeout} = setTimeout(function() { ${lock} = false; }, 100);
157165
}
158166
`);
159167

@@ -170,8 +178,10 @@ export default class Window extends Node {
170178

171179
block.builders.init.addBlock(deindent`
172180
#component.observe("${bindings.scrollX || bindings.scrollY}", function(${isX ? 'x' : 'y'}) {
173-
if (${lock}) return;
181+
${lock} = true;
182+
clearTimeout(${timeout});
174183
window.scrollTo(${isX ? 'x, window.scrollY' : 'window.scrollX, y'});
184+
${timeout} = setTimeout(function() { ${lock} = false; }, 100);
175185
});
176186
`);
177187
}

test/js/samples/window-binding-scroll/expected-bundle.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,10 @@ var proto = {
190190

191191
/* generated by Svelte vX.Y.Z */
192192
function create_main_fragment(state, component) {
193-
var window_updating = false, p, text, text_1;
193+
var window_updating = false, window_updating_timeout, p, text, text_1;
194194

195195
function onwindowscroll(event) {
196+
if (window_updating) return;
196197
window_updating = true;
197198

198199
component.set({
@@ -203,8 +204,10 @@ function create_main_fragment(state, component) {
203204
window.addEventListener("scroll", onwindowscroll);
204205

205206
component.observe("y", function(y) {
206-
if (window_updating) return;
207+
window_updating = true;
208+
clearTimeout(window_updating_timeout);
207209
window.scrollTo(window.scrollX, y);
210+
window_updating_timeout = setTimeout(function() { window_updating = false; }, 100);
208211
});
209212

210213
return {

test/js/samples/window-binding-scroll/expected.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import { appendNode, assign, createElement, createText, detachNode, init, insertNode, proto } from "svelte/shared.js";
33

44
function create_main_fragment(state, component) {
5-
var window_updating = false, p, text, text_1;
5+
var window_updating = false, window_updating_timeout, p, text, text_1;
66

77
function onwindowscroll(event) {
8+
if (window_updating) return;
89
window_updating = true;
910

1011
component.set({
@@ -15,8 +16,10 @@ function create_main_fragment(state, component) {
1516
window.addEventListener("scroll", onwindowscroll);
1617

1718
component.observe("y", function(y) {
18-
if (window_updating) return;
19+
window_updating = true;
20+
clearTimeout(window_updating_timeout);
1921
window.scrollTo(window.scrollX, y);
22+
window_updating_timeout = setTimeout(function() { window_updating = false; }, 100);
2023
});
2124

2225
return {

0 commit comments

Comments
 (0)