BlobCache.js
1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* BlobCache.js
*
* Released under LGPL License.
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/**
* Hold blob info objects where a blob has extra internal information.
*
* @private
* @class tinymce.file.BlobCache
*/
define("tinymce/file/BlobCache", [
"tinymce/util/Arr",
"tinymce/util/Fun"
], function(Arr, Fun) {
return function() {
var cache = [], constant = Fun.constant;
function create(id, blob, base64) {
return {
id: constant(id),
blob: constant(blob),
base64: constant(base64),
blobUri: constant(URL.createObjectURL(blob))
};
}
function add(blobInfo) {
if (!get(blobInfo.id())) {
cache.push(blobInfo);
}
}
function get(id) {
return findFirst(function(cachedBlobInfo) {
return cachedBlobInfo.id() === id;
});
}
function findFirst(predicate) {
return Arr.filter(cache, predicate)[0];
}
function getByUri(blobUri) {
return findFirst(function(blobInfo) {
return blobInfo.blobUri() == blobUri;
});
}
function removeByUri(blobUri) {
cache = Arr.filter(cache, function(blobInfo) {
if (blobInfo.blobUri() === blobUri) {
URL.revokeObjectURL(blobInfo.blobUri());
return false;
}
return true;
});
}
function destroy() {
Arr.each(cache, function(cachedBlobInfo) {
URL.revokeObjectURL(cachedBlobInfo.blobUri());
});
cache = [];
}
return {
create: create,
add: add,
get: get,
getByUri: getByUri,
findFirst: findFirst,
removeByUri: removeByUri,
destroy: destroy
};
};
});