Skip to content

Commit 284eda6

Browse files
author
Karl
committed
improve build, add poc using events for reactivity
1 parent d8c6c30 commit 284eda6

File tree

12 files changed

+73
-44
lines changed

12 files changed

+73
-44
lines changed

.yarnrc.yml

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
nodeLinker: node-modules
22

33
npmScopes:
4-
testscope:
5-
npmPublishRegistry: 'http://localhost:4873'
6-
npmRegistryServer: 'http://localhost:4873'
4+
testscope:
5+
npmPublishRegistry: "http://localhost:4873"
6+
npmRegistryServer: "http://localhost:4873"
77

88
plugins:
9-
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
10-
spec: '@yarnpkg/plugin-workspace-tools'
11-
- path: .yarn/plugins/@yarnpkg/plugin-version.cjs
12-
spec: '@yarnpkg/plugin-version'
9+
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
10+
spec: "@yarnpkg/plugin-workspace-tools"
11+
- path: .yarn/plugins/@yarnpkg/plugin-version.cjs
12+
spec: "@yarnpkg/plugin-version"
1313

1414
unsafeHttpWhitelist:
15-
- localhost
15+
- localhost
1616

1717
yarnPath: .yarn/releases/yarn-3.5.1.cjs

packages/js-sdk/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"@web3-name-sdk/core": "^0.2.0",
2323
"axios": "^0.27.2",
2424
"dotenv": "^16.0.1",
25-
"ethers": "5",
25+
"ethers": "5.7.2",
2626
"siwe": "^2.3.2",
2727
"socket.io-client": "^4.8.1"
2828
},

packages/js-sdk/src/Dm3.ts

-12
This file was deleted.

packages/js-sdk/src/Dm3Sdk.ts

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { EventEmitter } from 'events';
12
import {
23
normalizeEnsName,
34
ProfileKeys,
@@ -12,7 +13,7 @@ import { BackendConnector } from './api/BackendConnector';
1213
import { StorageAPI } from '@dm3-org/dm3-lib-storage';
1314
import { ethers } from 'ethers';
1415
import { Tld } from './tld/Tld';
15-
import { Dm3 } from './Dm3';
16+
// import { Dm3 } from './Dm3';
1617
import { ITLDResolver } from './tld/nameService/ITLDResolver';
1718

1819
/**
@@ -40,7 +41,7 @@ export interface Dm3SdkConfig {
4041
_tld?: ITLDResolver;
4142
}
4243

43-
export class Dm3Sdk {
44+
export class Dm3Sdk extends EventEmitter {
4445
private readonly mainnetProvider: ethers.providers.JsonRpcProvider;
4546
private readonly lukso?: ethers.providers.ExternalProvider;
4647

@@ -70,12 +71,38 @@ export class Dm3Sdk {
7071
*/
7172
public conversations: Conversations;
7273

74+
private _selectedConversationId: string;
75+
76+
getConversations() {
77+
return this.conversations;
78+
}
79+
80+
getMessagesByConversation(ensName?: string) {
81+
if (ensName) {
82+
this._selectedConversationId = ensName;
83+
}
84+
85+
if (!this._selectedConversationId) {
86+
throw new Error('No conversation selected');
87+
}
88+
89+
// TODO: ens name might not be the best option to identify the conversation, we should introduce some id
90+
const selectedConversation = this.conversations.list.find(c => c.contact.account.ensName === this._selectedConversationId);
91+
92+
if (!selectedConversation) {
93+
throw new Error('Selected conversation not found');
94+
}
95+
96+
return selectedConversation.messages;
97+
}
98+
7399
/**
74100
* DM3 TLD
75101
*/
76102
private _tld?: ITLDResolver;
77103

78104
constructor(config: Dm3SdkConfig) {
105+
super();
79106
//TODO keep ethers v5 for know but extract into common interface later
80107
this.mainnetProvider = config.mainnetProvider;
81108
this.nonce = config.nonce;
@@ -154,10 +181,15 @@ export class Dm3Sdk {
154181
account,
155182
profileKeys,
156183
this.addressEnsSubdomain,
184+
(event: string, eventData: any) => {
185+
this.emit('dm3_event', { event, eventData });
186+
},
157187
);
158188
await conversations._init();
159189

160-
return new Dm3(conversations, tld);
190+
this.conversations = conversations;
191+
192+
return this;
161193
}
162194

163195
/**

packages/js-sdk/src/conversation/Conversations.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class Conversations {
2222
private readonly account: Account;
2323
private readonly profileKeys: ProfileKeys;
2424
public list: Conversation[];
25+
private callback: (event: string, eventData: any) => void;
2526

2627
constructor(
2728
storageApi: StorageAPI,
@@ -30,6 +31,7 @@ export class Conversations {
3031
account: Account,
3132
profileKeys: ProfileKeys,
3233
addressEnsSubdomain: string,
34+
callback: (event: string, eventData: any) => void,
3335
) {
3436
this.storageApi = storageApi;
3537
this.tld = tld;
@@ -38,6 +40,7 @@ export class Conversations {
3840
this.addressEnsSubdomain = addressEnsSubdomain;
3941
this.profileKeys = profileKeys;
4042
this.list = [];
43+
this.callback = callback;
4144
}
4245

4346
public async _init() {
@@ -51,6 +54,7 @@ export class Conversations {
5154
}
5255

5356
public async addConversation(_ensName: string) {
57+
this.callback('start_add_conversation', { ensName: _ensName });
5458
const contactTldName = normalizeEnsName(_ensName);
5559

5660
const aliasName = await this.tld.resolveTLDtoAlias(contactTldName);
@@ -65,6 +69,7 @@ export class Conversations {
6569
const conversationPreview = this._addConversation(newConversation);
6670
//Add the contact to the storage in the background
6771
this.storageApi.addConversation(aliasName, [contactTldName]);
72+
this.callback('finalise_add_conversation', { ensName: _ensName });
6873
return conversationPreview;
6974
}
7075

@@ -170,7 +175,7 @@ export class Conversations {
170175
contact: hydratedContact,
171176
};
172177
this.list.push(hydratedConversation);
173-
178+
this.callback('add_message', { ensName: contact.account.ensName });
174179
return hydratedConversation;
175180
};
176181

packages/js-sdk/src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from './Dm3Sdk';
2-
export * from './Dm3';
32
export * from './conversation/types';
43
export * from './message/types';

packages/messenger-vue-demo/src/composables/chat.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { computed, ref, markRaw, type Ref } from 'vue';
2-
import { Dm3, Dm3Sdk, type Dm3SdkConfig } from '@dm3-org/dm3-js-sdk';
2+
import { Dm3Sdk, type Dm3SdkConfig } from '@dm3-org/dm3-js-sdk';
33
import {ethers} from 'ethers';
44
import type { Conversation } from '@dm3-org/dm3-lib-storage';
5-
import { transformToMessages, transformToRooms, type ChatRoom } from '@/chatUtils';
5+
import { transformToMessages, transformToRooms, type ChatRoom } from '../utils/chatUtils';
66
import { computedAsync } from '@vueuse/core';
77

88
const sepoliaProvider = new ethers.providers.JsonRpcProvider("https://eth-sepolia.g.alchemy.com/v2/cBTHRhVcZ3Vt4BOFpA_Hi5DcTB1KQQV1", {
@@ -23,6 +23,10 @@ const configLukso: Dm3SdkConfig = {
2323

2424
const sdk = new Dm3Sdk(configLukso);
2525

26+
sdk.on('dm3_event', (eventData) => {
27+
console.log('dm3_event', eventData);
28+
});
29+
2630
// TODO: check for installed extension
2731
// https://docs.lukso.tech/install-up-browser-extension/
2832

@@ -57,14 +61,14 @@ const requestProvider = (): Promise<ethers.providers.ExternalProvider> => {
5761
export function useDm3Chat(): UseDm3ChatReturnType {
5862
const roomsLoaded = ref(false);
5963
const messagesLoaded = ref(false);
60-
const dm3Instance = ref<Dm3 | null>(null);
64+
const dm3Instance = ref<Dm3Sdk | null>(null);
6165
const isReady = ref(false);
6266
const selectedConversation = ref<Conversation | null>(null);
6367
const loggedInAccount = ref<string | null>(null);
6468

6569
const init = async () => {
6670
const dm3 = await sdk.universalProfileLoginWithCache(requestProvider);
67-
dm3Instance.value = markRaw(dm3);
71+
dm3Instance.value = dm3;
6872
isReady.value = true;
6973
loggedInAccount.value = 'TODO';
7074
};
@@ -80,7 +84,7 @@ export function useDm3Chat(): UseDm3ChatReturnType {
8084
}));
8185
roomsLoaded.value = true;
8286

83-
return transformToRooms(conversationsPreview.value);
87+
return transformToRooms(conversationsPreview.value as any);
8488
});
8589

8690
const messages = ref<any[]>([]);

packages/messenger-vue-demo/src/chatUtils.ts packages/messenger-vue-demo/src/utils/chatUtils.ts

-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ export type ChatRoom = {
7979

8080
export function transformToMessages(messagesData: any[]): any[] {
8181
return messagesData.map((message) => {
82-
console.log('message', message);
83-
8482
// Ensure _id and senderId are valid
8583
const _id = message.envelop.id ? String(message.envelop.id) : "unknown_id";
8684
const senderId = message.envelop.message.metadata.from ? String(message.envelop.message.metadata.from) : "unknown_sender";

packages/messenger-vue-demo/tsconfig.app.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"include": ["env.d.ts", "src/**/*", "src/**/*.vue"],
44
"exclude": ["src/**/__tests__/*"],
55
"compilerOptions": {
6+
"allowSyntheticDefaultImports": true,
67
"noImplicitAny": false,
78
"allowJs": true,
89
"composite": true,

packages/messenger-vue-demo/tsconfig.node.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"composite": true,
1212
"noEmit": true,
1313
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
14-
14+
"allowSyntheticDefaultImports": true,
1515
"module": "ESNext",
1616
"moduleResolution": "Bundler",
1717
"types": ["node"]

packages/messenger-vue-demo/vite.config.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { fileURLToPath, URL } from 'node:url'
33
import { defineConfig } from 'vite'
44
import vue from '@vitejs/plugin-vue'
55
import vueDevTools from 'vite-plugin-vue-devtools'
6-
76
// https://vite.dev/config/
87
export default defineConfig({
98
plugins: [
@@ -20,21 +19,24 @@ export default defineConfig({
2019
],
2120
resolve: {
2221
alias: {
23-
'@': fileURLToPath(new URL('./src', import.meta.url))
22+
'@/': fileURLToPath(new URL('./src', import.meta.url))
2423
},
2524
},
2625
// due to built of libs with "module": "CommonJS", instead of "ESNext", we need to force pre-bundle them
2726
optimizeDeps: {
28-
include: ['@dm3-org/dm3-js-sdk', '@dm3-org/dm3-lib-crypto', 'dm3-org/dm3-lib-profile'],
27+
include: ['@dm3-org/dm3-js-sdk', '@dm3-org/dm3-lib-crypto', '@dm3-org/dm3-lib-profile'],
2928
},
3029
build: {
30+
sourcemap: 'inline',
3131
commonjsOptions: {
32+
transformMixedEsModules: true,
3233
// Ensure your library is included in CommonJS handling
33-
include: [/node_modules/,
34-
/@dm3-org\/dm3-js-sdk/,
35-
/@dm3-org\/dm3-lib-crypto/,
36-
/dm3-org\/dm3-lib-profile/,
37-
/@dm3-org\/dm3-lib-storage/,
34+
include: [
35+
'node_modules/*',
36+
'@dm3-org/dm3-js-sdk',
37+
'@dm3-org/dm3-lib-crypto',
38+
'@dm3-org/dm3-lib-profile',
39+
'@dm3-org/dm3-lib-storage',
3840
],
3941
},
4042
},

yarn.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -2520,7 +2520,7 @@ __metadata:
25202520
babel-jest: ^29.2.2
25212521
babel-preset-env: ^1.7.0
25222522
dotenv: ^16.0.1
2523-
ethers: 5
2523+
ethers: 5.7.2
25242524
jest: ^29.2.2
25252525
jest-mock-extended: 2.0.4
25262526
prettier: ^2.6.2
@@ -18277,7 +18277,7 @@ __metadata:
1827718277
languageName: node
1827818278
linkType: hard
1827918279

18280-
"ethers@npm:5, ethers@npm:5.7.2, ethers@npm:^5.0.13, ethers@npm:^5.7.0, ethers@npm:^5.7.1, ethers@npm:^5.7.2":
18280+
"ethers@npm:5.7.2, ethers@npm:^5.0.13, ethers@npm:^5.7.0, ethers@npm:^5.7.1, ethers@npm:^5.7.2":
1828118281
version: 5.7.2
1828218282
resolution: "ethers@npm:5.7.2"
1828318283
dependencies:

0 commit comments

Comments
 (0)