Skip to content

Commit

Permalink
fix race conditioons
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Apr 12, 2024
1 parent cd0d624 commit cd9f492
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
7 changes: 3 additions & 4 deletions src/lib/core/MRP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ export class MyRelayPage {

public $: MRPState = { signal: new EventEmitter() }

private called = 0

constructor(url?: string){
this.url = url
if(!this.url) throw new Error(`No valid URL provided/detected: ${this.url}`)
this.$.signal.emit('sync:start', Date.now())
this._nostr = new MRPNostr(this.$.signal, this.defaultRelays, this.url)
this.$.ndk = this._nostr.ndk
this._editor = new MRPEditor()
Expand All @@ -46,8 +45,8 @@ export class MyRelayPage {
await this.nostr?.connect()
await this.config?.init()
await this.nostr?.init()
await this.editor?.init()

// await this.editor?.init()
this.$.signal.once('relay:completed', () => { this.$.signal.emit('sync:finish', Date.now()) })
}

bindHandlers(){
Expand Down
12 changes: 2 additions & 10 deletions src/lib/core/MRPBlockLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,13 @@ type $Component = {
}

export class BlockLoader extends MRPData {
private readonly defaultComponents: Components = {
// 'speedtest': 'speedtest',
'operator-profile': 'operator-profile',
'relay-feed': 'relay-feed',
'map': 'map',
'monitors': 'monitors',
// 'feed': 'feed',
}
private $: MRPState
// private $: MRPState
private _components: Record<string, NodeModule> = {}
private _config: MRPConfig

constructor($state: MRPState, config: MRPConfig){
super($state.signal, 'blockLoader')
this.$ = $state
// this.$ = $state
this._config = config
}

Expand Down
24 changes: 17 additions & 7 deletions src/lib/core/MRPRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ export class MRPRelay extends MRPData {
}

public async init(){
this.$.signal.once('info:completed', this.initConfig.bind(this))
this.$.signal.once('config:completed', this.initOwner.bind(this))
await this.info?.init()
this.begin()
return new Promise( async (resolve) => {
this.$.signal.once('info:completed', this.initConfig.bind(this))
this.$.signal.once('config:completed', this.initOwner.bind(this))
await this.info?.init()
resolve(true)
})
}

private async initConfig(stage: MRPStage, status: MRPStatus){
Expand All @@ -37,14 +41,20 @@ export class MRPRelay extends MRPData {
}

private async initOwner(stage: MRPStage, status: MRPStatus){
const promises: Promise<any>[] = [];
if(MRPStatus.Failure === status || this.config.event.isBlockEnabled('relay-feed')) {
this.fetchRelayNotes()
promises.push(this.fetchRelayNotes())
}
if(MRPStatus.Failure === status || this.config.event.isBlockEnabled('operator-profile')) {
this.owner = new MRPUser(this.$, this.info.pubkey, 'operator')
await this.owner.init()
await this.owner.fetchNotes()
promises.push(new Promise( async (resolve) => {
this.owner = new MRPUser(this.$, this.info.pubkey, 'operator')
await this.owner.init()
await this.owner.fetchNotes()
resolve(true)
}))
}
await Promise.allSettled(promises)
this.complete()
}

async fetchOwnerFeed(opts: any){
Expand Down
4 changes: 2 additions & 2 deletions src/lib/core/kinds/app-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ const defaults = {
order: 0
},
'relay-feed': {
enabled: true,
enabled: false,
order: 1
},
'map': {
enabled: true,
enabled: false,
order: 2
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
export const trailingSlash = 'always';
let mrp: Writable<MyRelayPage> = writable()
let start: Writable<number> = writable(Date.now())
setContext(MY_RELAY_PAGE, mrp);
setContext(THEME_CSS, theme);
Expand All @@ -41,9 +42,20 @@
const _mrp = new MyRelayPage(url);
_mrp.$.signal.on('state:changed', function(){
// console.log('state:changed', ...arguments)
console.log('state:changed', ...arguments)
mrp.set(_mrp);
});
_mrp.$.signal.on('sync:start', function(timestamp: number){
document.body.classList.add('loading')
})
_mrp.$.signal.on('sync:finish', function(timestamp: number){
document.body.classList.remove('loading')
document.body.classList.add('loaded')
console.log(`loaded in ${Date.now()-$start}ms`)
})
mrp.set(_mrp);
await _mrp.init();
// mrp.set(_mrp);
Expand Down

0 comments on commit cd9f492

Please sign in to comment.