@signe/room 3.2.1 → 3.3.0
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/CHANGELOG.md +78 -0
- package/dist/index.d.ts +103 -2
- package/dist/index.js +476 -88
- package/dist/index.js.map +1 -1
- package/examples/cloudflare/CHANGELOG.md +22 -0
- package/examples/cloudflare/package.json +1 -1
- package/examples/node/CHANGELOG.md +22 -0
- package/examples/node/package.json +2 -2
- package/examples/node-game/CHANGELOG.md +22 -0
- package/examples/node-game/package.json +2 -2
- package/examples/node-shard/CHANGELOG.md +22 -0
- package/examples/node-shard/package.json +2 -2
- package/package.json +8 -3
- package/readme.md +81 -3
- package/src/decorators.ts +63 -0
- package/src/server.ts +517 -79
- package/src/utils.ts +84 -6
package/src/utils.ts
CHANGED
|
@@ -88,6 +88,66 @@ export function throttle<F extends (...args: any[]) => any>(
|
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Debounces `fn`: it runs `wait` ms after the last call, and at the latest `maxWait` ms
|
|
93
|
+
* after the first call that has not run yet (so continuous calls cannot postpone it forever).
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* const save = debounce(() => persist(), 2500, 25000);
|
|
97
|
+
*/
|
|
98
|
+
export function debounce(fn: () => void, wait: number, maxWait: number = Infinity): () => void {
|
|
99
|
+
let timeout: ReturnType<typeof setTimeout> | null = null;
|
|
100
|
+
let firstCallAt: number | null = null;
|
|
101
|
+
|
|
102
|
+
const run = () => {
|
|
103
|
+
timeout = null;
|
|
104
|
+
firstCallAt = null;
|
|
105
|
+
fn();
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
return () => {
|
|
109
|
+
const now = Date.now();
|
|
110
|
+
if (firstCallAt === null) firstCallAt = now;
|
|
111
|
+
if (timeout) clearTimeout(timeout);
|
|
112
|
+
const delay = Math.max(0, Math.min(wait, firstCallAt + maxWait - now));
|
|
113
|
+
timeout = setTimeout(run, delay);
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Coalesces calls into one run of `fn`.
|
|
119
|
+
*
|
|
120
|
+
* - `wait <= 0`: runs once at the end of the current task (microtask), however many calls were made.
|
|
121
|
+
* - `wait > 0`: also runs at most once per `wait` ms. The first call of a quiet period runs at the end
|
|
122
|
+
* of the current task; later calls are grouped into one trailing run. Unlike a debounce, continuous
|
|
123
|
+
* calls never postpone the run beyond `wait`.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* const flush = batch(() => sendPending(), 50);
|
|
127
|
+
* a.set(1); b.set(2); flush(); flush(); // sendPending() runs once
|
|
128
|
+
*/
|
|
129
|
+
export function batch(fn: () => void, wait: number = 0): () => void {
|
|
130
|
+
let scheduled = false;
|
|
131
|
+
let lastRun = -Infinity;
|
|
132
|
+
|
|
133
|
+
const run = () => {
|
|
134
|
+
scheduled = false;
|
|
135
|
+
lastRun = Date.now();
|
|
136
|
+
fn();
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
return () => {
|
|
140
|
+
if (scheduled) return;
|
|
141
|
+
scheduled = true;
|
|
142
|
+
const delay = wait > 0 ? lastRun + wait - Date.now() : 0;
|
|
143
|
+
if (delay > 0) {
|
|
144
|
+
setTimeout(run, delay);
|
|
145
|
+
} else {
|
|
146
|
+
queueMicrotask(run);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
91
151
|
/**
|
|
92
152
|
* Extracts parameters from a given string based on a specified pattern.
|
|
93
153
|
*
|
|
@@ -205,15 +265,33 @@ export function dremove(
|
|
|
205
265
|
* // result is { a: { b: { c: 1 } }, x: { y: { z: '$delete' } } }
|
|
206
266
|
* // allMemory is { a: { b: { c: 1 } }, x: { y: {} } }
|
|
207
267
|
*/
|
|
268
|
+
// Sync paths repeat on every tick (e.g. "players.p1.x"): keep their split form.
|
|
269
|
+
// Bounded so that a state with ever-changing ids cannot grow it forever.
|
|
270
|
+
const PATH_KEYS_CACHE_LIMIT = 10_000;
|
|
271
|
+
const pathKeysCache = new Map<string, string[]>();
|
|
272
|
+
|
|
273
|
+
function splitPath(path: string): string[] {
|
|
274
|
+
let keys = pathKeysCache.get(path);
|
|
275
|
+
if (!keys) {
|
|
276
|
+
if (pathKeysCache.size >= PATH_KEYS_CACHE_LIMIT) {
|
|
277
|
+
pathKeysCache.clear();
|
|
278
|
+
}
|
|
279
|
+
keys = path.split(".");
|
|
280
|
+
pathKeysCache.set(path, keys);
|
|
281
|
+
}
|
|
282
|
+
return keys;
|
|
283
|
+
}
|
|
284
|
+
|
|
208
285
|
export function buildObject(valuesMap: Map<string, any>, allMemory: Record<string, any>): Record<string, any> {
|
|
209
|
-
|
|
210
|
-
for (
|
|
211
|
-
|
|
212
|
-
|
|
286
|
+
const memoryObj = {};
|
|
287
|
+
for (const [path, value] of valuesMap) {
|
|
288
|
+
// Split once (and cached) for both the packet and $memoryAll
|
|
289
|
+
const keys = splitPath(path);
|
|
290
|
+
dset(memoryObj, keys, value);
|
|
213
291
|
if (value === "$delete") {
|
|
214
|
-
dremove(allMemory,
|
|
292
|
+
dremove(allMemory, keys);
|
|
215
293
|
} else {
|
|
216
|
-
dset(allMemory,
|
|
294
|
+
dset(allMemory, keys, value);
|
|
217
295
|
}
|
|
218
296
|
}
|
|
219
297
|
return memoryObj;
|