Skip to content

Commit 62e1e84

Browse files
author
kjelko
committed
extract custom signal merging logic to common function
1 parent 6898123 commit 62e1e84

File tree

1 file changed

+33
-54
lines changed

1 file changed

+33
-54
lines changed

packages/remote-config/src/storage/storage.ts

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -202,33 +202,7 @@ export class IndexedDbStorage extends Storage {
202202
'custom_signals',
203203
transaction
204204
);
205-
const combinedSignals = {
206-
...storedSignals,
207-
...customSignals
208-
};
209-
// Filter out key-value assignments with null values since they are signals being unset
210-
const updatedSignals = Object.fromEntries(
211-
Object.entries(combinedSignals)
212-
.filter(([_, v]) => v !== null)
213-
.map(([k, v]) => {
214-
// Stringify numbers to store a map of string keys and values which can be sent
215-
// as-is in a fetch call.
216-
if (typeof v === 'number') {
217-
return [k, v.toString()];
218-
}
219-
return [k, v];
220-
})
221-
);
222-
223-
// Throw an error if the number of custom signals to be stored exceeds the limit
224-
if (
225-
Object.keys(updatedSignals).length > RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS
226-
) {
227-
throw ERROR_FACTORY.create(ErrorCode.CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS, {
228-
maxSignals: RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS
229-
});
230-
}
231-
205+
const updatedSignals = mergeCustomSignals(customSignals, storedSignals || {});
232206
await this.setWithTransaction<CustomSignals>(
233207
'custom_signals',
234208
updatedSignals,
@@ -371,34 +345,39 @@ export class InMemoryStorage extends Storage {
371345
}
372346

373347
async setCustomSignals(customSignals: CustomSignals): Promise<CustomSignals> {
374-
const combinedSignals = {
375-
...(this.storage['custom_signals'] as CustomSignals),
376-
...customSignals
377-
};
378-
379-
const updatedSignals = Object.fromEntries(
380-
Object.entries(combinedSignals)
381-
.filter(([_, v]) => v !== null)
382-
.map(([k, v]) => {
383-
// Stringify numbers to store a map of string keys and values which can be sent
384-
// as-is in a fetch call.
385-
if (typeof v === 'number') {
386-
return [k, v.toString()];
387-
}
388-
return [k, v];
389-
})
390-
);
391-
392-
if (
393-
Object.keys(updatedSignals).length > RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS
394-
) {
395-
throw ERROR_FACTORY.create(ErrorCode.CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS, {
396-
maxSignals: RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS
397-
});
398-
}
348+
const storedSignals = (this.storage['custom_signals'] || {}) as CustomSignals;
349+
this.storage['custom_signals'] = mergeCustomSignals(customSignals, storedSignals);
350+
return Promise.resolve(this.storage['custom_signals'] as CustomSignals);
351+
}
352+
}
399353

400-
this.storage['custom_signals'] = updatedSignals;
354+
function mergeCustomSignals(customSignals: CustomSignals, storedSignals: CustomSignals): CustomSignals {
355+
const combinedSignals = {
356+
...storedSignals,
357+
...customSignals
358+
};
359+
360+
// Filter out key-value assignments with null values since they are signals being unset
361+
const updatedSignals = Object.fromEntries(
362+
Object.entries(combinedSignals)
363+
.filter(([_, v]) => v !== null)
364+
.map(([k, v]) => {
365+
// Stringify numbers to store a map of string keys and values which can be sent
366+
// as-is in a fetch call.
367+
if (typeof v === 'number') {
368+
return [k, v.toString()];
369+
}
370+
return [k, v];
371+
})
372+
);
401373

402-
return Promise.resolve(this.storage['custom_signals'] as CustomSignals);
374+
// Throw an error if the number of custom signals to be stored exceeds the limit
375+
if (
376+
Object.keys(updatedSignals).length > RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS
377+
) {
378+
throw ERROR_FACTORY.create(ErrorCode.CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS, {
379+
maxSignals: RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS
380+
});
403381
}
382+
return updatedSignals;
404383
}

0 commit comments

Comments
 (0)