Skip to content

Conversation

dcodeIO
Copy link
Member

@dcodeIO dcodeIO commented Oct 17, 2019

This makes loader.instantiate asynchronous to align with WebAssembly.instantiate and replaces loader.instantiateBuffer with loader.instantiateSync accepting both a pre-compiled module or a buffer, just like loader.instantiate.

@MaxGraey
Copy link
Member

MaxGraey commented Oct 17, 2019

Could we also use instantiateStreaming if available inside loader.instantiate? so it will automatically choose best possible async way for loading without extra efforts from user

@dcodeIO
Copy link
Member Author

dcodeIO commented Oct 17, 2019

I think so, if there is a proper check to determine that moduleOrBuffer is a Response object. Hesitated to do this because I noticed Response | PromiseLike<Response> below and wasn't sure if something like that is properly detectable?

@MaxGraey
Copy link
Member

MaxGraey commented Oct 17, 2019

you mean something like isPromiseLike guard?

function isPromiseLike(p: any): p is PromiseLike {
  return !!p && p.then instanceof Function;
}

@dcodeIO
Copy link
Member Author

dcodeIO commented Oct 17, 2019

The question I had in mind was "what if the argument is not directly a response, but a promise for a response - how can we differentiate that from a promise for something else?"

@MaxGraey
Copy link
Member

You mean how detect is object is Promise (promise like) and this Promise contain Response object without resolving Promise? I guess it hard to detect. Better try resolve this promise and check arrayBuffer method for example

@dcodeIO
Copy link
Member Author

dcodeIO commented Oct 17, 2019

Would then look like

async function instantiate(moduleOrBuffer, imports) {
  if (
    typeof Response !== "undefined" && (
      typeof moduleOrBuffer.then === "function" ||
      moduleOrBuffer instanceof Response
    )
  ) return instantiateStreaming(moduleOrBuffer, imports);
  return postInstantiate(
    preInstantiate(imports || (imports = {})),
    (await WebAssembly.instantiate(moduleOrBuffer, imports)).instance
  );
}

Hmm...

@MaxGraey
Copy link
Member

LGTM

@dcodeIO dcodeIO merged commit 58dbff7 into master Oct 18, 2019
@dcodeIO dcodeIO deleted the loader-async branch November 8, 2019 01:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants