Skip to content

Commit

Permalink
Added missing static IDs
Browse files Browse the repository at this point in the history
Added Subscriber.ts which is passed to ActionBus instead of functions.
  • Loading branch information
robere2 committed Aug 11, 2020
1 parent a226a53 commit 276dbd1
Show file tree
Hide file tree
Showing 32 changed files with 93 additions and 32 deletions.
41 changes: 19 additions & 22 deletions actions/ActionBus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Action from "./Action";
import Subscriber from "./Subscriber";

/**
* The ActionBus is the bus to allow different elements of your program to be notified when a new Action is published,
Expand All @@ -7,51 +8,47 @@ import Action from "./Action";
class ActionBus {

/**
* The subscribed functions. Map of Action IDs to all the subscribed Functions for that Action ID.
* The subscribed Subscribers. Map of Action IDs to all the Subscribers for that Action ID.
*/
private subscribers : Record<number, Function[]> = {}
private subscribers : Record<number, Subscriber[]> = {}

/**
* Subscribe to listen for new Actions of a certain type to come in, at which point the passed function
* will be called. The same Function can be subscribed multiple times.
* Subscribe to listen for new Actions of a certain type to come in, at which point the passed Subscriber
* will be notified. The same Subscriber can be subscribed multiple times.
* @param action {typeof Action|number} The Action type to listen for. Can be an Action class, or an Action ID.
* @param fn {Function} The function to be called when the Action is received. Should be non-null. Should take at
* least one argument, which is the Action. Further arbitrary "rest" arguments can be passed to {@link publish},
* which can be received by this function.
* @param sub {Subscriber} The Subscriber to be notified when the specified Action is published.
* @returns {boolean} True if the subscriber is added successfully, false otherwise.
*/
public subscribe(action: typeof Action|number, fn: Function) : boolean {
if(action == null || fn == null) {
public subscribe(action: typeof Action|number, sub: Subscriber) : boolean {
if(action == null || sub == null) {
return false
}
const actionId = typeof action == "number" ? action : action.id
if(this.subscribers[actionId] == null) {
this.subscribers[actionId] = []
}
this.subscribers[actionId].push(fn)
this.subscribers[actionId].push(sub)
return true
}

/**
* Unsubscribe to stop listening for a type of Action on a specific Function. Will only unsubscribe the Function
* once, so if the passed Function was subscribed multiple times, you will need to unsubscribe multiple times.
* Unsubscribe to stop listening for a type of Action on a specific Subscriber. Will only unsubscribe the Subscriber
* once, so if the passed Subscriber was subscribed multiple times, you will need to unsubscribe multiple times.
* @param action {typeof Action|number} The Action type to listen for. Can be an Action class, or an Action ID.
* @param fn {Function} The function to be called when the Action is received. Should be non-null. Should take at
* least one argument, which is the Action. Further arbitrary "rest" arguments can be passed to {@link publish},
* which can be received by this function.
* @param sub {Subscriber} The Subscriber to be notified when the specified Action is published.
* @returns {boolean} True if the subscriber is added successfully, false otherwise. Returns true if the
* passed Function was never subscribed to the passed Action in the first place.
*/
public unsubscribe(action: typeof Action|number, fn: Function) : boolean {
if(action == null || fn == null) {
public unsubscribe(action: typeof Action|number, sub: Subscriber) : boolean {
if(action == null || sub == null) {
return true
}

const actionId = typeof action == "number" ? action : action.id
if(this.subscribers[actionId] == null) {
return true
}
const index = this.subscribers[actionId].indexOf(fn)
const index = this.subscribers[actionId].indexOf(sub)
if(index < 0 || index >= this.subscribers[actionId].length) {
return true
}
Expand All @@ -61,16 +58,16 @@ class ActionBus {
}

/**
* Publish a new Action to this ActionBus, notifying all Functions subscribed to the type of Action published.
* Publish a new Action to this ActionBus, notifying all Subscribers subscribed to the type of Action published.
* @param action {Action} The action to publish. Should be non-null.
* @param args {Object[]} Arbitrary arguments to pass to the subscriber
* @param args {Object[]} Arbitrary arguments to pass to the subscriber.
*/
public publish(action: Action, ... args: Object[]) : void {
public async publish(action: Action, ... args: Object[]) : Promise<void> {
if(action == null || this.subscribers[action.id] == null) {
return
}
for(let i = 0; i < this.subscribers[action.id].length; i++) {
this.subscribers[action.id][i](action, ...args)
await this.subscribers[action.id][i].run(action, ...args)
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions actions/Subscriber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Action from "./Action";

/**
* Subscribers can be subscribed to the ActionBus to be notified when a specific Action is published to the bus.
*/
abstract class Subscriber {
/**
* Run function called when the ActionBus this Subscriber is called to notifies this Subscriber of a new Action.
* @param action {Action} The Action that caused this Subscriber to be notified.
* @param args {Object[]} Arbitrary arguments provided by the ActionBus publisher.
*/
abstract async run(action: Action, ... args: Object[]): Promise<void>
}

export default Subscriber

2 changes: 2 additions & 0 deletions actions/clientbound/AuthBeginHandshakeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {Buffer} from 'buffer'
*/
class AuthBeginHandshakeAction extends Action {

static id = 26

/**
* Create a new AuthBeginHandshakeAction.
* @param handshakeToken {string} Handshake token to send
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/AuthCompleteAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {Buffer} from 'buffer'
*/
class AuthCompleteAction extends Action {

static id = 28

/**
* Create a new AuthCompleteAction.
* @param sessionToken {string} Session token to send
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/DisableModAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {Buffer} from 'buffer'
*/
class DisableModAction extends Action {

static id = 2

/**
* Create a new DisableModAction.
* @param reason {string} The reason the mod was disabled
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/EnableModAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import Action from '../Action'
*/
class EnableModAction extends Action {

static id = 1

/**
* Create a new EnableModAction.
*/
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/OpenGuiAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {Buffer} from 'buffer'
*/
class OpenGuiAction extends Action {

static id = 10

/**
* Create a new OpenGuiAction.
* @param classpath {string} The path of the class to open as a GUI.
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/OpenScreenAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {Buffer} from 'buffer'
*/
class OpenScreenAction extends Action {

static id = 11

/**
* Create a new OpenScreenAction.
* @param screenName {string} The name of the screen that the client should open.
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/RefreshCacheAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Action from '../Action'
*/
class RefreshCacheAction extends Action {

static id = 12

/**
* Create a new RefreshCacheAction.
*/
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/ResetConfigAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import Action from '../Action'
*/
class ResetConfigAction extends Action {

static id = 5

/**
* Create a new ResetConfigAction.
*/
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/SendChatCommandAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {Buffer} from 'buffer'
*/
class SendChatCommandAction extends Action {

static id = 6

/**
* Create a new SendChatCommandAction.
* @param cmd {string} Command to send. Beginning slash will automatically be removed if provided,
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/SendChatComponentAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {Buffer} from 'buffer'
*/
class SendChatComponentAction extends Action {

static id = 3

/**
* Create a new SendChatComponentAction.
* @param component {Message} Chat component message for the client to send
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/SetAliasedActionAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {Buffer} from 'buffer'
*/
class SetAliasedActionAction extends Action {

static id = 7

/**
* Create a new SetAliasedActionAction.
* @param aliasedAction {AliasedAction} Aliased action to be saved to the client.
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/SetButtonAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {Buffer} from 'buffer'
*/
class SetButtonAction extends Action {

static id = 8

/**
* Create a new SetButtonAction.
* @param button {Button} Button to be saved to the client.
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/SetCurrentServerAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {Buffer} from 'buffer'
*/
class SetCurrentServerAction extends Action {

static id = 13

/**
* Create a new SetCurrentServerAction.
* @param serverName {string} The name of the server that the client has connected to.
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/SetGlyphForUserAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {Buffer} from 'buffer'
*/
class SetGlyphForUserAction extends Action {

static id = 14

/**
* Create a new SetGlyphForUserAction.
* @param uuid {string} The UUID of the user for which this Glyph belongs.
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/SetKeybindsAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {Buffer} from 'buffer'
*/
class SetKeybindsAction extends Action {

static id = 15

/**
* Create a new SetKeybindsAction.
* @param keybinds {Record<string, ?>[]} New keybinds to serialize and send to the client.
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/SetPremiumAboutAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {Buffer} from 'buffer'
*/
class SetPremiumAboutAction extends Action {

static id = 16

/**
* Create a new SetPremiumAboutAction.
* @param component {ChatComponent} Chat component to set the about text to.
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/SetScreenAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {Buffer} from 'buffer'
*/
class SetScreenAction extends Action {

static id = 9

/**
* Create a new SetScreenAction.
* @param screen {Screen} Screen to be saved to the client.
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/SetTranslationAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {Buffer} from 'buffer'
*/
class SetTranslationAction extends Action {

static id = 17

/**
* Create a new SetTranslationAction.
* @param key {string} The key of the translation to set.
Expand Down
2 changes: 2 additions & 0 deletions actions/clientbound/SystemOutAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {Buffer} from 'buffer'
*/
class SystemOutAction extends Action {

static id = 4

/**
* Create a new SystemOutAction.
* @param message {string} the message to send to the client's logs
Expand Down
2 changes: 1 addition & 1 deletion actions/serverbound/AuthEndHandshakeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Action from '../Action'
import {Buffer} from 'buffer'

/**
* SERVERBOUND - Server should not instantiate.
* ID: 27
* Received by the server when the client is done authenticating with
* Mojang's servers, and Quickplay's backend should check for authenticity.
Expand All @@ -12,6 +11,7 @@ import {Buffer} from 'buffer'
*/
class AuthEndHandshakeAction extends Action {

static id = 27
/**
* Create a new AuthEndHandshakeAction.
* @param username {string} The current client's username
Expand Down
3 changes: 2 additions & 1 deletion actions/serverbound/ButtonPressedAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Action from '../Action'
import {Buffer} from 'buffer'

/**
* SERVERBOUND - Server should not instantiate.
* ID: 18
* Received by the server when the client has pressed a Quickplay button, or a keybind which points to a button.
*
Expand All @@ -11,6 +10,8 @@ import {Buffer} from 'buffer'
*/
class ButtonPressedAction extends Action {

static id = 18

/**
* Create a new ButtonPressedAction.
* @param buttonKey {string} The key of the button which was pressed
Expand Down
3 changes: 2 additions & 1 deletion actions/serverbound/ExceptionThrownAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Action from '../Action'
import {Buffer} from 'buffer'

/**
* SERVERBOUND - Server should not instantiate.
* ID: 19
* Received by the server when an exception is reported by the client.
*
Expand All @@ -27,6 +26,8 @@ import {Buffer} from 'buffer'
*/
class ExceptionThrownAction extends Action {

static id = 19

/**
* Create a new ExceptionThrownAction.
* @param type {string} The type of this exception.
Expand Down
3 changes: 2 additions & 1 deletion actions/serverbound/HypixelLocationChangedAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Action from '../Action'
import {Buffer} from 'buffer'

/**
* SERVERBOUND - Server should not instantiate.
* ID: 20
* Received by the server when the client changes locations on Hypixel.
*
Expand All @@ -11,6 +10,8 @@ import {Buffer} from 'buffer'
*/
class HypixelLocationChangedAction extends Action {

static id = 20

/**
* Create a new HypixelLocationChangedAction.
* @param locationJson {string} JSON about this player's location.
Expand Down
3 changes: 2 additions & 1 deletion actions/serverbound/InitializeClientAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Action from '../Action'
import {Buffer} from 'buffer'

/**
* SERVERBOUND - Server should not instantiate.
* ID: 25
* Received by the server when the client first initializes the socket. Intended to send client metadata.
*
Expand Down Expand Up @@ -34,6 +33,8 @@ import {Buffer} from 'buffer'
*/
class InitializeClientAction extends Action {

static id = 25

/**
* Create a new InitializeClientAction.
*/
Expand Down
3 changes: 2 additions & 1 deletion actions/serverbound/LanguageChangedAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Action from '../Action'
import {Buffer} from 'buffer'

/**
* SERVERBOUND - Server should not instantiate.
* ID: 22
* Received by the server when the client changes languages.
*
Expand All @@ -11,6 +10,8 @@ import {Buffer} from 'buffer'
*/
class LanguageChangedAction extends Action {

static id = 22

/**
* Create a new LanguageChangedAction.
* @param langId {string} New language ID
Expand Down
3 changes: 2 additions & 1 deletion actions/serverbound/MigrateKeybindsAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Action from '../Action'
import {Buffer} from 'buffer'

/**
* SERVERBOUND - Server should not instantiate.
* ID: 21
* Send the list keybinds to the server so the server can respond with a migrated keybinds list.
* This is currently only used to migrate keybinds from pre-2.1.0 to post-2.1.0.
Expand All @@ -13,6 +12,8 @@ import {Buffer} from 'buffer'
*/
class MigrateKeybindsAction extends Action {

static id = 21

/**
* Create a new MigrateKeybindsAction.
* @param keybinds {Record<string, ?>[]} New keybinds to serialize and send to the server.
Expand Down
Loading

0 comments on commit 276dbd1

Please sign in to comment.