|
1
|
|
|
define(function(require) { |
|
2
|
|
|
'use strict'; |
|
3
|
|
|
|
|
4
|
|
|
var SyncMachineProxyCache; |
|
5
|
|
|
var _ = require('underscore'); |
|
6
|
|
|
var Backbone = require('backbone'); |
|
7
|
|
|
var Chaplin = require('chaplin'); |
|
8
|
|
|
var persistentStorage = require('oroui/js/persistent-storage'); |
|
9
|
|
|
var errorHandler = require('oroui/js/error'); |
|
10
|
|
|
|
|
11
|
|
|
SyncMachineProxyCache = _.extend({}, Chaplin.SyncMachine); |
|
12
|
|
|
SyncMachineProxyCache.__super__ = Chaplin.SyncMachine; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Mixin for Models and Collections |
|
16
|
|
|
* Extends Chaplin.SyncMachine and overloads ensureSync method |
|
17
|
|
|
* Utilizes persistentStorage to preserve data from previews session |
|
18
|
|
|
* Useful for time consuming API with rarely changing data |
|
19
|
|
|
* |
|
20
|
|
|
* Instance has to define two properties |
|
21
|
|
|
* - `SYNC_MACHINE_PROXY_CACHE_STORAGE_KEY` |
|
22
|
|
|
* - `SYNC_MACHINE_PROXY_CACHE_EXPIRE_TIME` |
|
23
|
|
|
* |
|
24
|
|
|
* Triggers 'proxy-cache:stale-data-in-use' event once actual data are loaded from server |
|
25
|
|
|
* and they different from restored once |
|
26
|
|
|
*/ |
|
27
|
|
|
SyncMachineProxyCache.ensureSync = function() { |
|
28
|
|
|
var storageKey = this.SYNC_MACHINE_PROXY_CACHE_STORAGE_KEY; |
|
29
|
|
|
var expireTime = this.SYNC_MACHINE_PROXY_CACHE_EXPIRE_TIME; |
|
30
|
|
|
var observer; |
|
31
|
|
|
var cache; |
|
32
|
|
|
var isModified = false; |
|
33
|
|
|
if (!storageKey || !expireTime) { |
|
34
|
|
|
errorHandler.showErrorInConsole(new Error('Improperly implemented SyncMachineProxyCache')); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if (this.isUnsynced() && storageKey && expireTime) { |
|
38
|
|
|
observer = Object.create(Backbone.Events); |
|
39
|
|
|
cache = persistentStorage.getItem(storageKey); |
|
40
|
|
|
try { |
|
41
|
|
|
cache = JSON.parse(cache); |
|
42
|
|
|
} catch (e) { |
|
43
|
|
|
// if data is not valid JSON, ignore it |
|
44
|
|
|
cache = void 0; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (cache && (Date.now() - cache.time) < expireTime) { |
|
48
|
|
|
this.set(cache.data, {parse: true}); |
|
49
|
|
|
this.fetch(); // fetch actual data in background |
|
50
|
|
|
this.markAsSynced(); // set flag that where's data ready to use |
|
51
|
|
|
observer.listenToOnce(this, 'update change', function() { |
|
52
|
|
|
isModified = true; |
|
53
|
|
|
}); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
observer.listenToOnce(this, 'sync', function(instance, data) { |
|
57
|
|
|
observer.stopListening(); |
|
58
|
|
|
persistentStorage.setItem(storageKey, JSON.stringify({ |
|
59
|
|
|
time: Date.now(), |
|
60
|
|
|
data: data |
|
61
|
|
|
})); |
|
62
|
|
|
if (isModified) { |
|
63
|
|
|
instance.trigger('proxy-cache:stale-data-in-use', instance); |
|
64
|
|
|
} |
|
65
|
|
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return SyncMachineProxyCache.__super__.ensureSync.call(this); |
|
69
|
|
|
}; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @export oroui/js/app/models/sync-machine-proxy-cache |
|
73
|
|
|
*/ |
|
74
|
|
|
return SyncMachineProxyCache; |
|
75
|
|
|
}); |
|
76
|
|
|
|