@@ -5,7 +5,7 @@ import { SessionManager } from '../core/session';
55import { showOverlay , initOverlayListeners } from '../ui/overlay/controller' ;
66import { ConnectionManager } from '../core/interfaces/connection-manager' ;
77import { StorageFacade } from '../core/storage/index' ;
8- import { PlatformAdapter } from '../types ' ;
8+ import { PlatformAdapter } from '../core/interfaces/platform-adapter ' ;
99import { StorageChange } from '../core/storage/driver' ;
1010import { MessageBus } from '../core/interfaces/message-bus' ;
1111
@@ -17,6 +17,10 @@ export class AppOrchestrator {
1717 private storage : StorageFacade ;
1818
1919 private sessionManager ! : SessionManager ;
20+ private messenger ! : Messenger ;
21+ private tracker ! : Tracker ;
22+
23+ private storageListener : ( ( changes : Record < string , StorageChange > ) => void ) | null = null ;
2024
2125 constructor (
2226 adapter : PlatformAdapter ,
@@ -32,9 +36,13 @@ export class AppOrchestrator {
3236
3337 public async start ( ) {
3438 initOverlayListeners ( ) ;
39+
40+ const pId = this . activeAdapter . id ;
41+
3542 const safeBlock = async ( reason : string = 'time' ) => {
3643 if ( ! this . isBlocked ) {
3744 this . isBlocked = true ;
45+ console . warn ( `[Limitra] Initiating block logic. Reason: ${ reason } ` ) ;
3846 if ( this . sessionManager ) {
3947 await this . sessionManager . blockSession ( ) ;
4048 }
@@ -43,21 +51,24 @@ export class AppOrchestrator {
4351 }
4452 } ;
4553
46- const messenger = new Messenger ( ( ) => {
47- void safeBlock ( 'time' ) ;
48- } , this . messageBus ) ;
49- messenger . init ( ) ;
54+ this . messenger = new Messenger (
55+ ( ) => {
56+ void safeBlock ( 'time' ) ;
57+ } ,
58+ this . messageBus ,
59+ pId ,
60+ ) ;
61+
62+ this . messenger . init ( ) ;
5063
5164 this . sessionManager = new SessionManager (
52- messenger ,
65+ this . messenger ,
5366 this . activeAdapter ,
5467 this . connectionManager ,
5568 this . storage ,
5669 ) ;
5770 await this . sessionManager . init ( ) ;
5871
59- const pId = this . activeAdapter . id ;
60-
6172 let limit = await this . storage . getLimit ( pId ) ;
6273 const initialCount = await this . storage . getCount ( pId ) ;
6374 let isLimitEnabled = await this . storage . getEnableLimit ( pId ) ;
@@ -77,7 +88,7 @@ export class AppOrchestrator {
7788 ) ;
7889 limiter . setInitialCount ( initialCount ) ;
7990
80- this . storage . onChange ( ( changes : Record < string , StorageChange > ) => {
91+ this . storageListener = ( changes : Record < string , StorageChange > ) => {
8192 if ( changes [ `limitra_${ pId } _limit` ] || changes [ `limitra_${ pId } _enable_limit` ] ) {
8293 if ( changes [ `limitra_${ pId } _limit` ] ) {
8394 limit = Number ( changes [ `limitra_${ pId } _limit` ] . newValue ) || 0 ;
@@ -114,7 +125,9 @@ export class AppOrchestrator {
114125 void safeBlock ( 'time' ) ;
115126 }
116127 }
117- } ) ;
128+ } ;
129+
130+ this . storage . onChange ( this . storageListener ) ;
118131
119132 const bypassed = await this . storage . detectBypass ( pId ) ;
120133 if ( bypassed ) {
@@ -125,7 +138,7 @@ export class AppOrchestrator {
125138 await safeBlock ( 'time' ) ;
126139 }
127140
128- const tracker = new Tracker ( async ( ) => {
141+ this . tracker = new Tracker ( async ( ) => {
129142 if ( this . isBlocked ) {
130143 const overlay = document . getElementById ( 'limitra-overlay' ) ;
131144 const isTampered =
@@ -135,19 +148,43 @@ export class AppOrchestrator {
135148 window . getComputedStyle ( overlay ) . opacity === '0' ;
136149 if ( isTampered ) {
137150 console . warn (
138- '[Limitra] Tampering with the blocking interface via DevTools has been detected. Reapplying enforcement...' ,
151+ '[Limitra] Tampering or SPA page refresh detected. Reapplying enforcement...' ,
139152 ) ;
140153 if ( overlay ) overlay . remove ( ) ;
141154 this . activeAdapter . executePunishment ( ) ;
142155 await showOverlay ( 'bypass' ) ;
143156 }
144157 return ;
145158 }
159+
146160 if ( isLimitEnabled && limit > 0 ) {
147161 await this . storage . incrementCount ( pId ) ;
148162 }
149163 } ) ;
150- tracker . setAdapter ( this . activeAdapter ) ;
151- tracker . init ( ) ;
164+ this . tracker . setAdapter ( this . activeAdapter ) ;
165+ this . tracker . init ( ) ;
166+ }
167+
168+ public destroy ( ) {
169+ if ( this . storageListener ) {
170+ this . storage . removeListener ( this . storageListener ) ;
171+ this . storageListener = null ;
172+ }
173+ if ( this . messenger ) {
174+ this . messenger . destroy ( ) ;
175+ }
176+ if ( this . activeAdapter ) {
177+ this . activeAdapter . disconnect ( ) ;
178+ }
179+ if ( this . sessionManager ) {
180+ this . sessionManager . destroy ( ) ;
181+ }
182+ if ( this . tracker ) {
183+ this . tracker . destroy ( ) ;
184+ }
185+
186+ const overlay = document . getElementById ( 'limitra-overlay' ) ;
187+ if ( overlay ) overlay . remove ( ) ;
188+ document . body . classList . remove ( 'limitra-global-punishment' ) ;
152189 }
153190}
0 commit comments