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

Feature: Serialize / Deserialize object to be able to store in indexedDB #18

Closed
yoHasse opened this issue Sep 29, 2022 · 8 comments
Closed
Labels
enhancement New feature or request

Comments

@yoHasse
Copy link

yoHasse commented Sep 29, 2022

Love this package, made my life a lot easier.

I am just missing one thing and thats the posibility to serialize / deserialize a filehandler to be stored in indexeddb.

I am able to serialize using the JSReference but am unable to deserialize it to a FileSystemHandler, would be a great feature.

@KristofferStrube
Copy link
Owner

Hey, Marcus. Awesome idea.

I think this would be possible with minor changes to some constructors that are currently internal and making them public.

Would being able to do something like this sound good to you?

FileSystemHandle handle = new FileSystemHandle(JSReference);
FileSystemFileHandle handle = new FileSystemFileHandle(JSReference);
FileSystemDirectoryHandle handle = new FileSystemDirectoryHandle(JSReference);

I think that would just be a minor release as I only add to the existing API surface.

Alternatively, I could add a custom Deserializer that would actually take an IJSObjectReference from JSON and create an instance from that. It is possible, but a bit more complicated. I also think the other solution would be more broadly usable.

I can work on this next week.

@yoHasse
Copy link
Author

yoHasse commented Sep 29, 2022

The public constructor would solve it for me so that would be awsome. 😀

Thanks!

@KristofferStrube
Copy link
Owner

KristofferStrube commented Sep 29, 2022

Currently, I also parse the IJSInProcessObjectReference helper through the internal constructor so that I can have a single instance of that across all objects in the library, but I'm thinking that instantiating the helper when using the public constructor should be fine.
I want to make this change in some of the other classes that are in the lib as well. You can see in Blob.cs that it has a CreateAsync method for creating an instance. That might also be an option for solving your problem which would result in something like this:

FileSystemHandle handle = await FileSystemHandle.CreateAsync(JSReference, JSRuntime);
FileSystemFileHandle handle = await FileSystemFileHandle.CreateAsync(JSReference, JSRuntime);
FileSystemDirectoryHandle handle = await FileSystemDirectoryHandle.CreateAsync(JSReference, JSRuntime);

@fixnil
Copy link
Contributor

fixnil commented Oct 4, 2022

Excellent!
I think this also solved my problem by the way #17

@KristofferStrube
Copy link
Owner

KristofferStrube commented Oct 4, 2022

Yep @fixnil,

I figured it was something similar that you were looking for.
I haven't gotten around to implementing it yet, but I will get to it later this week. 😉

I will make sure to add a demo of using it to write to IndexedDB. Is there any Blazor library that you can recommend for working with IndexedDB?

@fixnil
Copy link
Contributor

fixnil commented Oct 4, 2022

Maybe this one: https://github.com/Tavenem/Blazor.IndexedDB
Or this way: ericsink/SQLitePCL.raw#472 (comment)

@KristofferStrube KristofferStrube added the enhancement New feature or request label Oct 4, 2022
@KristofferStrube
Copy link
Owner

I am currently facing some problems writing the demo with IndexedDB so I might not use that as the final demo.

To clarify my problem with using https://github.com/wtulloch/Blazor.IndexedDB which I chose as my IndexedDB library:

Currently, you can't have IJSObjectReferences that live across multiple reloads of a Blazor application.
Blazor keeps track of IJSObjectReferences using an int __jsObjectId that it append to the JS object. Then if/when the object is returned to blazor it tries to look that id up across existing JS references that it has created so it is simply a Blazor limitation that you can't get IJSObjectReferences back through JsonSerialization and that you can only return an IJSObjectReference by calling InvokeAsync<IJSObjectReference(...).
I'm currently trying to develop a workaround by tricking Blazor into believing that it had already seen the object before, but I can't promise anything.

The issue in the ASP.NET Core repo that keeps track of this issue and many others related to JSInterop is the follow: dotnet/aspnetcore#31151
If you would like this to be supported with more ease then I would love if you gave that a thumbs up to show interest in working on that issue.

@KristofferStrube
Copy link
Owner

I got it resolved and created the IndexedDB demo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants