Skip to content

Commit

Permalink
Improve chat example
Browse files Browse the repository at this point in the history
  • Loading branch information
tshemsedinov committed Oct 11, 2023
1 parent 23707dc commit cb4a054
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 28 deletions.
11 changes: 8 additions & 3 deletions application/api/chat.1/send.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
async ({ room, message }) => {
domain.chat.send(room, message);
};
({
access: 'public',

method: async ({ room, message }) => {
domain.chat.send(room, message);
return true;
},
});
20 changes: 13 additions & 7 deletions application/api/chat.1/subscribe.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
async ({ room }) => {
const clients = domain.chat.getRoom(room);
clients.add(context.client);
context.client.on('close', () => {
clients.delete(context.client);
});
};
({
access: 'public',

method: async ({ room }) => {
const clients = domain.chat.getRoom(room);
clients.add(context.client);
context.client.on('close', () => {
clients.delete(context.client);
if (clients.size === 0) domain.chat.dropRoom(room);
});
return true;
},
});
11 changes: 11 additions & 0 deletions application/api/chat.1/unsubscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
({
access: 'public',

method: async ({ room }) => {
const clients = domain.chat.rooms.get(room);
if (!clients) throw new Error(`Room ${room} is not found`);
clients.delete(context.client);
if (clients.size === 0) domain.chat.dropRoom(room);
return true;
},
});
7 changes: 6 additions & 1 deletion application/domain/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
return room;
},

dropRoom(name) {
domain.chat.rooms.delete(name);
},

send(name, message) {
const room = domain.chat.getRoom(name);
const room = domain.chat.rooms.get(name);
if (!room) throw new Error(`Room ${name} is not found`);
for (const client of room) {
client.emit('chat/message', { room: name, message });
}
Expand Down
2 changes: 1 addition & 1 deletion application/domain/chat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
});

await t.test('Send message with domain.chat.send', async () => {
const result = domain.chat.send('Marcus', 'Hello there');
const result = domain.chat.send('example1', 'Hello there');
node.assert.strictEqual(result, undefined);
});
},
Expand Down
16 changes: 16 additions & 0 deletions application/domain/tests/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
({
async cases(t, metacom) {
if (t) return;
const res = await metacom.api.auth.signin({
login: 'marcus',
password: 'marcus',
});
node.assert.strictEqual(res.status, 'logged');
node.assert.strictEqual(typeof res.token, 'string');

await t.test(`Call example.add({ a, b })`, async () => {
const res = await metacom.api.example.add({ a: 10, b: 20 });
node.assert.strictEqual(res, 30);
});
},
});
16 changes: 0 additions & 16 deletions application/domain/tests/api.test.js

This file was deleted.

10 changes: 10 additions & 0 deletions application/domain/tests/http.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
({
name: 'Metacom over HTTP',

async run(t) {
const url = 'http://127.0.0.1:8001/api';
const metacom = metarhia.metacom.Metacom.create(url);
await metacom.load('auth', 'example');
await domain.tests.api.cases(t, metacom);
},
});
10 changes: 10 additions & 0 deletions application/domain/tests/ws.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
({
name: 'Metacom over Websocket',

async run(t) {
const url = 'ws://127.0.0.1:8001/api';
const metacom = metarhia.metacom.Metacom.create(url);
await metacom.load('auth', 'example');
await domain.tests.api.cases(t, metacom);
},
});

0 comments on commit cb4a054

Please sign in to comment.