@nativewrappers/common 0.0.58 → 0.0.60

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.
@@ -7,9 +7,11 @@ export declare class NetworkedMap<K, V> extends Map<K, V> {
7
7
  constructor(syncName: string, initialValue?: [K, V][]);
8
8
  get SyncName(): string;
9
9
  private onPlayerDropped;
10
- addSubscriber(sub: number): void;
10
+ resync(source: number): void;
11
+ addSubscriber(source: number): void;
11
12
  removeSubscriber(sub: number): boolean;
12
13
  hasSubscriber(sub: number): boolean;
14
+ subscriberCount(): number;
13
15
  private handleSync;
14
16
  listenForChange(key: K, fn: ChangeListener<V>): void;
15
17
  set(key: K, value: V): this;
@@ -15,13 +15,13 @@ var MapChangeType;
15
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
16
  class NetworkedMapEventManager {
17
17
  #syncedCalls = new Map();
18
- #playerDropped = new Map();
19
18
  constructor() {
20
19
  SERVER: if (GlobalData.IS_SERVER) {
21
20
  on("playerDropped", () => {
21
+ const src = source;
22
22
  // call the player dropped for each call
23
- for (const [_k, fn] of this.#playerDropped) {
24
- fn();
23
+ for (const [_k, map] of this.#syncedCalls) {
24
+ map.removeSubscriber(src);
25
25
  }
26
26
  });
27
27
  return;
@@ -45,12 +45,9 @@ class NetworkedMapEventManager {
45
45
  // abuse typescript, we don't want the end user to use these calls but we still want it to be accessible internally.
46
46
  // @ts-ignore
47
47
  this.#syncedCalls.set(map.SyncName, map);
48
- // @ts-ignore
49
- this.#playerDropped.set(map.SyncName, map.onPlayerDropped);
50
48
  }
51
49
  removeNetworkedMap(syncName) {
52
50
  this.#syncedCalls.delete(syncName);
53
- this.#playerDropped.delete(syncName);
54
51
  }
55
52
  }
56
53
  const netManager = new NetworkedMapEventManager();
@@ -86,15 +83,23 @@ export class NetworkedMap extends Map {
86
83
  this.removeSubscriber(source);
87
84
  }
88
85
  /*
89
- * Adds a new subscriber to the map, if the
86
+ * Resyncs the entire map to the client, useful for if there's a mismatch in the clients map (when multiple players change things, in cases like inventories)
87
+ *
88
+ * NOTE: This doesn't check that the player is already subscribed to the map, you should do your own due-diligence to only call this for players already subscribed
90
89
  */
91
- addSubscriber(sub) {
92
- this.#subscribers.add(sub);
90
+ resync(source) {
93
91
  const packed_data = msgpack_pack([
94
92
  this.#syncName,
95
93
  [[MapChangeType.Init, this.size === 0 ? [] : Array.from(this)]],
96
94
  ]);
97
- TriggerClientEventInternal(`${GlobalData.CurrentResource}:syncChanges`, sub, packed_data, packed_data.length);
95
+ TriggerClientEventInternal(`${GlobalData.CurrentResource}:syncChanges`, source, packed_data, packed_data.length);
96
+ }
97
+ /*
98
+ * Adds a new subscriber to the map
99
+ */
100
+ addSubscriber(source) {
101
+ this.#subscribers.add(source);
102
+ this.resync(source);
98
103
  }
99
104
  removeSubscriber(sub) {
100
105
  return this.#subscribers.delete(sub);
@@ -102,6 +107,9 @@ export class NetworkedMap extends Map {
102
107
  hasSubscriber(sub) {
103
108
  return this.#subscribers.has(sub);
104
109
  }
110
+ subscriberCount() {
111
+ return this.#subscribers.size;
112
+ }
105
113
  handleSync(data) {
106
114
  for (const [change_type, key, value, possibly_undefined_subvalue] of data) {
107
115
  switch (change_type) {
@@ -118,6 +126,8 @@ export class NetworkedMap extends Map {
118
126
  continue;
119
127
  }
120
128
  case MapChangeType.Init: {
129
+ // We also use this for whenever we want to resync a table
130
+ super.clear();
121
131
  const key_value = key;
122
132
  for (const [k, v] of key_value) {
123
133
  this.set(k, v);
@@ -192,6 +202,9 @@ export class NetworkedMap extends Map {
192
202
  }
193
203
  return this;
194
204
  }
205
+ /*
206
+ * Resets the map to its default state
207
+ */
195
208
  clear() {
196
209
  CLIENT: throw new Error(`Cannot call 'clear' on client`);
197
210
  // if we're clearing our map then we want to remove all queued changes and
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.58",
7
+ "version": "0.0.60",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "https://github.com/nativewrappers/nativewrappers.git"