feat: ResourceRegistry
This commit is contained in:
parent
92c23424a3
commit
5002d9b107
|
@ -1,83 +1,2 @@
|
|||
import uuid from 'uuid/v4';
|
||||
|
||||
import {compose, merge, Property} from '@avocado/core';
|
||||
|
||||
const decorate = compose(
|
||||
Property('instanceUuid'),
|
||||
Property('uri'),
|
||||
Property('uuid'),
|
||||
)
|
||||
|
||||
export class Resource extends decorate(class {}) {
|
||||
|
||||
static load(uri) {
|
||||
return this.read(uri).then((json) => {
|
||||
return new this(json);
|
||||
});
|
||||
}
|
||||
|
||||
static read(uri) {
|
||||
if (!this.readCache) {
|
||||
this.readCache = {};
|
||||
}
|
||||
if (this.readCache[uri]) {
|
||||
return this.readCache[uri].then((json) => {
|
||||
return JSON.parse(JSON.stringify(json));
|
||||
});
|
||||
}
|
||||
if ('undefined' !== typeof window) {
|
||||
this.readCache[uri] = fetch(uri).then((response) => {
|
||||
return response.json();
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.readCache[uri] = new Promise((resolve, reject) => {
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const resourcePath = path.resolve(process.cwd(), 'resource');
|
||||
fs.readFile(`${resourcePath}${uri}`, (error, buffer) => {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
resolve(JSON.parse(buffer.toString('utf8')));
|
||||
});
|
||||
});
|
||||
}
|
||||
// Allow "base" JSON.
|
||||
return this.readCache[uri].then((json) => {
|
||||
if ('undefined' !== typeof json.base) {
|
||||
return this.read(json.base).then((baseJSON) => {
|
||||
return merge({}, baseJSON, json);
|
||||
});
|
||||
}
|
||||
return json;
|
||||
});
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.instanceUuid = uuid();
|
||||
}
|
||||
|
||||
fromJSON({uri, uuid}) {
|
||||
if (uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
if (uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
regenerateUuid() {
|
||||
this.uuid = uuid();
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
uuid: this.uuid,
|
||||
uri: this.uri,
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
export {ResourceRegistry, globalRegistry} from './registry';
|
||||
export {Resource} from './resource';
|
||||
|
|
57
packages/resource/registry.js
Normal file
57
packages/resource/registry.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
import {Resource} from './resource';
|
||||
|
||||
const RESOURCE_PATH = path.resolve(process.cwd(), '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() {
|
||||
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();
|
87
packages/resource/resource.js
Normal file
87
packages/resource/resource.js
Normal file
|
@ -0,0 +1,87 @@
|
|||
import uuid from 'uuid/v4';
|
||||
|
||||
import {compose, merge, Property} from '@avocado/core';
|
||||
|
||||
const decorate = compose(
|
||||
Property('instanceUuid'),
|
||||
Property('uri'),
|
||||
Property('uuid'),
|
||||
)
|
||||
|
||||
export class Resource extends decorate(class {}) {
|
||||
|
||||
static load(uri) {
|
||||
return this.read(uri).then((json) => {
|
||||
return new this(json);
|
||||
});
|
||||
}
|
||||
|
||||
static read(uri) {
|
||||
if (!this.readCache) {
|
||||
this.readCache = {};
|
||||
}
|
||||
if (this.readCache[uri]) {
|
||||
return this.readCache[uri].then((json) => {
|
||||
return JSON.parse(JSON.stringify(json));
|
||||
});
|
||||
}
|
||||
if ('undefined' !== typeof window) {
|
||||
this.readCache[uri] = fetch(uri).then((response) => {
|
||||
return response.json();
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.readCache[uri] = new Promise((resolve, reject) => {
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const resourcePath = path.resolve(process.cwd(), 'resource');
|
||||
fs.readFile(`${resourcePath}${uri}`, (error, buffer) => {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
resolve(JSON.parse(buffer.toString('utf8')));
|
||||
});
|
||||
});
|
||||
}
|
||||
// Allow "base" JSON.
|
||||
return this.readCache[uri].then((json) => {
|
||||
if ('undefined' !== typeof json.base) {
|
||||
return this.read(json.base).then((baseJSON) => {
|
||||
return merge({}, baseJSON, json);
|
||||
});
|
||||
}
|
||||
return json;
|
||||
});
|
||||
}
|
||||
|
||||
static uuid() {
|
||||
return uuid();
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.instanceUuid = Resource.uuid();
|
||||
}
|
||||
|
||||
fromJSON({uri, uuid}) {
|
||||
if (uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
if (uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
regenerateUuid() {
|
||||
this.uuid = Resource.uuid();
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
uuid: this.uuid,
|
||||
uri: this.uri,
|
||||
};
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user