Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80657 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
var fs = require('fs');
17
18
var ResourceMap = require('./ResourceMap');
19
20
/**
21
* A class that loads and stores ResourceMaps from and to a given file.
22
* Usese simple JSON functions to seralize/deserialize the data
23
*
24
* @class
25
* @param {Array.<ResourceLoader>} loaders
26
*/
27
function ResourceMapSerializer(loaders, options) {
28
options = options || {};
29
this.version = options.version || '0.1';
30
this.typeMap = {};
31
loaders.forEach(function(loader) {
32
loader.getResourceTypes().forEach(function(type) {
33
this.typeMap[type.prototype.type] = type;
34
}, this);
35
}, this);
36
}
37
38
/**
39
* Loads and deserializes a map from a given path
40
* @param {String} path
41
* @param {ResourceMap} map
42
* @param {Function} callback
43
*/
44
ResourceMapSerializer.prototype.loadFromPath = function(path, callback) {
45
var me = this;
46
fs.readFile(path, 'utf-8', function(err, code) {
47
if (err || !code) {
48
callback(err, null);
49
return;
50
}
51
52
var ser = JSON.parse(code);
53
var map = me.fromObject(ser);
54
callback(null, map);
55
});
56
};
57
58
ResourceMapSerializer.prototype.loadFromPathSync = function(path) {
59
var code;
60
try {
61
code = fs.readFileSync(path, 'utf-8');
62
} catch (e) {
63
return null;
64
}
65
if (!code) {
66
return null;
67
}
68
69
var ser = JSON.parse(code);
70
var map = this.fromObject(ser);
71
return map;
72
};
73
74
ResourceMapSerializer.prototype.fromObject = function(ser) {
75
if (ser.version === this.version) {
76
var map = new ResourceMap();
77
ser.objects.forEach(function(obj) {
78
var type = this.typeMap[obj.type];
79
map.addResource(type.fromObject(obj));
80
}, this);
81
return map;
82
} else {
83
return null;
84
}
85
};
86
87
/**
88
* Serializes and stores a map to a given path
89
* @param {String} path
90
* @param {ResourceMap} map
91
* @param {Function} callback
92
*/
93
ResourceMapSerializer.prototype.storeToPath = function(path, map, callback) {
94
var ser = this.toObject(map);
95
fs.writeFile(path, JSON.stringify(ser), 'utf-8', callback);
96
};
97
98
ResourceMapSerializer.prototype.toObject = function(map) {
99
var ser = {
100
version: this.version,
101
objects: map.getAllResources().map(function(resource) {
102
return resource.toObject();
103
})
104
};
105
return ser;
106
};
107
108
109
module.exports = ResourceMapSerializer;
110
111