Skip to content
This repository has been archived by the owner on Aug 27, 2018. It is now read-only.

Welcome Atomics! #296

Merged
merged 10 commits into from
Jul 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ keyword. `exports.run = __async__ (client, msg, [...args])`.
command.

### Changed
- [[#296](https://github.com/dirigeants/komada/pull/296)] **[Update]** The JSON provider and schemaManager now uses atomics.
- [[#293](https://github.com/dirigeants/komada/pull/293)] **[Performance]** Faster prefix check and resolve for prefixes
stored inside an Array.
- [[#262](https://github.com/dirigeants/komada/pull/262)] **[Performance && Cleanup]** Refactored several pieces.
Expand Down
18 changes: 9 additions & 9 deletions classes/schemaManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SchemaManager {
await fs.ensureDir(baseDir);
this.filePath = resolve(baseDir, "schema.json");
const schema = await fs.readJSON(this.filePath)
.catch(() => fs.outputJSON(this.filePath, this.defaultDataSchema).then(() => this.defaultDataSchema));
.catch(() => fs.outputJSONAtomic(this.filePath, this.defaultDataSchema).then(() => this.defaultDataSchema));
return this.validate(schema);
}

Expand Down Expand Up @@ -60,9 +60,9 @@ class SchemaManager {
* @param {number} options.min The min value for the key (String.length for String, value for number).
* @param {number} options.max The max value for the key (String.length for String, value for number).
* @param {boolean} [force=false] Whether this change should modify all configurations or not.
* @returns {void}
* @returns {Promise<void>}
*/
add(key, options, force = false) {
async add(key, options, force = false) {
if (key in this.schema) throw `The key ${key} already exists in the current schema.`;
if (!options.type) throw "The option type is required.";
if (!validTypes.includes(options.type)) throw `The type ${options.type} is not supported.`;
Expand All @@ -79,21 +79,21 @@ class SchemaManager {
if (this.settingGateway.sql) options.sql = this.settingGateway.sql.buildSingleSQLSchema(options);
this.schema[key] = options;
this.defaults[key] = options.default;
if (force) this.force("add", key);
return fs.outputJSON(this.filePath, this.schema);
if (force) await this.force("add", key);
return fs.outputJSONAtomic(this.filePath, this.schema);
}

/**
* Remove a key from the schema.
* @param {string} key The key to remove.
* @param {boolean} [force=false] Whether this change should modify all configurations or not.
* @returns {void}
* @returns {Promise<void>}
*/
remove(key, force = false) {
async remove(key, force = false) {
if (key === "prefix") throw "You can't remove the prefix key.";
delete this.schema[key];
if (force) this.force("delete", key);
return fs.outputJSON(this.filePath, this.schema);
if (force) await this.force("delete", key);
return fs.outputJSONAtomic(this.filePath, this.schema);
}

/**
Expand Down
60 changes: 37 additions & 23 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"test": "eslint classes commands finalizers functions inhibitors events extendables"
},
"dependencies": {
"chalk": "^1.1.3",
"discord.js": "hydrabolt/discord.js",
"fs-nextra": "^0.1.2",
"chalk": "^2.0.1",
"discord.js": "github:hydrabolt/discord.js#master",
"fs-nextra": "^0.1.4",
"moment": "^2.18.1",
"moment-duration-format": "^1.3.0",
"performance-now": "^2.1.0"
Expand Down
6 changes: 3 additions & 3 deletions providers/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ exports.getRandom = table => this.getAll(table).then(data => data[Math.floor(Mat
* @param {Object} data The object with all properties you want to insert into the document.
* @returns {Promise<Void>}
*/
exports.create = (table, document, data) => fs.outputJSON(resolve(baseDir, table, `${document}.json`), Object.assign(data, { id: document }));
exports.create = (table, document, data) => fs.outputJSONAtomic(resolve(baseDir, table, `${document}.json`), Object.assign(data, { id: document }));
exports.set = (...args) => this.create(...args);
exports.insert = (...args) => this.create(...args);

Expand All @@ -88,7 +88,7 @@ exports.insert = (...args) => this.create(...args);
* @returns {Promise<Void>}
*/
exports.update = (table, document, data) => this.get(table, document)
.then(current => fs.outputJSON(resolve(baseDir, table, `${document}.json`), Object.assign(current, data)));
.then(current => fs.outputJSONAtomic(resolve(baseDir, table, `${document}.json`), Object.assign(current, data)));

/**
* Replace all the data from a document.
Expand All @@ -97,7 +97,7 @@ exports.update = (table, document, data) => this.get(table, document)
* @param {Object} data The new data for the document.
* @returns {Promise<Void>}
*/
exports.replace = (table, document, data) => fs.outputJSON(resolve(baseDir, table, `${document}.json`), data);
exports.replace = (table, document, data) => fs.outputJSONAtomic(resolve(baseDir, table, `${document}.json`), data);

/**
* Delete a document from the table.
Expand Down