Project

General

Profile

Download (3.13 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / background / reverse_use_info.js @ 6bae771d

1
/**
2
 * Myext scripts and bags usage index
3
 *
4
 * Copyright (C) 2021 Wojtek Kosior
5
 *
6
 * This code is dual-licensed under:
7
 * - Asshole license 1.0,
8
 * - GPLv3 or (at your option) any later version
9
 *
10
 * "dual-licensed" means you can choose the license you prefer.
11
 *
12
 * This code is released under a permissive license because I disapprove of
13
 * copyright and wouldn't be willing to sue a violator. Despite not putting
14
 * this code under copyleft (which is also kind of copyright), I do not want
15
 * it to be made proprietary. Hence, the permissive alternative to GPL is the
16
 * Asshole license 1.0 that allows me to call you an asshole if you use it.
17
 * This means you're legally ok regardless of how you utilize this code but if
18
 * you make it into something nonfree, you're an asshole.
19
 *
20
 * You should have received a copy of both GPLv3 and Asshole license 1.0
21
 * together with this code. If not, please see:
22
 * - https://www.gnu.org/licenses/gpl-3.0.en.html
23
 * - https://koszko.org/asshole-license.txt
24
 */
25

    
26
"use strict";
27

    
28
/*
29
 * Warning!
30
 * This script file has not been used for some time and underlying storage
31
 * model has changed since then! Fix required!
32
 */
33

    
34
/*
35
 * We want to count referenes to scripts and bags in order to know,
36
 * for example, whether one can be safely deleted.
37
 */
38

    
39
(() => {
40
    const TYPE_PREFIX = window.TYPE_PREFIX;
41
    const get_storage = window.get_storage;
42
    const make_once = window.make_once;
43

    
44
    var component_uses = new Map();
45
    var storage;
46

    
47
    function add_use_info_by_item(prefix, item, components)
48
    {
49
	for (let component of components) {
50
	    component = component.join("");
51

    
52
	    let used_by_info = component_uses.get(component);
53

    
54
	    if (used_by_info === undefined) {
55
		used_by_info = {};
56
		component_uses.set(component, used_by_info);
57
	    }
58

    
59
	    if (used_by_info[prefix] === undefined)
60
		used_by_info[prefix] = new Set();
61

    
62
	    used_by_info[prefix].add(item);
63
	}
64
    }
65

    
66
    function remove_use_info_by_item(prefix, item, components)
67
    {
68
	for (let component of components) {
69
	    used_by_info = component_uses.get(component.join(""));
70
	    if (used_by_info === undefined ||
71
		used_by_info[prefix] === undefined)
72
		return;
73

    
74
	    used_by_info[prefix].delete(item);
75
	}
76
    }
77

    
78
    function build_reverse_uses_info(entries_it, type_prefix)
79
    {
80
	for (let [item, components] of storage.get_all_it(type_prefix))
81
	    add_use_info_by_item(type_prefix, item, components);
82
    }
83

    
84
    function handle_change(change)
85
    {
86
	if (change.old_val !== undefined)
87
	    remove_use_info_by_item(change.prefix, change.item, change.old_val);
88

    
89
	if (change.new_val !== undefined)
90
	    add_use_info_by_item(change.prefix, change.item, change.new_val);
91
    }
92

    
93
    function get_uses(arg1, arg2=undefined)
94
    {
95
	let [prefix, item] = [arg1, arg2];
96

    
97
	if (arg2 === undefined)
98
	    [prefix, item] = arg1;
99

    
100
	return component_uses.get(prefix + item);
101
    }
102

    
103
    async function init()
104
    {
105
	storage = await get_storage();
106

    
107
	prefixes = [TYPE_PREFIX.PAGE, TYPE_PREFIX.BAG];
108
	for (let prefix of prefixes)
109
	    build_reverse_uses_info(prefix);
110

    
111
	storage.add_change_listener(handle_change, prefixes);
112

    
113
	return get_uses;
114
    }
115

    
116
    window.get_reverse_use_info = make_once(init);
117
})();
(6-6/9)