-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWindow.js
143 lines (142 loc) · 4.42 KB
/
Window.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const {
gRequest,
QUERY_WINDOW,
UPDATE_WINDOW_ACTIVE,
UPDATE_WINDOW_MEMBERS,
// NEW_WINDOW
} = require("./graphqlClient");
const { sameUser } = require("./utils");
const Windows = {};
// 只保留需要的字段
const filterMemberFields = (member = null) => {
if (!member) return member;
let keeps = ["id", "uid", "username", "avator", "creator", "photo"];
let tmp = {};
Object.keys(member).forEach(k => {
if (keeps.includes(k) && typeof member[k] !== "undefined") {
tmp[k] = member[k];
}
});
return tmp;
};
class Window {
constructor({ id, roomId, temp = false, title = "" }) {
this.id = id;
this.title = title;
this.temp = temp;
this.active = false;
this.members = [];
this.users = {};
this.roomId = roomId;
this.room = {};
this.tabs = null;
this.workspaceData = null;
}
get activeUsers() {
console.log("current users", this.users);
return Object.values(this.users);
}
async fetchData() {
console.log("start fetch", this.temp, typeof this.temp);
if (this.temp) { return; }
const result = await gRequest(QUERY_WINDOW, {
id: this.id,
});
console.log("data fetched", result.portal_window);
if (result && result.portal_window[0]) {
const [{ title, members, active, roomByRoom, tabs }] = result.portal_window;
this.title = title;
this.active = active;
this.members = members;
this.room = roomByRoom;
this.tabs = tabs.map(t => { return { url: t.url }; });
// 激活当前window
if (!active) {
this.setActive();
}
}
}
setActive() {
console.log("active the window");
// 设置为活跃window
gRequest(UPDATE_WINDOW_ACTIVE, { active: true, id: this.id }).then((wtf) => {
console.log(wtf);
});
}
setInactive() {
// 设置为不活跃window
gRequest(UPDATE_WINDOW_ACTIVE, { active: false, id: this.id }).then((wtf) => {
console.log(wtf);
});
}
appendMember(member) {
if (this.temp) return;
// 更新参与者
if (!this.members) return;
// 如果没有uid,就pass掉
if (!member.uid) return;
const filterd = this.members.filter((m) => sameUser(m, member));
console.log("filterd", filterd);
if (filterd.length == 0) {
// append member
console.log("append member", member);
gRequest(UPDATE_WINDOW_MEMBERS, {
member: filterMemberFields(member),
id: this.id,
});
}
}
addActiveUser(sid, user) {
console.log("add active user", user);
// 新增活跃用户
this.users[sid] = user;
return user;
}
beHost(sid, enable = true) {
const tmps = Object.entries(this.users);
const newUsers = enable ? Object.fromEntries(tmps.map(([key, user]) => {
return [key, { ...user, host: key == sid, follow: key !== sid }];
})) : Object.fromEntries(tmps.map(([key, user]) => {
return [key, { ...user, host: false, follow: false }];
}));
this.users = { ...newUsers };
}
updateUser(sid, params) {
if (this.users[sid]) {
this.users[sid] = { ...this.users[sid], ...params };
}
}
destory() {
if (!this.temp) {
// 非临时window,设置为非活跃状态
this.setInactive();
}
// 释放掉
Windows[this.id] = null;
delete Windows[this.id];
}
removeActiveUser(sid) {
const currUser = this.users[sid];
if (currUser.host) {
Object.keys(this.users).forEach(k => {
this.users[k].follow = false;
});
}
delete this.users[sid];
// window没人了
if (this.activeUsers.length == 0) {
this.destory();
}
}
}
const getWindowInstance = async ({ id, roomId, temp, title = "" }) => {
console.log("current window list", { Windows });
if (!Windows[id]) {
console.log("new window obj");
Windows[id] = new Window({ id, roomId, temp, title });
}
await Windows[id].fetchData();
return Windows[id];
};
module.exports = getWindowInstance;
module.exports.Windows = Windows;