configuration
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -32,5 +32,3 @@ "use strict"; | ||
triggerChange() { | ||
for (let i = 0, l = this.handlers.length; i < l; i++) { | ||
this.handlers[i](); | ||
} | ||
this.handlers.forEach(handler => handler()); | ||
} | ||
@@ -37,0 +35,0 @@ onChange(handler) { |
@@ -5,6 +5,8 @@ import { FSWatcher } from 'chokidar'; | ||
declare class ProviderFile<Options extends ProviderFileOptions = ProviderFileOptions> extends ProviderMemory<Options> { | ||
path: string; | ||
path?: string; | ||
watching: boolean; | ||
watcher?: FSWatcher; | ||
constructor(options: Partial<Options>); | ||
dispose(): void; | ||
swap(path?: string, _initial?: boolean): void; | ||
watch(): void; | ||
@@ -11,0 +13,0 @@ unwatch(): void; |
@@ -10,7 +10,4 @@ "use strict"; | ||
super(options); | ||
if (!options.path) | ||
throw new Error('You need to provide a path'); | ||
this.path = options.path; | ||
if (options.watch !== false) | ||
this.watch(); | ||
this.watching = !!options.watch; | ||
this.swap(options.path, true); | ||
} | ||
@@ -20,5 +17,21 @@ dispose() { | ||
} | ||
swap(path, _initial = false) { | ||
if (path === this.path) | ||
return; | ||
this.dispose(); | ||
this.path = path; | ||
this.init(); | ||
if (!_initial) | ||
this.triggerChange(); | ||
if (this.watching) | ||
this.watch(); | ||
} | ||
watch() { | ||
this.watcher = file_1.default.watch(this.path, async () => { | ||
if (!this.path) | ||
return; | ||
const path = this.path; | ||
this.watcher = file_1.default.watch(path, async () => { | ||
const { data, dataRaw } = await this.read(); | ||
if (path !== this.path) | ||
return; | ||
if (this.isEqual(dataRaw) && this.isEqual(data)) | ||
@@ -25,0 +38,0 @@ return; |
@@ -11,2 +11,4 @@ "use strict"; | ||
async read() { | ||
if (!this.path) | ||
return super.read(); | ||
try { | ||
@@ -22,2 +24,4 @@ const dataRaw = await file_1.default.read(this.path, { encoding: 'utf8' }) || config_1.DEFAULTS.dataRaw, data = serializer_1.default.deserialize(dataRaw); | ||
readSync() { | ||
if (!this.path) | ||
return super.readSync(); | ||
try { | ||
@@ -33,2 +37,4 @@ const dataRaw = file_1.default.readSync(this.path, { encoding: 'utf8' }) || config_1.DEFAULTS.dataRaw, data = serializer_1.default.deserialize(dataRaw); | ||
async write(data, force = false) { | ||
if (!this.path) | ||
return super.write(data, force); | ||
if (!force && this.isEqual(data)) | ||
@@ -40,2 +46,4 @@ return; | ||
writeSync(data, force = false) { | ||
if (!this.path) | ||
return super.writeSync(data, force); | ||
if (!force && this.isEqual(data)) | ||
@@ -42,0 +50,0 @@ return; |
@@ -8,3 +8,3 @@ "use strict"; | ||
constructor(options) { | ||
options = { storage: localStorage, ...options }; //TSC | ||
options = { ...options, storage: localStorage }; //TSC | ||
super(options); | ||
@@ -11,0 +11,0 @@ } |
@@ -8,3 +8,3 @@ "use strict"; | ||
constructor(options) { | ||
options = { storage: sessionStorage, ...options }; //TSC | ||
options = { ...options, storage: sessionStorage }; //TSC | ||
super(options); | ||
@@ -11,0 +11,0 @@ } |
{ | ||
"name": "configuration", | ||
"description": "Performant and feature rich library for managing configurations/settings.", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"main": "dist/index.js", | ||
@@ -6,0 +6,0 @@ "types": "dist/index.d.ts", |
@@ -58,8 +58,4 @@ | ||
for ( let i = 0, l = this.handlers.length; i < l; i++ ) { | ||
this.handlers.forEach ( handler => handler () ); | ||
this.handlers[i](); | ||
} | ||
} | ||
@@ -66,0 +62,0 @@ |
@@ -13,3 +13,4 @@ | ||
path: string; | ||
path?: string; | ||
watching: boolean; | ||
watcher?: FSWatcher; | ||
@@ -21,8 +22,6 @@ | ||
if ( !options.path ) throw new Error ( 'You need to provide a path' ); | ||
this.watching = !!options.watch; | ||
this.path = options.path; | ||
this.swap ( options.path, true ); | ||
if ( options.watch !== false ) this.watch (); | ||
} | ||
@@ -36,8 +35,30 @@ | ||
swap ( path?: string, _initial: boolean = false ): void { | ||
if ( path === this.path ) return; | ||
this.dispose (); | ||
this.path = path; | ||
this.init (); | ||
if ( !_initial ) this.triggerChange (); | ||
if ( this.watching ) this.watch (); | ||
} | ||
watch (): void { | ||
this.watcher = File.watch ( this.path, async () => { | ||
if ( !this.path ) return; | ||
const path = this.path; | ||
this.watcher = File.watch ( path, async () => { | ||
const {data, dataRaw} = await this.read (); | ||
if ( path !== this.path ) return; | ||
if ( this.isEqual ( dataRaw ) && this.isEqual ( data ) ) return; | ||
@@ -44,0 +65,0 @@ |
@@ -16,2 +16,4 @@ | ||
if ( !this.path ) return super.read (); | ||
try { | ||
@@ -36,2 +38,4 @@ | ||
if ( !this.path ) return super.readSync (); | ||
try { | ||
@@ -56,2 +60,4 @@ | ||
if ( !this.path ) return super.write ( data, force ); | ||
if ( !force && this.isEqual ( data ) ) return; | ||
@@ -67,2 +73,4 @@ | ||
if ( !this.path ) return super.writeSync ( data, force ); | ||
if ( !force && this.isEqual ( data ) ) return; | ||
@@ -69,0 +77,0 @@ |
@@ -13,3 +13,3 @@ | ||
options = { storage: localStorage, ...options } as Partial<Options>; //TSC | ||
options = { ...options, storage: localStorage } as Partial<Options>; //TSC | ||
@@ -16,0 +16,0 @@ super ( options ); |
@@ -13,3 +13,3 @@ | ||
options = { storage: sessionStorage, ...options } as Partial<Options>; //TSC | ||
options = { ...options, storage: sessionStorage } as Partial<Options>; //TSC | ||
@@ -16,0 +16,0 @@ super ( options ); |
@@ -830,4 +830,58 @@ | ||
describe ( 'watching', it => { | ||
describe ( 'path swapping', it => { | ||
it.only ( 'works', t => { | ||
const foo = new ProviderJSON ({ | ||
scope: 'foo', | ||
path: undefined, | ||
watch: true | ||
}); | ||
const options = { | ||
providers: [foo], | ||
defaults: Fixtures.defaults, | ||
schema: Fixtures.schema | ||
}; | ||
const conf = new Configuration ( options ); | ||
t.is ( conf.scopes.foo.watching, true ); | ||
t.is ( conf.scopes.foo.watcher, undefined ); | ||
t.is ( conf.get ( 'core.foo' ), 'defaults' ); | ||
foo.writeSync ( Fixtures.local.data ); | ||
t.is ( conf.get ( 'core.foo' ), 'local' ); | ||
foo.swap (); | ||
t.is ( conf.get ( 'core.foo' ), 'local' ); | ||
const tempPath = tempy.file ({ extension: 'json' }); | ||
fs.writeFileSync ( tempPath, '{ "core": { "foo": "temp" } }' ); | ||
foo.swap ( tempPath ); | ||
t.is ( conf.scopes.foo.watching, true ); | ||
t.is ( !!conf.scopes.foo.watcher, true ); | ||
t.is ( conf.get ( 'core.foo' ), 'temp' ); | ||
const tempPath2 = tempy.file ({ extension: 'json' }); | ||
fs.writeFileSync ( tempPath2, '{ "core": { "foo": "temp2" } }' ); | ||
foo.swap ( tempPath2 ); | ||
t.is ( conf.get ( 'core.foo' ), 'temp2' ); | ||
}); | ||
}); | ||
describe ( 'path watching', it => { | ||
it ( 'can be disabled', t => { | ||
@@ -851,2 +905,3 @@ | ||
t.is ( conf.scopes.foo.watching, false ); | ||
t.is ( conf.scopes.foo.watcher, undefined ); | ||
@@ -853,0 +908,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92128
2484