dkrprasetya / simple-firebase-unity

Firebase Realtime-Database's REST API Wrapper for Unity in C#
143 stars 40 forks source link

Consider async wrappers for Push/Pull methods #22

Open petrroll opened 5 years ago

petrroll commented 5 years ago

Hi, using your library (awesome btw!) I wrote little wrappers that take the REST functions and turn them to async methods.

I was thinking if you'd be interested in having something similar (more generic and with proper exception propagation, ofc) in the library itself. I was thinking about having one async method per operation e.g.:

class Firebase{
    public Task<(Firebase, DataSnapshot)> PushStateAsync(string str)
    {
        var fbNode = this.Copy(); // (so that we can guarantee there's no callback sharing)
        var taskCompletionSource = new TaskCompletionSource<(Firebase, DataSnapshot)>();

        fbNode.OnPushSuccess += (Firebase node, DataSnapshot pushedKeyInfo) => { taskCompletionSource.SetResult((node, pushedKeyInfo)); }; ;
        fbNode.OnPushFailed += (Firebase fireBase, FirebaseError error) => { taskCompletionSource.SetException(error); };
        fbNode.Push(str);

        return taskCompletionSource.Task;
    }
}

I know it's not the most efficient implementation (a lot of allocations and whatnot) but it's not all that bad either.

I'd be happy to implement it but don't want to work on it in case it wouldn't be merged.