mirror of
https://github.com/logos-messaging/OpChan.git
synced 2026-01-05 22:33:07 +00:00
chore: allow waku config through API
This commit is contained in:
parent
08c9acb25a
commit
05938b5127
@ -9,7 +9,13 @@ if (!(window as Window & typeof globalThis).Buffer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
createRoot(document.getElementById('root')!).render(
|
createRoot(document.getElementById('root')!).render(
|
||||||
<OpChanProvider config={{ ordiscanApiKey: '6bb07766-d98c-4ddd-93fb-6a0e94d629dd' }}>
|
<OpChanProvider config={{
|
||||||
|
ordiscanApiKey: '6bb07766-d98c-4ddd-93fb-6a0e94d629dd',
|
||||||
|
wakuConfig: {
|
||||||
|
contentTopic: '/opchan/1/messages/proto',
|
||||||
|
reliableChannelId: 'opchan-messages'
|
||||||
|
}
|
||||||
|
}}>
|
||||||
<App />
|
<App />
|
||||||
</OpChanProvider>
|
</OpChanProvider>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -7,9 +7,11 @@ import { UserIdentityService } from '../lib/services/UserIdentityService';
|
|||||||
import { DelegationManager, delegationManager } from '../lib/delegation';
|
import { DelegationManager, delegationManager } from '../lib/delegation';
|
||||||
import WalletManager from '../lib/wallet';
|
import WalletManager from '../lib/wallet';
|
||||||
import { MessageService } from '../lib/services/MessageService';
|
import { MessageService } from '../lib/services/MessageService';
|
||||||
|
import { WakuConfig } from '../types';
|
||||||
|
|
||||||
export interface OpChanClientConfig {
|
export interface OpChanClientConfig {
|
||||||
ordiscanApiKey: string;
|
ordiscanApiKey: string;
|
||||||
|
wakuConfig: WakuConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class OpChanClient {
|
export class OpChanClient {
|
||||||
@ -35,6 +37,10 @@ export class OpChanClient {
|
|||||||
|
|
||||||
environment.configure(env);
|
environment.configure(env);
|
||||||
|
|
||||||
|
// Initialize message manager with WakuConfig
|
||||||
|
this.messageManager.initialize(config.wakuConfig).catch(error => {
|
||||||
|
console.error('Failed to initialize message manager:', error);
|
||||||
|
});
|
||||||
|
|
||||||
this.messageService = new MessageService(this.delegation);
|
this.messageService = new MessageService(this.delegation);
|
||||||
this.userIdentityService = new UserIdentityService(this.messageService);
|
this.userIdentityService = new UserIdentityService(this.messageService);
|
||||||
|
|||||||
@ -9,14 +9,15 @@ import {
|
|||||||
} from '../../types/waku';
|
} from '../../types/waku';
|
||||||
import { CONTENT_TOPIC } from './constants';
|
import { CONTENT_TOPIC } from './constants';
|
||||||
import { OpchanMessage } from '../../types/forum';
|
import { OpchanMessage } from '../../types/forum';
|
||||||
|
import { WakuConfig } from '../../types';
|
||||||
|
|
||||||
export class CodecManager {
|
export class CodecManager {
|
||||||
private encoder: IEncoder;
|
private encoder: IEncoder;
|
||||||
private decoder: IDecoder<IDecodedMessage>;
|
private decoder: IDecoder<IDecodedMessage>;
|
||||||
|
|
||||||
constructor(private node: LightNode) {
|
constructor(private node: LightNode, config: WakuConfig) {
|
||||||
this.encoder = this.node.createEncoder({ contentTopic: CONTENT_TOPIC });
|
this.encoder = this.node.createEncoder({ contentTopic: config.contentTopic || CONTENT_TOPIC });
|
||||||
this.decoder = this.node.createDecoder({ contentTopic: CONTENT_TOPIC });
|
this.decoder = this.node.createDecoder({ contentTopic: config.contentTopic || CONTENT_TOPIC });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import {
|
|||||||
import { CodecManager } from '../CodecManager';
|
import { CodecManager } from '../CodecManager';
|
||||||
import { generateStringId } from '../../utils';
|
import { generateStringId } from '../../utils';
|
||||||
import { OpchanMessage } from '../../../types/forum';
|
import { OpchanMessage } from '../../../types/forum';
|
||||||
|
import { WakuConfig } from '../../../types';
|
||||||
|
|
||||||
export interface MessageStatusCallback {
|
export interface MessageStatusCallback {
|
||||||
onSent?: (messageId: string) => void;
|
onSent?: (messageId: string) => void;
|
||||||
@ -22,9 +23,9 @@ export class ReliableMessaging {
|
|||||||
private incomingMessageCallbacks: Set<IncomingMessageCallback> = new Set();
|
private incomingMessageCallbacks: Set<IncomingMessageCallback> = new Set();
|
||||||
private codecManager: CodecManager;
|
private codecManager: CodecManager;
|
||||||
|
|
||||||
constructor(node: LightNode) {
|
constructor(node: LightNode, config: WakuConfig) {
|
||||||
this.codecManager = new CodecManager(node);
|
this.codecManager = new CodecManager(node, config);
|
||||||
this.initializeChannel(node);
|
this.initializeChannel(node, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== PUBLIC METHODS =====
|
// ===== PUBLIC METHODS =====
|
||||||
@ -65,11 +66,11 @@ export class ReliableMessaging {
|
|||||||
|
|
||||||
// ===== PRIVATE METHODS =====
|
// ===== PRIVATE METHODS =====
|
||||||
|
|
||||||
private async initializeChannel(node: LightNode): Promise<void> {
|
private async initializeChannel(node: LightNode, config: WakuConfig): Promise<void> {
|
||||||
const encoder = this.codecManager.getEncoder();
|
const encoder = this.codecManager.getEncoder();
|
||||||
const decoder = this.codecManager.getDecoder();
|
const decoder = this.codecManager.getDecoder();
|
||||||
const senderId = generateStringId();
|
const senderId = generateStringId();
|
||||||
const channelId = 'opchan-messages';
|
const channelId = config.reliableChannelId || 'opchan-messages';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.channel = await ReliableChannel.create(
|
this.channel = await ReliableChannel.create(
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import {
|
|||||||
MessageStatusCallback,
|
MessageStatusCallback,
|
||||||
} from './services/MessageService';
|
} from './services/MessageService';
|
||||||
import { ReliableMessaging } from './core/ReliableMessaging';
|
import { ReliableMessaging } from './core/ReliableMessaging';
|
||||||
|
import { WakuConfig } from '../../types';
|
||||||
|
|
||||||
export type { HealthChangeCallback, MessageStatusCallback };
|
export type { HealthChangeCallback, MessageStatusCallback };
|
||||||
|
|
||||||
@ -13,13 +14,16 @@ class MessageManager {
|
|||||||
private nodeManager: WakuNodeManager | null = null;
|
private nodeManager: WakuNodeManager | null = null;
|
||||||
private messageService: MessageService | null = null;
|
private messageService: MessageService | null = null;
|
||||||
private reliableMessaging: ReliableMessaging | null = null;
|
private reliableMessaging: ReliableMessaging | null = null;
|
||||||
|
private wakuConfig: WakuConfig;
|
||||||
|
|
||||||
constructor() {}
|
constructor(wakuConfig: WakuConfig) {
|
||||||
|
this.wakuConfig = wakuConfig;
|
||||||
|
}
|
||||||
|
|
||||||
// ===== PUBLIC STATIC METHODS =====
|
// ===== PUBLIC STATIC METHODS =====
|
||||||
|
|
||||||
public static async create(): Promise<MessageManager> {
|
public static async create(wakuConfig: WakuConfig): Promise<MessageManager> {
|
||||||
const manager = new MessageManager();
|
const manager = new MessageManager(wakuConfig);
|
||||||
await manager.initialize();
|
await manager.initialize();
|
||||||
return manager;
|
return manager;
|
||||||
}
|
}
|
||||||
@ -101,7 +105,8 @@ class MessageManager {
|
|||||||
try {
|
try {
|
||||||
console.log('Initializing reliable messaging...');
|
console.log('Initializing reliable messaging...');
|
||||||
this.reliableMessaging = new ReliableMessaging(
|
this.reliableMessaging = new ReliableMessaging(
|
||||||
this.nodeManager.getNode()
|
this.nodeManager.getNode(),
|
||||||
|
this.wakuConfig
|
||||||
);
|
);
|
||||||
this.messageService?.updateReliableMessaging(this.reliableMessaging);
|
this.messageService?.updateReliableMessaging(this.reliableMessaging);
|
||||||
console.log('Reliable messaging initialized successfully');
|
console.log('Reliable messaging initialized successfully');
|
||||||
@ -126,13 +131,18 @@ export class DefaultMessageManager {
|
|||||||
private _initPromise: Promise<MessageManager> | null = null;
|
private _initPromise: Promise<MessageManager> | null = null;
|
||||||
private _pendingHealthSubscriptions: HealthChangeCallback[] = [];
|
private _pendingHealthSubscriptions: HealthChangeCallback[] = [];
|
||||||
private _pendingMessageSubscriptions: ((message: any) => void)[] = [];
|
private _pendingMessageSubscriptions: ((message: any) => void)[] = [];
|
||||||
|
private _wakuConfig: WakuConfig | null = null;
|
||||||
|
|
||||||
// ===== PUBLIC METHODS =====
|
// ===== PUBLIC METHODS =====
|
||||||
|
|
||||||
// Initialize the manager asynchronously
|
// Initialize the manager asynchronously
|
||||||
async initialize(): Promise<void> {
|
async initialize(wakuConfig?: WakuConfig): Promise<void> {
|
||||||
if (!this._initPromise) {
|
if (wakuConfig) {
|
||||||
this._initPromise = MessageManager.create();
|
this._wakuConfig = wakuConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this._initPromise && this._wakuConfig) {
|
||||||
|
this._initPromise = MessageManager.create(this._wakuConfig);
|
||||||
}
|
}
|
||||||
this._instance = await this._initPromise;
|
this._instance = await this._initPromise;
|
||||||
|
|
||||||
@ -160,6 +170,9 @@ export class DefaultMessageManager {
|
|||||||
|
|
||||||
async sendMessage(message: any, callback?: any): Promise<void> {
|
async sendMessage(message: any, callback?: any): Promise<void> {
|
||||||
if (!this._instance) {
|
if (!this._instance) {
|
||||||
|
if (!this._wakuConfig) {
|
||||||
|
throw new Error('WakuConfig must be provided before sending messages');
|
||||||
|
}
|
||||||
await this.initialize();
|
await this.initialize();
|
||||||
}
|
}
|
||||||
return this._instance!.sendMessage(message, callback);
|
return this._instance!.sendMessage(message, callback);
|
||||||
@ -200,9 +213,4 @@ export class DefaultMessageManager {
|
|||||||
|
|
||||||
const messageManager = new DefaultMessageManager();
|
const messageManager = new DefaultMessageManager();
|
||||||
|
|
||||||
// Initialize in the background
|
|
||||||
messageManager.initialize().catch(error => {
|
|
||||||
console.error('Failed to initialize default MessageManager:', error);
|
|
||||||
});
|
|
||||||
|
|
||||||
export default messageManager;
|
export default messageManager;
|
||||||
|
|||||||
@ -1 +1,6 @@
|
|||||||
export * as forum from './forum';
|
export * as forum from './forum';
|
||||||
|
|
||||||
|
export interface WakuConfig {
|
||||||
|
contentTopic?: string;
|
||||||
|
reliableChannelId?: string;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user