-
Notifications
You must be signed in to change notification settings - Fork 4
/
file_prefs.ts
113 lines (99 loc) · 3.26 KB
/
file_prefs.ts
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// -*- mode: typescript; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Thingpedia
//
// Copyright 2019 The Board of Trustees of the Leland Stanford Junior University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>
import * as util from 'util';
import * as fs from 'fs';
import Preferences from '../prefs';
/**
* A simple implementation of {@link Preferences} that uses a single file.
*
*/
export default class FilePreferences extends Preferences {
private _file : string;
private _writeTimeout : number;
private _prefs : { [key : string] : unknown };
private _dirty : boolean;
private _writeScheduled : boolean;
/**
* Construct a new FilePreferences object.
*
* @param {string} file - the file to write to
* @param {number} [writeTimeout=100] - timeout after which writes will be persisted on disk (in ms)
*/
constructor(file : string, writeTimeout = 100) {
super();
this._file = file;
this._writeTimeout = writeTimeout;
this._prefs = {};
try {
this._prefs = JSON.parse(fs.readFileSync(file, 'utf8'));
} catch(e) {
if (e.name === 'SyntaxError')
console.error('Syntax error loading preference file from disk: ' + e.message);
else if (e.code !== 'ENOENT')
throw e;
}
if (!this._prefs) {
this._prefs = {};
this._scheduleWrite();
}
this._dirty = false;
this._writeScheduled = false;
}
keys() : string[] {
return Object.keys(this._prefs);
}
get(name : string) : unknown {
return this._prefs[name];
}
set<T>(name : string, value : T) : T {
const changed = this._prefs[name] !== value;
this._prefs[name] = value;
this._scheduleWrite();
if (changed)
this.emit('changed', name);
return value;
}
delete(name : string) : void {
delete this._prefs[name];
this.emit('changed', name);
this._scheduleWrite();
}
changed() : void {
this._scheduleWrite();
this.emit('changed', null);
}
flush() : Promise<void> {
if (!this._dirty)
return Promise.resolve();
return util.promisify(fs.writeFile)(this._file, JSON.stringify(this._prefs, undefined, 2));
}
saveCopy(to : string) : Promise<void> {
return util.promisify(fs.writeFile)(to, JSON.stringify(this._prefs));
}
private _scheduleWrite() {
this._dirty = true;
if (this._writeScheduled)
return;
setTimeout(() => {
this._writeScheduled = false;
this.flush();
}, this._writeTimeout);
}
}