Skip to content

Commit 276dbd1

Browse files
committed
Added missing static IDs
Added Subscriber.ts which is passed to ActionBus instead of functions.
1 parent a226a53 commit 276dbd1

32 files changed

+93
-32
lines changed

actions/ActionBus.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Action from "./Action";
2+
import Subscriber from "./Subscriber";
23

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

910
/**
10-
* The subscribed functions. Map of Action IDs to all the subscribed Functions for that Action ID.
11+
* The subscribed Subscribers. Map of Action IDs to all the Subscribers for that Action ID.
1112
*/
12-
private subscribers : Record<number, Function[]> = {}
13+
private subscribers : Record<number, Subscriber[]> = {}
1314

1415
/**
15-
* Subscribe to listen for new Actions of a certain type to come in, at which point the passed function
16-
* will be called. The same Function can be subscribed multiple times.
16+
* Subscribe to listen for new Actions of a certain type to come in, at which point the passed Subscriber
17+
* will be notified. The same Subscriber can be subscribed multiple times.
1718
* @param action {typeof Action|number} The Action type to listen for. Can be an Action class, or an Action ID.
18-
* @param fn {Function} The function to be called when the Action is received. Should be non-null. Should take at
19-
* least one argument, which is the Action. Further arbitrary "rest" arguments can be passed to {@link publish},
20-
* which can be received by this function.
19+
* @param sub {Subscriber} The Subscriber to be notified when the specified Action is published.
2120
* @returns {boolean} True if the subscriber is added successfully, false otherwise.
2221
*/
23-
public subscribe(action: typeof Action|number, fn: Function) : boolean {
24-
if(action == null || fn == null) {
22+
public subscribe(action: typeof Action|number, sub: Subscriber) : boolean {
23+
if(action == null || sub == null) {
2524
return false
2625
}
2726
const actionId = typeof action == "number" ? action : action.id
2827
if(this.subscribers[actionId] == null) {
2928
this.subscribers[actionId] = []
3029
}
31-
this.subscribers[actionId].push(fn)
30+
this.subscribers[actionId].push(sub)
3231
return true
3332
}
3433

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

5047
const actionId = typeof action == "number" ? action : action.id
5148
if(this.subscribers[actionId] == null) {
5249
return true
5350
}
54-
const index = this.subscribers[actionId].indexOf(fn)
51+
const index = this.subscribers[actionId].indexOf(sub)
5552
if(index < 0 || index >= this.subscribers[actionId].length) {
5653
return true
5754
}
@@ -61,16 +58,16 @@ class ActionBus {
6158
}
6259

6360
/**
64-
* Publish a new Action to this ActionBus, notifying all Functions subscribed to the type of Action published.
61+
* Publish a new Action to this ActionBus, notifying all Subscribers subscribed to the type of Action published.
6562
* @param action {Action} The action to publish. Should be non-null.
66-
* @param args {Object[]} Arbitrary arguments to pass to the subscriber
63+
* @param args {Object[]} Arbitrary arguments to pass to the subscriber.
6764
*/
68-
public publish(action: Action, ... args: Object[]) : void {
65+
public async publish(action: Action, ... args: Object[]) : Promise<void> {
6966
if(action == null || this.subscribers[action.id] == null) {
7067
return
7168
}
7269
for(let i = 0; i < this.subscribers[action.id].length; i++) {
73-
this.subscribers[action.id][i](action, ...args)
70+
await this.subscribers[action.id][i].run(action, ...args)
7471
}
7572
}
7673
}

actions/Subscriber.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Action from "./Action";
2+
3+
/**
4+
* Subscribers can be subscribed to the ActionBus to be notified when a specific Action is published to the bus.
5+
*/
6+
abstract class Subscriber {
7+
/**
8+
* Run function called when the ActionBus this Subscriber is called to notifies this Subscriber of a new Action.
9+
* @param action {Action} The Action that caused this Subscriber to be notified.
10+
* @param args {Object[]} Arbitrary arguments provided by the ActionBus publisher.
11+
*/
12+
abstract async run(action: Action, ... args: Object[]): Promise<void>
13+
}
14+
15+
export default Subscriber
16+

actions/clientbound/AuthBeginHandshakeAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {Buffer} from 'buffer'
1010
*/
1111
class AuthBeginHandshakeAction extends Action {
1212

13+
static id = 26
14+
1315
/**
1416
* Create a new AuthBeginHandshakeAction.
1517
* @param handshakeToken {string} Handshake token to send

actions/clientbound/AuthCompleteAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {Buffer} from 'buffer'
1111
*/
1212
class AuthCompleteAction extends Action {
1313

14+
static id = 28
15+
1416
/**
1517
* Create a new AuthCompleteAction.
1618
* @param sessionToken {string} Session token to send

actions/clientbound/DisableModAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {Buffer} from 'buffer'
1010
*/
1111
class DisableModAction extends Action {
1212

13+
static id = 2
14+
1315
/**
1416
* Create a new DisableModAction.
1517
* @param reason {string} The reason the mod was disabled

actions/clientbound/EnableModAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import Action from '../Action'
66
*/
77
class EnableModAction extends Action {
88

9+
static id = 1
10+
911
/**
1012
* Create a new EnableModAction.
1113
*/

actions/clientbound/OpenGuiAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {Buffer} from 'buffer'
1111
*/
1212
class OpenGuiAction extends Action {
1313

14+
static id = 10
15+
1416
/**
1517
* Create a new OpenGuiAction.
1618
* @param classpath {string} The path of the class to open as a GUI.

actions/clientbound/OpenScreenAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {Buffer} from 'buffer'
1010
*/
1111
class OpenScreenAction extends Action {
1212

13+
static id = 11
14+
1315
/**
1416
* Create a new OpenScreenAction.
1517
* @param screenName {string} The name of the screen that the client should open.

actions/clientbound/RefreshCacheAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import Action from '../Action'
88
*/
99
class RefreshCacheAction extends Action {
1010

11+
static id = 12
12+
1113
/**
1214
* Create a new RefreshCacheAction.
1315
*/

actions/clientbound/ResetConfigAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import Action from '../Action'
66
*/
77
class ResetConfigAction extends Action {
88

9+
static id = 5
10+
911
/**
1012
* Create a new ResetConfigAction.
1113
*/

0 commit comments

Comments
 (0)