MCPcopy
hub / github.com/aholstenson/miio / _load

Method _load

lib/tokens.js:47–85  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

45 }
46
47 _load() {
48 if(this._loading) return this._loading;
49
50 return this._loading = new Promise((resolve, reject) => {
51 debug('Loading token storage from', this._file);
52 fs.stat(this._file, (err, stat) => {
53 if(err) {
54 delete this._loading;
55 if(err.code === 'ENOENT') {
56 debug('Token storage does not exist');
57 this._lastSync = Date.now();
58 resolve(this._data);
59 } else {
60 reject(err);
61 }
62
63 return;
64 }
65
66 if(! stat.isFile()) {
67 // tokens.json does not exist
68 delete this._loading;
69 reject(new Error('tokens.json exists but is not a file'));
70 } else if(Date.now() - this._lastSync > MAX_STALE_TIME || stat.mtime.getTime() > this._lastSync) {
71 debug('Loading tokens');
72 fs.readFile(this._file, (err, result) => {
73 this._data = JSON.parse(result.toString());
74 this._lastSync = Date.now();
75 delete this._loading;
76 resolve(this._data);
77 });
78 } else {
79 delete this._loading;
80 this._lastSync = Date.now();
81 resolve(this._data);
82 }
83 });
84 });
85 }
86
87 update(deviceId, token) {
88 return this._load()

Callers 2

_loadAndGetMethod · 0.95
updateMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected