PageSourceSearch

https://vc64web.github.io/js/vc64_updater.js

js vc64web.github.io collected 2026-10-03 10:08:05 UTC 11,345 bytes, 282 lines download raw bytes

1function register_updater()
2{
3    set_settings_cache_value = async function (key, value)
4    {
5        let settings = await caches.open('settings');
6        await settings.put(key, new Response(value) );
7    }
8    get_settings_cache_value= async function (key)
9    {
10        try {
11            let settings = await caches.open('settings');
12            let response=await settings.match(key)
13            if(response==undefined)
14                return null;
15            return await response.text();    
16        }
17        catch(e){
18            console.error(e);
19            return "can't read version";
20        }
21    }
22
23    execute_update = async function() 
24    {
25        let current_version= await get_settings_cache_value('active_version');
26        if(current_version == null)
27        {//no version management then clear all caches
28            let keys = await caches.keys();
29            console.log('deleting cache files:'+keys);
30            await Promise.all(keys.map(key => caches.delete(key)));
31        }
32        if(typeof sw_ver
32sion != 'undefined')
33        {
34            set_settings_cache_value('active_version', sw_version.cache_name);        
35        }
36        window.location.reload();
37    }
38
39    $("#div_toast").hide();
40    show_new_version_toast= ()=>{
41        $("#div_toast").show();
42        $(".toast").toast({autohide: false});
43        $('.toast').toast('show');
44    }
45    $('.toast').on('hidden.bs.toast', function () {
46        $("#div_toast").hide();
47    });
48
49    has_installed_version=async function (cache_name){
50        let cache_names=await caches.keys();
51        for(c_name of cache_names)
52            if(c_name == cache_name)
53                return true;        
54        return false;
55    }
56    get_current_ui_version = async function (){
57        current_version = await get_settings_cache_value("active_version");
58        
59        current_ui='unkown';
60        if(current_version != null)
61        {
62            current_ui=current_version.split('@')[1];
63        }
64    }
65
66
67    try{
68        //when the serviceworker talks with us ...  
69        navigator.serviceWorker.addEventListener("message", async (evt) => {
70            await get_current_ui_version();
71            let cache_names=null;
72            try{
73                cache_names=await caches.keys();
74            }
75            catch(e)
76            {
77                console.error(e);
78                return;
79            }
80            let version_selector = `
81            manage already installed versions:
82            <br>
83            <div style="display:flex">
84            <select id="version_selector" class="ml-2">`;
85            for(c_name of cache_names)
86            {
87                let name_parts=c_name.split('@');
88                let core_name= name_parts[0];
89                let ui_name= name_parts[1];
90                let selected=c_name==current_version?"selected":"";
91
92                if(c_name.includes('@'))
93                {   
94                    if(//uat version should not show regular versions and vice versa
95                        location.pathname.startsWith("/uat") ?
96                            ui_name.endsWith("uat")
97                        :
98                            !ui_name.endsWith("uat")
99                    )
100                    {
101                        version_selector+=`<option ${selected} value="${c_name}">core ${core_name}, ui ${ui_name}</option>`;
102                    }
103                }
104            }
105            version_selector+=
106            `</select>
107            
108            <button type="button" id="activate_version" disabled class="btn btn-primary btn-sm px-1 mx-1">activate</button>
109            <button type="button" id="remove_version" class="btn btn-danger btn-sm px-1 mx-1"><svg style="width:1.5em;height:1.5em"><use xlink:href="/img/sprites.svg#trash"/></svg>
110            </button>
111            </div>
112            `;
113
114            //2. diese vergleichen mit der des Service workers
115            $("#div_toast").hide();
116            sw_version=evt.data;
117            if(sw_version.cache_name != current_version)
118            {
119                let new_version_already_installed=await has_installed_version(sw_version.cache_name);
120                let new_version_installed_or_not = new_version_already_installed ?
121                `newest version (already installed)`:
122                `new version available`;
123
124                let activate_or_install = `
125                <button type="button" id="activate_or_install" class="btn btn-${new_version_already_installed ?"primary":"success"} btn-sm px-1 mx-1">${
126                    new_version_already_installed ? "activate": "install"
127                }</button>`;
128
129
130
131                let upgrade_info = `    
132                currently active version (old):<br>
133                <div style="display:flex">
134                <span class="ml-2 px-1 py-1 outlined">core <i>${wasm_get_core_version()}</i></span> <span class="ml-2 px-1 py-1 outlined">ui <i>${current_ui}</i></span>
135                </div><br>
136                <span id="new_version_installed_or_not">${new_version_installed_or_not}</span>:<br> 
137                <div style="display:flex">
138                <span class="ml-2 px-1 py-1 outlined">core <i>${sw_version.core}</i></span> <span class="ml-2 px-1 py-1 outlined">ui <i>${sw_version.ui}</i></span> ${activate_or_install}
139                </div>
140                <div id="install_warning" class="my-1">Did you know that upgrading the core may break your saved snapshots?<br/>
141                In that case you can still select and activate an older compatible installation to run it ...
142                </div>`;
143
144                $('#update_dialog').html(upgrade_info);
145                $('#activate_or_install').remove();
146                $('#install_warning').remove();
147                $('#version_display').html(`${upgrade_info} 
148                <br>
149                ${version_selector}`);
150                if(!new_version_already_installed)
151                {
152                    show_new_version_toast();
153                }
154            }
155            else
156            {
157                $("#version_display").html(`
158                currently active version (latest):<br>
159                <div style="display:flex">
160                <span class="ml-2 px-1 py-1 outlined">core <i>${wasm_get_core_version()}</i></span> <span class="ml-2 px-1 py-1 outlined">ui <i>${current_ui}</i></span>
161                <button type="button" id="activate_or_install" class="btn btn-success btn-sm px-1 py-1">
162                install</button>
163                </div>
164                <br>
165                ${version_selector}`
166                );
167                $("#activate_or_install").hide();
168            }
169            document.getElementById('version_selector').onchange = function() {
170                let select = document.getElementById('version_selector');
171                document.getElementById('activate_version').disabled=
172                    (select.options[select.selectedIndex].value == current_version);
173            }
174            document.getElementById('remove_version').onclick = function() {
175                let select = document.getElementById('version_selector');
176                let cache_name = select.value;
177                if(cache_name == sw_version.cache_name)
178                {
179                    $("#new_version_installed_or_not").text("new version available");
180                    $("#activate_or_install").text("install").attr("class","btn btn-success btn-sm px-1 mx-1").show();
181                }
182                caches.delete(cache_name);
183                select.options[select.selectedIndex].remove();
184                if(current_version == cache_name)
185                {//when removing the current active version, activate another installed version
186                    if(select.options.length>0)
187                    {
188                        select.selectedIndex=select.options.length-1;
189                        set_settings_cache_value("active_version",select.options[select.selectedIndex].value); 
190                    }
191                    else
192                    {
193                        set_settings_cache_value("active_version",sw_version.cache_name); 
194                    }   
195                }
196                if(select.options.length==0)
197                {
198                    document.getElementById('remove_version').disabled=true;        
199                    document.getElementById('activate_version').disabled=true;
200                }
201                else 
202                {
203                    document.getElementById('activate_version').disabled=
204                    (select.options[select.selectedIndex].value == current_version);
205                }
206                show_browser_storage_info();
207            }
208            document.getElementById('activate_version').onclick = function() {
209                let cache_name = document.getElementById('version_selector').value; 
210                set_settings_cache_value("active_version",cache_name);
211                window.location.reload();
212            }
213            let activate_or_install_btn = document.getElementById('activate_or_install');
214            if(activate_or_install_btn != null)
215            {
216                activate_or_install_btn.onclick = () => {
217                    (async ()=>{
218                        let new_version_already_installed=await has_installed_ver
218sion(sw_version.cache_name); 
219                        if(new_version_already_installed)
220                        {
221                            set_settings_cache_value("active_version",sw_version.cache_name);
222                            window.location.reload();
223                        }
224                        else
225                        {
226                            execute_update();
227                        }
228                    })();
229                }
230            }        
231        });
232
233
234        // ask service worker to send us a version message
235        // wait until it is active
236        navigator.serviceWorker.ready
237        .then( (registration) => {
238            if (registration.active) {
239                registration.active.postMessage('version');
240            }
241        });
242
243
244        wasm_get_core_version = () => {
245            try{
246                let get_core_version = Module.cwrap('wasm_get_core_version', 'string');
247                return get_core_version();
248            }
249            catch(e){
250                return e.message; 
251            }
252        }
253
254        //in the meantime until message from service worker has not yet arrived show this
255        let init_version_display= async ()=>{
256            await get_current_ui_version();
257            wasm_get_core_version = Module.cwrap('wasm_get_core_version', 'string');
258            $("#version_display").html(`
259            currently active version:<br>
260            <span class="ml-2 px-1 outlined">core <i>${wasm_get_core_version()}</i></span> <span class="ml-2 px-1 outlined">ui <i>${current_ui}</i></span>
261            <br><br>
262            waiting for service worker...`
263            );
264        };
265        init_version_display();
266    } catch(e)
267    {
268        console.error(e.message);
269    }
270}
271
272
273//when wasm Module is already defined...
274(async ()=>
275{
276    while(typeof(Module) === 'undefined')
277    {
278        const sleep = (milliseconds) => new Promise(resolve => setTimeout(resolve, milliseconds));
279        await sleep(1000);
280    }
281    register_updater();
282})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.