0

I have a Blazor server side project and I am trying to interop with js. Everything works as expected except this strange (for me) behavior

Inside a js module I have registered an ondrop event and I am assigning the dataTransfer items from the event to a const;

After awaiting the dotnet method the const items is kind of "resetting".

dropzone.addEventListener("drop", dropHandler, false);

const dropHandler = async (event) => {
    event.preventDefault();
    const items = event.dataTransfer.items;
    console.log(items);
    // outputs: DataTransferItemList {0: DataTransferItem, length: 1}
    dotnetRefObject.InvokeMethodAsync("FireAndForget");
    console.log(items);
    // outputs: DataTransferItemList {0: DataTransferItem, length: 1}
    const result = await dotnetRefObject.invokeMethodAsync('MethodWithReturn');
    console.log(result);
    //ouputs: true
    console.log(items);
    //ouputs: DataTransferItemList {length: 0}
}
[JSInvokable]
public async Task<bool> MethodWithReturn()
{
    await Task.Delay(1);
    return true;
}
4
  • I'm not a JS expert, but I think declaring const items just takes a reference to the datatransfer items and store the reference as a constant. The underlying items being referenced can still change. In addition I don't think events are natively async, so this is going to fire and forget in JS as well - and so I believe the dataTransfer is probably cleared down while you await the .NET method. If you clone the datatransfer items instead that should solve the problem I would hope Commented Feb 18, 2022 at 1:03
  • @MisterMagoo thanks for the valuable info. cloning didn't work either Commented Feb 18, 2022 at 1:22
  • I was right about one thing then - I am not a JS expert 😁 Commented Feb 18, 2022 at 17:37
  • I have managed to overcome the problem by getting the entries before the awaitable dotnet call. i.e. const getEntriesFromEvent = (event) => { const items = event.dataTransfer.items; const entries = []; for (var i = 0; i < items.length; i++) { const item = items[i]; const entry = item.webkitGetAsEntry() entries.push(entry); } return entries; } Commented Feb 18, 2022 at 20:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.