Background events

Summary:
`this.client.subscribe` was used to listen for navigation events. This means, navigation events are not collected while the plugin is in background.
In this diff, this is changed to a persistedStateReducer, so the events are collected with the plugin not active.

Reviewed By: jknoxville

Differential Revision: D17419668

fbshipit-source-id: 88d9476cb7461ff6774d42a992d32b4c8948ac86
This commit is contained in:
Daniel Büchele
2019-09-17 10:15:33 -07:00
committed by Facebook Github Bot
parent 1d6fc9e3ac
commit 1666cf5ee5

View File

@@ -59,45 +59,45 @@ export default class extends FlipperPlugin<State, any, PersistedState> {
static persistedStateReducer = ( static persistedStateReducer = (
persistedState: PersistedState, persistedState: PersistedState,
method: string, method: string,
payload: any,
) => { ) => {
switch (method) { switch (method) {
default: case 'nav_event':
return {
...persistedState,
};
}
};
subscribeToNavigationEvents = () => {
this.client.subscribe('nav_event', payload => {
let {persistedState} = this.props;
const {setPersistedState} = this.props;
const navigationEvent: NavigationEvent = { const navigationEvent: NavigationEvent = {
uri: payload.uri === undefined ? null : decodeURIComponent(payload.uri), uri:
payload.uri === undefined ? null : decodeURIComponent(payload.uri),
date: new Date(payload.date) || new Date(), date: new Date(payload.date) || new Date(),
className: payload.class === undefined ? null : payload.class, className: payload.class === undefined ? null : payload.class,
screenshot: null, screenshot: null,
}; };
setPersistedState({
return {
...persistedState, ...persistedState,
currentURI: currentURI:
payload.uri == null navigationEvent.uri == null
? persistedState.currentURI ? persistedState.currentURI
: decodeURIComponent(payload.uri), : decodeURIComponent(navigationEvent.uri),
navigationEvents: [navigationEvent, ...persistedState.navigationEvents], navigationEvents: [
}); navigationEvent,
...persistedState.navigationEvents,
],
};
default:
return persistedState;
}
};
subscribeToNavigationEvents = () => {
this.client.subscribe('nav_event', () =>
// Wait for view to render and then take a screenshot // Wait for view to render and then take a screenshot
setTimeout(() => { setTimeout(async () => {
persistedState = this.props.persistedState; const device = await this.getDevice();
this.getDevice() const screenshot = await device.screenshot();
.then(device => device.screenshot()) const blobURL = URL.createObjectURL(bufferToBlob(screenshot));
.then((buffer: Buffer) => { this.props.persistedState.navigationEvents[0].screenshot = blobURL;
const blobURL = URL.createObjectURL(bufferToBlob(buffer)); this.props.setPersistedState({...this.props.persistedState});
navigationEvent.screenshot = blobURL; }, 1000),
setPersistedState({...persistedState}); );
});
}, 1000);
});
}; };
componentDidMount = () => { componentDidMount = () => {