-
Notifications
You must be signed in to change notification settings - Fork 3
Server Utilities
The plaything.io server is written from ground up and uses its own solutions. Here are the widespread types used across the codebase:
The Events.ts file defines the event API:
CreateEvent
creates 2 objects in a tuple - the event interface (Event<T>
) and the event trigger.
The interface allows you to:
-
addEventListener
- adds a listener that fires every time the trigger is used -
removeEventListener
- removed the listener -
addOnce
- adds a listener that will fire on the next trigger and then be removed -
addOnceWhen
- adds a listener that will fire once when a condition is met, then be removed
The Reactive.ts file defines the Reactive<T>
class
A reactive is a wrapper for a value, which has an event for when the value changes. It also can be synchronized with another reactive with a WeakRef
.
It is also possible to create an aggregate reactive with CreateReactiveAggregate<Tin, Tout>
.
This will create a reactive whose value will be determined based on several input reactives of a homogenous type.
The Subscription.ts file defines 2 pool types:
-
SubscribablePool<T>
- any object which has an event for when an entry is added and removed as well as a way to get all current entries -
ManagedPool<T>
created withCreatePool<T>
- a default implementation of aSubscribablePool<T>
which allows you to add, remove and clear the pool
Pools are used as a list which can be watched for updates.
The Session.ts file defines a pool of sessions created with the CreateSessionPool<T>
function
This is a simple pool which in addition to values also has keys
The User.ts file defines a pool of User
s created with CreateUserPool<Tsession>
.
A single user might have multiple sessions active at once - this pool keeps track of unique users with at least one session.
The User.ts file defines a pool of active User
s created with CreateActiveUserPool<Tsession>
.
The input session needs to have an isActive: Reactive<boolean>
field. This pool will keep track of all unique users which are active on at least one session, as defined by the reactive field.
The Subscription.ts file defines PoolSubscription<T>
A pool subscription is a wrapper for event listeners linked to a pool.
A pool subscription can be created with the CreatePoolSubscription
function. The parameters are:
- The pool to subscribe to
- What to do when an entry is added. This callback has 2 arguments - the entry and whether it already was in the pool when creating the subscription
- What to do when an entry is removed
- Filter options
The returned object has a ReactTo<T>
and unsubscribe
function.
The ReactTo<T>
function requires a getter which returns a Reactive<T>
from a given entry. The second argument is the callback which accepts the entry, then the new value, then the old value. This function can be chained multiple times. It will be removed when the entry is removed
The unsubscribe
function will remove all added event listeners and reactives
The CreateNestedPool<T>
function accepts a pool of pools and generates the flattened pool which is kept in sync with the input pool.
The CreateWebsocketSubscriptionManager
function creates an object which will manage any kind of subscription for Web Sockets.
This is a simple mechanism which accepts a Web Socket and name for a subscription and allows you to:
- Check if it already exists, in which case you dont need to subscribe again
- Create a subscription - this accepts a callback to call when the subscription expires. It will be called when the Web Socket closes
- Remove a subscription - will call the callback defined when creating the subscription and remove it