Skip to content

Commit 6bcc799

Browse files
committed
feat(): add clearCache method
1 parent dbb43af commit 6bcc799

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,15 @@ export class MyMainAppComponent {
9393

9494
// disable spinners by default, you can add [spinner]="true" to a specific component instance later on to override this
9595
imageLoaderConfig.enableSpinner(false);
96+
97+
// set the maximum concurrent connections to 10
98+
imageLoaderConfig.setConcurrency(10);
9699
}
97100

98101
}
99102
```
100103

101-
Below are all the methods that the provider has:
104+
Below are all the methods that the config provider has:
102105

103106
#### enableDebugMode()
104107
Enables debug mode to receive console logs, errors, warnings.
@@ -134,6 +137,21 @@ Set a custom directory name to store the cached images in.
134137
#### setConcurrency(concurrency: number)
135138
Set the maximum number of concurrent connections. Cached images will be loaded instantly, this limit is only for new images.
136139

140+
# Clearing the cache
141+
```typescript
142+
143+
import { ImageLoader } from 'ionic-image-loader';
144+
145+
@Component(...)
146+
class MyComponent {
147+
148+
constructor(imageLoader: ImageLoader) {
149+
imageLoader.clearCache();
150+
}
151+
152+
}
153+
154+
```
137155

138156
# Contributing
139157
- **Having trouble?** Create an issue [here](https://github.com/zyramedia/ionic-image-loader/issues/new)

src/providers/image-loader.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ export class ImageLoader {
5252
}
5353
}
5454

55+
/**
56+
* Clears the cache
57+
*/
58+
clearCache(): void {
59+
60+
const clear = () => {
61+
62+
if (!this.isInit) {
63+
// do not run this method until our service is initialized
64+
setTimeout(clear.bind(this), 500);
65+
return;
66+
}
67+
68+
// pause any operations
69+
this.isInit = false;
70+
71+
File.removeRecursively(cordova.file.cacheDirectory, this.config.cacheDirectoryName)
72+
.then(() => {
73+
this.initCache(true);
74+
})
75+
.catch(this.throwError.bind(this));
76+
77+
};
78+
79+
clear();
80+
81+
}
82+
5583
/**
5684
* Downloads an image via cordova-plugin-file-transfer
5785
* @param imageUrl {string} The remote URL of the image

0 commit comments

Comments
 (0)