@nativewrappers/common 0.0.57 → 0.0.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/GlobalData.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export declare class GlobalData {
2
+ static CurrentResource: string;
2
3
  static IS_SERVER: boolean;
3
4
  static IS_CLIENT: boolean;
4
5
  static NetworkTick: number | null;
package/GlobalData.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export class GlobalData {
2
+ static CurrentResource = GetCurrentResourceName();
2
3
  static IS_SERVER = IsDuplicityVersion();
3
4
  static IS_CLIENT = !GlobalData.IS_SERVER;
4
5
  static NetworkTick = null;
@@ -5,10 +5,12 @@ type ChangeListener<V> = (value: V) => void;
5
5
  export declare class NetworkedMap<K, V> extends Map<K, V> {
6
6
  #private;
7
7
  constructor(syncName: string, initialValue?: [K, V][]);
8
+ get SyncName(): string;
8
9
  private onPlayerDropped;
9
10
  addSubscriber(sub: number): void;
10
11
  removeSubscriber(sub: number): boolean;
11
12
  hasSubscriber(sub: number): boolean;
13
+ private handleSync;
12
14
  listenForChange(key: K, fn: ChangeListener<V>): void;
13
15
  set(key: K, value: V): this;
14
16
  clear(): void;
@@ -12,6 +12,48 @@ var MapChangeType;
12
12
  // Whenever they're subscribed initially and get the initial load of data
13
13
  MapChangeType[MapChangeType["Init"] = 4] = "Init";
14
14
  })(MapChangeType || (MapChangeType = {}));
15
+ // Used to not make a bunch of on/raw events, we just reuse the same one and look up the data in the map
16
+ class NetworkedMapEventManager {
17
+ #syncedCalls = new Map();
18
+ #playerDropped = new Map();
19
+ constructor() {
20
+ SERVER: if (GlobalData.IS_SERVER) {
21
+ on("playerDropped", () => {
22
+ // call the player dropped for each call
23
+ for (const [_k, fn] of this.#playerDropped) {
24
+ fn();
25
+ }
26
+ });
27
+ return;
28
+ }
29
+ CLIENT: {
30
+ RegisterResourceAsEventHandler(`${GlobalData.CurrentResource}:syncChanges`);
31
+ addRawEventListener(`${GlobalData.CurrentResource}:syncChanges`, (msgpack_data) => {
32
+ const data = msgpack_unpack(msgpack_data);
33
+ const syncName = data[0];
34
+ const syncData = data[1];
35
+ const map = this.#syncedCalls.get(syncName);
36
+ if (!map) {
37
+ throw new Error(`Tried to sync changes for a networked map but ${syncName} does't exist.`);
38
+ }
39
+ // @ts-ignore
40
+ map.handleSync(syncData);
41
+ });
42
+ }
43
+ }
44
+ addNetworkedMap(map) {
45
+ // abuse typescript, we don't want the end user to use these calls but we still want it to be accessible internally.
46
+ // @ts-ignore
47
+ this.#syncedCalls.set(map.SyncName, map);
48
+ // @ts-ignore
49
+ this.#playerDropped.set(map.SyncName, map.onPlayerDropped);
50
+ }
51
+ removeNetworkedMap(syncName) {
52
+ this.#syncedCalls.delete(syncName);
53
+ this.#playerDropped.delete(syncName);
54
+ }
55
+ }
56
+ const netManager = new NetworkedMapEventManager();
15
57
  /**
16
58
  * not ready to be used just thoughts right now
17
59
  */
@@ -24,7 +66,7 @@ export class NetworkedMap extends Map {
24
66
  super(initialValue);
25
67
  this.#syncName = syncName;
26
68
  GlobalData.NetworkedTicks.push(this);
27
- on("playerDropped", () => this.onPlayerDropped());
69
+ netManager.addNetworkedMap(this);
28
70
  // if we don't have a network tick then we want to register it.
29
71
  SERVER: {
30
72
  if (!GlobalData.NetworkTick && GlobalData.IS_SERVER) {
@@ -35,12 +77,9 @@ export class NetworkedMap extends Map {
35
77
  });
36
78
  }
37
79
  }
38
- SERVER: if (GlobalData.IS_SERVER)
39
- return;
40
- CLIENT: {
41
- RegisterResourceAsEventHandler(`${this.#syncName}:syncChanges`);
42
- addRawEventListener(`${this.#syncName}:syncChanges`, (data) => this.#handleSync(data));
43
- }
80
+ }
81
+ get SyncName() {
82
+ return this.#syncName;
44
83
  }
45
84
  // handles removing the player from the map whenever they're dropped
46
85
  onPlayerDropped() {
@@ -52,9 +91,10 @@ export class NetworkedMap extends Map {
52
91
  addSubscriber(sub) {
53
92
  this.#subscribers.add(sub);
54
93
  const packed_data = msgpack_pack([
55
- [MapChangeType.Init, this.size === 0 ? [] : Array.from(this)],
94
+ this.#syncName,
95
+ [[MapChangeType.Init, this.size === 0 ? [] : Array.from(this)]],
56
96
  ]);
57
- TriggerClientEventInternal(`${this.#syncName}:syncChanges`, sub, packed_data, packed_data.length);
97
+ TriggerClientEventInternal(`${GlobalData.CurrentResource}:syncChanges`, sub, packed_data, packed_data.length);
58
98
  }
59
99
  removeSubscriber(sub) {
60
100
  return this.#subscribers.delete(sub);
@@ -62,8 +102,7 @@ export class NetworkedMap extends Map {
62
102
  hasSubscriber(sub) {
63
103
  return this.#subscribers.has(sub);
64
104
  }
65
- #handleSync(msgpack_data) {
66
- const data = msgpack_unpack(msgpack_data);
105
+ handleSync(data) {
67
106
  for (const [change_type, key, value, possibly_undefined_subvalue] of data) {
68
107
  switch (change_type) {
69
108
  case MapChangeType.Add: {
@@ -103,9 +142,9 @@ export class NetworkedMap extends Map {
103
142
  listener ? listener.push(fn) : this.#changeListeners.set(key, [fn]);
104
143
  }
105
144
  #triggerEventForSubscribers(data) {
106
- const packed_data = msgpack_pack(data);
145
+ const packed_data = msgpack_pack([this.#syncName, data]);
107
146
  for (const sub of this.#subscribers) {
108
- TriggerClientEventInternal(`${this.#syncName}:syncChanges`, sub, packed_data, packed_data.length);
147
+ TriggerClientEventInternal(`${GlobalData.CurrentResource}:syncChanges`, sub, packed_data, packed_data.length);
109
148
  }
110
149
  }
111
150
  #pushChangeForListener(key, value) {
@@ -173,10 +212,10 @@ export class NetworkedMap extends Map {
173
212
  }
174
213
  }
175
214
  [Symbol.dispose]() {
176
- removeEventListener("playerDropped", this.onPlayerDropped);
177
215
  this.#subscribers.clear();
178
216
  this.#changeListeners.clear();
179
217
  this.#queuedChanges = [];
218
+ netManager.removeNetworkedMap(this.#syncName);
180
219
  GlobalData.NetworkedTicks.filter((v) => v !== this);
181
220
  }
182
221
  /**
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "author": "Remco Troost <d0p3t>",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
- "version": "0.0.57",
7
+ "version": "0.0.58",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "https://github.com/nativewrappers/nativewrappers.git"