Skip to content

Commit be5b978

Browse files
committed
Only show 'reconnecting...' message when we're reconnecting live
1 parent ab4e8c6 commit be5b978

File tree

7 files changed

+50
-4
lines changed

7 files changed

+50
-4
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ResolveState } from 'overmind';
2+
import { Reaction, Config } from '..';
3+
4+
export default (() => {
5+
let _reaction: Reaction;
6+
7+
return {
8+
initialize(reaction: Reaction) {
9+
_reaction = reaction;
10+
},
11+
waitUntil(
12+
test: (state: ResolveState<Config['state']>) => boolean
13+
): Promise<boolean> {
14+
return new Promise(resolve => {
15+
_reaction(test, bool => {
16+
if (bool === true) {
17+
resolve();
18+
}
19+
});
20+
});
21+
},
22+
};
23+
})();

packages/app/src/app/overmind/effects/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ export { default as executor } from './executor';
2525
export { default as stripe } from './stripe';
2626
export { default as jwt } from './jwt';
2727
export { default as preview } from './preview';
28+
export { default as flows } from './flows';

packages/app/src/app/overmind/effects/notificationToast.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
export default {
1111
convertTypeToStatus,
1212
add(notification: NotificationMessage) {
13-
notificationState.addNotification(notification);
13+
return notificationState.addNotification(notification);
14+
},
15+
remove(id: string) {
16+
notificationState.removeNotification(id);
1417
},
1518
error(message: string) {
1619
notificationState.addNotification({

packages/app/src/app/overmind/namespaces/live/liveMessageOperators.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,16 @@ export const onOperation: Operator<LiveMessage<{
460460
});
461461

462462
export const onConnectionLoss: Operator<LiveMessage> = mutate(
463-
({ state, effects }) => {
463+
async ({ state, effects }) => {
464464
if (!state.live.reconnecting) {
465-
effects.notificationToast.add({
465+
const id = effects.notificationToast.add({
466466
message: 'We lost connection with the live server, reconnecting...',
467467
status: NotificationStatus.ERROR,
468468
});
469469
state.live.reconnecting = true;
470+
471+
await effects.flows.waitUntil(s => s.live.reconnecting === false);
472+
effects.notificationToast.remove(id);
470473
}
471474
}
472475
);

packages/app/src/app/overmind/onInitialize.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export const onInitialize: OnInitialize = async (
1717
onApplyOperation: actions.live.applyTransformation,
1818
});
1919

20+
effects.flows.initialize(overmindInstance.reaction);
21+
2022
effects.keybindingManager.initialize(overmindInstance);
2123

2224
effects.api.initialize({

packages/notifications/src/component/Toasts.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const DEFAULT_COLORS = {
7171
};
7272

7373
const DEFAULT_BUTTON: IButtonType = props =>
74-
React.createElement('button', props);
74+
React.createElement('button', props); // eslint-disable-line
7575

7676
export function Toasts({
7777
state,
@@ -150,6 +150,18 @@ export function Toasts({
150150
};
151151
}, [state]);
152152

153+
React.useEffect(() => {
154+
const removeListener = state.onNotificationRemoved(event => {
155+
setNotificationsToShow(notifications =>
156+
notifications.filter(n => n.id !== event.id)
157+
);
158+
});
159+
160+
return () => {
161+
removeListener();
162+
};
163+
}, [state]);
164+
153165
const transitions = useTransition<
154166
NotificationToast,
155167
{ overflow: string; opacity: number; height: number }

packages/notifications/src/state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ export class NotificationState {
5555
private readonly _onNotificationUpdated = new Emitter<
5656
NotificationUpdatedEvent
5757
>();
58+
5859
private readonly _onNotificationRemoved = new Emitter<
5960
NotificationRemovedEvent
6061
>();
62+
6163
onNotificationUpdated = this._onNotificationUpdated.event;
6264
onNotificationRemoved = this._onNotificationRemoved.event;
6365

0 commit comments

Comments
 (0)