@@ -4,6 +4,7 @@ import { DockerEvent } from 'dockerode';
44import { switchMap , takeUntil , startWith } from 'rxjs/operators' ;
55import { DockerService } from './docker.service' ;
66import { streamToObservable } from './stream-to-observable' ;
7+ import { ElectronService } from '../electron-tools/electron.service' ;
78
89@Injectable ( )
910export class DockerEventsService implements OnDestroy {
@@ -12,25 +13,29 @@ export class DockerEventsService implements OnDestroy {
1213
1314 private disposed = new Subject ( ) ;
1415
15- constructor ( dockerService : DockerService ,
16- ngZone : NgZone ) {
16+ private unbinded = new Subject ( ) ;
1717
18- dockerService . docker ( d => d . getEvents ( ) )
19- . pipe (
20- switchMap ( stream => streamToObservable < Buffer > ( stream , ngZone ) ) ,
21- takeUntil ( this . disposed )
22- )
23- . subscribe ( chunk => {
24- const decoded = JSON . parse ( chunk . toString ( 'utf8' ) ) as DockerEvent ;
25- if ( this . events . has ( decoded . Action ) ) {
26- this . events . get ( decoded . Action ) . next ( decoded ) ;
27- }
18+ constructor ( private dockerService : DockerService ,
19+ private ngZone : NgZone ,
20+ electron : ElectronService ) {
2821
29- const combined = `${ decoded . Type } .${ decoded . Action } ` ;
30- if ( this . events . has ( combined ) ) {
31- this . events . get ( combined ) . next ( decoded ) ;
22+ const monitor = electron . remote . powerMonitor ;
23+
24+ // Bind and unbind when the machine suspends/resumes.
25+ monitor . on ( 'suspend' , ( ) => {
26+ this . unbindFromDocker ( ) ;
27+ } ) ;
28+
29+ monitor . on ( 'resume' , ( ) => {
30+ setTimeout ( ( ) => {
31+ if ( ! this . unbinded . closed ) {
32+ this . unbindFromDocker ( ) ;
3233 }
33- } ) ;
34+ this . bindToDocker ( ) ;
35+ } , 2000 ) ;
36+ } ) ;
37+
38+ this . bindToDocker ( ) ;
3439 }
3540
3641 public ngOnDestroy ( ) {
@@ -51,4 +56,30 @@ export class DockerEventsService implements OnDestroy {
5156 public bindAll ( events : string [ ] , type ?: string ) {
5257 return combineLatest ( events . map ( e => this . bind ( type ? `${ type } .${ e } ` : e ) . pipe ( startWith ( null ) ) ) ) ;
5358 }
59+
60+ private unbindFromDocker ( ) {
61+ this . unbinded . next ( ) ;
62+ this . unbinded . unsubscribe ( ) ;
63+ }
64+
65+ private bindToDocker ( ) {
66+ this . unbinded = new Subject < any > ( ) ;
67+ this . dockerService . docker ( d => d . getEvents ( ) )
68+ . pipe (
69+ switchMap ( stream => streamToObservable < Buffer > ( stream , this . ngZone ) ) ,
70+ takeUntil ( this . disposed ) ,
71+ takeUntil ( this . unbinded ) ,
72+ )
73+ . subscribe ( chunk => {
74+ const decoded = JSON . parse ( chunk . toString ( 'utf8' ) ) as DockerEvent ;
75+ if ( this . events . has ( decoded . Action ) ) {
76+ this . events . get ( decoded . Action ) . next ( decoded ) ;
77+ }
78+
79+ const combined = `${ decoded . Type } .${ decoded . Action } ` ;
80+ if ( this . events . has ( combined ) ) {
81+ this . events . get ( combined ) . next ( decoded ) ;
82+ }
83+ } ) ;
84+ }
5485}
0 commit comments