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

async/ non-blocking tween calculation #116

Open
HannesGitH opened this issue Sep 30, 2022 · 3 comments
Open

async/ non-blocking tween calculation #116

HannesGitH opened this issue Sep 30, 2022 · 3 comments

Comments

@HannesGitH
Copy link

since the tween calculation is very slow, it would be nice if it was async, ran in another web-worker

@thednp
Copy link
Owner

thednp commented Oct 1, 2022

This feature is real cool indeed, just I don't have the time to develop.

@HannesGitH
Copy link
Author

HannesGitH commented Oct 1, 2022

understandable :)

i dont have time to dig through this package, but i managed to tuck together an fromToAsync without touching your code ;)

interface.ts

export interface TweenResultMessage {
	error?: String;
	data?: KUTE.Tween;
}

export interface TweenInputMessage {
	elem: Element;
	from: KUTE.tweenProps;
	to: KUTE.tweenProps;
	opts?: KUTE.tweenOptions;
}

morphWorker.ts

import KUTE from 'kute.js';
import type {TweenInputMessage, TweenResultMessage} from './interface';

onmessage = (e:MessageEvent<TweenInputMessage>):void => {
  const {elem, from, to, opts} = e.data;
  try {
    const tween = KUTE.fromTo(elem, from, to, opts)
    postMessage({data: tween} as TweenResultMessage);
  } catch (error) {
    postMessage({error} as TweenResultMessage);
  }
};

export {};

KUTEAsync.ts

import TweenCalculationWorker from "./morphWorker?worker";
import type {TweenInputMessage, TweenResultMessage} from './interface';
import type KUTE from "kute.js";


const fromToAsync = (elem:Element, from:KUTE.tweenProps, to:KUTE.tweenProps, opts:KUTE.tweenOptions):Promise<KUTE.Tween> => {
    const worker = new TweenCalculationWorker();
    worker.postMessage({elem, from, to, opts} as TweenInputMessage);
    return new Promise((resolve, reject) => {
        worker.onmessage = (e:MessageEvent<TweenResultMessage>) => {
            const m = e.data;
            if (m.error) reject(m.error);
            else resolve(m.data);
        }
    });
}

export default {fromToAsync};

Usage

import KUTEA from 'KUTEAsync'
const tween = await KUTEA.fromToAsync(...args);

maybe someone in this future can use this as a basis to actually implement this in KUTE

@thednp
Copy link
Owner

thednp commented Oct 1, 2022

Wow you are awesome! Thanks for that!

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

No branches or pull requests

2 participants