Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Vanilla #31

Merged
merged 99 commits into from
Mar 13, 2018
Merged
Changes from 1 commit
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
fcef9eb
blank slate
callum-oakley Feb 5, 2018
b346f51
tests passing up to and including added to room hook
callum-oakley Feb 5, 2018
7dd6aa2
removed .prettierrc
callum-oakley Feb 5, 2018
f1d5976
basic user and presence store
callum-oakley Feb 6, 2018
6b37c3c
presence updates
callum-oakley Feb 6, 2018
f2b926c
typing indicators, plus function style consistency
callum-oakley Feb 7, 2018
d3ea427
room store
callum-oakley Feb 7, 2018
b1b95a4
make Store.set async
callum-oakley Feb 7, 2018
992b27e
removed from room, pop store
callum-oakley Feb 7, 2018
b41a60f
get users (wip)
callum-oakley Feb 7, 2018
b1790de
pull user subscription out in to own class
callum-oakley Feb 8, 2018
c9b1447
split presence subscription out in to own class
callum-oakley Feb 8, 2018
3f56054
simplify presence sub onEvent
callum-oakley Feb 8, 2018
2c05ffe
clean up userSubscription onEvent
callum-oakley Feb 8, 2018
3726718
user left hook
callum-oakley Feb 8, 2018
01ce329
onUserJoined
callum-oakley Feb 8, 2018
fcdf1f8
room updated hook
callum-oakley Feb 8, 2018
2683794
room deleted hook
callum-oakley Feb 8, 2018
16bd3fc
create room
callum-oakley Feb 8, 2018
79f37b1
getJoinableRooms
callum-oakley Feb 8, 2018
7f83f5b
getAllRooms
callum-oakley Feb 8, 2018
b4c0f43
join room
callum-oakley Feb 8, 2018
0084f07
fetch user details in bulk every time a room is set or updated
callum-oakley Feb 8, 2018
fea38ce
leaveRoom
callum-oakley Feb 8, 2018
cb9d857
remove room from store on leaveRoom, don't fire removedFromRoom
callum-oakley Feb 9, 2018
b3ec037
add user, remove user
callum-oakley Feb 9, 2018
d0d18f4
remove redundant call to roomStore in user sub handlers
callum-oakley Feb 9, 2018
85eb0dc
basic sendMessage
callum-oakley Feb 9, 2018
e30060a
basic fetch messages
callum-oakley Feb 9, 2018
2cdce74
fetchMessages with extra options
callum-oakley Feb 9, 2018
3889430
decorators and comments about getSync and snapshot
callum-oakley Feb 9, 2018
a77b62a
room subscription
callum-oakley Feb 9, 2018
73b44e9
room subscription
callum-oakley Feb 9, 2018
2b69b86
subscribe to room variations
callum-oakley Feb 9, 2018
234b66d
userJoined and online/offline hooks on room sub
callum-oakley Feb 12, 2018
7133cf0
room level typing indicators
callum-oakley Feb 12, 2018
c93f6b0
room level user left
callum-oakley Feb 12, 2018
4aad1d4
avoid looping forever if no newMessage callback set... -_- all tests …
callum-oakley Feb 12, 2018
076c0ea
join room presence update
callum-oakley Feb 12, 2018
b346530
dist
callum-oakley Feb 12, 2018
d8797e3
clean dist
callum-oakley Feb 12, 2018
806dc1c
encode user ids in path
callum-oakley Feb 13, 2018
d159ae8
remove redundant todos
callum-oakley Feb 13, 2018
8676afc
token cache
callum-oakley Feb 13, 2018
9b3da3a
comments
callum-oakley Feb 13, 2018
7968636
failing test
callum-oakley Feb 13, 2018
21a031e
implicitly join room on subscribe if not already a member
callum-oakley Feb 13, 2018
1b5fcc0
tests
callum-oakley Feb 13, 2018
33f03ce
return room from subscribeToRoom
callum-oakley Feb 13, 2018
da233fe
remove dist
callum-oakley Feb 13, 2018
20fcb36
simplify appendQueryParam
callum-oakley Feb 13, 2018
1b13479
joining -> leaving
callum-oakley Feb 14, 2018
1e3e6d9
encode userId once at construction
callum-oakley Feb 14, 2018
0cf155d
introduce slight delay before calling isTypingIn and leave FIXME for …
callum-oakley Feb 14, 2018
a82a52b
user[CameOnline|WentOffline]InRoom -> user[CameOnline|WentOffline]
callum-oakley Feb 14, 2018
2985a76
callRelevantHooks
callum-oakley Feb 14, 2018
3fc5c70
attachments
callum-oakley Feb 20, 2018
69d7703
dist
callum-oakley Feb 20, 2018
7741724
fetchAttachment
callum-oakley Feb 20, 2018
f0b7b13
publish prerelease to npm (so can remove dist)
callum-oakley Feb 21, 2018
d1f0ad6
data attachment tests
callum-oakley Feb 22, 2018
64e4e61
get and set cursors most basic implementation (fetch every time)
callum-oakley Feb 23, 2018
c81f5fe
cursor store
callum-oakley Feb 26, 2018
11d9b0c
user object for symmetry with rooms, cursors, messages etc
callum-oakley Feb 26, 2018
3db6242
new read cursor hook (own cursors)
callum-oakley Feb 26, 2018
ffc4a29
room level cursor subscriptions
callum-oakley Feb 26, 2018
eca1151
sync readCursor
callum-oakley Feb 26, 2018
ae2e433
initial_state
callum-oakley Feb 27, 2018
c8a8b18
cursor_set -> new_cursor
callum-oakley Feb 28, 2018
79087a2
bundle the message and cursor sub together as a "room sub" (and renam…
callum-oakley Feb 28, 2018
25a7a91
throw an error if readCursors is used to get another's cursor before …
callum-oakley Mar 1, 2018
eedc802
a word
callum-oakley Mar 1, 2018
a2f29b1
0.7.0-alpha.1
callum-oakley Mar 1, 2018
9f34ef8
updateRoom & deleteRoom
callum-oakley Mar 2, 2018
4a41fd1
0.7.0-alpha.2
callum-oakley Mar 5, 2018
7998301
enable fetch data attachment test
callum-oakley Mar 5, 2018
e014749
Use the presence service for presence-related things
hamchapman Mar 5, 2018
0da726e
make switching between environments while testing easier
callum-oakley Mar 5, 2018
f015497
cancel method on subscriptions
callum-oakley Mar 6, 2018
881e223
0.7.0-alpha.3
callum-oakley Mar 7, 2018
5bcc0c3
test userWentOffline hook
callum-oakley Mar 7, 2018
8f08e3b
minimal changes to get existing example working in firefox
callum-oakley Mar 7, 2018
0194589
Bump pusher-platform dependency to 0.15.0 and unskip previously skipp…
hamchapman Mar 7, 2018
2d4a602
Fix CSS in example
hamchapman Mar 8, 2018
89b1d93
don't lint example
callum-oakley Mar 9, 2018
8b86e27
add some missing timeouts
callum-oakley Mar 9, 2018
04c569c
0.7.0-alpha.4
callum-oakley Mar 9, 2018
d7db4dc
only make a single token provider request at a time
callum-oakley Mar 12, 2018
775f144
0.7.0-alpha.5
callum-oakley Mar 12, 2018
42e92d0
queryPrams and headers options for TokenProvider
callum-oakley Mar 9, 2018
bc2f2ab
0.7.0-alpha.6
callum-oakley Mar 12, 2018
8d1d3d1
react native build
callum-oakley Mar 12, 2018
2bad618
inline test babel config
callum-oakley Mar 12, 2018
2787635
addUser -> addUserToRoom; removeUser -> removeUserFromRoom
callum-oakley Mar 13, 2018
c9a696f
functions all recive a single object parameter
callum-oakley Mar 13, 2018
83f1545
append 'on' to all hooks
callum-oakley Mar 13, 2018
f282852
0.7.0-alpha.7
callum-oakley Mar 13, 2018
36b73c5
changelog, 0.7.0
callum-oakley Mar 13, 2018
b77674a
point to @pusher/chatkit in readme
callum-oakley Mar 13, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pull user subscription out in to own class
  • Loading branch information
callum-oakley committed Mar 13, 2018
commit b1790de89c1ff61ca6c67111c1387af2d088973e
68 changes: 11 additions & 57 deletions src/current-user.js
Original file line number Diff line number Diff line change
@@ -15,8 +15,9 @@ import { appendQueryParam } from './utils'
import { Store } from './store'
import { UserStore } from './user-store'
import { RoomStore } from './room-store'
import { parseUser, parseBasicRoom, parsePresenceState } from './parsers'
import { parseUser, parsePresenceState } from './parsers'
import { TypingIndicators } from './typing-indicators'
import { UserSubscription } from './user-subscription'

export class CurrentUser {
constructor ({ id, apiInstance }) {
@@ -55,63 +56,16 @@ export class CurrentUser {

/* internal */

establishUserSubscription = hooks => new Promise((resolve, reject) =>
this.apiInstance.subscribeNonResuming({
path: '/users',
listeners: {
onError: reject,
onEvent: this.onUserEvent({
...hooks,
subscriptionEstablished: resolve
})
}
establishUserSubscription = hooks => {
this.userSubscription = new UserSubscription({ hooks, ...this })
return this.userSubscription.connect().then(({ user, basicRooms }) => {
this.avatarURL = user.avatarURL
this.createdAt = user.createdAt
this.customData = user.customData
this.name = user.name
this.updatedAt = user.updatedAt
this.roomStore.initialize(indexBy(prop('id'), basicRooms))
})
)

onUserEvent = hooks => ({ body }) => {
switch (body.event_name) {
case 'initial_state':
this.onUserInitialState(body.data)
if (hooks.subscriptionEstablished) {
hooks.subscriptionEstablished()
}
break
case 'added_to_room':
// TODO fetch new user details in bulk when added to room (etc)
const basicRoom = parseBasicRoom(body.data.room)
this.roomStore.set(basicRoom.id, basicRoom).then(room => {
if (hooks.addedToRoom) {
hooks.addedToRoom(room)
}
})
break
case 'removed_from_room':
this.roomStore.pop(body.data.room_id).then(room => {
if (hooks.removedFromRoom) {
hooks.removedFromRoom(room)
}
})
break
case 'typing_start': // TODO 'is_typing'
const { room_id: roomId, user_id: userId } = body.data
Promise.all([this.roomStore.get(roomId), this.userStore.get(userId)])
.then(([r, u]) => this.typingIndicators.onIsTyping(r, u, hooks))
break
}
}

onUserInitialState = ({ current_user: currentUser, rooms }) => {
this.avatarURL = currentUser.avatar_url
this.createdAt = currentUser.created_at
this.customData = currentUser.custom_data
this.id = currentUser.id
this.name = currentUser.name
this.updatedAt = currentUser.updated_at
compose(
this.roomStore.initialize,
indexBy(prop('id')),
map(parseBasicRoom)
)(rooms)
}

establishPresenceSubscription = hooks => new Promise((resolve, reject) =>
4 changes: 4 additions & 0 deletions src/room-store.js
Original file line number Diff line number Diff line change
@@ -23,6 +23,10 @@ export class RoomStore {

pop = roomId => this.store.pop(roomId).then(this.decorate)

removeUserFromRoom = (roomId, userId) => this.store.pop(roomId).then(r =>
this.set(roomId, { ...r, userIds: r.userIds.filter(id => id !== userId) })
)

fetchBasicRoom = roomId => {
return this.apiInstance
.request({
2 changes: 1 addition & 1 deletion src/room.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { map } from 'ramda'
import { contains, filter, map, values } from 'ramda'

export class Room {
constructor (basicRoom, userStore) {
72 changes: 72 additions & 0 deletions src/user-subscription.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { map } from 'ramda'

import { parseBasicRoom, parseUser } from './parsers'

export class UserSubscription {
constructor ({ hooks, apiInstance, userStore, roomStore, typingIndicators }) {
this.hooks = hooks
this.apiInstance = apiInstance
this.userStore = userStore
this.roomStore = roomStore
this.typingIndicators = typingIndicators
}

connect = () => new Promise((resolve, reject) => {
this.hooks.subscriptionEstablished = resolve
this.apiInstance.subscribeNonResuming({
path: '/users',
listeners: {
onError: reject,
onEvent: this.onEvent
}
})
})

onEvent = ({ body }) => {
switch (body.event_name) {
case 'initial_state':
this.onInitialState(body.data)
break
case 'added_to_room':
// TODO fetch new user details in bulk when added to room (etc)
const basicRoom = parseBasicRoom(body.data.room)
this.roomStore.set(basicRoom.id, basicRoom).then(room => {
if (this.hooks.addedToRoom) {
this.hooks.addedToRoom(room)
}
})
break
case 'removed_from_room':
this.roomStore.pop(body.data.room_id).then(room => {
if (this.hooks.removedFromRoom) {
this.hooks.removedFromRoom(room)
}
})
break
case 'user_joined':
console.log('user_joined', body.data)
break
case 'user_left':
console.log('user_left', body.data)
// const { room_id: roomId, user_id: userId } = body.data
// this.roomStore.removeUserFromRoom(roomId, userId)
// if (this.hooks.userLeftRoom) {
// Promise.all([this.roomStore.get(roomId), this.userStore.get(userId)])
// .then(([r, u]) => this.hooks.userLeftRoom(r, u))
// }
break
case 'typing_start': // TODO 'is_typing'
const { room_id: roomId, user_id: userId } = body.data
Promise.all([this.roomStore.get(roomId), this.userStore.get(userId)])
.then(([r, u]) => this.typingIndicators.onIsTyping(r, u, this.hooks))
break
}
}

onInitialState = ({ current_user: userData, rooms: roomsData }) => {
this.hooks.subscriptionEstablished({
user: parseUser(userData),
basicRooms: map(parseBasicRoom, roomsData)
})
}
}