]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - data/UpdateStore.js
4368835637e6b5ba545930d237ab76e00de9d143
[proxmox-widget-toolkit.git] / data / UpdateStore.js
1 /* Extends the Ext.data.Store type
2 * with startUpdate() and stopUpdate() methods
3 * to refresh the store data in the background
4 * Components using this store directly will flicker
5 * due to the redisplay of the element ater 'config.interval' ms
6 *
7 * Note that you have to call yourself startUpdate() for the background load
8 * to begin
9 */
10 Ext.define('Proxmox.data.UpdateStore', {
11 extend: 'Ext.data.Store',
12 alias: 'store.update',
13
14 isStopped: true,
15
16 autoStart: false,
17
18 destroy: function() {
19 var me = this;
20 me.load_task.cancel();
21 Proxmox.data.UpdateQueue.unqueue(me);
22 me.callParent();
23 },
24
25 constructor: function(config) {
26 var me = this;
27
28 config = config || {};
29
30 if (!config.interval) {
31 config.interval = 3000;
32 }
33
34 if (!config.storeid) {
35 throw "no storeid specified";
36 }
37
38 var load_task = new Ext.util.DelayedTask();
39
40 var run_load_task = function() {
41 if (me.isStopped) {
42 return;
43 }
44
45 if (Proxmox.Utils.authOK()) {
46 Proxmox.data.UpdateQueue.queue(me, function(runtime, success) {
47 var interval = config.interval + runtime*2;
48 load_task.delay(interval, run_load_task);
49 });
50 } else {
51 load_task.delay(200, run_load_task);
52 }
53 };
54
55 Ext.apply(config, {
56 startUpdate: function() {
57 me.isStopped = false;
58 run_load_task();
59 },
60 stopUpdate: function() {
61 me.isStopped = true;
62 load_task.cancel();
63 Proxmox.data.UpdateQueue.unqueue(me);
64 }
65 });
66
67 me.callParent([config]);
68
69 me.load_task = load_task;
70
71 if (me.autoStart) {
72 me.startUpdate();
73 }
74 }
75 });