1
0
Fork 0

chore(core): Change init to be private (__init)

This commit is contained in:
Joost De Cock 2022-09-20 15:24:10 +02:00
parent 4a29e25d50
commit 22680fbddc
5 changed files with 73 additions and 73 deletions

View file

@ -80,7 +80,7 @@ Pattern.prototype.addPart = function (part) {
* @return {object} this - The Pattern instance * @return {object} this - The Pattern instance
*/ */
Pattern.prototype.draft = function () { Pattern.prototype.draft = function () {
this.init() this.__init()
this.__runHooks('preDraft') this.__runHooks('preDraft')
// Iterate over the provided sets of settings (typically just one) // Iterate over the provided sets of settings (typically just one)
@ -152,7 +152,7 @@ Pattern.prototype.draft = function () {
* @return {object} config - The initialized config * @return {object} config - The initialized config
*/ */
Pattern.prototype.getConfig = function () { Pattern.prototype.getConfig = function () {
return this.init().config return this.__init().config
} }
/** Returns props required to render this pattern through /** Returns props required to render this pattern through
@ -206,47 +206,13 @@ Pattern.prototype.getRenderProps = function () {
return props return props
} }
/**
* Initializes the pattern coniguration and settings
*
* @return {object} this - The Pattern instance
*/
Pattern.prototype.init = function () {
this.__runHooks('preInit')
/*
* We allow late-stage updating of the design config (adding parts for example)
* so we need to do the things we used to do in the contructor at a later stage.
* This methods does that, and resolves the design config + user settings
*/
this.__resolveParts() // Resolves parts
.__resolveDependencies() // Resolves dependencies
.__resolveDraftOrder() // Resolves draft order
.__loadPlugins() // Loads plugins
.__filterOptionalMeasurements() // Removes required m's from optional list
.__loadConfigData() // Makes config data available in store
.__loadOptionDefaults() // Merges default options with user provided ones
// Say hello
this.stores[0].log.info(
`New \`${this.stores[0].get('data.name', 'No Name')}:` +
`${this.stores[0].get(
'data.version',
'No version'
)}\` pattern using \`@freesewing/core:${version}\``
)
this.stores[0].log.info(`Pattern initialized. Draft order is: ${this.__draftOrder.join(', ')}`)
this.__runHooks('postInit')
return this
}
/** /**
* Handles pattern sampling * Handles pattern sampling
* *
* @return {object} this - The Pattern instance * @return {object} this - The Pattern instance
*/ */
Pattern.prototype.sample = function () { Pattern.prototype.sample = function () {
this.init() this.__init()
if (this.settings[0].sample.type === 'option') { if (this.settings[0].sample.type === 'option') {
return this.sampleOption(this.settings[0].sample.option) return this.sampleOption(this.settings[0].sample.option)
} else if (this.settings[0].sample.type === 'measurement') { } else if (this.settings[0].sample.type === 'measurement') {
@ -265,7 +231,7 @@ Pattern.prototype.sampleMeasurement = function (measurementName) {
this.stores[0].log.debug(`Sampling measurement \`${measurementName}\``) this.stores[0].log.debug(`Sampling measurement \`${measurementName}\``)
this.__runHooks('preSample') this.__runHooks('preSample')
this.__applySettings(this.__measurementSets(measurementName)) this.__applySettings(this.__measurementSets(measurementName))
this.init() this.__init()
this.__runHooks('postSample') this.__runHooks('postSample')
return this.draft() return this.draft()
@ -280,7 +246,7 @@ Pattern.prototype.sampleModels = function (models, focus = false) {
this.stores[0].log.debug(`Sampling models \`${Object.keys(models).join(', ')}\``) this.stores[0].log.debug(`Sampling models \`${Object.keys(models).join(', ')}\``)
this.__runHooks('preSample') this.__runHooks('preSample')
this.__applySettings(this.__modelSets(models, focus)) this.__applySettings(this.__modelSets(models, focus))
this.init() this.__init()
this.__runHooks('postSample') this.__runHooks('postSample')
return this.draft() return this.draft()
@ -295,7 +261,7 @@ Pattern.prototype.sampleOption = function (optionName) {
this.stores[0].log.debug(`Sampling option \`${optionName}\``) this.stores[0].log.debug(`Sampling option \`${optionName}\``)
this.__runHooks('preSample') this.__runHooks('preSample')
this.__applySettings(this.__optionSets(optionName)) this.__applySettings(this.__optionSets(optionName))
this.init() this.__init()
this.__runHooks('postSample') this.__runHooks('postSample')
return this.draft() return this.draft()
@ -460,6 +426,40 @@ Pattern.prototype.__filterOptionalMeasurements = function () {
return this return this
} }
/**
* Initializes the pattern coniguration and settings
*
* @return {object} this - The Pattern instance
*/
Pattern.prototype.__init = function () {
this.__runHooks('preInit')
/*
* We allow late-stage updating of the design config (adding parts for example)
* so we need to do the things we used to do in the contructor at a later stage.
* This methods does that, and resolves the design config + user settings
*/
this.__resolveParts() // Resolves parts
.__resolveDependencies() // Resolves dependencies
.__resolveDraftOrder() // Resolves draft order
.__loadPlugins() // Loads plugins
.__filterOptionalMeasurements() // Removes required m's from optional list
.__loadConfigData() // Makes config data available in store
.__loadOptionDefaults() // Merges default options with user provided ones
// Say hello
this.stores[0].log.info(
`New \`${this.stores[0].get('data.name', 'No Name')}:` +
`${this.stores[0].get(
'data.version',
'No version'
)}\` pattern using \`@freesewing/core:${version}\``
)
this.stores[0].log.info(`Pattern initialized. Draft order is: ${this.__draftOrder.join(', ')}`)
this.__runHooks('postInit')
return this
}
/** /**
* Checks whether a part is hidden in the config * Checks whether a part is hidden in the config
* *

View file

@ -77,7 +77,7 @@ describe('Pattern', () => {
const pattern = new Pattern({ const pattern = new Pattern({
only: ['test.partB'], only: ['test.partB'],
}) })
pattern.init() pattern.__init()
expect(pattern.__needs('test.partA')).to.equal(true) expect(pattern.__needs('test.partA')).to.equal(true)
expect(pattern.__needs('test.partB')).to.equal(true) expect(pattern.__needs('test.partB')).to.equal(true)
expect(pattern.__needs('test.partC')).to.equal(false) expect(pattern.__needs('test.partC')).to.equal(false)
@ -126,7 +126,7 @@ describe('Pattern', () => {
const pattern = new Pattern({ const pattern = new Pattern({
only: ['test.partB'], only: ['test.partB'],
}) })
pattern.init() pattern.__init()
expect(pattern.__wants('test.partA')).to.equal(false) expect(pattern.__wants('test.partA')).to.equal(false)
expect(pattern.__wants('test.partB')).to.equal(true) expect(pattern.__wants('test.partB')).to.equal(true)
expect(pattern.__wants('test.partC')).to.equal(false) expect(pattern.__wants('test.partC')).to.equal(false)

View file

@ -65,7 +65,7 @@ describe('Pattern', () => {
}) })
}) })
describe('Pattern.init()', () => { describe('Pattern.__init()', () => {
const partA = { const partA = {
name: 'test.partA', name: 'test.partA',
measurements: ['head', 'knee'], measurements: ['head', 'knee'],
@ -112,27 +112,27 @@ describe('Pattern', () => {
parts: [partC], parts: [partC],
}) })
const pattern = new Pattern() const pattern = new Pattern()
pattern.init() pattern.__init()
it('Pattern.init() should resolve all measurements', () => { it('Pattern.__init() should resolve all measurements', () => {
expect( expect(
[...pattern.config.measurements, ...pattern.config.optionalMeasurements].length [...pattern.config.measurements, ...pattern.config.optionalMeasurements].length
).to.equal(4) ).to.equal(4)
}) })
it('Pattern.init() should resolve required measurements', () => { it('Pattern.__init() should resolve required measurements', () => {
expect(pattern.config.measurements.length).to.equal(2) expect(pattern.config.measurements.length).to.equal(2)
expect(pattern.config.measurements[0]).to.equal('head') expect(pattern.config.measurements[0]).to.equal('head')
expect(pattern.config.measurements[1]).to.equal('knee') expect(pattern.config.measurements[1]).to.equal('knee')
}) })
it('Pattern.init() should resolve optional measurements', () => { it('Pattern.__init() should resolve optional measurements', () => {
expect(pattern.config.optionalMeasurements.length).to.equal(2) expect(pattern.config.optionalMeasurements.length).to.equal(2)
expect(pattern.config.optionalMeasurements[0]).to.equal('chest') expect(pattern.config.optionalMeasurements[0]).to.equal('chest')
expect(pattern.config.optionalMeasurements[1]).to.equal('waist') expect(pattern.config.optionalMeasurements[1]).to.equal('waist')
}) })
it('Pattern.init() should resolve options', () => { it('Pattern.__init() should resolve options', () => {
expect(Object.keys(pattern.config.options).length).to.equal(3) expect(Object.keys(pattern.config.options).length).to.equal(3)
for (const [key, value] of Object.entries(partA.options.optA)) { for (const [key, value] of Object.entries(partA.options.optA)) {
expect(pattern.config.options.optA[key]).to.equal(value) expect(pattern.config.options.optA[key]).to.equal(value)
@ -145,20 +145,20 @@ describe('Pattern', () => {
} }
}) })
it('Pattern.init() should resolve parts', () => { it('Pattern.__init() should resolve parts', () => {
expect(pattern.config.parts.length).to.equal(3) expect(pattern.config.parts.length).to.equal(3)
}) })
it('Pattern.init() should resolve plugins', () => { it('Pattern.__init() should resolve plugins', () => {
expect(pattern.config.plugins.length).to.equal(1) expect(pattern.config.plugins.length).to.equal(1)
}) })
it('Pattern.init() should set config data in the store', () => { it('Pattern.__init() should set config data in the store', () => {
expect(pattern.stores[0].get('data.name')).to.equal('test') expect(pattern.stores[0].get('data.name')).to.equal('test')
expect(pattern.stores[0].get('data.version')).to.equal('1.2.3') expect(pattern.stores[0].get('data.version')).to.equal('1.2.3')
}) })
it('Pattern.init() should resolve dependencies', () => { it('Pattern.__init() should resolve dependencies', () => {
expect(typeof pattern.config.resolvedDependencies).to.equal('object') expect(typeof pattern.config.resolvedDependencies).to.equal('object')
expect(Array.isArray(pattern.config.resolvedDependencies['test.partA'])).to.equal(true) expect(Array.isArray(pattern.config.resolvedDependencies['test.partA'])).to.equal(true)
expect(pattern.config.resolvedDependencies['test.partA'].length).to.equal(0) expect(pattern.config.resolvedDependencies['test.partA'].length).to.equal(0)
@ -175,7 +175,7 @@ describe('Pattern', () => {
).to.equal(true) ).to.equal(true)
}) })
it('Pattern.init() should resolve the draft order', () => { it('Pattern.__init() should resolve the draft order', () => {
expect(Array.isArray(pattern.config.draftOrder)).to.equal(true) expect(Array.isArray(pattern.config.draftOrder)).to.equal(true)
expect(pattern.config.draftOrder[0]).to.equal('test.partA') expect(pattern.config.draftOrder[0]).to.equal('test.partA')
expect(pattern.config.draftOrder[1]).to.equal('test.partB') expect(pattern.config.draftOrder[1]).to.equal('test.partB')
@ -184,7 +184,7 @@ describe('Pattern', () => {
// I am aware this does too much for one unit test, but this is to simplify TDD // I am aware this does too much for one unit test, but this is to simplify TDD
// we can split it up later // we can split it up later
it('Pattern.init() should resolve nested injections', () => { it('Pattern.__init() should resolve nested injections', () => {
const partA = { const partA = {
name: 'partA', name: 'partA',
options: { optionA: { bool: true } }, options: { optionA: { bool: true } },
@ -336,7 +336,7 @@ describe('Pattern', () => {
expect(pattern.parts[0].partR.paths.r.ops[1].to.y).to.equal(44) expect(pattern.parts[0].partR.paths.r.ops[1].to.y).to.equal(44)
}) })
it('Pattern.init() should resolve nested dependencies', () => { it('Pattern.__init() should resolve nested dependencies', () => {
const partA = { const partA = {
name: 'partA', name: 'partA',
options: { optionA: { bool: true } }, options: { optionA: { bool: true } },
@ -478,7 +478,7 @@ describe('Pattern', () => {
expect(pattern.parts[0].partD.paths.d.ops[1].to.y).to.equal(44) expect(pattern.parts[0].partD.paths.d.ops[1].to.y).to.equal(44)
}) })
it('Pattern.init() should load a single plugin', () => { it('Pattern.__init() should load a single plugin', () => {
const plugin = { const plugin = {
name: 'example', name: 'example',
version: 1, version: 1,
@ -495,11 +495,11 @@ describe('Pattern', () => {
} }
const design = new Design({ parts: [part] }) const design = new Design({ parts: [part] })
const pattern = new design() const pattern = new design()
pattern.init() pattern.__init()
expect(pattern.hooks.preRender.length).to.equal(1) expect(pattern.hooks.preRender.length).to.equal(1)
}) })
it('Pattern.init() should load array of plugins', () => { it('Pattern.__init() should load array of plugins', () => {
const plugin1 = { const plugin1 = {
name: 'example1', name: 'example1',
version: 1, version: 1,
@ -521,10 +521,10 @@ describe('Pattern', () => {
const design = new Design({ plugins: [plugin1, plugin2] }) const design = new Design({ plugins: [plugin1, plugin2] })
const pattern = new design() const pattern = new design()
pattern.init() pattern.__init()
expect(pattern.hooks.preRender.length).to.equal(2) expect(pattern.hooks.preRender.length).to.equal(2)
}) })
it('Pattern.init() should load conditional plugin', () => { it('Pattern.__init() should load conditional plugin', () => {
const plugin = { const plugin = {
name: 'example', name: 'example',
version: 1, version: 1,
@ -537,11 +537,11 @@ describe('Pattern', () => {
const condition = () => true const condition = () => true
const design = new Design({ plugins: [{ plugin, condition }] }) const design = new Design({ plugins: [{ plugin, condition }] })
const pattern = new design() const pattern = new design()
pattern.init() pattern.__init()
expect(pattern.hooks.preRender.length).to.equal(1) expect(pattern.hooks.preRender.length).to.equal(1)
}) })
it('Pattern.init() should not load conditional plugin', () => { it('Pattern.__init() should not load conditional plugin', () => {
const plugin = { const plugin = {
name: 'example', name: 'example',
version: 1, version: 1,
@ -557,7 +557,7 @@ describe('Pattern', () => {
expect(pattern.hooks.preRender.length).to.equal(0) expect(pattern.hooks.preRender.length).to.equal(0)
}) })
it('Pattern.init() should load multiple conditional plugins', () => { it('Pattern.__init() should load multiple conditional plugins', () => {
const plugin = { const plugin = {
name: 'example', name: 'example',
version: 1, version: 1,
@ -576,7 +576,7 @@ describe('Pattern', () => {
], ],
}) })
const pattern = new design() const pattern = new design()
pattern.init() pattern.__init()
expect(pattern.hooks.preRender.length).to.equal(1) expect(pattern.hooks.preRender.length).to.equal(1)
}) })
@ -621,11 +621,11 @@ describe('Pattern', () => {
parts: [ part1, part2 ] parts: [ part1, part2 ]
}) })
const pattern = new design() const pattern = new design()
pattern.init() pattern.__init()
expect(pattern.hooks.preRender.length).to.equal(2) expect(pattern.hooks.preRender.length).to.equal(2)
}) })
it('Pattern.init() should register a hook via on', () => { it('Pattern.__init() should register a hook via on', () => {
const Pattern = new Design() const Pattern = new Design()
const pattern = new Pattern() const pattern = new Pattern()
let count = 0 let count = 0
@ -636,7 +636,7 @@ describe('Pattern', () => {
expect(count).to.equal(1) expect(count).to.equal(1)
}) })
it('Pattern.init() should register a hook from a plugin', () => { it('Pattern.__init() should register a hook from a plugin', () => {
const Pattern = new Design() const Pattern = new Design()
const pattern = new Pattern() const pattern = new Pattern()
let count = 0 let count = 0
@ -655,7 +655,7 @@ describe('Pattern', () => {
expect(count).to.equal(1) expect(count).to.equal(1)
}) })
it('Pattern.init() should register multiple methods on a single hook', () => { it('Pattern.__init() should register multiple methods on a single hook', () => {
const plugin = { const plugin = {
name: 'test', name: 'test',
version: '0.1-test', version: '0.1-test',
@ -737,7 +737,7 @@ describe('Pattern', () => {
parts: [part], parts: [part],
}) })
const pattern = new Pattern() const pattern = new Pattern()
pattern.init() pattern.__init()
it('Pattern settings should contain percentage options', () => { it('Pattern settings should contain percentage options', () => {
expect(pattern.settings[0].options.pct).to.equal(0.3) expect(pattern.settings[0].options.pct).to.equal(0.3)
@ -774,7 +774,7 @@ describe('Pattern', () => {
parts: [part], parts: [part],
}) })
const pattern = new Pattern() const pattern = new Pattern()
expect(() => pattern.init()).to.throw() expect(() => pattern.__init()).to.throw()
}) })
}) })
}) })

View file

@ -104,7 +104,7 @@ describe('Pattern', () => {
} }
const design = new Design() const design = new Design()
const pattern = new design({ only: ['test']}) const pattern = new design({ only: ['test']})
pattern.init() pattern.__init()
expect(pattern.__isPartHidden('test')).to.equal(false) expect(pattern.__isPartHidden('test')).to.equal(false)
}) })

View file

@ -5,7 +5,7 @@ const expect = chai.expect
describe('Stacks', () => { describe('Stacks', () => {
describe('Pattern.init()', () => { describe('Pattern.__init()', () => {
const partA = { const partA = {
name: 'test.partA', name: 'test.partA',
measurements: ['head'], measurements: ['head'],
@ -69,7 +69,7 @@ describe('Stacks', () => {
}) })
pattern.draft() pattern.draft()
it('Pattern.init() should resolve dependencies', () => { it('Pattern.__init() should resolve dependencies', () => {
expect(typeof pattern.config.resolvedDependencies).to.equal('object') expect(typeof pattern.config.resolvedDependencies).to.equal('object')
expect(Array.isArray(pattern.config.resolvedDependencies['test.partA'])).to.equal(true) expect(Array.isArray(pattern.config.resolvedDependencies['test.partA'])).to.equal(true)
expect(pattern.config.resolvedDependencies['test.partA'].length).to.equal(0) expect(pattern.config.resolvedDependencies['test.partA'].length).to.equal(0)