Add observe logging
This commit is contained in:
parent
12f8e1245d
commit
eda84b964c
@ -18,8 +18,9 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* @property {string} listId
|
||||
* @class uListItem
|
||||
* @property {string} listValue
|
||||
* @property {string} listText
|
||||
*/
|
||||
export default class uListItem {
|
||||
/**
|
||||
@ -30,4 +31,11 @@ export default class uListItem {
|
||||
this.listValue = String(id);
|
||||
this.listText = String(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
toString() {
|
||||
return `[${this.listValue}, ${this.listText}]`;
|
||||
}
|
||||
}
|
||||
|
@ -26,8 +26,8 @@ export default class uObserve {
|
||||
* observe(obj, prop, observer) observes given property prop;
|
||||
* observe(obj, observer) observes all properties of object obj.
|
||||
* @param {Object} obj
|
||||
* @param {(string|function)} p1
|
||||
* @param {function=} p2
|
||||
* @param {(string|ObserveCallback)} p1
|
||||
* @param {ObserveCallback=} p2
|
||||
*/
|
||||
static observe(obj, p1, p2) {
|
||||
if (typeof p2 === 'function') {
|
||||
@ -47,7 +47,7 @@ export default class uObserve {
|
||||
* Observe object's proporty. On change call observer
|
||||
* @param {Object} obj
|
||||
* @param {?string} property
|
||||
* @param {function} observer
|
||||
* @param {ObserveCallback} observer
|
||||
*/
|
||||
static observeProperty(obj, property, observer) {
|
||||
this.addObserver(obj, observer, property);
|
||||
@ -60,6 +60,7 @@ export default class uObserve {
|
||||
set: (newValue) => {
|
||||
if (obj._values[property] !== newValue) {
|
||||
obj._values[property] = newValue;
|
||||
console.log(`${property} = ` + (Array.isArray(newValue) && newValue.length ? `[${newValue[0]}, …](${newValue.length})` : newValue));
|
||||
uObserve.notify(obj._observers[property], newValue);
|
||||
}
|
||||
if (Array.isArray(obj[property])) {
|
||||
@ -75,7 +76,7 @@ export default class uObserve {
|
||||
/**
|
||||
* Recursively add observer to all properties
|
||||
* @param {Object} obj
|
||||
* @param {function} observer
|
||||
* @param {ObserveCallback} observer
|
||||
*/
|
||||
static observeRecursive(obj, observer) {
|
||||
for (const prop in obj) {
|
||||
@ -88,7 +89,7 @@ export default class uObserve {
|
||||
/**
|
||||
* Observe array
|
||||
* @param {Object} arr
|
||||
* @param {function} observer
|
||||
* @param {ObserveCallback} observer
|
||||
*/
|
||||
static observeArray(arr, observer) {
|
||||
this.addObserver(arr, observer);
|
||||
@ -97,6 +98,7 @@ export default class uObserve {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(Array.prototype, operation);
|
||||
descriptor.value = function () {
|
||||
const result = Array.prototype[operation].apply(arr, arguments);
|
||||
console.log(`[${operation}] ` + arr.length ? `[${arr[0]}, …](${arr.length})` : arr);
|
||||
uObserve.notify(arr._observers, arr);
|
||||
return result;
|
||||
};
|
||||
@ -107,7 +109,7 @@ export default class uObserve {
|
||||
/**
|
||||
* Store observer in object
|
||||
* @param {Object} obj Object
|
||||
* @param {function} observer Observer
|
||||
* @param {ObserveCallback} observer Observer
|
||||
* @param {string=} property Optional property
|
||||
*/
|
||||
static addObserver(obj, observer, property) {
|
||||
@ -130,7 +132,7 @@ export default class uObserve {
|
||||
|
||||
/**
|
||||
* Notify observers
|
||||
* @param {Set<function>} observers
|
||||
* @param {Set<ObserveCallback>} observers
|
||||
* @param {*} value
|
||||
*/
|
||||
static notify(observers, value) {
|
||||
@ -140,4 +142,10 @@ export default class uObserve {
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify callback
|
||||
* @callback ObserveCallback
|
||||
* @param {*} value
|
||||
*/
|
||||
}
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
import uAjax from './ajax.js';
|
||||
import uListItem from './listitem.js';
|
||||
import uTrack from './track.js';
|
||||
|
||||
/**
|
||||
* @class uUser
|
||||
@ -37,6 +38,13 @@ export default class uUser extends uListItem {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {Promise<uTrack, string>}
|
||||
*/
|
||||
fetchLastPosition() {
|
||||
return uTrack.fetchLatest(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws
|
||||
* @return {Promise<uUser[], string>}
|
||||
|
@ -17,6 +17,7 @@
|
||||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import uTrack from '../src/track.js';
|
||||
import uUser from '../src/user.js';
|
||||
|
||||
describe('User tests', () => {
|
||||
@ -33,6 +34,28 @@ describe('User tests', () => {
|
||||
expect(user.id).toBe(id);
|
||||
expect(user.login).toBe(login);
|
||||
});
|
||||
|
||||
it('should call uTrack.fetchLatest method', () => {
|
||||
// given
|
||||
const id = 1;
|
||||
const login = 'test';
|
||||
const user = new uUser(id, login);
|
||||
spyOn(uTrack, 'fetchLatest');
|
||||
// when
|
||||
user.fetchLastPosition();
|
||||
// then
|
||||
expect(uTrack.fetchLatest).toHaveBeenCalledWith(user);
|
||||
});
|
||||
|
||||
it('should get class string representation', () => {
|
||||
// given
|
||||
const id = 1;
|
||||
const login = 'test';
|
||||
// when
|
||||
const user = new uUser(id, login);
|
||||
// then
|
||||
expect(user.toString()).toBe(`[${id}, ${login}]`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ajax tests', () => {
|
||||
@ -58,5 +81,6 @@ describe('User tests', () => {
|
||||
})
|
||||
.catch(() => done.fail('reject callback called'));
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user