diff --git a/packages/core/src/pattern.mjs b/packages/core/src/pattern.mjs index 365544b9455..a594d5bd6dc 100644 --- a/packages/core/src/pattern.mjs +++ b/packages/core/src/pattern.mjs @@ -86,7 +86,7 @@ export function Pattern(config = { options: {} }) { this.is = '' // Will be set when drafting/sampling this.autoLayout = { parts: {} } // Will hold auto-generated layout 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.hooks = new Hooks() // Hooks container this.Point = Point // Point constructor diff --git a/packages/core/src/store.mjs b/packages/core/src/store.mjs index f1fb1a2c7a2..cb708a8ae79 100644 --- a/packages/core/src/store.mjs +++ b/packages/core/src/store.mjs @@ -11,6 +11,32 @@ export function Store(methods=[]) { } 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 } @@ -18,8 +44,11 @@ export function Store(methods=[]) { Store.prototype.extend = function (...methods) { for (const [path, method] of methods) { if (avoid.indexOf(method[0]) !== -1) { - console.log(`WARNING: You can't squat ${method[0]}in the store`) - } else set(this, path, (...args) => method(this, ...args)) + this.log.warning(`You can't squat ${method[0]}in the store`) + } else { + this.log.info(`Extending store with ${path}`) + set(this, path, (...args) => method(this, ...args)) + } } return this @@ -27,6 +56,9 @@ Store.prototype.extend = function (...methods) { /** Set key at path to 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) 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 */ 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') { return set(this, path, value) } @@ -46,6 +81,8 @@ Store.prototype.push = function (path, ...values) { const arr = get(this, path) if (Array.isArray(arr)) { 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 @@ -62,9 +99,7 @@ Store.prototype.unset = function (path) { Store.prototype.get = function (path) { const val = get(this, path) if (typeof val === 'undefined') { - const msg = `Tried to access \`${path}\` in the \`store\` but it is not set` - if (typeof this.emit?.warning === 'function') this.emit.warning(msg) - else console.log(msg) + this.log.warning(`Store.get(key) on key \`${path}\`, which is undefined`) } return val diff --git a/packages/core/tests/store.test.mjs b/packages/core/tests/store.test.mjs index a207bff5707..b15a2bd7b4d 100644 --- a/packages/core/tests/store.test.mjs +++ b/packages/core/tests/store.test.mjs @@ -31,12 +31,11 @@ describe('Store', () => { }); it("Should emit a warning when retrieving a invalid key", () => { - const events = [] const warning = msg => events.push(msg) - const store = new Store([[ "emit.warning", warning]]) + const store = new Store() store.get('nope') - expect(events.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').length).to.equal(1) + 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", () => {