@signe/room 3.2.0 → 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.
@@ -147,7 +147,7 @@ import type {
147
147
  export type ConnectionSetStateFn<T> = (prevState: ConnectionState<T>) => T;
148
148
 
149
149
  /** A WebSocket connected to the Room */
150
- export type Connection<TState = unknown> = WebSocket & {
150
+ export interface Connection<TState = unknown> extends WebSocket {
151
151
  /** Connection identifier */
152
152
  id: string;
153
153
 
@@ -178,7 +178,7 @@ import type {
178
178
 
179
179
  /** @deprecated use Connection.state instead */
180
180
  deserializeAttachment<T = unknown>(): T | null;
181
- };
181
+ }
182
182
 
183
183
  type CustomBindings = {
184
184
  r2: Record<string, R2Bucket>;
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
- let memoryObj = {};
210
- for (let path of valuesMap.keys()) {
211
- const value = valuesMap.get(path);
212
- dset(memoryObj, path, value);
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, path);
292
+ dremove(allMemory, keys);
215
293
  } else {
216
- dset(allMemory, path, value);
294
+ dset(allMemory, keys, value);
217
295
  }
218
296
  }
219
297
  return memoryObj;