Skip to content

Commit

Permalink
config hydrated
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Apr 12, 2024
1 parent 611f006 commit 9670175
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 126 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/partials/login.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</script>
{#if browser && window?.nostr}
<div class="inline-block">
{#if mrp?.nostr && (typeof mrp?.nostr?.user === 'undefined' || mrp?.nostr?.user === null)}
{#if mrp?.nostr && (typeof mrp?.nostr?.user === 'undefined' || mrp?.nostr?.user == null)}
<Button variant="outline" class="relative -top-3" on:click={login}>login</Button>
{:else if mrp?.nostr?.user?.profile}
<div class="flex items-center space-x-4">
Expand Down
74 changes: 33 additions & 41 deletions src/lib/core/MRPBlockLoader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { R } from "vitest/dist/reporters-P7C2ytIv.js"
import type { MRPConfig } from "./MRPConfig"
import type { MRPConfig, ConfigBlocks } from "./MRPConfig"
import type { MRPState } from "./MRP"
import { MRPData } from "./MRPData"
import { AppConfig } from "./kinds/app-config"

type Components = {
[key: string]: string | boolean
Expand All @@ -27,96 +28,87 @@ export class BlockLoader extends MRPData {
// 'feed': 'feed',
}
private $: MRPState
private _componentsDef: Components = {}
private _components: Record<string, NodeModule> = {}
private _config: MRPConfig | undefined
private _config: MRPConfig

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

async init(){
this.begin()
if(this.config?.componentVisible && Object.keys(this.config?.componentVisible)?.length){
this.config = { ...config.componentVisible, ...this.defaultComponents }
}
else {
console.log('loading default components')
this._componentsDef = this.defaultComponents
}
await this.loadNativeComponents().catch(BlockLoader.errorHandler)
}

private set componentDef( component: ComponentOption ){
if(!component?.url){
this._componentsDef[component.name] = this.nativeComponentPath(component.name)
}
else {
this._componentsDef[component.name] = component.url
async loadAllComponents(){
await this.loadNativeComponents().catch(BlockLoader.errorHandler)
await this.loadRemoteComponents().catch(BlockLoader.errorHandler)
}

async loadNativeComponents(){
console.log(this.config.event.blocks)
for(let name in this.config.event.blocks){
console.log(`tryinng to load`, name)
if(!this.config.event.blocks[name].enabled || this.config.event.blocks[name].custom === true) continue;
await this.loadNativeComponent(name).catch(BlockLoader.errorHandler)
}
}

private set component( component: $Component ){
this._components[component.name] = component.module
async loadRemoteComponents(){
for(let name in this.config.event.blocks){
const block = this.config.event.blocks[name]
if(!block.enabled || block.custom === false)
continue;
if(block.customType === 'url')
await this.loadRemoteComponent(name, block.customUrl).catch(BlockLoader.errorHandler)
}
}

nativeComponentPath(name: string): string {
return `../components/blocks/${this._componentsDef[name]}/index.svelte`
}

async addComponent(name: string, url?: string, native: boolean = true){
this.componentDef = {
name: name,
url: url
} as ComponentOption
if(native)
await this.loadNativeComponent(name).catch(BlockLoader.errorHandler)
else if(url)
await this.loadRemoteComponent(name, url).catch(BlockLoader.errorHandler)
this.config.event.setBlock(name, { enabled: true, order: Object.keys(this.config.event.blocks).length, options: {} })
}

private async loadNativeComponent(name: string): Promise<NodeModule | undefined> {
const block = this.config.event.blocks?.[name]
console.log('block', block)
let $component;
if(this._componentsDef[name]){
$component = await import(`../components/blocks/${this._componentsDef[name]}/${this._componentsDef[name]}.svelte`).catch(BlockLoader.errorHandler)
if(block){
console.log('loading native component:', name)
$component = await import(`../components/blocks/${name}/${name}.svelte`).catch(BlockLoader.errorHandler)
this._components[name] = $component?.default? $component.default: $component
}
return $component
}

private async loadRemoteComponent(name: string, url: string): Promise<NodeModule | undefined> {
const block = this.config.event.blocks?.[name]
let $component;
if(this._componentsDef[name]){
if(block){
$component = await import(/* @vite-ignore */ url).catch(BlockLoader.errorHandler)
this._components[name] = $component?.default? $component.default: $component
}
return $component
}

async loadAllComponents(){
await this.loadNativeComponents().catch(BlockLoader.errorHandler)
await this.loadRemoteComponents().catch(BlockLoader.errorHandler)
}

async loadNativeComponents(){
for(let name in this._componentsDef){
await this.loadNativeComponent(name).catch(BlockLoader.errorHandler)
}
}

async loadRemoteComponents(){
for(let name in this._componentsDef){
await this.loadNativeComponent(name).catch(BlockLoader.errorHandler)
}
}


static errorHandler(error: Error){
console.error(`BlockLoader: ${error}`)
}

get config(): MRPConfig | undefined {
get config(): MRPConfig {
return this._config
}

Expand Down
68 changes: 15 additions & 53 deletions src/lib/core/MRPConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,37 @@ import { MRPUser } from "./MRPUser";

export class MRPConfig extends MRPData {

private readonly kind: NDKKind.AppSpecificData;
private readonly namespace: `myrelay.page`;

private _$: MRPState;
private _type: string; //operator, user
private _event: AppConfig | null;
private _data: any;
private _pubkey: string | undefined;
private _relay: string | undefined
private pubkey: string | undefined;
private relay: string | undefined

public event: AppConfig;

constructor($state: MRPState, relay?: string, pubkey?: string){
super($state.signal, 'config')
this._$ = $state
this._pubkey = pubkey
this._relay = relay
this.pubkey = pubkey
this.relay = relay
}

async init(){
this.begin()
console.log('fetching config')
const result = await this.fetch()
let result = await this.fetch()
console.log('fetched config', result)
if(result === null) return this.complete(false)
if(this.event == null) result = this.create()
this.event = result as AppConfig
console.log(this.event)
this.complete(true)
}

create(){
if(this?.event) return
if(!this.$?.ndk) return
this.event = new AppConfig(this.$.ndk)
create(): AppConfig {
this.event = new AppConfig(this.$.ndk as NDK)
this.$.signal.emit('config:created', this.event)
return this.event
}

async fetch(): Promise<AppConfig | null> {
Expand All @@ -49,7 +49,7 @@ export class MRPConfig extends MRPData {
.fetchEvent(
{ kinds: [NDKKind.AppSpecificData], authors: [this.pubkey], '#d': [this.configKey()] },
undefined,
this._relay? NDKRelaySet.fromRelayUrls([this._relay], this?.$?.ndk as NDK): undefined
this.relay? NDKRelaySet.fromRelayUrls([this.relay], this?.$?.ndk as NDK): undefined
)
) as AppConfig
if(config === null) return null
Expand All @@ -58,18 +58,6 @@ export class MRPConfig extends MRPData {
return this.event
}

update(key: string, config: Record<string, any>): boolean{
if(!this?.event) return false
this.data[key] = config;
this.event.set(key, this.data[key])
this.$.signal.emit(`config:updated`, this.type, key, this.event)
return true
}

get(key: string): any {
return this.event?.get(key)
}

configKey(): string {
return `${this?.relay}@myrelay.page`
}
Expand All @@ -86,11 +74,7 @@ export class MRPConfig extends MRPData {
formatType(): string {
return `${this.type.toLowerCase()}@${this.namespace}`.toLowerCase()
}

get relay(): string | undefined {
return this._relay
}


get type(){
return this._type
}
Expand All @@ -99,14 +83,6 @@ export class MRPConfig extends MRPData {
this._type = type
}

get event(): AppConfig | null {
return this._event
}

private set event(event: AppConfig){
this._event = event
}

private get $(): MRPState{
return this._$
}
Expand All @@ -115,19 +91,5 @@ export class MRPConfig extends MRPData {
this._$ = $
}

get data(){
return this._data
}

private set data(data: any){
this._data = data
}

get pubkey(): string | undefined{
return this._pubkey
}

private set pubkey(pubkey: string){
this._pubkey = pubkey
}
}
4 changes: 2 additions & 2 deletions src/lib/core/MRPRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export class MRPRelay extends MRPData {
}

private async initOwner(stage: MRPStage, status: MRPStatus){
if(MRPStatus.Failure === status || this.config?.get('relay-feed')?.enabled) {
if(MRPStatus.Failure === status || this.config.event.isBlockEnabled('relay-feed')) {
this.fetchRelayNotes()
}
if(MRPStatus.Failure === status || this.config?.get('owner')?.enabled) {
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()
Expand Down
4 changes: 2 additions & 2 deletions src/lib/core/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class MRPConfig extends MRPData {
async init(){
this.begin()
const result = await this.fetch()
if(result === null) return this.complete(false)
if(result == null) return this.complete(false)
this.complete(true)
}

Expand All @@ -40,7 +40,7 @@ export class MRPConfig extends MRPData {
if(!this.$?.ndk) return null
if(!this.pubkey) return null
const config: AppConfig | null = (await this.$.ndk.fetchEvent({ kinds: [this.kind], authors: [this.pubkey] })) as AppConfig
if(config === null) return null
if(config == null) return null
this.event = new AppConfig(this.$.ndk, config?.rawEvent())
this.$.signal.emit(`config:fetched`, this.type, this.event)
return this.event
Expand Down
Loading

0 comments on commit 9670175

Please sign in to comment.