Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / InstanceCounter.js
1 /**
2 * Created by onvelocity on 1/28/16.
3 */
4
5 function getInstanceCount(type, bump = true) {
6 // keep a global counter
7 let count = 0;
8 const data = sessionStorage.getItem('instance-counts');
9 const counts = JSON.parse(data || '{}');
10 if (counts.hasOwnProperty(type)) {
11 count = counts[type];
12 if (bump) {
13 counts[type] = ++count;
14 } else {
15 return count;
16 }
17 } else {
18 count = counts[type] = 1;
19 }
20 sessionStorage.setItem('instance-counts', JSON.stringify(counts));
21 return count;
22 }
23
24 export default {
25
26 /**
27 * Get instance count for given type without bumping the number.
28 *
29 * @param type
30 * @returns {number}
31 */
32 current(type) {
33 return getInstanceCount(type, false);
34 },
35
36 /**
37 * Get the instance count for the given type - will bump the counter and return the new value.
38 *
39 * @param type
40 * @returns {number}
41 */
42 count(type) {
43 return getInstanceCount(type);
44 }
45
46 }