1
0
Fork 0

feat(core): Add default logging to store

This commit is contained in:
Joost De Cock 2022-09-07 16:55:44 +02:00
parent 575d7c3de8
commit 411f617ffe
3 changed files with 44 additions and 10 deletions

View file

@ -86,7 +86,7 @@ export function Pattern(config = { options: {} }) {
this.is = '' // Will be set when drafting/sampling this.is = '' // Will be set when drafting/sampling
this.autoLayout = { parts: {} } // Will hold auto-generated layout this.autoLayout = { parts: {} } // Will hold auto-generated layout
this.cutList = {} // Will hold the cutlist this.cutList = {} // Will hold the cutlist
this.store = new Store([[ 'emit', this.raise]]) // Store for sharing data across parts this.store = new Store([[ 'log', this.raise]]) // Store for sharing data across parts
this.parts = {} // Parts container this.parts = {} // Parts container
this.hooks = new Hooks() // Hooks container this.hooks = new Hooks() // Hooks container
this.Point = Point // Point constructor this.Point = Point // Point constructor

View file

@ -11,6 +11,32 @@ export function Store(methods=[]) {
} else set(this, ...method) } else set(this, ...method)
} }
/*
* Default logging methods
* You can override these with a plugin
*/
const logs = {
debug: [],
info: [],
warning: [],
error: [],
}
this.log = {
debug: function (...data) {
logs.debug.push(...data)
},
info: function (...data) {
logs.info.push(...data)
},
warning: function (...data) {
logs.warning.push(...data)
},
error: function (...data) {
logs.error.push(...data)
},
}
this.logs = logs
return this return this
} }
@ -18,8 +44,11 @@ export function Store(methods=[]) {
Store.prototype.extend = function (...methods) { Store.prototype.extend = function (...methods) {
for (const [path, method] of methods) { for (const [path, method] of methods) {
if (avoid.indexOf(method[0]) !== -1) { if (avoid.indexOf(method[0]) !== -1) {
console.log(`WARNING: You can't squat ${method[0]}in the store`) this.log.warning(`You can't squat ${method[0]}in the store`)
} else set(this, path, (...args) => method(this, ...args)) } else {
this.log.info(`Extending store with ${path}`)
set(this, path, (...args) => method(this, ...args))
}
} }
return this return this
@ -27,6 +56,9 @@ Store.prototype.extend = function (...methods) {
/** Set key at path to value */ /** Set key at path to value */
Store.prototype.set = function (path, value) { Store.prototype.set = function (path, value) {
if (typeof value === 'undefined') {
this.log.warning(`Store.set(value) on key \`${path}\`, but value is undefined`)
}
set(this, path, value) set(this, path, value)
return this return this
@ -34,6 +66,9 @@ Store.prototype.set = function (path, value) {
/** Set key at path to value, but only if it's not currently set */ /** Set key at path to value, but only if it's not currently set */
Store.prototype.setIfUnset = function (path, value) { Store.prototype.setIfUnset = function (path, value) {
if (typeof value === 'undefined') {
this.log.warning(`Store.setIfUnset(value) on key \`${path}\`, but value is undefined`)
}
if (typeof get(this, path) === 'undefined') { if (typeof get(this, path) === 'undefined') {
return set(this, path, value) return set(this, path, value)
} }
@ -46,6 +81,8 @@ Store.prototype.push = function (path, ...values) {
const arr = get(this, path) const arr = get(this, path)
if (Array.isArray(arr)) { if (Array.isArray(arr)) {
return this.set(path, [...arr, ...values]) return this.set(path, [...arr, ...values])
} else {
this.log.warning(`Store.push(value) on key \`${path}\`, but key does not hold an array`)
} }
return this return this
@ -62,9 +99,7 @@ Store.prototype.unset = function (path) {
Store.prototype.get = function (path) { Store.prototype.get = function (path) {
const val = get(this, path) const val = get(this, path)
if (typeof val === 'undefined') { if (typeof val === 'undefined') {
const msg = `Tried to access \`${path}\` in the \`store\` but it is not set` this.log.warning(`Store.get(key) on key \`${path}\`, which is undefined`)
if (typeof this.emit?.warning === 'function') this.emit.warning(msg)
else console.log(msg)
} }
return val return val

View file

@ -31,12 +31,11 @@ describe('Store', () => {
}); });
it("Should emit a warning when retrieving a invalid key", () => { it("Should emit a warning when retrieving a invalid key", () => {
const events = []
const warning = msg => events.push(msg) const warning = msg => events.push(msg)
const store = new Store([[ "emit.warning", warning]]) const store = new Store()
store.get('nope') store.get('nope')
expect(events.length).to.equal(1) expect(store.get('logs.warning').length).to.equal(1)
expect(events[0]).to.equal('Tried to access `nope` in the `store` but it is not set') expect(store.get('logs.warning')[0]).to.equal('Store.get(key) on key `nope`, which is undefined')
}); });
it("Should add methods to the store from a plugin", () => { it("Should add methods to the store from a plugin", () => {