Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: init WASM during decode, remove top-level init #24

Merged
merged 1 commit into from
Jun 14, 2022

Conversation

pastelmind
Copy link
Collaborator

@pastelmind pastelmind commented Jun 13, 2022

The initial implementation of WebAssembly-based decoder in #20 caused some ugly API changes. It required users to await a top-level init promise:

import PSD, {init} from '@webtoon/psd'

PSD.parse(/* ... */) // TypeError: undefined is not an object
init.then(() => {
  PSD.parse(/* ... */) // OK
})

Following a suggestion from @dlehdanakf, this PR removes the need for an init promise:

import PSD From '@webtoon/psd'

PSD.parse(/* ... */) // This works again, yay

Instead, the composite() method (available on Psd, Layer, and Group objects) is now async:

const psd = PSD.parse(/* ... */)
psd.composite().then((arr) => {
  /* ... */
})

Details

This PR extracts the WebAssembly-based image decoder into a separate package named @webtoon/psd-decoder. This package exports the init promise in addition to methods generated by wasm-pack.

The generateRgba() function in @webtoon/psd imports @webtoon/psd-decoder and awaits the init promise, ensuring that the WebAssembly module is ready before using any of its methods. This allows the rest of @webtoon/psd to work without worrying about init.

This PR effectively "moves" the top-level init from @webtoon/psd to @webtoon/psd-decoder. The asynchrony of WebAssembly is safely contained in generateRgba(), instead of contaminating the entire library.
On the flip side, generateRgba() is now asynchronous, even though it technically doesn't have to be...not yet, anyway.

Separate the WebAssembly-based image decoder into a separate package
named @webtoon/psd-decoder. This is built separately using Vite, and
exports the `init` promise, as well as methods generated by wasm-pack.
This effectively "moves" the top-level `init` from @webtoon/psd to
@webtoon/psd-decoder.

The `generateRgba()` function in @webtoon/psd imports
@webtoon/psd-decoder and awaits the `init` promise, ensuring that the
WebAssembly module is ready before using any of its methods. This allows
the rest of @webtoon/psd to be treated like a regular JavaScript module,
and no longer exports an `init` promise.

The overall change means that the "asynchrony" of WebAssembly is safely
contained in `generateRgba()`, instead of spread all over the library.
On the flip side, `generateRgba()` is now asynchronous, even though it
technically doesn't have to be.
@pastelmind pastelmind merged commit 0b0bb68 into main Jun 14, 2022
@pastelmind pastelmind deleted the feat-async-decode branch June 14, 2022 05:31
@pastelmind pastelmind mentioned this pull request Jun 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants