Skip to content

Instantly share code, notes, and snippets.

@sonnyp
Created November 25, 2019 11:55
Show Gist options
  • Save sonnyp/bb48b9bba1817df97911db2abc76eb44 to your computer and use it in GitHub Desktop.
Save sonnyp/bb48b9bba1817df97911db2abc76eb44 to your computer and use it in GitHub Desktop.
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