forked from ably/spaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceUpdate.ts
73 lines (61 loc) · 1.92 KB
/
SpaceUpdate.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
70
71
72
73
import { nanoid } from 'nanoid';
import { Types } from 'ably';
import type { SpaceMember, ProfileData } from './types.js';
import type { PresenceMember } from './utilities/types.js';
export interface SpacePresenceData {
data: PresenceMember['data'];
extras: PresenceMember['extras'];
}
class SpaceUpdate {
private self: SpaceMember | null;
private extras: Types.PresenceMessage['extras'];
constructor({ self, extras }: { self: SpaceMember | null; extras?: Types.PresenceMessage['extras'] }) {
this.self = self;
this.extras = extras;
}
private profileUpdate(id: string | null, current: ProfileData) {
return { id, current };
}
private profileNoChange() {
return this.profileUpdate(null, this.self ? this.self.profileData : null);
}
private locationUpdate(id: string | null, current: SpaceMember['location'], previous: SpaceMember['location']) {
return { id, current, previous };
}
private locationNoChange() {
const location = this.self ? this.self.location : null;
return this.locationUpdate(null, location, null);
}
updateProfileData(current: ProfileData): SpacePresenceData {
return {
data: {
profileUpdate: this.profileUpdate(nanoid(), current),
locationUpdate: this.locationNoChange(),
},
extras: this.extras,
};
}
updateLocation(location: SpaceMember['location'], previousLocation?: SpaceMember['location']): SpacePresenceData {
return {
data: {
profileUpdate: this.profileNoChange(),
locationUpdate: this.locationUpdate(
nanoid(),
location,
previousLocation ? previousLocation : this.self?.location,
),
},
extras: this.extras,
};
}
noop(): SpacePresenceData {
return {
data: {
profileUpdate: this.profileNoChange(),
locationUpdate: this.locationNoChange(),
},
extras: this.extras,
};
}
}
export default SpaceUpdate;