From 6afbf09aed112242d4007496887b60e5705dc300 Mon Sep 17 00:00:00 2001
From: Jono Wenger <jono@elixxir.io>
Date: Thu, 20 Oct 2022 11:17:07 -0700
Subject: [PATCH] Add global tracker for number of cmix followers running

---
 wasm/follow.go |  4 ++++
 wasm/purge.go  | 15 +++++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 wasm/purge.go

diff --git a/wasm/follow.go b/wasm/follow.go
index 4b343314..3794bb2d 100644
--- a/wasm/follow.go
+++ b/wasm/follow.go
@@ -11,6 +11,7 @@ package wasm
 
 import (
 	"gitlab.com/elixxir/xxdk-wasm/utils"
+	"sync/atomic"
 	"syscall/js"
 )
 
@@ -60,6 +61,8 @@ func (c *Cmix) StartNetworkFollower(_ js.Value, args []js.Value) interface{} {
 		return nil
 	}
 
+	atomic.AddUint64(&NumClientsRunning, 1)
+
 	return nil
 }
 
@@ -77,6 +80,7 @@ func (c *Cmix) StopNetworkFollower(js.Value, []js.Value) interface{} {
 		utils.Throw(utils.TypeError, err)
 		return nil
 	}
+	atomic.AddUint64(&NumClientsRunning, ^uint64(0))
 
 	return nil
 }
diff --git a/wasm/purge.go b/wasm/purge.go
new file mode 100644
index 00000000..25c3664c
--- /dev/null
+++ b/wasm/purge.go
@@ -0,0 +1,15 @@
+////////////////////////////////////////////////////////////////////////////////
+// Copyright © 2022 xx foundation                                             //
+//                                                                            //
+// Use of this source code is governed by a license that can be found in the  //
+// LICENSE file.                                                              //
+////////////////////////////////////////////////////////////////////////////////
+
+package wasm
+
+// NumClientsRunning is an atomic that tracks the current number of Cmix
+// followers that have been started. Every time one is started, this counter
+// must be incremented and every time one is stopped, it must be decremented.
+//
+// This variable is an atomic. Only access it with atomic functions
+var NumClientsRunning uint64
-- 
GitLab