You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Its importance has faded. Mini apps do not work well with sessions due to race conditions. At the same time, storage adapters are used in more plugins, so the storage enhancements should not be tied to sessions, either.
The text was updated successfully, but these errors were encountered:
I suggest slightly reworking the session persistence condition. Currently, on any read, we write to persist the session, which produces unnecessary write operations. We can deeply proxy the session object to track mutations more precisely.
This is just a raw example of proxying; it can be adjusted only for objects and arrays (JSON serializable) and with if's for performance reasons.
constmutated=Symbol("mutated");// for Set, Map, and Arrayconstmutation_methods=newSet(["set","add","delete","clear","push","pop","shift","unshift","splice","sort","reverse","fill","copyWithin",]);/** * Creates a proxy wrapper around an object to track mutations and handle cleanup * @param {any} object - The target object to proxify * @param {Function} [dispose] - Optional disposal function that runs during cleanup * @returns {Proxy} Proxied object that tracks mutations * * @example * const obj = { count: 0 }; * const proxiedObj = proxify(obj, () => { * console.log('cleanup'); * }); */exportfunctionproxify<T>(object: any,dispose?: ()=>Promise<void>|void,): T&{[Symbol.asyncDispose]: ()=>Promise<void>;[mutated]: boolean}{object[Symbol.asyncDispose]=dispose;object[mutated]=false;consthandler: ProxyHandler<any>={get(target,key: string){if(typeoftarget[key]==="object"&&target[key]!==null){returnnewProxy(target[key],handler);}if(typeoftarget[key]==="function"){return(...args: unknown[])=>{if(mutation_methods.has(key)){object[mutated]=true;}returntarget[key](...args);};}returntarget[key];},set(target,prop: string,value){object[mutated]=true;target[prop]=value;returntrue;},};returnnewProxy(object,handler);}proxify.is_mutated=mutated;
usage with using:
asyncfunctionapp(){await using tracked_state=proxify(state,async()=>{if(state[proxify.is_mutated]){console.log("saving state");state[proxify.is_mutated]=false;awaitdb.set(["state"],state);}});tracked_state.a=1;// -> mutation}// -> save
Its importance has faded. Mini apps do not work well with sessions due to race conditions. At the same time, storage adapters are used in more plugins, so the storage enhancements should not be tied to sessions, either.
The text was updated successfully, but these errors were encountered: