Skip to content

Commit

Permalink
disable loop protection inside generators (#4716)
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyLevin authored Apr 29, 2020
1 parent edeeb05 commit 3d81131
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export default class Component {

const variable = this.var_lookup.get(subscribable_name);
if (variable) {
variable.referenced = true;
variable.referenced = true;
variable.subscribable = true;
}
} else {
Expand Down Expand Up @@ -714,8 +714,14 @@ export default class Component {
};
let scope_updated = false;

let generator_count = 0;

walk(content, {
enter(node: Node, parent, prop, index) {
if ((node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') && node.generator === true) {
generator_count++;
}

if (map.has(node)) {
scope = map.get(node);
}
Expand All @@ -742,8 +748,12 @@ export default class Component {
},

leave(node: Node) {
if ((node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') && node.generator === true) {
generator_count--;
}

// do it on leave, to prevent infinite loop
if (component.compile_options.dev && component.compile_options.loopGuardTimeout > 0) {
if (component.compile_options.dev && component.compile_options.loopGuardTimeout > 0 && generator_count <= 0) {
const to_replace_for_loop_protect = component.loop_protect(node, scope, component.compile_options.loopGuardTimeout);
if (to_replace_for_loop_protect) {
this.replace(to_replace_for_loop_protect);
Expand Down Expand Up @@ -1259,7 +1269,7 @@ export default class Component {

const add_declaration = declaration => {
if (this.reactive_declarations.includes(declaration)) return;

declaration.dependencies.forEach(name => {
if (declaration.assignees.has(name)) return;
const earlier_declarations = lookup.get(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
compileOptions: {
dev: true,
loopGuardTimeout: 1,
},
};
14 changes: 14 additions & 0 deletions test/runtime/samples/loop-protect-generator-opt-out/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
const it = gen();
it.next();
setTimeout(() => {
it.next();
}, 20)
function* gen() {
while (true) {
yield;
}
}
</script>

0 comments on commit 3d81131

Please sign in to comment.