-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle await block promises in a shared helper
- Loading branch information
1 parent
6aa254a
commit 46c6b4b
Showing
4 changed files
with
93 additions
and
64 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { assign, isPromise } from './utils.js'; | ||
|
||
export function handlePromise(promise, info, mount, anchor) { | ||
var token = info.token = {}; | ||
|
||
function update(type) { | ||
if (info.token !== token) return; | ||
|
||
const child_ctx = assign(assign({}, info.ctx), info.resolved); | ||
const block = type && (info.current = type)(info.component, child_ctx); | ||
|
||
if (info.block) { | ||
info.block.u(); | ||
info.block.d(); | ||
block.c(); | ||
block.m(info.mount(), info.anchor); | ||
|
||
info.component.root.set({}); | ||
} | ||
|
||
info.block = block; | ||
} | ||
|
||
if (isPromise(promise)) { | ||
promise.then(value => { | ||
if (info.value) { | ||
info.resolved = { [info.value]: value }; | ||
update(info.then); | ||
} else { | ||
info.resolved = null; | ||
update(null); | ||
} | ||
}, error => { | ||
if (info.error) { | ||
info.resolved = { [info.error]: error }; | ||
update(info.catch); | ||
} else { | ||
info.resolved = null; | ||
update(null); | ||
} | ||
}); | ||
|
||
// if we previously had a then/catch block, destroy it | ||
if (info.current !== info.pending) { | ||
update(info.pending); | ||
return true; | ||
} | ||
} else { | ||
info.resolved = { [info.value]: promise }; | ||
if (info.current !== info.then) { | ||
update(info.then); | ||
return true; | ||
} | ||
} | ||
} |
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