AnnulusGames / LitMotion

Lightning-fast and Zero Allocation Tween Library for Unity.

Home Page:https://annulusgames.github.io/LitMotion/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WithCancellationToken(CancellationToken)

Akeit0 opened this issue · comments

Allocation of AddTo(Monobehaviour) is not negligible.

I think it would be better to add a CancellationToken to the callback.

In addition, adding linker disableCancellationToken would make it even easier to use.

Indeed, it might be better to be able to pass a CancellationToken at creation time.

Regarding disableCancellationToken, I honestly don't see any practical need for it. It would be best to combine CancellationTokenSource and callback on your own.

I suggested it because destroyCancellationtoken is not compatible with the object pool.

Well, I'm sorry if I misunderstood, but MonoBehaviour shouldn't have a property such as disableCancellationToken...?

Sorry, I meant adding that feature to MotionHandleLinker.

Ah, I see, I understand.
Certainly, it would be useful to be able to adjust the behavior of AddTo. I will think about it.

Back to the first topic, ToUnitask(token, bool cancelImmediately) is also needed if this is implemented.

With the implementation of WithCancellationToken, I plan to add CancelBehaviour (equivalent to TweenCancelBehaviour in UniTask's DOTween extension). However, if this is incorporated into WithCancellationToken and UniTask, these settings will conflict. (As for CancellationToken, they are compatible.)

To add WithCancellationToken, this needs to be resolved.

Here's a code example that shows it. Having CancelBehaviour arguments for both of these makes it difficult to resolve.
The solution is to have this only in WithCancellationToken (or prepare a dedicated WithCancelBehaviour), but it is somewhat unnatural that the cancellation behavior cannot be specified in ToUniTask.

using CancellationTokenSource source = new();
source.CancelAfter(TimeSpan.FromSeconds(0.2f));

await LMotion.Create(startValue, endValue, 0.5f)
    .WithCancellationToken(CancelBehaviour.CompleteMotion, source.Token)
    .Bind(x => value = x)
    .ToUniTask(CancelBehaviour.CompleteMotionAndCancelAwait, source.Token);

In my opinion, there is no need for WithCancellationToken in the first place. LitMotion currently has ToUniTask, ToValueTask, and ToAwaitable; these methods should be sufficient to pass the CancellationToken.

OK.
However, I have seen some people always use DOTween's SetLink to avoid error, so the original issue in my opinion that LitMotion's AddTo(CancellationToken) should be more lightweight remains.

It may be possible to separate it only as an enum for asynchronous processing as AwaitingBehaviour.

Yes, we need a replacement for AddTo, but perhaps this issue is not the place to discuss it. Should we close this and start an another issue?