Skip to content

Commit

Permalink
fix(rn): subscribers generator and subscriptions
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Sep 12, 2018
1 parent 298ab73 commit 1ce66be
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 67 deletions.
13 changes: 7 additions & 6 deletions client/react-native/common/components/App.js
Expand Up @@ -9,27 +9,28 @@ export default class App extends PureComponent {
async componentDidMount () {
try {
await CoreModule.start()
subscriptions.EventStream.subscribe({
subscriptions.eventStream.subscribe({
iterator: function * () {
try {
while (true) {
console.log(yield)
console.log('EventStream: ', yield)
}
} catch (error) {
console.error(error)
console.error('EventStream: ', error)
}
console.log('EventStream: return')
},
updater: undefined,
})
subscriptions.EventStream.start()
subscriptions.eventStream.start()
} catch (err) {
console.error(err)
subscriptions.EventStream.dispose()
subscriptions.eventStream.dispose()
}
}

componentWillUnmount () {
subscriptions.EventStream.dispose()
subscriptions.eventStream.dispose()
}

render () {
Expand Down
Expand Up @@ -46,11 +46,8 @@ const Item = ({
)

export default class Request extends PureComponent {
subscriber = null
retry = null

componentDidMount () {
this.subscriber = subscriptions.ContactRequest.subscribe({
this.subscriber = subscriptions.contactRequest.subscribe({
updater: (store, data) => this.retry && this.retry(),
})
}
Expand Down
Expand Up @@ -10,7 +10,7 @@ export default class RequestValidation extends PureComponent {
acceptRequest = async () => {
try {
const contactID = this.props.navigation.getParam('id')
await mutations.contactAcceptRequest.commit({ contactID })
await mutations.contactRequestAccepted.commit({ contactID })
this.props.navigation.goBack(null)
} catch (err) {
console.error(err)
Expand Down
57 changes: 38 additions & 19 deletions client/react-native/common/components/Screens/Contacts/List.js
Expand Up @@ -17,7 +17,7 @@ import {
paddingBottom,
} from '../../../styles'
import { QueryReducer } from '../../../relay'
import { queries } from '../../../graphql'
import { queries, subscriptions } from '../../../graphql'

// TODO: implement pagination

Expand Down Expand Up @@ -121,29 +121,48 @@ export default class List extends PureComponent {
})
}

componentDidMount () {
this.subscribers = [
subscriptions.contactRequest.subscribe({
updater: (store, data) => this.retry && this.retry(),
}),
subscriptions.contactRequestAccepted.subscribe({
updater: (store, data) => this.retry && this.retry(),
}),
]
}

componentWillUnmount () {
this.subscribers.forEach(subscriber => subscriber.unsubscribe())
}

render () {
const { navigation } = this.props
return (
<Screen style={[{ backgroundColor: colors.white }]}>
<QueryReducer query={queries.ContactList}>
{(state, retry) => (
<FlatList
data={this.sortContacts([].concat(state.data.ContactList || []))}
ItemSeparatorComponent={({ highlighted }) => (
<Separator highlighted={highlighted} />
)}
refreshing={state.type === state.loading}
onRefresh={retry}
renderItem={data => (
<Item
key={data.id}
data={data.item}
separators={data.separators}
navigation={navigation}
/>
)}
/>
)}
{(state, retry) =>
(this.retry = retry) && (
<FlatList
data={this.sortContacts(
[].concat(state.data.ContactList || [])
)}
ItemSeparatorComponent={({ highlighted }) => (
<Separator highlighted={highlighted} />
)}
refreshing={state.type === state.loading}
onRefresh={retry}
renderItem={data => (
<Item
key={data.id}
data={data.item}
separators={data.separators}
navigation={navigation}
/>
)}
/>
)
}
</QueryReducer>
</Screen>
)
Expand Down
4 changes: 2 additions & 2 deletions client/react-native/common/graphql/mutations/index.js
@@ -1,2 +1,2 @@
export contactRequest from './contactRequest'
export contactAcceptRequest from './contactAcceptRequest'
export contactRequest from './ContactRequest'
export contactAcceptRequest from './ContactAcceptRequest'
Expand Up @@ -10,8 +10,8 @@ export default {
try {
while (true) {
const response = yield
if (response.kind === 'ContactRequest') {
iterator.next(response)
if (response.EventStream.kind === 'ContactRequest') {
iterator.next(response.EventStream)
}
}
} catch (error) {
Expand All @@ -22,8 +22,8 @@ export default {
updater:
updater &&
((store, data) => {
if (data.kind === 'ContactRequest') {
return updater(store, data)
if (data.EventStream.kind === 'ContactRequest') {
return updater(store, data.EventStream)
}
}),
}),
Expand Down
Expand Up @@ -10,8 +10,8 @@ export default {
try {
while (true) {
const response = yield
if (response.kind === 'ContactAcceptRequest') {
iterator.next(response)
if (response.EventStream.kind === 'ContactRequestAccepted') {
iterator.next(response.EventStream)
}
}
} catch (error) {
Expand All @@ -22,8 +22,8 @@ export default {
updater:
updater &&
((store, data) => {
if (data.kind === 'ContactAcceptRequest') {
return updater(store, data)
if (data.EventStream.kind === 'ContactRequestAccepted') {
return updater(store, data.EventStream)
}
}),
}),
Expand Down
5 changes: 3 additions & 2 deletions client/react-native/common/graphql/subscriptions/index.js
@@ -1,2 +1,3 @@
export EventStream from './EventStream'
export ContactRequest from './ContactRequest'
export eventStream from './EventStream'
export contactRequest from './ContactRequest'
export contactRequestAccepted from './ContactRequestAccepted'
2 changes: 0 additions & 2 deletions client/react-native/common/relay/environment.js
Expand Up @@ -49,7 +49,6 @@ let getIP = () =>

export const fetchQuery = async (operation, variables) => {
try {
console.log(variables)
const port = await CoreModule.getPort()
const response = await fetch(`http://${await getIP()}:${port}/query`, {
method: 'POST',
Expand All @@ -65,7 +64,6 @@ export const fetchQuery = async (operation, variables) => {
return await response.json()
} catch (err) {
console.error(err)
console.log(err)
}
}

Expand Down
23 changes: 12 additions & 11 deletions client/react-native/common/relay/subscriber.js
Expand Up @@ -2,7 +2,7 @@ import environment from './environment'
import { requestSubscription } from 'react-relay'

export default ({ subscription, iterators = [], updaters = [] }) => {
let _iterators = iterators
let _generators = iterators
let _updaters = updaters

let dispose = () => {}
Expand All @@ -11,24 +11,25 @@ export default ({ subscription, iterators = [], updaters = [] }) => {
dispose = requestSubscription(environment, {
subscription,
onNext: response =>
_iterators.forEach(iterator => iterator.next(response)),
onError: error => _iterators.forEach(iterator => iterator.throw(error)),
onCompleted: () => _iterators.forEach(iterator => iterator.return()),
updater: (store, data) =>
_updaters.forEach(updater => updater(store, data)),
_generators.forEach(generator => generator.next(response)),
onError: error =>
_generators.forEach(generator => generator.throw(error)),
onCompleted: () => _generators.forEach(generator => generator.return()),
updater: (store, data) => _updaters.forEach(updater => updater(store, data)),
}).dispose
return { dispose }
}

const subscribe = ({ updater, iterator }) => {
iterator && _iterators.push(iterator)
const generator = iterator && iterator()
iterator && _generators.push(generator)
updater && _updaters.push(updater)

generator && generator.next()
return {
unsubscribe: () => {
_iterators = iterator
? _iterators.filter(_ => _ !== iterator)
: _iterators
_generators = iterator
? _generators.filter(_ => _ !== iterator)
: _generators
_updaters = updater ? _updaters.filter(_ => _ !== updater) : _updaters
},
}
Expand Down
13 changes: 1 addition & 12 deletions core/api/node/graphql/resolver.go
Expand Up @@ -480,18 +480,7 @@ func (r *subscriptionResolver) EventStream(ctx context.Context, kind *string, co
break
}

gid := &globalID{
Kind: EventKind,
ID: entry.ID,
}

ret := &model.BertyP2pEvent{
ID: gid.String(),
SenderID: &entry.SenderID,
ConversationID: &entry.ConversationID,
}

ce <- ret
ce <- convertEvent(entry)
}
}()

Expand Down

0 comments on commit 1ce66be

Please sign in to comment.