snowflake and cleaning up classes
This commit is contained in:
parent
14d1c69c7d
commit
7eb3ff6cab
21 changed files with 584 additions and 361 deletions
53
webpage/snowflake.ts
Normal file
53
webpage/snowflake.ts
Normal file
|
@ -0,0 +1,53 @@
|
|||
class SnowFlake<x>{
|
||||
public readonly id:string;
|
||||
private static readonly SnowFlakes:Map<any,Map<string,WeakRef<SnowFlake<any>>>>=new Map();
|
||||
private static readonly FinalizationRegistry=new FinalizationRegistry((id:string)=>{
|
||||
SnowFlake.SnowFlakes.delete(id);
|
||||
});
|
||||
private obj:x;
|
||||
constructor(id:string,obj:x){
|
||||
if(!obj){
|
||||
this.id=id;
|
||||
return;
|
||||
}
|
||||
if(!SnowFlake.SnowFlakes.get(obj.constructor)){
|
||||
SnowFlake.SnowFlakes.set(obj.constructor,new Map());
|
||||
}
|
||||
if(SnowFlake.SnowFlakes.get(obj.constructor).get(id)){
|
||||
const snowflake=SnowFlake.SnowFlakes.get(obj.constructor).get(id).deref();
|
||||
snowflake.obj=obj;
|
||||
return snowflake;
|
||||
}
|
||||
this.id=id;
|
||||
SnowFlake.SnowFlakes.get(obj.constructor).set(id,new WeakRef(this));
|
||||
SnowFlake.FinalizationRegistry.register(this,id);
|
||||
this.obj=obj;
|
||||
}
|
||||
static getSnowFlakeFromID(id:string,type:any):SnowFlake<any>{
|
||||
if(!SnowFlake.SnowFlakes.get(type)){
|
||||
SnowFlake.SnowFlakes.set(type,new Map());
|
||||
}
|
||||
const snowflake=SnowFlake.SnowFlakes.get(type).get(id);
|
||||
if(snowflake){
|
||||
return snowflake.deref();
|
||||
}
|
||||
{
|
||||
const snowflake=new SnowFlake(id,undefined);
|
||||
|
||||
SnowFlake.SnowFlakes.get(type).set(id,new WeakRef(snowflake));
|
||||
SnowFlake.FinalizationRegistry.register(snowflake,id);
|
||||
|
||||
return snowflake;
|
||||
}
|
||||
}
|
||||
getUnixTime():number{
|
||||
return Number((BigInt(this.id)>>22n)+1420070400000n);
|
||||
}
|
||||
toString(){
|
||||
return this.id;
|
||||
}
|
||||
getObject():x{
|
||||
return this.obj;
|
||||
}
|
||||
}
|
||||
export {SnowFlake};
|
Loading…
Add table
Add a link
Reference in a new issue