Add observe logging

This commit is contained in:
Bartek Fabiszewski 2019-11-19 21:40:31 +01:00
parent 12f8e1245d
commit eda84b964c
4 changed files with 56 additions and 8 deletions

View File

@ -18,8 +18,9 @@
*/ */
/** /**
* @property {string} listId * @class uListItem
* @property {string} listValue * @property {string} listValue
* @property {string} listText
*/ */
export default class uListItem { export default class uListItem {
/** /**
@ -30,4 +31,11 @@ export default class uListItem {
this.listValue = String(id); this.listValue = String(id);
this.listText = String(value); this.listText = String(value);
} }
/**
* @return {string}
*/
toString() {
return `[${this.listValue}, ${this.listText}]`;
}
} }

View File

@ -26,8 +26,8 @@ export default class uObserve {
* observe(obj, prop, observer) observes given property prop; * observe(obj, prop, observer) observes given property prop;
* observe(obj, observer) observes all properties of object obj. * observe(obj, observer) observes all properties of object obj.
* @param {Object} obj * @param {Object} obj
* @param {(string|function)} p1 * @param {(string|ObserveCallback)} p1
* @param {function=} p2 * @param {ObserveCallback=} p2
*/ */
static observe(obj, p1, p2) { static observe(obj, p1, p2) {
if (typeof p2 === 'function') { if (typeof p2 === 'function') {
@ -47,7 +47,7 @@ export default class uObserve {
* Observe object's proporty. On change call observer * Observe object's proporty. On change call observer
* @param {Object} obj * @param {Object} obj
* @param {?string} property * @param {?string} property
* @param {function} observer * @param {ObserveCallback} observer
*/ */
static observeProperty(obj, property, observer) { static observeProperty(obj, property, observer) {
this.addObserver(obj, observer, property); this.addObserver(obj, observer, property);
@ -60,6 +60,7 @@ export default class uObserve {
set: (newValue) => { set: (newValue) => {
if (obj._values[property] !== newValue) { if (obj._values[property] !== newValue) {
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); uObserve.notify(obj._observers[property], newValue);
} }
if (Array.isArray(obj[property])) { if (Array.isArray(obj[property])) {
@ -75,7 +76,7 @@ export default class uObserve {
/** /**
* Recursively add observer to all properties * Recursively add observer to all properties
* @param {Object} obj * @param {Object} obj
* @param {function} observer * @param {ObserveCallback} observer
*/ */
static observeRecursive(obj, observer) { static observeRecursive(obj, observer) {
for (const prop in obj) { for (const prop in obj) {
@ -88,7 +89,7 @@ export default class uObserve {
/** /**
* Observe array * Observe array
* @param {Object} arr * @param {Object} arr
* @param {function} observer * @param {ObserveCallback} observer
*/ */
static observeArray(arr, observer) { static observeArray(arr, observer) {
this.addObserver(arr, observer); this.addObserver(arr, observer);
@ -97,6 +98,7 @@ export default class uObserve {
const descriptor = Object.getOwnPropertyDescriptor(Array.prototype, operation); const descriptor = Object.getOwnPropertyDescriptor(Array.prototype, operation);
descriptor.value = function () { descriptor.value = function () {
const result = Array.prototype[operation].apply(arr, arguments); const result = Array.prototype[operation].apply(arr, arguments);
console.log(`[${operation}] ` + arr.length ? `[${arr[0]}, …](${arr.length})` : arr);
uObserve.notify(arr._observers, arr); uObserve.notify(arr._observers, arr);
return result; return result;
}; };
@ -107,7 +109,7 @@ export default class uObserve {
/** /**
* Store observer in object * Store observer in object
* @param {Object} obj Object * @param {Object} obj Object
* @param {function} observer Observer * @param {ObserveCallback} observer Observer
* @param {string=} property Optional property * @param {string=} property Optional property
*/ */
static addObserver(obj, observer, property) { static addObserver(obj, observer, property) {
@ -130,7 +132,7 @@ export default class uObserve {
/** /**
* Notify observers * Notify observers
* @param {Set<function>} observers * @param {Set<ObserveCallback>} observers
* @param {*} value * @param {*} value
*/ */
static notify(observers, value) { static notify(observers, value) {
@ -140,4 +142,10 @@ export default class uObserve {
})(); })();
} }
} }
/**
* Notify callback
* @callback ObserveCallback
* @param {*} value
*/
} }

View File

@ -19,6 +19,7 @@
import uAjax from './ajax.js'; import uAjax from './ajax.js';
import uListItem from './listitem.js'; import uListItem from './listitem.js';
import uTrack from './track.js';
/** /**
* @class uUser * @class uUser
@ -37,6 +38,13 @@ export default class uUser extends uListItem {
this.login = login; this.login = login;
} }
/**
* @return {Promise<uTrack, string>}
*/
fetchLastPosition() {
return uTrack.fetchLatest(this);
}
/** /**
* @throws * @throws
* @return {Promise<uUser[], string>} * @return {Promise<uUser[], string>}

View File

@ -17,6 +17,7 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>. * along with this program; if not, see <http://www.gnu.org/licenses/>.
*/ */
import uTrack from '../src/track.js';
import uUser from '../src/user.js'; import uUser from '../src/user.js';
describe('User tests', () => { describe('User tests', () => {
@ -33,6 +34,28 @@ describe('User tests', () => {
expect(user.id).toBe(id); expect(user.id).toBe(id);
expect(user.login).toBe(login); 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', () => { describe('ajax tests', () => {
@ -58,5 +81,6 @@ describe('User tests', () => {
}) })
.catch(() => done.fail('reject callback called')); .catch(() => done.fail('reject callback called'));
}); });
}); });
}); });