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