Test Setup Failed
Push — master ( d60e73...a71a5d )
by
unknown
04:37 queued 10s
created

  D

Complexity

Conditions 9
Paths 10

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
nc 10
dl 0
loc 43
rs 4.909
c 1
b 0
f 0
nop 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A sync-machine-proxy-cache.js ➔ ... ➔ observer.listenToOnce 0 3 1
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;
0 ignored issues
show
Coding Style introduced by
Consider using undefined instead of void(0). It is equivalent and more straightforward to read.
Loading history...
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