-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdata.ts
69 lines (60 loc) · 1.91 KB
/
data.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import redis from "redis";
import NRP from "node-redis-pubsub";
import { StreamMessage } from "./proto/randomPackage/StreamMessage";
import { User } from "./proto/randomPackage/User";
const REDIST_KEYS = {
broadcastRoom: "room:0:messages",
users: "users",
};
const client = redis.createClient();
client.on("error", console.error);
client.on("connect", console.log);
type errCB = (err: Error | null) => void;
type replyCB<T> = (err: Error | null, reply: T) => void;
export const listMessagesFromMainRoom = (
done?: (data: Array<StreamMessage>) => void
) => {
client.lrange(REDIST_KEYS.broadcastRoom, 0, -1, (err, reply) => {
const msgs: Array<StreamMessage> = [];
for (const res of reply) {
msgs.push(JSON.parse(res));
}
done && done(msgs);
});
};
export const addChatToMainRoom = (msg: StreamMessage, fn: errCB) => {
client.rpush(REDIST_KEYS.broadcastRoom, JSON.stringify(msg), fn);
};
export const addUser = (user: User, fn: errCB) => {
client.rpush(REDIST_KEYS.users, JSON.stringify(user), fn);
};
export const listUsers = (fn: replyCB<User[]>) => {
client.lrange(REDIST_KEYS.users, 0, -1, (err, reply) => {
if (err) return fn(err, []);
const users: Array<User> = [];
for (const r of reply) {
users.push(JSON.parse(r));
}
fn(null, users);
});
};
export const findUser = (userId: number, fn: replyCB<User>) => {
listUsers((err, users) => {
if(err) return fn(err, {} as User)
const i = users.findIndex((e) => e.id === userId)
fn(null, users[i])
})
}
export const updateUser = (user: User, fn: errCB) => {
listUsers((err, users) => {
if(err) return fn(err)
const i = users.findIndex((e) => e.id === user.id)
if(i === -1) return fn(Error('cannot find user'))
client.lset(REDIST_KEYS.users, i, JSON.stringify(user), fn)
})
}
export default client;
export const nrp = NRP({
emitter: redis.createClient(),
receiver: redis.createClient(),
});