Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
6174490e8e | |||
d952a761b2 | |||
05909f776e | |||
779883fbab | |||
349f074bb9 | |||
d209661586 |
@ -119,6 +119,6 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
npmci node install stable
|
npmci node install stable
|
||||||
npmci npm install
|
npmci npm install
|
||||||
pnpm install -g @gitzone/tsdoc
|
pnpm install -g @git.zone/tsdoc
|
||||||
npmci command tsdoc
|
npmci command tsdoc
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/npmextra",
|
"name": "@push.rocks/npmextra",
|
||||||
"version": "4.0.6",
|
"version": "4.0.9",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "do more with npm",
|
"description": "do more with npm",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -26,12 +26,13 @@
|
|||||||
"@push.rocks/smartlog": "^3.0.2",
|
"@push.rocks/smartlog": "^3.0.2",
|
||||||
"@push.rocks/smartpath": "^5.0.11",
|
"@push.rocks/smartpath": "^5.0.11",
|
||||||
"@push.rocks/smartpromise": "^4.0.2",
|
"@push.rocks/smartpromise": "^4.0.2",
|
||||||
|
"@push.rocks/smartrx": "^3.0.7",
|
||||||
"@push.rocks/taskbuffer": "^3.1.7"
|
"@push.rocks/taskbuffer": "^3.1.7"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.66",
|
"@git.zone/tsbuild": "^2.1.66",
|
||||||
"@gitzone/tsrun": "^1.2.44",
|
"@git.zone/tsrun": "^1.2.44",
|
||||||
"@gitzone/tstest": "^1.0.77",
|
"@git.zone/tstest": "^1.0.77",
|
||||||
"@push.rocks/tapbundle": "^5.0.15",
|
"@push.rocks/tapbundle": "^5.0.15",
|
||||||
"@types/node": "^20.11.6"
|
"@types/node": "^20.11.6"
|
||||||
},
|
},
|
||||||
|
2370
pnpm-lock.yaml
generated
2370
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/npmextra',
|
name: '@push.rocks/npmextra',
|
||||||
version: '4.0.6',
|
version: '4.0.9',
|
||||||
description: 'do more with npm'
|
description: 'do more with npm'
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,11 @@ import * as plugins from './npmextra.plugins.js';
|
|||||||
import * as paths from './npmextra.paths.js';
|
import * as paths from './npmextra.paths.js';
|
||||||
import { KeyValueStore } from './npmextra.classes.keyvaluestore.js';
|
import { KeyValueStore } from './npmextra.classes.keyvaluestore.js';
|
||||||
|
|
||||||
|
export interface IAppDataOptions {
|
||||||
|
dirPath?: string;
|
||||||
|
requiredKeys?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
export class AppData {
|
export class AppData {
|
||||||
/**
|
/**
|
||||||
* creates appdata. If no pathArg is given, data will be stored here:
|
* creates appdata. If no pathArg is given, data will be stored here:
|
||||||
@ -9,18 +14,18 @@ export class AppData {
|
|||||||
* @param pathArg
|
* @param pathArg
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
public static async createAndInit(pathArg?: string) {
|
public static async createAndInit(optionsArg: IAppDataOptions = {}) {
|
||||||
const appData = new AppData(pathArg);
|
const appData = new AppData(optionsArg);
|
||||||
await appData.readyDeferred.promise;
|
await appData.readyDeferred.promise;
|
||||||
return appData;
|
return appData;
|
||||||
}
|
}
|
||||||
|
|
||||||
// instance
|
// instance
|
||||||
public readyDeferred = plugins.smartpromise.defer();
|
public readyDeferred = plugins.smartpromise.defer();
|
||||||
public dirPathArg: string;
|
public options: IAppDataOptions;
|
||||||
private kvStore: KeyValueStore;
|
private kvStore: KeyValueStore;
|
||||||
constructor(pathArg?: string) {
|
constructor(optionsArg: IAppDataOptions = {}) {
|
||||||
this.dirPathArg = pathArg;
|
this.options = optionsArg;
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,7 +34,7 @@ export class AppData {
|
|||||||
* @param pathArg
|
* @param pathArg
|
||||||
*/
|
*/
|
||||||
private async init(pathArg?: string) {
|
private async init(pathArg?: string) {
|
||||||
if (this.dirPathArg) {
|
if (this.options.dirPath) {
|
||||||
// ok, nothing to do here;
|
// ok, nothing to do here;
|
||||||
} else {
|
} else {
|
||||||
const appDataDir = '/app/data';
|
const appDataDir = '/app/data';
|
||||||
@ -38,15 +43,15 @@ export class AppData {
|
|||||||
const appDataExists = plugins.smartfile.fs.isDirectory(appDataDir);
|
const appDataExists = plugins.smartfile.fs.isDirectory(appDataDir);
|
||||||
const dataExists = plugins.smartfile.fs.isDirectory(dataDir);
|
const dataExists = plugins.smartfile.fs.isDirectory(dataDir);
|
||||||
if (appDataExists) {
|
if (appDataExists) {
|
||||||
this.dirPathArg = appDataDir;
|
this.options.dirPath = appDataDir;
|
||||||
} else if (dataExists) {
|
} else if (dataExists) {
|
||||||
this.dirPathArg = dataDir;
|
this.options.dirPath = dataDir;
|
||||||
} else {
|
} else {
|
||||||
await plugins.smartfile.fs.ensureDir(nogitAppData);
|
await plugins.smartfile.fs.ensureDir(nogitAppData);
|
||||||
this.dirPathArg = nogitAppData;
|
this.options.dirPath = nogitAppData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.kvStore = new KeyValueStore('custom', 'appkv', this.dirPathArg);
|
this.kvStore = new KeyValueStore('custom', 'appkv', this.options.dirPath, this.options.requiredKeys);
|
||||||
this.readyDeferred.resolve();
|
this.readyDeferred.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,12 +11,17 @@ export type TKeyValueStore = 'custom' | 'userHomeDir';
|
|||||||
export class KeyValueStore {
|
export class KeyValueStore {
|
||||||
private dataObject: any = {};
|
private dataObject: any = {};
|
||||||
private deletedObject: any = {};
|
private deletedObject: any = {};
|
||||||
|
private mandatoryKeys: Set<string> = new Set();
|
||||||
|
public changeSubject = new plugins.smartrx.rxjs.Subject();
|
||||||
|
|
||||||
|
private storedStateString: string = '';
|
||||||
public syncTask = new Task({
|
public syncTask = new Task({
|
||||||
name: 'syncTask',
|
name: 'syncTask',
|
||||||
buffered: true,
|
buffered: true,
|
||||||
bufferMax: 1,
|
bufferMax: 1,
|
||||||
execDelay: 0,
|
execDelay: 0,
|
||||||
taskFunction: async () => {
|
taskFunction: async () => {
|
||||||
|
|
||||||
this.dataObject = {
|
this.dataObject = {
|
||||||
...plugins.smartfile.fs.toObjectSync(this.filePath),
|
...plugins.smartfile.fs.toObjectSync(this.filePath),
|
||||||
...this.dataObject,
|
...this.dataObject,
|
||||||
@ -29,6 +34,13 @@ export class KeyValueStore {
|
|||||||
plugins.smartjson.stringifyPretty(this.dataObject),
|
plugins.smartjson.stringifyPretty(this.dataObject),
|
||||||
this.filePath
|
this.filePath
|
||||||
);
|
);
|
||||||
|
const newStateString = plugins.smartjson.stringify(this.dataObject);
|
||||||
|
|
||||||
|
// change detection
|
||||||
|
if (newStateString !== this.storedStateString) {
|
||||||
|
this.storedStateString = newStateString;
|
||||||
|
this.changeSubject.next(this.dataObject);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -70,7 +82,7 @@ export class KeyValueStore {
|
|||||||
* @param identityArg
|
* @param identityArg
|
||||||
* @param customPath Optional custom path for the keyValue store
|
* @param customPath Optional custom path for the keyValue store
|
||||||
*/
|
*/
|
||||||
constructor(typeArg: TKeyValueStore, identityArg: string, customPath?: string) {
|
constructor(typeArg: TKeyValueStore, identityArg: string, customPath?: string, mandatoryKeys?: string[]) {
|
||||||
if (customPath && typeArg !== 'custom') {
|
if (customPath && typeArg !== 'custom') {
|
||||||
throw new Error('customPath can only be provided if typeArg is custom');
|
throw new Error('customPath can only be provided if typeArg is custom');
|
||||||
}
|
}
|
||||||
@ -81,6 +93,9 @@ export class KeyValueStore {
|
|||||||
this.identity = identityArg;
|
this.identity = identityArg;
|
||||||
this.customPath = customPath; // Store custom path if provided
|
this.customPath = customPath; // Store custom path if provided
|
||||||
this.initFilePath();
|
this.initFilePath();
|
||||||
|
if (mandatoryKeys) {
|
||||||
|
this.setMandatoryKeys(mandatoryKeys);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -143,4 +158,28 @@ export class KeyValueStore {
|
|||||||
|
|
||||||
await this.syncTask.trigger(); // Sync again to reflect the deletion
|
await this.syncTask.trigger(); // Sync again to reflect the deletion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private setMandatoryKeys(keys: string[]) {
|
||||||
|
keys.forEach(key => this.mandatoryKeys.add(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
public getMissingMandatoryKeys(): string[] {
|
||||||
|
return Array.from(this.mandatoryKeys).filter(key => !(key in this.dataObject));
|
||||||
|
}
|
||||||
|
|
||||||
|
public async waitForKeysPresent(keysArg: []): Promise<void> {
|
||||||
|
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
|
||||||
|
if (missingKeys.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const subscription = this.changeSubject.subscribe(() => {
|
||||||
|
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
|
||||||
|
if (missingKeys.length === 0) {
|
||||||
|
subscription.unsubscribe();
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import * as smartfile from '@push.rocks/smartfile';
|
|||||||
import * as smartjson from '@push.rocks/smartjson';
|
import * as smartjson from '@push.rocks/smartjson';
|
||||||
import * as smartpath from '@push.rocks/smartpath';
|
import * as smartpath from '@push.rocks/smartpath';
|
||||||
import * as smartpromise from '@push.rocks/smartpromise';
|
import * as smartpromise from '@push.rocks/smartpromise';
|
||||||
|
import * as smartrx from '@push.rocks/smartrx';
|
||||||
import * as taskbuffer from '@push.rocks/taskbuffer';
|
import * as taskbuffer from '@push.rocks/taskbuffer';
|
||||||
|
|
||||||
export { smartlog, path, smartfile, smartjson, smartpath, smartpromise, taskbuffer };
|
export { smartlog, path, smartfile, smartjson, smartpath, smartpromise, smartrx, taskbuffer };
|
||||||
|
Reference in New Issue
Block a user