/**
* Module: MoralisSubscriptionQuery.cs
* Descriptiontion: Wraps subscription processes to facilitate automation
* of pause / unpause handling.
* Author: Moralis Web3 Technology AB, 559307-5988 - David B. Goodrich
*
* MIT License
*
* Copyright (c) 2021 Moralis Web3 Technology AB, 559307-5988
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
using MoralisUnity.Platform.Services.ClientServices;
using MoralisUnity.Platform.Objects;
using MoralisUnity.Platform.Queries;
namespace MoralisUnity
{
///
/// Provides a wrapper around the query subscription process to facilitate automated
/// subscribe and suspension processes.
///
///
public class MoralisSubscriptionQuery : ISubscriptionQuery where T : MoralisObject
{
private MoralisLiveQueryClient subscription;
///
/// The client event handlers used to react to this subscription.
///
public MoralisLiveQueryCallbacks Callbacks { get; private set; }
///
/// Query against which the subscription is made.
///
public MoralisQuery Query { get; private set; }
///
/// Indicates if a connection the server was established.
///
public bool Connected { get; private set; }
///
/// Indicates if a subscription for the query has been established.
///
public bool Subscribed { get; private set; }
///
/// Key name used to identify this subscription and included in any
/// error logs generated.
///
public string SubscriptionName { get; private set; }
public MoralisSubscriptionQuery(string keyName, MoralisQuery q, MoralisLiveQueryCallbacks c)
{
Query = q;
Callbacks = c;
SubscriptionName = keyName;
// Internally track connection state
Callbacks.OnConnectedEvent += (() => { Connected = true; });
// Internally track subscription state
Callbacks.OnSubscribedEvent += ((requestId) => { Subscribed = true; });
Callbacks.OnUnsubscribedEvent += ((requestId) => { Subscribed = false; });
// Create initial subscription.
subscription = Query.Subscribe(Callbacks);
}
///
/// Attempts to re-establish a previous subscriptions. If the
/// subscription is already active, unsubscribe is called.
/// Subscription is then re-created.
///
///
public async UniTask RenewSubscription()
{
// Make sure the subscription is not active.
if (Subscribed)
{
await Unsubscribe();
}
// Re-establish the subscription.
subscription = Query.Subscribe(Callbacks);
}
///
/// Unsubscribes from and disposes of the subscription.
///
///
public async UniTask Unsubscribe()
{
if (Subscribed)
{
await UniTask.Run(() =>
{
// Try to close down the subscription properly
subscription.Unsubscribe();
subscription.Dispose();
subscription = null;
Subscribed = false;
});
}
}
}
}