avocado-old/packages/resource/store.js

57 lines
1.0 KiB
JavaScript
Raw Normal View History

2019-05-21 21:08:06 -05:00
import fs from 'fs';
import path from 'path';
import {Resource} from './resource';
export class ResourceRegistry {
constructor() {
this.idToUriMap = {};
this.resources = {};
this.uriToOtherMap = {};
this.id = 1;
}
load(uri) {
return Resource.read(uri).then((json) => {
for (const uri in json) {
this.register(uri, json.uuid, json.id);
}
});
}
flush() {
2019-05-22 05:24:08 -05:00
const RESOURCE_PATH = path.resolve(process.cwd(), 'resource');
2019-05-21 21:08:06 -05:00
fs.writeFileSync(
path.join(RESOURCE_PATH, 'registry.json'),
JSON.stringify(this.uriToOtherMap)
);
}
idToUri(id) {
return this.idToUriMap[id];
}
register(uri, uuid, id) {
id = id || this.id++;
this.idToUriMap[id] = uri;
this.uriToOtherMap[uri] = {
id,
uuid,
};
}
uriToId(uri) {
const entry = this.uriToOtherMap[uri];
return entry && entry.id;
}
uriToUuid(uri) {
const entry = this.uriToOtherMap[uri];
return entry && entry.uuid;
}
}
export const globalRegistry = new ResourceRegistry();