8000 PlotlyService.getPlotly now returns a Promise · plotly/angular-plotly.js@4bb45ac · GitHub
[go: up one dir, main page]

Skip to content

Commit 4bb45ac

Browse files
committed
PlotlyService.getPlotly now returns a Promise
1 parent 8cf1d5f commit 4bb45ac

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [3.2.0] - 2021-05-03
4+
### Changed
5+
- PlotlyService.getPlotly now returns a Promise
6+
37
## [3.1.0] - 2021-03-17
48
### Changed
59
- Updated peerDependency to angular >10.0 (see https://github.com/plotly/angular-plotly.js/issues/154)

projects/plotly/src/lib/plotly.service.spec.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@ describe('PlotlyService', () => {
2424

2525
it('should check the plotly dependency', inject([PlotlyService], (service: PlotlyService) => {
2626
(PlotlyService as any).plotly = undefined;
27-
expect(() => service.getPlotly()).toThrowError(`Peer dependency plotly.js isn't installed`);
28-
PlotlyService.setPlotly(PlotlyJS);
27+
28+
return service.getPlotly().catch(err => {
29+
expect(err.message).toBe(`Peer dependency plotly.js isn't installed`);
30+
PlotlyService.setPlotly(PlotlyJS);
31+
});
2932
}));
3033

3134
it('should be created', inject([PlotlyService], (service: PlotlyService) => {
3235
expect(service).toBeTruthy();
3336
}));
3437

35-
it('should return the plotly object', inject([PlotlyService], (service: PlotlyService) => {
36-
expect(service.getPlotly()).toBe(PlotlyJS);
38+
it('should return the plotly object', inject([PlotlyService], async (service: PlotlyService) => {
39+
expect(await service.getPlotly()).toBe(PlotlyJS);
3740
}));
3841

3942
it('should set the module name', () => {
@@ -47,7 +50,7 @@ describe('PlotlyService', () => {
4750

4851
it('should call plotly.newPlot method', inject([PlotlyService], async (service: PlotlyService) => {
4952
return new Promise<void>(async (resolve) => {
50-
const plotly = service.getPlotly();
53+
const plotly = await (service as any).getPlotly();
5154
PlotlyService.setPlotly('waiting');
5255

5356
setTimeout(() => PlotlyService.setPlotly(PlotlyJS), 100);
@@ -62,8 +65,8 @@ describe('PlotlyService', () => {
6265
});
6366
}));
6467

65-
it('should call plotly.plot method', inject([PlotlyService], (service: PlotlyService) => {
66-
const plotly = service.getPlotly();
68+
it('should call plotly.plot method', inject([PlotlyService], async (service: PlotlyService) => {
69+
const plotly = await service.getPlotly();
6770

6871
spyOn(plotly, 'plot').and.returnValue(new Promise(() => {}));
6972
service.plot('one' as any, 'two' as any, 'three' as any, 'four' as any);
@@ -73,8 +76,8 @@ describe('PlotlyService', () => {
7376
expect(plotly.plot).toHaveBeenCalledWith('one', {data: 'two', layout: 'three', config: 'four', frames: 'five'});
7477
}));
7578

76-
it('should call plotly.update method', inject([PlotlyService], (service: PlotlyService) => {
77-
const plotly = service.getPlotly();
79+
it('should call plotly.update method', inject([PlotlyService], async (service: PlotlyService) => {
80+
const plotly = await service.getPlotly();
7881

7982
spyOn(plotly, 'react').and.returnValue(new Promise(() => {}));
8083
service.update('one' as any, 'two' as any, 'three' as any, 'four' as any);
@@ -84,8 +87,8 @@ describe('PlotlyService', () => {
8487
expect(plotly.react).toHaveBeenCalledWith('one', {data: 'two', layout: 'three', config: 'four', frames: 'five'});
8588
}));
8689

87-
it('should call plotly.Plots.resize method', inject([PlotlyService], (service: PlotlyService) => {
88-
const plotly = service.getPlotly();
90+
it('should call plotly.Plots.resize method', inject([PlotlyService], async (service: PlotlyService) => {
91+
const plotly = await service.getPlotly();
8992

9093
spyOn(plotly.Plots, 'resize').and.returnValue(new Promise(() => {}));
9194
service.resize('one' as any);

projects/plotly/src/lib/plotly.service.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ export class PlotlyService {
5353
return undefined;
5454
}
5555

56-
public getPlotly(): any {
56+
public async getPlotly(): Promise<any> {
57+
await this.waitFor(() => this._getPlotly() !== 'waiting');
58+
return this._getPlotly();
59+
}
60+
61+
protected _getPlotly(): any {
5762
if (typeof PlotlyService.plotly === 'undefined') {
5863
const msg = PlotlyService.moduleName === 'ViaCDN'
5964
? `Error loading Peer dependency plotly.js from CDN url`
@@ -77,36 +82,36 @@ export class PlotlyService {
7782

7883
// tslint:disable max-line-length
7984
public async newPlot(div: HTMLDivElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>, frames?: Partial<Plotly.Config>[]): Promise<any> {
80-
await this.waitFor(() => this.getPlotly() !== 'waiting');
85+
await this.waitFor(() => this._getPlotly() !== 'waiting');
8186

8287
if (frames) {
8388
const obj = {data, layout, config, frames};
84-
return this.getPlotly().newPlot(div, obj).then(() => PlotlyService.insert(div as any)) as Promise<any>;
89+
return this._getPlotly().newPlot(div, obj).then(() => PlotlyService.insert(div as any)) as Promise<any>;
8590
}
8691

87-
return this.getPlotly().newPlot(div, data, layout, config).then(() => PlotlyService.insert(div as any)) as Promise<any>;
92+
return this._getPlotly().newPlot(div, data, layout, config).then(() => PlotlyService.insert(div as any)) as Promise<any>;
8893
}
8994

9095
public plot(div: Plotly.PlotlyHTMLElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>, frames?: Partial<Plotly.Config>[]): Promise<any> {
9196
if (frames) {
9297
const obj = {data, layout, config, frames};
93-
return this.getPlotly().plot(div, obj) as Promise<any>;
98+
return this._getPlotly().plot(div, obj) as Promise<any>;
9499
}
95100

96-
return this.getPlotly().plot(div, data, layout, config) as Promise<any>;
101+
return this._getPlotly().plot(div, data, layout, config) as Promise<any>;
97102
}
98103

99104
public update(div: Plotly.PlotlyHTMLElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>, frames?: Partial<Plotly.Config>[]): Promise<any> {
100105
if (frames) {
101106
const obj = {data, layout, config, frames};
102-
return this.getPlotly().react(div, obj) as Promise<any>;
107+
return this._getPlotly().react(div, obj) as Promise<any>;
103108
}
104109

105-
return this.getPlotly().react(div, data, layout, config) as Promise<any>;
110+
return this._getPlotly().react(div, data, layout, config) as Promise<any>;
106111
}
107112
// tslint:enable max-line-length
108113

109114
public resize(div: Plotly.PlotlyHTMLElement): void {
110-
return this.getPlotly().Plots.resize(div);
115+
return this._getPlotly().Plots.resize(div);
111116
}
112117
}

0 commit comments

Comments
 (0)
0