Files
Unify/framework/server/objectCacheManager.js
2025-12-25 11:16:59 +01:00

196 lines
3.4 KiB
JavaScript

/*
Copyright (c) 2020, 2023, The Unified Company.
This code is part of Unify.
This program is free software; you can redistribute it and/or modify
it under the terms of the ESA Software Community License - Strong Copyleft LICENSE,
as published by the ESA.
See the ESA Software Community License - Strong Copyleft LICENSE, for more details.
https://unifyjs.org
*/
import unify from '../unify/unify.js';
import tools from '../unify/tools.js';
export default class objectCacheManager{
objects = new Array();
removeObjectsByClientID( userID ) {
var objects = this.objects;
var entries = Object.entries( objects );
var count = 0;
var total = 0;
for( var b = 0; b < entries.length; b++ ) {
var entry = entries[b];
var objectsByID = Object.entries( tools.getObjectByEntry( entry ) );
var path = tools.getClassNameByEntry( entry );
var empty = true;
for ( var c = 0; c < objectsByID.length; c++ ) {
var objectsByUserID = tools.getObjectByEntry( objectsByID[c] );
var objectID = tools.getClassNameByEntry( objectsByID[c] );
total++;
if( objectsByUserID[ userID ] ) {
delete this.objects[path][objectID][userID];
this.objects[path][objectID].splice( userID, 1 );
count++;
} else {
empty = false;
}
this.objects[path][objectID] = this.objects[path][objectID].filter(Boolean);
if( this.objects[path][objectID].length == 0 ) {
this.objects[path][objectID] = undefined;
empty = true;
}
}
if( empty ) {
delete delete this.objects[path][objectID];
}
this.objects[path] = this.objects[path].filter(Boolean);
}
// console.log("removed ", count, "objects");
// console.log("total ", total, "objects");
}
addClientObjectByPathID( clientObject, id, applicationPathString, userID ) {
var arrayWithObjects = this.objects[ applicationPathString ];
if( arrayWithObjects ) {
var arrayWithObjectByID = arrayWithObjects[ id ];
if( arrayWithObjectByID ) {
var arrayByUserID = arrayWithObjectByID[ userID ];
if( arrayByUserID ) {
return arrayByUserID;
} else {
arrayWithObjectByID[ userID ] = clientObject;
// this.objects[ applicationPathString ][ id ][ userID ]
}
} else {
this.objects[ applicationPathString ][ id ] = new Array();
return this.addClientObjectByPathID( clientObject, id, applicationPathString, userID );
}
} else {
this.objects[ applicationPathString ] = new Array();
return this.addClientObjectByPathID( clientObject, id, applicationPathString, userID );
}
}
getClientObjects( applicationPathString, id ) {
var arrayWithObjects = this.objects[ applicationPathString ];
if( arrayWithObjects ) {
var arrayWithObjectByID = arrayWithObjects[ id ];
if( arrayWithObjectByID ) {
return arrayWithObjectByID;
} else {
return false
}
} else {
return false;
}
}
addClientObject( clientObject, userID ) {
var object = clientObject.object;
unify.extend(object);
if(object.getApplicationPathString) {
var applicationPathString = object.getApplicationPathString();
var id = object.id;
if( !id ) {
id = 0;
}
var userID = userID;
if( object.bidirectional ) {
this.addClientObjectByPathID( clientObject, id, applicationPathString, userID );
}
}
}
}