feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -7,19 +7,36 @@
/** @typedef {import("./ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("./ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
/** @typedef {(arg0?: any) => void} CacheAssoc */
/**
* @template T
* @typedef {WeakMap<CacheAssoc, ObjectStructure<T>>}
*/
const cache = new WeakMap();
/**
* @template T
*/
class ObjectStructure {
constructor() {
this.keys = undefined;
this.children = undefined;
}
/**
* @param {keyof T[]} keys keys
* @returns {keyof T[]} keys
*/
getKeys(keys) {
if (this.keys === undefined) this.keys = keys;
return this.keys;
}
/**
* @param {keyof T} key key
* @returns {ObjectStructure<T>} object structure
*/
key(key) {
if (this.children === undefined) this.children = new Map();
const child = this.children.get(key);
@@ -30,6 +47,12 @@ class ObjectStructure {
}
}
/**
* @template T
* @param {(keyof T)[]} keys keys
* @param {CacheAssoc} cacheAssoc cache assoc fn
* @returns {(keyof T)[]} keys
*/
const getCachedKeys = (keys, cacheAssoc) => {
let root = cache.get(cacheAssoc);
if (root === undefined) {
@@ -45,11 +68,12 @@ const getCachedKeys = (keys, cacheAssoc) => {
class PlainObjectSerializer {
/**
* @param {object} obj plain object
* @template {object} T
* @param {T} obj plain object
* @param {ObjectSerializerContext} context context
*/
serialize(obj, context) {
const keys = Object.keys(obj);
const keys = /** @type {(keyof T)[]} */ (Object.keys(obj));
if (keys.length > 128) {
// Objects with so many keys are unlikely to share structure
// with other objects
@@ -70,19 +94,21 @@ class PlainObjectSerializer {
context.write(null);
}
}
/**
* @template {object} T
* @param {ObjectDeserializerContext} context context
* @returns {object} plain object
* @returns {T} plain object
*/
deserialize(context) {
const keys = context.read();
const obj = {};
const obj = /** @type {T} */ ({});
if (Array.isArray(keys)) {
for (const key of keys) {
obj[key] = context.read();
obj[/** @type {keyof T} */ (key)] = context.read();
}
} else if (keys !== null) {
obj[keys] = context.read();
obj[/** @type {keyof T} */ (keys)] = context.read();
}
return obj;
}