Skip to content
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module.exports = {
'jsdoc/check-alignment': 'warn',
'jsdoc/check-param-names': 'warn',
'jsdoc/check-property-names': 'warn',
'jsdoc/check-tag-names': ['warn', { definedTags: ['category'] }],
'jsdoc/check-tag-names': ['warn', { definedTags: ['category', 'internal'] }],
'jsdoc/check-types': 'warn',
'jsdoc/empty-tags': 'warn',
'jsdoc/implements-on-classes': 'warn',
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ and this project adheres to

- Added HYPERLINK function.
[#1215](https://github.com/handsontable/hyperformula/issues/1215)

### Changed

- Optimized method `updateConfig` to rebuild the engine only when new config is different from the old one.
[#1251](https://github.com/handsontable/hyperformula/issues/1251)

### Fixed

Expand Down
71 changes: 44 additions & 27 deletions src/HyperFormula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ export class HyperFormula implements TypedEmitter {
private readonly _emitter: Emitter = new Emitter()
private _evaluationSuspended: boolean = false

/**
* Constructor
*
* @internal
*/
protected constructor(
private _config: Config,
private _stats: Statistics,
Expand Down Expand Up @@ -477,7 +482,7 @@ export class HyperFormula implements TypedEmitter {
*
* @param {string} functionId - function id, e.g. 'SUMIF'
* @param {FunctionPluginDefinition} plugin - plugin class
* @param translations
* @param {FunctionTranslationsPackage} translations - translations for the function name
*
* @throws [[ExpectedValueOfTypeError]] if any of its basic type argument is of wrong type
* @throws [[FunctionPluginValidationError]] when function with a given id does not exist in plugin or plugin class definition is not consistent with metadata
Expand Down Expand Up @@ -967,7 +972,7 @@ export class HyperFormula implements TypedEmitter {
}

/**
* Updates the config with given new metadata.
* Updates the config with given new metadata. It is an expensive operation, as it might trigger rebuilding the engine and recalculation of all formulas.
*
* @param {Partial<ConfigParams>} newParams configuration options to be updated or added
*
Expand All @@ -988,28 +993,13 @@ export class HyperFormula implements TypedEmitter {
* @category Instance
*/
public updateConfig(newParams: Partial<ConfigParams>): void {
const newConfig = this._config.mergeConfig(newParams)

const configNewLanguage = this._config.mergeConfig({language: newParams.language})
const serializedSheets = this._serialization.withNewConfig(configNewLanguage, this._namedExpressions).getAllSheetsSerialized()
const serializedNamedExpressions = this._serialization.getAllNamedExpressionsSerialized()
const isNewConfigTheSame = Object.entries(newParams).every(([key, value]) => this._config[key as keyof ConfigParams] === value)

const newEngine = BuildEngineFactory.rebuildWithConfig(newConfig, serializedSheets, serializedNamedExpressions, this._stats)
if (isNewConfigTheSame) {
return
}

this._config = newEngine.config
this._stats = newEngine.stats
this._dependencyGraph = newEngine.dependencyGraph
this._columnSearch = newEngine.columnSearch
this._parser = newEngine.parser
this._unparser = newEngine.unparser
this._cellContentParser = newEngine.cellContentParser
this._evaluator = newEngine.evaluator
this._lazilyTransformingAstService = newEngine.lazilyTransformingAstService
this._crudOperations = newEngine.crudOperations
this._exporter = newEngine.exporter
this._namedExpressions = newEngine.namedExpressions
this._serialization = newEngine.serialization
this._functionRegistry = newEngine.functionRegistry
this.rebuildWithConfig(newParams)
}

/**
Expand Down Expand Up @@ -1038,7 +1028,7 @@ export class HyperFormula implements TypedEmitter {
* @category Instance
*/
public rebuildAndRecalculate(): void {
this.updateConfig({})
this.rebuildWithConfig({})
}

/**
Expand Down Expand Up @@ -3398,7 +3388,7 @@ export class HyperFormula implements TypedEmitter {
*
* Note that this method may trigger dependency graph recalculation.
*
* @param {() => void} batchOperations
* @param {() => void} batchOperations - a function with operations to be performed
*
* @fires [[valuesUpdated]] if recalculation was triggered by this change
* @fires [[evaluationSuspended]] always
Expand Down Expand Up @@ -4188,9 +4178,9 @@ export class HyperFormula implements TypedEmitter {
* Interprets number as a date.
*
* @param {number} inputNumber - number of days since nullDate, should be non-negative, fractions are ignored.

*
* @throws [[ExpectedValueOfTypeError]] if any of its basic type argument is of wrong type

*
* @example
* ```js
* const hfInstance = HyperFormula.buildEmpty();
Expand Down Expand Up @@ -4336,7 +4326,7 @@ export class HyperFormula implements TypedEmitter {
throw new EvaluationSuspendedError()
}
}

private extractTemporaryFormula(formulaString: string, sheetId: number = 1): { ast?: Ast, address: SimpleCellAddress, dependencies: RelativeDependency[] } {
const parsedCellContent = this._cellContentParser.parse(formulaString)
const address = {sheet: sheetId, col: 0, row: 0}
Expand All @@ -4353,6 +4343,33 @@ export class HyperFormula implements TypedEmitter {
return {ast, address, dependencies}
}

/**
* Rebuilds the engine with new configuration.
*/
private rebuildWithConfig(newParams: Partial<ConfigParams>): void {
const newConfig = this._config.mergeConfig(newParams)
const configNewLanguage = this._config.mergeConfig({language: newParams.language})
const serializedSheets = this._serialization.withNewConfig(configNewLanguage, this._namedExpressions).getAllSheetsSerialized()
const serializedNamedExpressions = this._serialization.getAllNamedExpressionsSerialized()

const newEngine = BuildEngineFactory.rebuildWithConfig(newConfig, serializedSheets, serializedNamedExpressions, this._stats)

this._config = newEngine.config
this._stats = newEngine.stats
this._dependencyGraph = newEngine.dependencyGraph
this._columnSearch = newEngine.columnSearch
this._parser = newEngine.parser
this._unparser = newEngine.unparser
this._cellContentParser = newEngine.cellContentParser
this._evaluator = newEngine.evaluator
this._lazilyTransformingAstService = newEngine.lazilyTransformingAstService
this._crudOperations = newEngine.crudOperations
this._exporter = newEngine.exporter
this._namedExpressions = newEngine.namedExpressions
this._serialization = newEngine.serialization
this._functionRegistry = newEngine.functionRegistry
}

/**
* Runs a recomputation starting from recently changed vertices.
*
Expand Down
14 changes: 13 additions & 1 deletion test/rebuild.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {ErrorType, HyperFormula} from '../src'
import {ErrorMessage} from '../src/error-message'
import {adr, detailedError} from './testUtils'
import {adr, detailedError, resetSpy} from './testUtils'
import {BuildEngineFactory} from '../src/BuildEngineFactory'

describe('Rebuilding engine', () => {
it('should preserve absolute named expression', () => {
Expand Down Expand Up @@ -41,4 +42,15 @@ describe('Rebuilding engine', () => {
engine.rebuildAndRecalculate()
expect(engine.getCellValue(adr('B1'))).toEqual(false)
})

it('rebuildAndRecalculate rebuilds the engine if the config is empty', () => {
const engine = HyperFormula.buildFromArray([[]], {})

const rebuildEngineSpy = spyOn(BuildEngineFactory, 'rebuildWithConfig')
resetSpy(rebuildEngineSpy)

engine.rebuildAndRecalculate()

expect(rebuildEngineSpy).toHaveBeenCalled()
})
})
51 changes: 48 additions & 3 deletions test/update-config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {HyperFormula} from '../src'
import {ErrorType} from '../src/Cell'
import {HyperFormula, ErrorType} from '../src'
import {ErrorMessage} from '../src/error-message'
import {plPL} from '../src/i18n/languages'
import {adr, detailedError} from './testUtils'
import {adr, detailedError, resetSpy} from './testUtils'
import {BuildEngineFactory} from '../src/BuildEngineFactory'

describe('update config', () => {
it('simple reload preserves all values', () => {
Expand Down Expand Up @@ -67,4 +67,49 @@ describe('update config', () => {
expect(engine.getCellValue(adr('A1'))).toBe(true)
expect(engine.getCellValue(adr('B1'))).toBe(false)
})

Comment thread
sequba marked this conversation as resolved.
Resolved
it('doesn\'t rebuild the engine when new config is the same as the old one', () => {
const config = { useArrayArithmetic: true }
const engine = HyperFormula.buildFromArray([[]], config)

const rebuildEngineSpy = spyOn(BuildEngineFactory, 'rebuildWithConfig')
resetSpy(rebuildEngineSpy)

engine.updateConfig(config)
expect(rebuildEngineSpy).not.toHaveBeenCalled()
})

it('rebuilds the engine when new config adds new timeFormat to the same instance of timeFormats array', () => {
const config = { timeFormats: ['hh:mm:ss.sss'] }
const engine = HyperFormula.buildFromArray([[]], config)

const rebuildEngineSpy = spyOn(BuildEngineFactory, 'rebuildWithConfig')
resetSpy(rebuildEngineSpy)

config.timeFormats.push('hh:mm:ss')
engine.updateConfig(config)
expect(rebuildEngineSpy).toHaveBeenCalled()
})

it('rebuilds the engine when new config adds new currencySymbol to the same instance of currencySymbol array', () => {
const config = { currencySymbol: ['$'] }
const engine = HyperFormula.buildFromArray([[]], config)

const rebuildEngineSpy = spyOn(BuildEngineFactory, 'rebuildWithConfig')
resetSpy(rebuildEngineSpy)

config.currencySymbol.push('€')
engine.updateConfig(config)
expect(rebuildEngineSpy).toHaveBeenCalled()
})

it('doesn\'t rebuild the engine when new config contains only the default config values', () => {
const engine = HyperFormula.buildFromArray([[]], {})

const rebuildEngineSpy = spyOn(BuildEngineFactory, 'rebuildWithConfig')
resetSpy(rebuildEngineSpy)

engine.updateConfig({ useColumnIndex: false, functionArgSeparator: ',', undoLimit: 20 })
expect(rebuildEngineSpy).not.toHaveBeenCalled()
})
})