@nativewrappers/common 0.0.129 → 0.0.131
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.
|
@@ -70,10 +70,12 @@ class NetworkedMap extends Map {
|
|
|
70
70
|
}
|
|
71
71
|
/*
|
|
72
72
|
* 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)
|
|
73
|
-
*
|
|
74
|
-
* 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
|
|
75
73
|
*/
|
|
76
74
|
resync(source2) {
|
|
75
|
+
if (!this.#subscribers.has(source2)) {
|
|
76
|
+
console.error(`[NetworkedMap:resync] Tried to call resync on a source that wasn't already subscribed`);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
77
79
|
const packed_data = msgpack_pack([this.#syncName, [[4 /* Init */, this.size === 0 ? [] : Array.from(this)]]]);
|
|
78
80
|
TriggerClientEventInternal(
|
|
79
81
|
`${GlobalData.CurrentResource}:syncChanges`,
|
|
@@ -83,7 +85,7 @@ class NetworkedMap extends Map {
|
|
|
83
85
|
);
|
|
84
86
|
}
|
|
85
87
|
/*
|
|
86
|
-
* Adds a new subscriber to the map
|
|
88
|
+
* Adds a new subscriber to the map, this will automatically call resync on the client.
|
|
87
89
|
*/
|
|
88
90
|
addSubscriber(source2) {
|
|
89
91
|
this.#subscribers.add(source2);
|
|
@@ -155,10 +157,45 @@ class NetworkedMap extends Map {
|
|
|
155
157
|
ln(value);
|
|
156
158
|
}
|
|
157
159
|
}
|
|
160
|
+
/*
|
|
161
|
+
* Adds {@param value} to the map under {@param key}, {@param value} will automatically be turned into a proxy
|
|
162
|
+
* object and the NetworkedMap will automatically send events on SubValue changes.
|
|
163
|
+
* ```ts
|
|
164
|
+
*
|
|
165
|
+
* const map = new NetworkedMap<number, { name:string , quantity: number, obj: { nested_obj_description: string }}>("someUniqueName");
|
|
166
|
+
*
|
|
167
|
+
* const item = {
|
|
168
|
+
* name: "magic_item",
|
|
169
|
+
* quantity: 1,
|
|
170
|
+
* obj: {
|
|
171
|
+
* something_description: "Will never be updated after init"
|
|
172
|
+
* }
|
|
173
|
+
* }
|
|
174
|
+
*
|
|
175
|
+
* // automatically converts `item` into a proxy object
|
|
176
|
+
* map.set(1, item)
|
|
177
|
+
*
|
|
178
|
+
* // if you immediately change the value this will not be recognized, you have to get the object again from the map
|
|
179
|
+
* item.quantity = 2;
|
|
180
|
+
*
|
|
181
|
+
* // reactive object
|
|
182
|
+
* const proxy_object = map.get(1)!;
|
|
183
|
+
*
|
|
184
|
+
* // sub value change will be sent to the client on the next network tick.
|
|
185
|
+
* proxy_object.quantity = 2;
|
|
186
|
+
*
|
|
187
|
+
* // NOTE: this doesn't work on deeply nested objects, it will only work on anything assigned to the first object
|
|
188
|
+
* // This update will never be sent to the client, similar to how state bags work.
|
|
189
|
+
* proxy_object.obj.nested_obj_description = "Some new value"
|
|
190
|
+
* ```
|
|
191
|
+
* i.e. if you have { name: "magic_item", quantity: 1}
|
|
192
|
+
* if you change the item.quantity field this will automatically be sync'd to the client, is only one depth deep
|
|
193
|
+
* so if you have { obj: { some_quantity: 1 } } setting item.obj.some_quantity will not update the client.
|
|
194
|
+
*/
|
|
158
195
|
set(key, value) {
|
|
159
196
|
let v = value;
|
|
160
197
|
if (value instanceof Object) {
|
|
161
|
-
const
|
|
198
|
+
const curThis = this;
|
|
162
199
|
const objectChangeHandler = {
|
|
163
200
|
get(target, prop, reciever) {
|
|
164
201
|
return Reflect.get(target, prop, reciever);
|
|
@@ -166,9 +203,9 @@ class NetworkedMap extends Map {
|
|
|
166
203
|
set(target, p, newValue, receiver) {
|
|
167
204
|
const success = Reflect.set(target, p, newValue, receiver);
|
|
168
205
|
if (success) {
|
|
169
|
-
|
|
206
|
+
curThis.#pushChangeForListener(key, target);
|
|
170
207
|
$SERVER: {
|
|
171
|
-
|
|
208
|
+
curThis.#queuedChanges.push([0 /* SubValueChanged */, key, p, newValue]);
|
|
172
209
|
}
|
|
173
210
|
}
|
|
174
211
|
return success;
|
package/net/NetworkedMap.js
CHANGED
|
@@ -70,10 +70,12 @@ class NetworkedMap extends Map {
|
|
|
70
70
|
}
|
|
71
71
|
/*
|
|
72
72
|
* 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)
|
|
73
|
-
*
|
|
74
|
-
* 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
|
|
75
73
|
*/
|
|
76
74
|
resync(source2) {
|
|
75
|
+
if (!this.#subscribers.has(source2)) {
|
|
76
|
+
console.error(`[NetworkedMap:resync] Tried to call resync on a source that wasn't already subscribed`);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
77
79
|
const packed_data = msgpack_pack([this.#syncName, [[4 /* Init */, this.size === 0 ? [] : Array.from(this)]]]);
|
|
78
80
|
TriggerClientEventInternal(
|
|
79
81
|
`${GlobalData.CurrentResource}:syncChanges`,
|
|
@@ -83,7 +85,7 @@ class NetworkedMap extends Map {
|
|
|
83
85
|
);
|
|
84
86
|
}
|
|
85
87
|
/*
|
|
86
|
-
* Adds a new subscriber to the map
|
|
88
|
+
* Adds a new subscriber to the map, this will automatically call resync on the client.
|
|
87
89
|
*/
|
|
88
90
|
addSubscriber(source2) {
|
|
89
91
|
this.#subscribers.add(source2);
|
|
@@ -155,10 +157,45 @@ class NetworkedMap extends Map {
|
|
|
155
157
|
ln(value);
|
|
156
158
|
}
|
|
157
159
|
}
|
|
160
|
+
/*
|
|
161
|
+
* Adds {@param value} to the map under {@param key}, {@param value} will automatically be turned into a proxy
|
|
162
|
+
* object and the NetworkedMap will automatically send events on SubValue changes.
|
|
163
|
+
* ```ts
|
|
164
|
+
*
|
|
165
|
+
* const map = new NetworkedMap<number, { name:string , quantity: number, obj: { nested_obj_description: string }}>("someUniqueName");
|
|
166
|
+
*
|
|
167
|
+
* const item = {
|
|
168
|
+
* name: "magic_item",
|
|
169
|
+
* quantity: 1,
|
|
170
|
+
* obj: {
|
|
171
|
+
* something_description: "Will never be updated after init"
|
|
172
|
+
* }
|
|
173
|
+
* }
|
|
174
|
+
*
|
|
175
|
+
* // automatically converts `item` into a proxy object
|
|
176
|
+
* map.set(1, item)
|
|
177
|
+
*
|
|
178
|
+
* // if you immediately change the value this will not be recognized, you have to get the object again from the map
|
|
179
|
+
* item.quantity = 2;
|
|
180
|
+
*
|
|
181
|
+
* // reactive object
|
|
182
|
+
* const proxy_object = map.get(1)!;
|
|
183
|
+
*
|
|
184
|
+
* // sub value change will be sent to the client on the next network tick.
|
|
185
|
+
* proxy_object.quantity = 2;
|
|
186
|
+
*
|
|
187
|
+
* // NOTE: this doesn't work on deeply nested objects, it will only work on anything assigned to the first object
|
|
188
|
+
* // This update will never be sent to the client, similar to how state bags work.
|
|
189
|
+
* proxy_object.obj.nested_obj_description = "Some new value"
|
|
190
|
+
* ```
|
|
191
|
+
* i.e. if you have { name: "magic_item", quantity: 1}
|
|
192
|
+
* if you change the item.quantity field this will automatically be sync'd to the client, is only one depth deep
|
|
193
|
+
* so if you have { obj: { some_quantity: 1 } } setting item.obj.some_quantity will not update the client.
|
|
194
|
+
*/
|
|
158
195
|
set(key, value) {
|
|
159
196
|
let v = value;
|
|
160
197
|
if (value instanceof Object) {
|
|
161
|
-
const
|
|
198
|
+
const curThis = this;
|
|
162
199
|
const objectChangeHandler = {
|
|
163
200
|
get(target, prop, reciever) {
|
|
164
201
|
return Reflect.get(target, prop, reciever);
|
|
@@ -166,9 +203,9 @@ class NetworkedMap extends Map {
|
|
|
166
203
|
set(target, p, newValue, receiver) {
|
|
167
204
|
const success = Reflect.set(target, p, newValue, receiver);
|
|
168
205
|
if (success) {
|
|
169
|
-
|
|
206
|
+
curThis.#pushChangeForListener(key, target);
|
|
170
207
|
$SERVER: {
|
|
171
|
-
|
|
208
|
+
curThis.#queuedChanges.push([0 /* SubValueChanged */, key, p, newValue]);
|
|
172
209
|
}
|
|
173
210
|
}
|
|
174
211
|
return success;
|