forked from leancloud/javascript-sdk-installation-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
89 lines (81 loc) · 2.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict';
var jstz = require('jstz');
module.exports = function(AV) {
if (!AV) {
throw new Error('AV must be specified.');
}
var INSTALLATION_KEY = AV._getAVPath('installation');
var DEFAULT_INSTALLATION = {
deviceType: 'ios',
};
var Installation = AV.Object.extend('_Installation', {
save: function(arg1, arg2, arg3) {
var options;
var attrs = {};
if (arg3) {
attrs[arg1] = arg2;
options = arg3;
} else {
attrs = arg1;
options = arg2;
}
options = options || {};
var newOptions = {};
// change default endpoint from `/classes/_Installation` to `/installations`
newOptions._makeRequest = function(route, className, id, method, json) {
return AV.request({
service: 'push',
path: '/installations' + ( id ? '/' + id : '' ),
method: method,
data: json
});
};
var promise;
// installation should be updated once a day.
var lastUpdateExpired =
this.id && this.updatedAt &&
new Date() - new Date(this.updatedAt) > 24 * 3600000;
if (this.dirty() || lastUpdateExpired) {
promise = AV.Object.prototype.save
.call(this, attrs, newOptions)
.then(function(model) {
// update local cache.
AV.localStorage.setItemAsync(
INSTALLATION_KEY,
JSON.stringify(model.toFullJSON())
);
return model;
});
} else {
promise = AV.Promise.resolve(this);
}
return promise;
}
});
return {
getCurrent: function() {
return AV.localStorage.getItemAsync(INSTALLATION_KEY)
.then(function(installationString) {
var installation;
if (installationString) {
try {
var installationData = JSON.parse(installationString);
// if local cache exists, installation.objectId exists
// installation.dirty() will be false
installation = AV.parseJSON(installationData);
} catch (e) {}
}
if (!installation) {
// create a new Installation
// installation.dirty() will be true
installation = new Installation(DEFAULT_INSTALLATION);
}
var timeZone = jstz.determine().name();
if (installation.get('timeZone') !== timeZone) {
installation.set('timeZone', timeZone);
}
return installation;
});
}
};
};