(deviceId, token)
| 85 | } |
| 86 | |
| 87 | update(deviceId, token) { |
| 88 | return this._load() |
| 89 | .then(() => { |
| 90 | this._data[deviceId] = token; |
| 91 | |
| 92 | if(this._saving) { |
| 93 | this._dirty = true; |
| 94 | return this._saving; |
| 95 | } |
| 96 | |
| 97 | return this._saving = new Promise((resolve, reject) => { |
| 98 | const save = () => { |
| 99 | debug('About to save tokens'); |
| 100 | fs.writeFile(this._file, JSON.stringify(this._data, null, 2), (err) => { |
| 101 | if(err) { |
| 102 | reject(err); |
| 103 | } else { |
| 104 | if(this._dirty) { |
| 105 | debug('Redoing save due to multiple updates'); |
| 106 | this._dirty = false; |
| 107 | save(); |
| 108 | } else { |
| 109 | delete this._saving; |
| 110 | resolve(); |
| 111 | } |
| 112 | } |
| 113 | }); |
| 114 | }; |
| 115 | |
| 116 | mkdirp(dirs.userData(), (err) => { |
| 117 | if(err) { |
| 118 | reject(err); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | save(); |
| 123 | }); |
| 124 | }); |
| 125 | }); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | module.exports = new Tokens(); |
no test coverage detected