sling-framework
Advanced tools
Comparing version 1.12.2 to 1.12.3
@@ -59,2 +59,4 @@ "use strict"; | ||
var CHANGES_QUEUE = Symbol('CHANGES_QUEUE'); | ||
var withRequestParams = function withRequestParams() { | ||
@@ -101,2 +103,3 @@ var Base = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : | ||
_this.requestParams = {}; | ||
_this[CHANGES_QUEUE] = []; | ||
@@ -124,2 +127,4 @@ _this.constructor.requestAttrNames.forEach(function (attrName) { | ||
value: function attributeChangedCallback(attrName, oldValue, newValue) { | ||
var _this2 = this; | ||
var _this$constructor = this.constructor, | ||
@@ -129,5 +134,6 @@ requestAttrNames = _this$constructor.requestAttrNames, | ||
var requestAttrIndex = requestAttrNames.indexOf(attrName); | ||
var shouldTrigger = requestAttrIndex > -1 && (0, _slingHelpers.isFunction)(this.requestParamsChangedCallback) && oldValue !== newValue; | ||
var shouldUpdate = requestAttrIndex > -1 && oldValue !== newValue; | ||
var shouldTrigger = (0, _slingHelpers.isFunction)(this.requestParamsChangedCallback); | ||
if (shouldTrigger) { | ||
if (shouldUpdate) { | ||
var changedParamName = requestParamNames[requestAttrIndex]; | ||
@@ -137,4 +143,17 @@ | ||
this.requestParams = Object.entries(_objectSpread({}, this.requestParams, changedParam)).filter(isValidEntry).reduce(_slingHelpers.toFlatEntries, {}); | ||
this.requestParamsChangedCallback(this.requestParams, changedParam); | ||
this[CHANGES_QUEUE].push(changedParam); | ||
var queueSize = this[CHANGES_QUEUE].length; | ||
Promise.resolve().then(function () { | ||
if (_this2[CHANGES_QUEUE].length === queueSize) { | ||
var allChanges = _this2[CHANGES_QUEUE].reduce(_slingHelpers.toFlatObject, {}); | ||
_this2.requestParams = Object.entries(_objectSpread({}, _this2.requestParams, allChanges)).filter(isValidEntry).reduce(_slingHelpers.toFlatEntries, {}); | ||
if (shouldTrigger) { | ||
_this2.requestParamsChangedCallback(_this2.requestParams, allChanges); | ||
} | ||
_this2[CHANGES_QUEUE] = []; | ||
} | ||
}); | ||
} | ||
@@ -141,0 +160,0 @@ |
@@ -12,2 +12,4 @@ "use strict"; | ||
const CHANGES_QUEUE = Symbol('CHANGES_QUEUE'); | ||
const withRequestParams = (Base = class {}) => class extends Base { | ||
@@ -29,2 +31,3 @@ static get requestParamNames() { | ||
this.requestParams = {}; | ||
this[CHANGES_QUEUE] = []; | ||
this.constructor.requestAttrNames.forEach(attrName => { | ||
@@ -54,5 +57,6 @@ Object.defineProperty(this, attrName, { | ||
const requestAttrIndex = requestAttrNames.indexOf(attrName); | ||
const shouldTrigger = requestAttrIndex > -1 && (0, _slingHelpers.isFunction)(this.requestParamsChangedCallback) && oldValue !== newValue; | ||
const shouldUpdate = requestAttrIndex > -1 && oldValue !== newValue; | ||
const shouldTrigger = (0, _slingHelpers.isFunction)(this.requestParamsChangedCallback); | ||
if (shouldTrigger) { | ||
if (shouldUpdate) { | ||
const changedParamName = requestParamNames[requestAttrIndex]; | ||
@@ -62,6 +66,18 @@ const changedParam = { | ||
}; | ||
this.requestParams = Object.entries({ ...this.requestParams, | ||
...changedParam | ||
}).filter(isValidEntry).reduce(_slingHelpers.toFlatEntries, {}); | ||
this.requestParamsChangedCallback(this.requestParams, changedParam); | ||
this[CHANGES_QUEUE].push(changedParam); | ||
const queueSize = this[CHANGES_QUEUE].length; | ||
Promise.resolve().then(() => { | ||
if (this[CHANGES_QUEUE].length === queueSize) { | ||
const allChanges = this[CHANGES_QUEUE].reduce(_slingHelpers.toFlatObject, {}); | ||
this.requestParams = Object.entries({ ...this.requestParams, | ||
...allChanges | ||
}).filter(isValidEntry).reduce(_slingHelpers.toFlatEntries, {}); | ||
if (shouldTrigger) { | ||
this.requestParamsChangedCallback(this.requestParams, allChanges); | ||
} | ||
this[CHANGES_QUEUE] = []; | ||
} | ||
}); | ||
} | ||
@@ -68,0 +84,0 @@ |
@@ -43,3 +43,3 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
import { isFunction, toFlatEntries } from 'sling-helpers'; | ||
import { isFunction, toFlatEntries, toFlatObject } from 'sling-helpers'; | ||
@@ -53,2 +53,3 @@ var isValidEntry = function isValidEntry(_ref) { | ||
var CHANGES_QUEUE = Symbol('CHANGES_QUEUE'); | ||
export var withRequestParams = function withRequestParams() { | ||
@@ -95,2 +96,3 @@ var Base = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : | ||
_this.requestParams = {}; | ||
_this[CHANGES_QUEUE] = []; | ||
@@ -118,2 +120,4 @@ _this.constructor.requestAttrNames.forEach(function (attrName) { | ||
value: function attributeChangedCallback(attrName, oldValue, newValue) { | ||
var _this2 = this; | ||
var _this$constructor = this.constructor, | ||
@@ -123,5 +127,6 @@ requestAttrNames = _this$constructor.requestAttrNames, | ||
var requestAttrIndex = requestAttrNames.indexOf(attrName); | ||
var shouldTrigger = requestAttrIndex > -1 && isFunction(this.requestParamsChangedCallback) && oldValue !== newValue; | ||
var shouldUpdate = requestAttrIndex > -1 && oldValue !== newValue; | ||
var shouldTrigger = isFunction(this.requestParamsChangedCallback); | ||
if (shouldTrigger) { | ||
if (shouldUpdate) { | ||
var changedParamName = requestParamNames[requestAttrIndex]; | ||
@@ -131,4 +136,17 @@ | ||
this.requestParams = Object.entries(_objectSpread({}, this.requestParams, changedParam)).filter(isValidEntry).reduce(toFlatEntries, {}); | ||
this.requestParamsChangedCallback(this.requestParams, changedParam); | ||
this[CHANGES_QUEUE].push(changedParam); | ||
var queueSize = this[CHANGES_QUEUE].length; | ||
Promise.resolve().then(function () { | ||
if (_this2[CHANGES_QUEUE].length === queueSize) { | ||
var allChanges = _this2[CHANGES_QUEUE].reduce(toFlatObject, {}); | ||
_this2.requestParams = Object.entries(_objectSpread({}, _this2.requestParams, allChanges)).filter(isValidEntry).reduce(toFlatEntries, {}); | ||
if (shouldTrigger) { | ||
_this2.requestParamsChangedCallback(_this2.requestParams, allChanges); | ||
} | ||
_this2[CHANGES_QUEUE] = []; | ||
} | ||
}); | ||
} | ||
@@ -135,0 +153,0 @@ |
@@ -1,5 +0,7 @@ | ||
import { isFunction, toFlatEntries } from 'sling-helpers'; | ||
import { isFunction, toFlatEntries, toFlatObject } from 'sling-helpers'; | ||
const isValidEntry = ([, value]) => value != null && value !== ''; | ||
const CHANGES_QUEUE = Symbol('CHANGES_QUEUE'); | ||
export const withRequestParams = (Base = class {}) => | ||
@@ -25,2 +27,3 @@ class extends Base { | ||
this.requestParams = {}; | ||
this[CHANGES_QUEUE] = []; | ||
@@ -48,19 +51,30 @@ this.constructor.requestAttrNames | ||
const shouldTrigger = requestAttrIndex > -1 && | ||
isFunction(this.requestParamsChangedCallback) && | ||
oldValue !== newValue; | ||
const shouldUpdate = requestAttrIndex > -1 && oldValue !== newValue; | ||
const shouldTrigger = isFunction(this.requestParamsChangedCallback); | ||
if (shouldTrigger) { | ||
if (shouldUpdate) { | ||
const changedParamName = requestParamNames[requestAttrIndex]; | ||
const changedParam = { [changedParamName]: newValue || null }; | ||
this[CHANGES_QUEUE].push(changedParam); | ||
const queueSize = this[CHANGES_QUEUE].length; | ||
this.requestParams = Object | ||
.entries({ | ||
...this.requestParams, | ||
...changedParam, | ||
}) | ||
.filter(isValidEntry) | ||
.reduce(toFlatEntries, {}); | ||
Promise.resolve().then(() => { | ||
if (this[CHANGES_QUEUE].length === queueSize) { | ||
const allChanges = this[CHANGES_QUEUE].reduce(toFlatObject, {}); | ||
this.requestParamsChangedCallback(this.requestParams, changedParam); | ||
this.requestParams = Object | ||
.entries({ | ||
...this.requestParams, | ||
...allChanges, | ||
}) | ||
.filter(isValidEntry) | ||
.reduce(toFlatEntries, {}); | ||
if (shouldTrigger) { | ||
this.requestParamsChangedCallback(this.requestParams, allChanges); | ||
} | ||
this[CHANGES_QUEUE] = []; | ||
} | ||
}); | ||
} | ||
@@ -67,0 +81,0 @@ |
{ | ||
"name": "sling-framework", | ||
"version": "1.12.2", | ||
"version": "1.12.3", | ||
"description": "Sling Framework", | ||
@@ -10,4 +10,4 @@ "module": "src/index.js", | ||
"dependencies": { | ||
"sling-helpers": "^1.12.2" | ||
"sling-helpers": "^1.12.3" | ||
} | ||
} |
@@ -1,5 +0,7 @@ | ||
import { isFunction, toFlatEntries } from 'sling-helpers'; | ||
import { isFunction, toFlatEntries, toFlatObject } from 'sling-helpers'; | ||
const isValidEntry = ([, value]) => value != null && value !== ''; | ||
const CHANGES_QUEUE = Symbol('CHANGES_QUEUE'); | ||
export const withRequestParams = (Base = class {}) => | ||
@@ -25,2 +27,3 @@ class extends Base { | ||
this.requestParams = {}; | ||
this[CHANGES_QUEUE] = []; | ||
@@ -48,19 +51,30 @@ this.constructor.requestAttrNames | ||
const shouldTrigger = requestAttrIndex > -1 && | ||
isFunction(this.requestParamsChangedCallback) && | ||
oldValue !== newValue; | ||
const shouldUpdate = requestAttrIndex > -1 && oldValue !== newValue; | ||
const shouldTrigger = isFunction(this.requestParamsChangedCallback); | ||
if (shouldTrigger) { | ||
if (shouldUpdate) { | ||
const changedParamName = requestParamNames[requestAttrIndex]; | ||
const changedParam = { [changedParamName]: newValue || null }; | ||
this[CHANGES_QUEUE].push(changedParam); | ||
const queueSize = this[CHANGES_QUEUE].length; | ||
this.requestParams = Object | ||
.entries({ | ||
...this.requestParams, | ||
...changedParam, | ||
}) | ||
.filter(isValidEntry) | ||
.reduce(toFlatEntries, {}); | ||
Promise.resolve().then(() => { | ||
if (this[CHANGES_QUEUE].length === queueSize) { | ||
const allChanges = this[CHANGES_QUEUE].reduce(toFlatObject, {}); | ||
this.requestParamsChangedCallback(this.requestParams, changedParam); | ||
this.requestParams = Object | ||
.entries({ | ||
...this.requestParams, | ||
...allChanges, | ||
}) | ||
.filter(isValidEntry) | ||
.reduce(toFlatEntries, {}); | ||
if (shouldTrigger) { | ||
this.requestParamsChangedCallback(this.requestParams, allChanges); | ||
} | ||
this[CHANGES_QUEUE] = []; | ||
} | ||
}); | ||
} | ||
@@ -67,0 +81,0 @@ |
@@ -178,6 +178,6 @@ import chai from 'chai'; | ||
it('Should call requestParamsChangedCallback if a request ' + | ||
'param changes.', () => { | ||
'param changes.', async () => { | ||
const dummy = new Dummy(); | ||
dummy.requestParamsChangedCallback = sinon.spy(); | ||
dummy.attributeChangedCallback('startdate', '20180901', '20180915'); | ||
await dummy.attributeChangedCallback('startdate', '20180901', '20180915'); | ||
expect(dummy.requestParamsChangedCallback).to.have.been.called; | ||
@@ -187,6 +187,6 @@ }); | ||
it('Should not call requestParamsChangedCallback if a request param ' + | ||
'changes to the previous value.', () => { | ||
'changes to the previous value.', async () => { | ||
const dummy = new Dummy(); | ||
dummy.requestParamsChangedCallback = sinon.spy(); | ||
dummy.attributeChangedCallback('startdate', 'sameDate', 'sameDate'); | ||
await dummy.attributeChangedCallback('startdate', 'sameDate', 'sameDate'); | ||
expect(dummy.requestParamsChangedCallback).not.to.have.been.called; | ||
@@ -196,6 +196,6 @@ }); | ||
it('Should not call requestParamsChangedCallback if a changed attribute ' + | ||
'is not a request param.', () => { | ||
'is not a request param.', async () => { | ||
const dummy = new Dummy(); | ||
dummy.requestParamsChangedCallback = sinon.spy(); | ||
dummy.attributeChangedCallback('notSet', 'oldValue', 'newValue'); | ||
await dummy.attributeChangedCallback('notSet', 'oldValue', 'newValue'); | ||
expect(dummy.requestParamsChangedCallback).not.to.have.been.called; | ||
@@ -205,6 +205,6 @@ }); | ||
it('Should receive an object containing all the request parameters ' + | ||
'as the first argument.', () => { | ||
'as the first argument.', async () => { | ||
const dummy = new Dummy(); | ||
dummy.requestParamsChangedCallback = sinon.spy(); | ||
dummy.attributeChangedCallback('startdate', '20180901', '20180915'); | ||
await dummy.attributeChangedCallback('startdate', '20180901', '20180915'); | ||
@@ -215,3 +215,3 @@ expect(dummy.requestParamsChangedCallback).to.have.been.calledWith({ | ||
dummy.attributeChangedCallback('finaldate', '20200901', '20200915'); | ||
await dummy.attributeChangedCallback('finaldate', '20200901', '20200915'); | ||
@@ -225,13 +225,13 @@ expect(dummy.requestParamsChangedCallback).to.have.been.calledWith({ | ||
it('Should exclude parameters with empty values ' + | ||
'at the first argument.', () => { | ||
'at the first argument.', async () => { | ||
const dummy = new Dummy(); | ||
dummy.requestParamsChangedCallback = sinon.spy(); | ||
dummy.attributeChangedCallback('startdate', '20180901', null); | ||
await dummy.attributeChangedCallback('startdate', '20180901', null); | ||
expect(dummy.requestParamsChangedCallback).to.have.been.calledWith({}); | ||
dummy.attributeChangedCallback('finaldate', '20200901', ''); | ||
await dummy.attributeChangedCallback('finaldate', '20200901', ''); | ||
expect(dummy.requestParamsChangedCallback).to.have.been.calledWith({}); | ||
dummy.attributeChangedCallback('affiliationcode', '', '123456789'); | ||
await dummy.attributeChangedCallback('affiliationcode', '', '123456789'); | ||
expect(dummy.requestParamsChangedCallback).to.have.been.calledWith({ | ||
@@ -243,6 +243,6 @@ affiliationCode: '123456789', | ||
it('Should receive an object containing only the changed parameters ' + | ||
'as the second argument.', () => { | ||
'as the second argument.', async () => { | ||
const dummy = new Dummy(); | ||
dummy.requestParamsChangedCallback = sinon.spy(); | ||
dummy.attributeChangedCallback('startdate', '20180901', '20180915'); | ||
await dummy.attributeChangedCallback('startdate', '20180901', '20180915'); | ||
@@ -255,3 +255,3 @@ expect(dummy.requestParamsChangedCallback).to.have.been.calledWith({ | ||
dummy.attributeChangedCallback('finaldate', '20200901', '20200915'); | ||
await dummy.attributeChangedCallback('finaldate', '20200901', '20200915'); | ||
@@ -272,6 +272,6 @@ expect(dummy.requestParamsChangedCallback).to.have.been.calledWith({ | ||
it('Should not exclude parameters with empty or undefined values ' + | ||
'at the second argument.', () => { | ||
'at the second argument.', async () => { | ||
const dummy = new Dummy(); | ||
dummy.requestParamsChangedCallback = sinon.spy(); | ||
dummy.attributeChangedCallback('startdate', '20180901', null); | ||
await dummy.attributeChangedCallback('startdate', '20180901', null); | ||
@@ -282,3 +282,3 @@ expect(dummy.requestParamsChangedCallback).to.have.been.calledWith({}, { | ||
dummy.attributeChangedCallback('finaldate', '20200901', ''); | ||
await dummy.attributeChangedCallback('finaldate', '20200901', ''); | ||
@@ -293,9 +293,53 @@ expect(dummy.requestParamsChangedCallback).to.have.been.calledWith({}, { | ||
it('Should not break if requestParamsChangedCallback ' + | ||
'is not a function.', () => { | ||
'is not a function.', async () => { | ||
const dummy = new Dummy(); | ||
const value = dummy | ||
.attributeChangedCallback('startdate', '20180901', '20200915'); | ||
expect(value).to.be.undefined; | ||
await dummy.attributeChangedCallback('startdate', '20180901', '20200915'); | ||
expect(dummy.requestParams).to.deep.equal({ | ||
startDate: '20200915', | ||
}); | ||
}); | ||
it('Should change requestParams only once after many ' + | ||
'attribute changes.', async () => { | ||
const dummy = new Dummy(); | ||
dummy.attributeChangedCallback('affiliationcode', '123', '321'); | ||
dummy.attributeChangedCallback('startdate', '20180901', '20180915'); | ||
dummy.attributeChangedCallback('finaldate', '20200901', '20200915'); | ||
expect(dummy.requestParams).to.deep.equal({}); | ||
await Promise.resolve(); | ||
expect(dummy.requestParams).to.deep.equal({ | ||
affiliationCode: '321', | ||
startDate: '20180915', | ||
finalDate: '20200915', | ||
}); | ||
}); | ||
it('Should trigger requestParamsChangedCallback only once after many ' + | ||
'attribute changes.', async () => { | ||
const dummy = new Dummy(); | ||
dummy.requestParamsChangedCallback = sinon.spy(); | ||
dummy.attributeChangedCallback('affiliationcode', '123', '321'); | ||
dummy.attributeChangedCallback('startdate', '20180901', '20180915'); | ||
dummy.attributeChangedCallback('finaldate', '20200901', '20200915'); | ||
expect(dummy.requestParamsChangedCallback).not.to.have.been.called; | ||
await Promise.resolve(); | ||
const changes = { | ||
affiliationCode: '321', | ||
startDate: '20180915', | ||
finalDate: '20200915', | ||
}; | ||
expect(dummy.requestParamsChangedCallback) | ||
.to.have.been.calledWith(changes, changes); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1283814
31257
Updatedsling-helpers@^1.12.3