import { ObjPoolMgr } from "../src" ; interface ITestObjGetDataMap { TestObj1 : { num : number ; } ; TestObj2 : { name : string ; } ; TestObj3 : { name : string ; } ; TestSetObjHandler2 : { a : number ; } ; TestSetObjHandler1 : { a : number ; } ; } class TestObj1 implements objPool . IObj { poolSign : string ; isInPool : boolean ; pool : objPool . IPool < any , any > ; curNum : number ; onGet ( data ) { this . curNum = data ? data . num : 0 ; } onCreate ( ) { } onReturn ( ) { this . curNum = undefined ; } onKill ( ) { } } class TestObj3 implements objPool . IObj { poolSign : string ; isInPool : boolean ; pool : objPool . IPool < any , any > ; name : string ; onGet ( data ) { this . name = data ? data . name : "" ; } onCreate ( ) { } onReturn ( ) { } onKill ( ) { } } class TestObj2 implements objPool . IObj { poolSign : string ; isInPool : boolean ; pool : objPool . IPool < any , any > ; name : string ; onGet ( data ) { this . name = data ? data . name : "" ; } onCreate ( ) { } onReturn ( ) { } onKill ( ) { } } class TestAnyObj { a : number ; } test ( "test createPool by Mgr" , function ( ) { const poolMgr = new ObjPoolMgr < ITestObjGetDataMap > ( ) as any ; poolMgr . createByClass ( "TestObj1" , TestObj1 ) ; poolMgr . createByFunc ( "TestObj2" , ( ) => { return new TestObj2 ( ) ; } ) ; expect ( poolMgr . getPool ( "TestObj1" ) ) . toBeDefined ( ) ; expect ( poolMgr . getPool ( "TestObj2" ) ) . toBeDefined ( ) ; let testObj3Pool = poolMgr . createObjPool ( { sign : "TestObj3" , clas : TestObj3 } ) ; expect ( testObj3Pool ) . toBeDefined ( ) ; expect ( testObj3Pool ) . toEqual ( poolMgr . getPool ( "TestObj3" ) ) ; expect ( testObj3Pool . threshold ) . toBeUndefined ( ) ; testObj3Pool . threshold = 100 ; expect ( testObj3Pool . threshold ) . toBe ( 100 ) ; poolMgr . destroyPool ( "TestObj3" ) ; poolMgr . get ( "TestObj3" ) ; expect ( poolMgr . getPool ( "TestObj3" ) ) . toBeUndefined ( ) ; testObj3Pool = poolMgr . createObjPool ( { sign : "TestObj3" , createFunc : ( ) => { return new TestObj3 ( ) ; } , threshold : 200 } ) ; expect ( testObj3Pool . threshold ) . toBe ( 200 ) ; } ) ; test ( "test mgr functions" , function ( ) { const poolMgr = new ObjPoolMgr < ITestObjGetDataMap > ( ) ; poolMgr . createByClass ( "TestObj1" , TestObj1 ) ; poolMgr . createByFunc ( "TestObj2" , ( ) => { return new TestObj2 ( ) ; } ) ; poolMgr . preCreate ( "TestObj1" , 5 ) ; const testObj1 = poolMgr . get ( "TestObj1" , { num : 2 } ) ; expect ( testObj1 . curNum ) . toBe ( 2 ) ; expect ( poolMgr . getPoolObjsBySign ( "TestObj1" ) . length ) . toBe ( 4 ) ; expect ( poolMgr . getPool ( "TestObj1" ) . usedCount ) . toBe ( 1 ) ; const testObj2s = poolMgr . getMore ( "TestObj2" , { name : "testObj2" } , 4 ) ; expect ( testObj2s [ 0 ] . name ) . toBe ( "testObj2" ) ; expect ( poolMgr . getPool ( "TestObj2" ) . usedCount ) . toBe ( 4 ) ; poolMgr . return ( testObj1 ) ; expect ( testObj1 . curNum ) . toBe ( undefined ) ; expect ( poolMgr . getPoolObjsBySign ( "TestObj1" ) . length ) . toBe ( 5 ) ; poolMgr . returnAll ( "TestObj2" ) ; expect ( poolMgr . getPoolObjsBySign ( "TestObj2" ) . length ) . toBe ( 4 ) ; const newTestObj2s = poolMgr . getMore ( "TestObj2" , { name : "testObj2" } , 4 ) ; poolMgr . return ( newTestObj2s [ 0 ] ) ; expect ( poolMgr . getPoolObjsBySign ( "TestObj2" ) . length ) . toBe ( 1 ) ; poolMgr . returnAll ( "TestObj2" ) ; expect ( poolMgr . getPoolObjsBySign ( "TestObj2" ) . length ) . toBe ( 4 ) ; poolMgr . clearPool ( "TestObj2" ) ; expect ( poolMgr . getPoolObjsBySign ( "TestObj2" ) . length ) . toBe ( 0 ) ; poolMgr . destroyPool ( "TestObj1" ) ; expect ( poolMgr . getPool ( "TestObj1" ) ) . toBeUndefined ( ) ; const testObj2 = poolMgr . get ( "TestObj2" ) ; expect ( poolMgr . getPool ( "TestObj2" ) . usedCount ) . toBe ( 1 ) ; poolMgr . kill ( testObj2 ) ; expect ( poolMgr . getPool ( "TestObj2" ) . usedCount ) . toBe ( 0 ) ; expect ( poolMgr . getPool ( "TestObj2" ) . poolObjs . length ) . toBe ( 0 ) ; } ) ; test ( "test setObjPoolHandler to pool" , function ( ) { const poolMgr = new ObjPoolMgr < ITestObjGetDataMap > ( ) ; poolMgr . createByFunc ( "TestSetObjHandler1" , ( ) => { return { a : 1 } ; } ) ; const handler = { onGet ( obj , data ) { if ( data ) { obj . a = data . a ; } } } ; poolMgr . createObjPool ( { sign : "TestSetObjHandler2" , objHandler : handler , createFunc : ( ) => { return { a : 1 } ; } } ) ; poolMgr . setPoolHandler ( "TestSetObjHandler1" , handler ) ; const spyonGet = jest . spyOn ( handler , "onGet" ) ; const obj = poolMgr . get ( "TestSetObjHandler1" , { a : 2 } ) ; const obj2 = poolMgr . get ( "TestSetObjHandler2" , { a : 3 } ) ; expect ( obj ) . toBeDefined ( ) ; expect ( obj2 ) . toBeDefined ( ) ; expect ( obj . a ) . toBe ( 2 ) ; expect ( obj2 . a ) . toBe ( 3 ) ; expect ( spyonGet ) . toBeCalledTimes ( 2 ) ; } ) ; test ( "test objPool thredshold" , function ( ) { const poolMgr = new ObjPoolMgr ( ) ; poolMgr . createObjPool ( { sign : "thredshold" , clas : TestObj1 , threshold : 5 } ) ; poolMgr . preCreate ( "thredshold" , 6 ) ; let obj = poolMgr . get ( "thredshold" ) ; const spyonKill = jest . spyOn ( obj , "onKill" ) ; poolMgr . return ( obj ) ; expect ( spyonKill ) . toBeCalledTimes ( 1 ) ; expect ( poolMgr . getPool ( "thredshold" ) . size ) . toBe ( 5 ) ; } ) ;