Skip to content

Commit

Permalink
Merge pull request #6 from techno-dwarf-works/bug/monoservice_fix
Browse files Browse the repository at this point in the history
Fix MonoService Token NullReference
  • Loading branch information
uurha committed May 3, 2024
1 parent 1ec5735 commit 2ad09fc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
31 changes: 29 additions & 2 deletions Runtime/Services/MonoService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Better.Services.Runtime.Interfaces;
Expand All @@ -11,11 +12,32 @@ public abstract class MonoService : MonoBehaviour, IService
private CancellationTokenSource _destroyCancellationToken;

public bool Initialized { get; private set; }
protected CancellationToken DestroyCancellationToken => _destroyCancellationToken.Token;

protected CancellationToken DestroyCancellationToken
{
get
{
if (_destroyCancellationToken == null)
{
_destroyCancellationToken = new CancellationTokenSource();
}
return _destroyCancellationToken.Token;
}
}

protected virtual void Awake()
{
_destroyCancellationToken = new();

}

protected virtual void OnEnable()
{

}

protected virtual void Start()
{

}

async Task IService.InitializeAsync(CancellationToken cancellationToken)
Expand Down Expand Up @@ -56,6 +78,11 @@ Task IService.PostInitializeAsync(CancellationToken cancellationToken)
protected abstract Task OnInitializeAsync(CancellationToken cancellationToken);
protected abstract Task OnPostInitializeAsync(CancellationToken cancellationToken);

protected virtual void OnDisable()
{

}

protected virtual void OnDestroy()
{
_destroyCancellationToken?.Cancel();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.tdw.better.services",
"displayName": "Better Services",
"version": "0.1.0",
"version": "0.1.1",
"unity": "2021.3",
"description": " ",
"dependencies": {
Expand Down

0 comments on commit 2ad09fc

Please sign in to comment.