-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This class is responsible for creating objects that will be passed to presence. It takes care of creating an update id and encapsulates the structure of the update.
- Loading branch information
Showing
5 changed files
with
174 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { describe, it, vi, expect } from 'vitest'; | ||
|
||
import SpaceUpdate from './SpaceUpdate.js'; | ||
import { createSpaceMember } from './utilities/test/fakes.js'; | ||
|
||
vi.mock('nanoid'); | ||
|
||
describe('SpaceUpdate', () => { | ||
it('creates a profileUpdate', () => { | ||
const self = createSpaceMember({ profileData: { name: 'Berry' } }); | ||
const update = new SpaceUpdate({ self }); | ||
expect(update.updateProfileData({ name: 'Barry' })).toEqual({ | ||
data: { | ||
locationUpdate: { | ||
current: null, | ||
id: null, | ||
previous: null, | ||
}, | ||
profileUpdate: { | ||
current: { | ||
name: 'Barry', | ||
}, | ||
id: 'NanoidID', | ||
}, | ||
}, | ||
extras: undefined, | ||
}); | ||
}); | ||
|
||
it('creates a locationUpdate', () => { | ||
const self = createSpaceMember({ location: { slide: 3 }, profileData: { name: 'Berry' } }); | ||
const update = new SpaceUpdate({ self }); | ||
expect(update.updateLocation({ slide: 1 }, null)).toEqual({ | ||
data: { | ||
locationUpdate: { | ||
current: { slide: 1 }, | ||
id: 'NanoidID', | ||
previous: { slide: 3 }, | ||
}, | ||
profileUpdate: { | ||
current: { | ||
name: 'Berry', | ||
}, | ||
id: null, | ||
}, | ||
}, | ||
extras: undefined, | ||
}); | ||
}); | ||
|
||
it('creates an object with no updates to current data', () => { | ||
const self = createSpaceMember({ location: { slide: 3 }, profileData: { name: 'Berry' } }); | ||
const update = new SpaceUpdate({ self }); | ||
expect(update.noop()).toEqual({ | ||
data: { | ||
locationUpdate: { | ||
current: { slide: 3 }, | ||
id: null, | ||
previous: null, | ||
}, | ||
profileUpdate: { | ||
current: { | ||
name: 'Berry', | ||
}, | ||
id: null, | ||
}, | ||
}, | ||
extras: undefined, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; |