Skip to content

Commit

Permalink
added callback function & doublecheck double files
Browse files Browse the repository at this point in the history
  • Loading branch information
Topener committed Aug 13, 2015
1 parent ad339a5 commit 2d495bb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ This method is preferred for bigger images, as this can happen in the background
require('To.ImageCache').cache('http://example.com/image.jpg');
```

This function will NOT return a blob, but will cache the file using `XHR`.
This function will NOT return a blob, but will cache the file using `XHR`.

Aditonally, you can add a timeout and callback function:

```js
require('To.ImageCache').cache('http://example.com/image.jpg', 25000, function(blob){
$.imageView.image = blob;
});
```

## Clearing Cache

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ var storeFile = function(filename, blob){
// check if directory has been created
checkDir();

// we already have this file
if (hasFile(filename)){
blob = null;
return;
}

var path = Ti.Filesystem.applicationDataDirectory + c.folder;
var file = Ti.Filesystem.getFile(path, filename);
file.write(blob);
Expand Down Expand Up @@ -231,9 +237,10 @@ var remoteImage = function(url){
* This function will fetch the image in the background
* with a configurable cache period
* @param {String} url of the image to cache
* @param {Integer} Timeout in milliseconds
* @param {Integer} (Optional) Timeout in milliseconds
* @param {Function} (Optional) callback function, blob will be returned
*/
var cache = function(url, timeout){
var cache = function(url, timeout, cb){
var timeout = timeout || 30000;

// if file is already cached, don't do so again
Expand All @@ -248,6 +255,7 @@ var cache = function(url, timeout){
var xhr = Titanium.Network.createHTTPClient({
onload: function() {
storeFile(filename, this.responseData);
cb && cb(readFile(filename));
},
timeout: timeout
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 0.1
version: 0.1.1
description: A simple Commonjs module to cache images a little less temporarily
author: Rene Pot
license: Apache 2.0
Expand Down
Binary file added dist/To.ImageCache-commonjs-0.1.1.zip
Binary file not shown.

0 comments on commit 2d495bb

Please sign in to comment.