Created
November 25, 2019 11:55
-
-
Save sonnyp/bb48b9bba1817df97911db2abc76eb44 to your computer and use it in GitHub Desktop.
XMPP CSI on React Native https://xmpp.org/extensions/xep-0352.html
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
import {AppState} from 'react-native'; | |
import xml from './xmpp/packages/xml'; | |
// https://xmpp.org/extensions/xep-0352.html | |
// https://facebook.github.io/react-native/docs/appstate | |
module.exports = function csi({entity}) { | |
let supported = false; | |
let inactive = false; | |
function processAppState(state) { | |
if (!supported) return; | |
if (state === 'background') { | |
if (inactive === true) return; | |
entity.send(xml('inactive', {xmlns: 'urn:xmpp:csi:0'})); | |
inactive = true; | |
} else { | |
if (inactive === false) return; | |
entity.send(xml('active', {xmlns: 'urn:xmpp:csi:0'})); | |
inactive = false; | |
} | |
} | |
AppState.addEventListener('change', processAppState); | |
entity.on('nonza', nonza => { | |
if (!nonza.is('features', 'http://etherx.jabber.org/streams')) return; | |
supported = !!nonza.getChild('csi', 'urn:xmpp:csi:0'); | |
}); | |
entity.on('online', () => { | |
processAppState(AppState.currentState); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment