Skip to content

Commit 564dc30

Browse files
committed
fix(js-component-bindgen): use ponyfill for Promise.withResolvers
Hi! Thanks for this project :) Polyfills are nice but [also have some problems](https://github.com/sindresorhus/ponyfill#the-problem-with-polyfills). Would it be of interest to use a ponyfill, along the lines of the changes in this PR, instead? Thanks for considering!
1 parent 314b865 commit 564dc30

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

crates/js-component-bindgen/src/intrinsics/component.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl ComponentIntrinsic {
135135
"
136136
class {class_name} {{
137137
#callingAsyncImport = false;
138-
#syncImportWait = Promise.withResolvers();
138+
#syncImportWait = promiseWithResolvers();
139139
#lock = null;
140140
141141
mayLeave = true;
@@ -156,7 +156,7 @@ impl ComponentIntrinsic {
156156
157157
#notifySyncImportEnd() {{
158158
const existing = this.#syncImportWait;
159-
this.#syncImportWait = Promise.withResolvers();
159+
this.#syncImportWait = promiseWithResolvers();
160160
existing.resolve();
161161
}}
162162
@@ -218,7 +218,7 @@ impl ComponentIntrinsic {
218218
if (finishedTicket === ticket - 1n) {{ break; }}
219219
}}
220220
221-
const {{ promise, resolve }} = Promise.withResolvers();
221+
const {{ promise, resolve }} = promiseWithResolvers();
222222
this.#lock = {{
223223
ticket,
224224
promise,

crates/js-component-bindgen/src/intrinsics/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub enum Intrinsic {
6161
Host(HostIntrinsic),
6262

6363
// Polyfills
64-
PromiseWithResolversPolyfill,
64+
PromiseWithResolversPonyfill,
6565

6666
/// Enable debug logging
6767
DebugLog,
@@ -198,7 +198,7 @@ pub fn render_intrinsics(args: RenderIntrinsicsArgs) -> Source {
198198
AsyncTaskIntrinsic::UnpackCallbackResult,
199199
));
200200
args.intrinsics
201-
.insert(Intrinsic::PromiseWithResolversPolyfill);
201+
.insert(Intrinsic::PromiseWithResolversPonyfill);
202202
args.intrinsics
203203
.insert(Intrinsic::Host(HostIntrinsic::PrepareCall));
204204
args.intrinsics
@@ -674,19 +674,21 @@ pub fn render_intrinsics(args: RenderIntrinsicsArgs) -> Source {
674674
));
675675
}
676676

677-
Intrinsic::PromiseWithResolversPolyfill => {
677+
Intrinsic::PromiseWithResolversPonyfill => {
678678
output.push_str(
679679
r#"
680-
if (!Promise.withResolvers) {
681-
Promise.withResolvers = () => {
680+
function promiseWithResolvers() {
681+
if (Promise.withResolvers) {
682+
return Promise.withResolvers();
683+
} else {
682684
let resolve;
683685
let reject;
684686
const promise = new Promise((res, rej) => {
685687
resolve = res;
686688
reject = rej;
687689
});
688690
return { promise, resolve, reject };
689-
};
691+
}
690692
}
691693
"#,
692694
);
@@ -959,7 +961,7 @@ impl Intrinsic {
959961

960962
// Debugging
961963
Intrinsic::DebugLog => "_debugLog",
962-
Intrinsic::PromiseWithResolversPolyfill => unreachable!("always global"),
964+
Intrinsic::PromiseWithResolversPonyfill => unreachable!("always global"),
963965

964966
// Types
965967
Intrinsic::ConstantI32Min => "I32_MIN",

crates/js-component-bindgen/src/intrinsics/p3/async_task.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ impl AsyncTaskIntrinsic {
599599
promise: completionPromise,
600600
resolve: resolveCompletionPromise,
601601
reject: rejectCompletionPromise,
602-
}} = Promise.withResolvers();
602+
}} = promiseWithResolvers();
603603
this.#completionPromise = completionPromise;
604604
605605
this.#onResolve = (results) => {{
@@ -785,7 +785,7 @@ impl AsyncTaskIntrinsic {
785785
}}
786786
787787
// Build a promise that this task can await on which resolves when it is awoken
788-
const {{ promise, resolve, reject }} = Promise.withResolvers();
788+
const {{ promise, resolve, reject }} = promiseWithResolvers();
789789
this.awaitableResume = () => {{
790790
{debug_log_fn}('[{task_class}] resuming after onBlock', {{ taskID: this.#id }});
791791
resolve();
@@ -988,7 +988,7 @@ impl AsyncTaskIntrinsic {
988988
if (args.waitable) {{
989989
this.#waitable = args.waitable;
990990
}} else {{
991-
const {{ promise, resolve, reject }} = Promise.withResolvers();
991+
const {{ promise, resolve, reject }} = promiseWithResolvers();
992992
this.#waitableResolve = resolve;
993993
this.#waitableReject = reject;
994994

0 commit comments

Comments
 (0)