Skip to content

Commit 7d72eec

Browse files
committed
fix #139 change from Promise to Observable
1 parent 09567d5 commit 7d72eec

6 files changed

+40
-28
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [112](https://github.com/mseemann/angular2-mdl/issues/112) (ViewEncapsulation.None for all components)
1212
* [111](https://github.com/mseemann/angular2-mdl/issues/111) (forRoot module)
1313
* [131](https://github.com/mseemann/angular2-mdl/issues/131) (Dialog design)
14+
* [139](https://github.com/mseemann/angular2-mdl/issues/139) (change return types from Promise to Observable)
1415

1516
### Breaking Changes
1617
* The distribution structure has change:
@@ -22,9 +23,9 @@
2223
- use `MdlDialogOutletService` and `setDefaultViewContainerRef`
2324
- use the `<dialog-outlet></dialog-outlet>` component. This component can be anywhere in your html site. The best place is the last child of the `body` element. (see https://github.com/mseemann/angular2-mdl/wiki/How-to-use-the-MdlDialogService for more information)
2425
- The possibility to set a `ViewContainerRef` for each dialog or snackbar has been removed.
25-
- `alert` and `confirm` return an `Observable` and no longer a `Promise` (the enum `ConfirmResult` has been removed)
26+
- `alert` and `confirm` , `showDialog*` return an `Observable` and no longer a `Promise` (the enum `ConfirmResult` has been removed)
2627
* This version now supports AOT - but you should be aware that AOT seems not to be ready for production. AOT behaves in many cases different then JIT. (see https://github.com/angular/angular/issues/11606)
27-
28+
* The MdlSnachbarService returns now an Observable and no longer a Promise. Just change from `then` to `subscribe`.
2829

2930
<a name="1.8.2"></a>
3031
# 1.8.2 (2016-10-06)

src/lib/components/dialog/mdl-dialog.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('MdlDialog (embedded/declarative)', () => {
2929
spyOn(fixture.componentInstance, 'onDialogShow').and.callThrough();
3030
spyOn(fixture.componentInstance, 'onDialogHide');
3131

32-
dialog.show().then( () => {
32+
dialog.show().subscribe( () => {
3333

3434
dialog.close();
3535

src/lib/components/dialog/mdl-dialog.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@angular/core';
99
import { MdlDialogService, MdlDialogReference } from './mdl-dialog.service';
1010
import { BooleanProperty } from './../common/boolean-property';
11+
import { Observable } from 'rxjs';
1112

1213

1314

@@ -33,15 +34,15 @@ export class MdlDialogComponent {
3334
constructor(private dialogService: MdlDialogService) {}
3435

3536

36-
public show(): Promise<MdlDialogReference> {
37+
public show(): Observable<MdlDialogReference> {
3738

3839
if(this.isShown){
3940
throw new Error('Only one instance of an embedded mdl-dialog can exist!');
4041
}
4142
this.isShown = true;
4243

4344
let p = this.dialogService.showDialogTemplate(this.template, {isModal: this.modal});
44-
p.then( (dialogRef: MdlDialogReference) => {
45+
p.subscribe( (dialogRef: MdlDialogReference) => {
4546

4647
this.dialogRef = dialogRef;
4748
this.showEmitter.emit(dialogRef);

src/lib/components/dialog/mdl-dialog.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export class MdlDialogService {
136136
/**
137137
* Shows a dialog that is specified by the provided configuration.
138138
* @param config The simple dialog configuration.
139-
* @returns A promise that returns the MdlDialogReference.
139+
* @returns An Observable that returns the MdlDialogReference.
140140
*/
141141
public showDialog(config: IMdlSimpleDialogConfiguration): Observable<MdlDialogReference> {
142142

@@ -186,7 +186,7 @@ export class MdlDialogService {
186186
return Observable.of(internalDialogRef.dialogRef);
187187
}
188188

189-
public showDialogTemplate(template: TemplateRef<any>, config: IMdlDialogConfiguration): Promise<MdlDialogReference> {
189+
public showDialogTemplate(template: TemplateRef<any>, config: IMdlDialogConfiguration): Observable<MdlDialogReference> {
190190

191191
let internalDialogRef = new InternalMdlDialogReference();
192192

@@ -197,7 +197,7 @@ export class MdlDialogService {
197197

198198
hostComponentRef.instance.dialogTarget.createEmbeddedView(template);
199199

200-
return Promise.resolve(internalDialogRef.dialogRef);
200+
return Observable.of(internalDialogRef.dialogRef);
201201
}
202202

203203

src/lib/components/snackbar/mdl-snackbar.service.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('Service: MdlSnackbar', () => {
4343
});
4444

4545
fixture.detectChanges();
46-
p.then( (mdlSnackbarComponent) => {
46+
p.subscribe( (mdlSnackbarComponent) => {
4747

4848
expect(mdlSnackbarComponent.isActive()).toBe(true);
4949
mdlSnackbarComponent.onClick();
@@ -62,7 +62,7 @@ describe('Service: MdlSnackbar', () => {
6262

6363
fixture.detectChanges();
6464

65-
p.then( (mdlSnackbarComponent) => {
65+
p.subscribe( (mdlSnackbarComponent) => {
6666

6767
expect(mdlSnackbarComponent.isActive()).toBe(true);
6868

src/lib/components/snackbar/mdl-snackbar.service.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { CommonModule } from '@angular/common';
1212
import { MdlDialogOutletService } from '../dialog-outlet/mdl-dialog-outlet.service';
1313
import { MdlDialogOutletModule } from '../dialog-outlet/index';
14+
import { Observable, Subject } from 'rxjs';
1415

1516

1617
const ANIMATION_TIME = 250;
@@ -39,29 +40,35 @@ export class MdlSnackbarComponent {
3940
return this.showIt;
4041
}
4142

42-
public show(): Promise<void> {
43-
44-
return new Promise<void>((resolve, reject) => {
43+
public show(): Observable<void> {
44+
let result: Subject<any> = new Subject();
4545
// wait unit the dom is in place - then showIt will change the css class
4646
setTimeout(() => {
4747
this.showIt = true;
4848
// fire after the view animation is done
4949
setTimeout(() => {
50-
resolve();
50+
result.next(null);
51+
result.complete();
5152
}, ANIMATION_TIME);
5253
}, 10);
53-
});
5454

55+
56+
return result.asObservable();
5557
}
5658

57-
public hide(): Promise<void> {
59+
public hide(): Observable<void> {
5860
this.showIt = false;
59-
return new Promise<void>(function(resolve, reject) {
60-
// fire after the view animation is done
61-
setTimeout(() => {
62-
resolve();
63-
}, ANIMATION_TIME);
64-
});
61+
62+
let result: Subject<any> = new Subject();
63+
64+
// fire after the view animation is done
65+
setTimeout(() => {
66+
result.next(null);
67+
result.complete();
68+
}, ANIMATION_TIME);
69+
70+
71+
return result.asObservable();
6572
}
6673
}
6774

@@ -88,14 +95,14 @@ export class MdlSnackbarService {
8895
}
8996

9097

91-
public showToast(message: string, timeout?: number, vcRef?: ViewContainerRef): Promise<MdlSnackbarComponent> {
98+
public showToast(message: string, timeout?: number, vcRef?: ViewContainerRef): Observable<MdlSnackbarComponent> {
9299
return this.showSnackbar({
93100
message: message,
94101
timeout: timeout
95102
});
96103
}
97104

98-
public showSnackbar(snackbarMessage: IMdlSnackbarMessage): Promise<MdlSnackbarComponent> {
105+
public showSnackbar(snackbarMessage: IMdlSnackbarMessage): Observable<MdlSnackbarComponent> {
99106

100107
let optTimeout = snackbarMessage.timeout || 2750;
101108
let viewContainerRef = this.dialogOutletService.viewContainerRef;
@@ -118,22 +125,25 @@ export class MdlSnackbarService {
118125
if (snackbarMessage.action) {
119126
mdlSnackbarComponent.actionText = snackbarMessage.action.text;
120127
mdlSnackbarComponent.onAction = () => {
121-
mdlSnackbarComponent.hide().then(() => {
128+
mdlSnackbarComponent.hide().subscribe(() => {
122129
cRef.destroy();
123130
snackbarMessage.action.handler();
124131
});
125132
};
126133
} else {
127134
setTimeout( () => {
128-
mdlSnackbarComponent.hide().then(() => {cRef.destroy(); });
135+
mdlSnackbarComponent.hide().subscribe(() => {cRef.destroy(); });
129136
}, optTimeout);
130137
}
131138

139+
let result: Subject<MdlSnackbarComponent> = new Subject<MdlSnackbarComponent>();
132140

133-
return mdlSnackbarComponent.show().then( () => {
134-
return mdlSnackbarComponent;
141+
mdlSnackbarComponent.show().subscribe( () => {
142+
result.next(mdlSnackbarComponent);
143+
result.complete();
135144
});
136145

146+
return result.asObservable();
137147
}
138148
}
139149

0 commit comments

Comments
 (0)