-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #657 from silx-kit/provider-files
Move base `ProviderApi` in its own file and rename providers' API files
- Loading branch information
Showing
11 changed files
with
89 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import axios, { | ||
AxiosInstance, | ||
AxiosRequestConfig, | ||
AxiosResponse, | ||
CancelTokenSource, | ||
} from 'axios'; | ||
import { Entity, ProviderError, ValueRequestParams } from './models'; | ||
|
||
interface ValueRequest { | ||
params: ValueRequestParams; | ||
cancelSource: CancelTokenSource; | ||
} | ||
|
||
export abstract class ProviderApi { | ||
public readonly filepath: string; | ||
public readonly cancelledValueRequests = new Set<ValueRequest>(); | ||
|
||
protected readonly client: AxiosInstance; | ||
protected readonly valueRequests = new Set<ValueRequest>(); | ||
|
||
public constructor(filepath: string, config?: AxiosRequestConfig) { | ||
this.filepath = filepath; | ||
this.client = axios.create(config); | ||
} | ||
|
||
public cancelValueRequests(): void { | ||
// Cancel every active value request | ||
this.valueRequests.forEach((request) => { | ||
request.cancelSource.cancel(ProviderError.Cancelled); | ||
|
||
// Save request so params can later be evicted from the values store (cf. `Provider.tsx`) | ||
this.cancelledValueRequests.add(request); | ||
}); | ||
|
||
this.valueRequests.clear(); | ||
} | ||
|
||
protected async cancellableFetchValue<T>( | ||
endpoint: string, | ||
params: ValueRequestParams | ||
): Promise<AxiosResponse<T>> { | ||
const cancelSource = axios.CancelToken.source(); | ||
const request = { params, cancelSource }; | ||
this.valueRequests.add(request); | ||
|
||
try { | ||
const { token: cancelToken } = cancelSource; | ||
return await this.client.get<T>(endpoint, { cancelToken }); | ||
} finally { | ||
// Remove cancellation source when request fulfills | ||
this.valueRequests.delete(request); | ||
} | ||
} | ||
|
||
public abstract getEntity(path: string): Promise<Entity>; | ||
public abstract getValue(params: ValueRequestParams): Promise<unknown>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 8 additions & 2 deletions
10
src/h5web/providers/jupyter/api.ts → src/h5web/providers/jupyter/jupyter-api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
src/h5web/providers/jupyter/devApi.ts → ...5web/providers/jupyter/jupyter-dev-api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters