Skip to content

Commit dc97fa7

Browse files
committed
less code
1 parent ccef13a commit dc97fa7

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

src/generators/dom/visitors/AwaitBlock.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@ export default function visitAwaitBlock(
3737

3838
const promise = block.getUniqueName(`promise`);
3939
const resolved = block.getUniqueName(`resolved`);
40-
const status = block.getUniqueName(`status`);
41-
const select_block_type = block.getUniqueName(`select_block_type`);
4240
const await_block = block.getUniqueName(`await_block`);
4341
const await_block_type = block.getUniqueName(`await_block_type`);
4442
const token = block.getUniqueName(`token`);
4543
const await_token = block.getUniqueName(`await_token`);
46-
const update = block.getUniqueName(`update`);
4744
const handle_promise = block.getUniqueName(`handle_promise`);
45+
const replace_await_block = block.getUniqueName(`replace_await_block`);
46+
const old_block = block.getUniqueName(`old_block`);
4847
const value = block.getUniqueName(`value`);
4948
const error = block.getUniqueName(`error`);
5049
const create_pending_block = node.pending._block.name;
@@ -58,43 +57,39 @@ export default function visitAwaitBlock(
5857
block.addVariable(resolved);
5958

6059
block.builders.init.addBlock(deindent`
60+
function ${replace_await_block}(${token}, type, ${value}, ${params}) {
61+
if (${token} !== ${await_token}) return;
62+
63+
var ${old_block} = ${await_block};
64+
${await_block} = (${await_block_type} = type)(${params}, ${resolved} = ${value}, #component);
65+
66+
if (${old_block}) {
67+
${old_block}.u();
68+
${old_block}.d();
69+
${await_block}.c();
70+
${await_block}.m(${anchor}.parentNode, ${anchor});
71+
}
72+
}
73+
6174
function ${handle_promise}(${promise}, ${params}) {
6275
var ${token} = ${await_token} = {};
6376
6477
if (@isPromise(${promise})) {
6578
${promise}.then(function(${value}) {
66-
if (${token} !== ${await_token}) return;
67-
${await_block}.u();
68-
${await_block}.d();
69-
${await_block} = (${await_block_type} = ${create_then_block})(${params}, ${resolved} = ${value}, #component);
70-
${await_block}.c();
71-
${await_block}.m(${anchor}.parentNode, ${anchor});
79+
${replace_await_block}(${token}, ${create_then_block}, ${value}, ${params});
7280
}, function (${error}) {
73-
if (${token} !== ${await_token}) return;
74-
${await_block}.u();
75-
${await_block}.d();
76-
${await_block} = (${await_block_type} = ${create_catch_block})(${params}, ${resolved} = ${error}, #component);
77-
${await_block}.c();
78-
${await_block}.m(${anchor}.parentNode, ${anchor});
81+
${replace_await_block}(${token}, ${create_catch_block}, ${error}, ${params});
7982
});
8083
8184
// if we previously had a then/catch block, destroy it
8285
if (${await_block_type} !== ${create_pending_block}) {
83-
if (${await_block}) {
84-
${await_block}.u();
85-
${await_block}.d();
86-
}
87-
${await_block} = (${await_block_type} = ${create_pending_block})(${params}, ${resolved} = null, #component);
86+
${replace_await_block}(${token}, ${create_pending_block}, null, ${params});
8887
return true;
8988
}
9089
} else {
9190
${resolved} = ${promise};
9291
if (${await_block_type} !== ${create_then_block}) {
93-
if (${await_block}) {
94-
${await_block}.u();
95-
${await_block}.d();
96-
}
97-
${await_block} = (${await_block_type} = ${create_then_block})(${params}, ${resolved}, #component);
92+
${replace_await_block}(${token}, ${create_then_block}, ${resolved}, ${params});
9893
return true;
9994
}
10095
}
@@ -133,8 +128,7 @@ export default function visitAwaitBlock(
133128
if (node.pending._block.hasUpdateMethod) {
134129
block.builders.update.addBlock(deindent`
135130
if (${conditions.join(' && ')}) {
136-
${await_block}.c();
137-
${await_block}.m(${anchor}.parentNode, ${anchor});
131+
// nothing
138132
} else {
139133
${await_block}.p(changed, ${params}, ${resolved});
140134
}

0 commit comments

Comments
 (0)