Skip to content

Commit 827ee1b

Browse files
committed
speedy, snappy, tab leader works without breaking (in sane cases, dumb cases like switching tabs very quickly at some point. if I detect rapidity, I may just show a middle finger emoji)
1 parent 2b123e2 commit 827ee1b

File tree

11 files changed

+85
-42
lines changed

11 files changed

+85
-42
lines changed

apps/gui/src/lib/utils/lifecycle.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ let emittersBound: boolean = false;
3333

3434
monitorsMap.subscribe( ($m: Map<string, Monitor>) => $monitorsMap = $m )
3535

36-
let $route66: Route66;
36+
let $route66: Route66 | null;
3737
let initializing: boolean = false;
3838

3939
let liveSyncBatcher: Batcher<IEvent, any> = new Batcher<IEvent, any>({
@@ -46,7 +46,7 @@ let count = 0
4646

4747
export const bindBootstrapEmitters = (from?: string) => {
4848
if(emittersBound) return;
49-
if(from) console.log('Lifecycle:bindBootstrapEmitters', from)
49+
// if(from) console.log('Lifecycle:bindBootstrapEmitters', from)
5050
const $nip05Service: Nip05Service = get(nip05Service)
5151

5252
if (!$route66 || typeof $route66.on !== 'function') {
@@ -309,13 +309,15 @@ export const pauseLiveSync = async (): Promise<LiveSyncResumer> => {
309309
}
310310

311311
export const destroy = () => {
312-
route66.update(($route66: Route66) => {
312+
initializing = false;
313+
$route66 = null;
314+
route66.update( ($route66) => {
313315
if ($route66 && typeof $route66.destroy === 'function') {
314316
$route66.destroy();
315317
} else {
316318
console.error('route66 instance is missing or does not have a destroy method.');
317319
}
318-
return $route66;
320+
return null;
319321
});
320322
};
321323

@@ -347,7 +349,7 @@ export const seedChecksFromCache = async () => {
347349
if(cachedEvents.length === 0) return;
348350
// publishEventsToMemoryRelay(cachedEvents, 'seedChecksFromCache');
349351
isSeeded.set(true)
350-
console.log('seedChecksFromCache:events', cachedEvents.length)
352+
// console.log('seedChecksFromCache:events', cachedEvents.length)
351353
return cachedEvents
352354
}
353355

apps/gui/src/routes/+layout.svelte

+14-10
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
window.addEventListener('keydown', toggleDebugger);
6464
6565
const shutdown = async () => {
66-
console.log('[Lifecycle] onReleaseLeader triggered.');
66+
console.log('Shutdown...');
6767
try {
6868
const route66 = await instance();
6969
await route66.ready();
@@ -80,11 +80,11 @@
8080
// // --------------------------------------------------------------------------------
8181
// // Utility to change tabState using store.get instead of $tabState in TS context
8282
// // --------------------------------------------------------------------------------
83-
// function setTabState(newState: TabStateType) {
84-
// if (get(tabState) === newState) return;
85-
// tabState.update(() => newState);
86-
// console.log(`Tab state updated to: ${newState}`);
87-
// }
83+
function setTabState(newState: TabStateType) {
84+
if (get(tabState) === newState) return;
85+
tabState.update(() => newState);
86+
console.log(`Tab state updated to: ${newState}`);
87+
}
8888
8989
// // --------------------------------------------------------------------------------
9090
// // Idle Logic
@@ -172,11 +172,15 @@
172172
// --------------------------------------------------------------------------------
173173
async function boot() {
174174
if (get(unsupported)) return;
175+
console.log('Booting...');
175176
appState.set('booting');
176177
await initServices();
178+
// console.log('Services initialized.');
177179
appState.set('running');
178180
const route66 = await instance();
181+
// console.log('Instance acquired.');
179182
await route66.ready();
183+
// console.log('Route66 ready.');
180184
dataRegisterInit();
181185
await delay(3000)
182186
await $dataRegister.require([
@@ -264,15 +268,15 @@
264268
activityManager = new ActivityManager(IDLE_TIMEOUT_MS);
265269
266270
activityManager.on('active', async () => {
271+
// console.log('ActivityManager: handler: TAB IS ACTIVE', '+layout.svelte');
272+
// console.log(`STATE IS ${$tabState}`, '+layout.svelte')
267273
await boot();
268-
console.log('ActivityManager: handler: TAB IS ACTIVE', '+layout.svelte');
269-
console.log(`STATE IS ${$tabState}`, '+layout.svelte')
270274
});
271275
272276
activityManager.on('inactive', async () => {
277+
// console.log('TAB IS INACTIVE');
278+
// console.log(`STATE IS ${$tabState}`, '+layout.svelte')
273279
await shutdown();
274-
console.log('TAB IS INACTIVE');
275-
console.log(`STATE IS ${$tabState}`, '+layout.svelte')
276280
});
277281
278282
return () => {

apps/gui/src/routes/relays/[protocol]/[...relay]/+layout.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
}
127127
128128
const mount = () => {
129-
doLiveSync.set(false);
129+
// doLiveSync.set(true);
130130
if (currentRelay === relayUrl) return;
131131
132132
let resume: LiveSyncResumer;
@@ -152,7 +152,7 @@
152152
const destroy = () => {
153153
if (currentRelay === relayUrl) return;
154154
loading = true;
155-
doLiveSync.set(true);
155+
// doLiveSync.set(false);
156156
// currentRelay = '';
157157
// monitors.set([]);
158158
// nip11Ready.set(false);

libraries/route66/adapters/cache/NostrSqliteAdapter/src/NostrSqliteAdapter.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ export class NostrSqliteAdapter extends CacheAdapter implements INostrSqliteAdap
4141
}
4242

4343
destroy(){
44-
if(this.relay.worker instanceof Worker){
45-
this.relay.worker.terminate()
46-
}
44+
this.abort()
4745
}
4846

4947
setup(){}
5048

51-
async abort(): Promise<boolean>{ return true }
49+
async abort(): Promise<boolean>{
50+
this.relay.abort();
51+
return true;
52+
}
5253

5354
/**
5455
* overload defaults with inop because WorkerRelayInterface handles it.

libraries/route66/adapters/websocket/NostrToolsAdapter/src/NostrToolsWorker.ts

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ export class NostrToolsWorker extends AdapterWebsocketWorker implements IAdapter
276276
closeSubscription(hash?: string): void {}
277277

278278
unsubscribe({ hash }: WebsocketRequestBody): void {
279+
console.log('WebsocketAdapter:WebsocketAdapterWorker:NostrToolsWorker:unsubscribe', hash)
279280
if(!hash) return
280281
if(!this.subs.has(hash as string)) return
281282
const closer = this.subs.get(hash as string);

libraries/route66/src/core/Adapter.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ export abstract class Adapter {
125125
}
126126

127127
async shutdown(): Promise<void> {
128-
if(this?.worker) {
129-
if(this?.worker instanceof Worker){
130-
this.worker?.terminate()
131-
}
132-
}
133-
this.emitter.emit('shutdown')
128+
// if(this?.worker) {
129+
// if(this?.worker instanceof Worker){
130+
// this.worker?.terminate()
131+
// }
132+
// }
133+
// this.emitter.emit('shutdown')
134134
}
135135

136136
async newWorker(): Promise<any> {

libraries/route66/src/core/AdapterCacheWorker.ts

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export class AdapterCacheWorker extends AdapterWorker {
7171
}
7272

7373
async onMainThreadMessage(command: AdapterCacheWorkerCommand): Promise<void> {
74+
// this.onMessage(command)
7475
return void 0;
7576
}
7677

libraries/route66/src/core/AdapterWebsocketWorker.ts

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export class AdapterWebsocketWorker extends AdapterWorker {
108108
}
109109

110110
async onMainThreadMessage(request: WebsocketRequest): Promise<void> {
111+
console.log('WebsocketAdapter:AdapterWebsocketWorker: onMainThreadMessage', request)
111112
this.onMessage(request)
112113
}
113114

@@ -116,6 +117,7 @@ export class AdapterWebsocketWorker extends AdapterWorker {
116117
}
117118

118119
onMessage(request: WebsocketRequest = defaultWebsocketRequest){
120+
console.log('WebsocketAdapter:AdapterWebsocketWorker: onMessage', request)
119121
const { action, args } = request
120122
if(action === 'publish'){
121123
return this.publish(args)

libraries/route66/src/core/CacheAdapter.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ export class CacheAdapter extends Adapter {
146146

147147
constructor(worker?: Worker | SharedWorker | URL) {
148148
super(worker)
149-
StateManager.on('destroy', () => {
150-
if(this.worker instanceof Worker) {
151-
this.worker?.terminate()
152-
}
149+
// StateManager.on('destroy', () => {
150+
// if(this.worker instanceof Worker) {
151+
// this.worker?.terminate()
152+
// }
153153

154-
})
154+
// })
155155
this.init();
156156
}
157157

libraries/route66/src/core/WebsocketAdapter.ts

+27-9
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ export class WebsocketAdapter extends Adapter implements IWebsocketAdapter {
101101

102102
constructor(worker?: Worker | SharedWorker | URL) {
103103
super(worker)
104-
StateManager.on('destroy', () => {
105-
if(this.worker instanceof Worker)
106-
this.worker?.terminate()
107-
})
104+
// StateManager.on('destroy', () => {
105+
// if(this.worker instanceof Worker)
106+
// this.worker?.terminate()
107+
// })
108108
}
109109

110110
get worker(): Worker | SharedWorker | undefined {
@@ -131,30 +131,43 @@ export class WebsocketAdapter extends Adapter implements IWebsocketAdapter {
131131
}
132132

133133
async unsubscribe(hash?: string): Promise<boolean> {
134+
console.log('WebsocketAdapter:unsubscribe', hash)
134135
if(hash && !this.subscriptions.has(hash)) return true;
136+
console.log('WebsocketAdapter:unsubscribe:found', hash)
135137
const unsub = this.request({
136138
action: 'unsubscribe',
137139
args: {
138140
...defaultWebsocketRequestBody,
139-
hash
141+
hash,
142+
options: {
143+
...defaultWebsocketAdapterOptions,
144+
returnResults: true
145+
}
140146
}
141147
})
148+
console.log('WebsocketAdapter:unsubscribe:sentrequest', hash)
142149
return this.response(unsub) as Promise<boolean>
143150
}
144151

145152
async unsubscribeAll(hash?: string): Promise<boolean> {
146153
const hashes = [...Array.from(this.subscriptions)]
147154
for(let hash of hashes){
155+
console.log('WebsocketAdapter:unsubscribeAll', hash)
148156
await this.unsubscribe(hash)
149157
}
150158
return true;
151159
}
152160

153161
async shutdown(): Promise<void> {
154-
await this.unsubscribeAll();
155-
await this.abort();
156-
await delay(1000);
162+
console.log('WebsocketAdapter:shutdown')
163+
// console.log('WebsocketAdapter:shutdown:unsubscribing', this.subscriptions.size)
164+
// await this.unsubscribeAll();
165+
// console.log('WebsocketAdapter:shutdown:unsubscribed', this.subscriptions.size)
166+
// await this.abort();
167+
// console.log('WebsocketAdapter:shutdown:aborted')
168+
// await delay(1000);
157169
this.terminate()
170+
console.log('WebsocketAdapter:shutdown:terminated')
158171
await delay(100);
159172
}
160173

@@ -164,6 +177,10 @@ export class WebsocketAdapter extends Adapter implements IWebsocketAdapter {
164177
args: {
165178
...defaultWebsocketRequestBody,
166179
hash: 'abort',
180+
options: {
181+
...defaultWebsocketAdapterOptions,
182+
returnResults: true
183+
}
167184
}
168185
})
169186
return this.response(abort) as Promise<boolean>
@@ -239,12 +256,13 @@ export class WebsocketAdapter extends Adapter implements IWebsocketAdapter {
239256
message.args.hash = deterministicHash(message?.args?.filters ?? {})
240257
}
241258
const { hash } = message.args
259+
console.log('WebsocketAdapter:request', hash, message)
242260
if(!this?.worker) {
243261
console.warn('[WebsocketAdapter] Error sending command: no worker found')
244262
return hash
245263
}
246264
if(this.worker instanceof Worker) {
247-
// console.log('WebsocketAdapter:request', message)
265+
console.log('WebsocketAdapter:request', message)
248266
this.worker.postMessage(message)
249267
} else if(this.worker instanceof SharedWorker)
250268
this.worker.port.postMessage(message)

libraries/worker-relay/src/interface.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export type batchNip11s = Nip11Args[];
2323
export class WorkerRelayInterface {
2424
#worker: Worker | SharedWorker;
2525
#commandQueue: Map<string, (v: unknown, ports: ReadonlyArray<MessagePort>) => void> = new Map();
26+
#timeouts: any[] = [];
2627

2728
// Command timeout
2829
timeout: number = 5_000;
@@ -147,6 +148,18 @@ export class WorkerRelayInterface {
147148
return await this.#workerRpc<string, boolean>("debug", v);
148149
}
149150

151+
abort() {
152+
this.#timeouts.forEach(t => clearTimeout(t));
153+
this.#timeouts = [];
154+
this.#commandQueue.clear();
155+
if(this.#worker instanceof Worker) {
156+
this.#worker.terminate();
157+
}
158+
else if(this.#worker instanceof SharedWorker) {
159+
this.#worker.port.close();
160+
}
161+
}
162+
150163
async #workerRpc<T, R>(cmd: WorkerMessageCommand, args?: T) {
151164
const id = uuid();
152165
const msg = {
@@ -165,8 +178,9 @@ export class WorkerRelayInterface {
165178
this.#commandQueue.delete(id);
166179
reject(new Error("Timeout"));
167180
}, this.timeout);
181+
this.#timeouts.push(t);
168182
this.#commandQueue.set(id, (v, port) => {
169-
clearTimeout(t);
183+
if(t) clearTimeout(t);
170184
const cmdReply = v as WorkerMessage<R & { error?: any }>;
171185
if (cmdReply.args.error) {
172186
reject(cmdReply.args.error);

0 commit comments

Comments
 (0)