all files / src/core/util/ DataSync.js

50% Statements 21/42
45.45% Branches 10/22
25% Functions 5/20
59.38% Lines 19/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149                                                                                                                                                                                                                                                                   
import cache from './cache';
 
/**
 * Get the event string for Webcom from Reach events
 * @access protected
 * @param {string} event A Reach event
 * @return {string}
 */
export const eventType = (event) => {
	let evt;
	Iif((/_ADDED$/i).test(event) || /_PUBLISHED$/.test(event)) {
		evt = 'added';
	} else Iif(/_CHANGED$/.test(event) || /_REFRESHED$/.test(event)) {
		evt = 'changed';
	} else Iif(/_REMOVED$/.test(event) || /_UNPUBLISHED$/.test(event)) {
		evt = 'removed';
	}
	return evt ? `child_${evt}` : event;
};
 
/**
 * Write method
 * @param {string} method Write method (set,update)
 * @param {string} path The path to set
 * @param {object} data The data to set
 * @ignore
 */
const _write = (method, path, data) => new Promise((resolve, reject) => {
	cache.base.child(path)[method](data, error => error ? reject(error) : resolve());
});
 
/**
 * {@link Webcom#set} as a {@link Promise}
 * @access protected
 * @param {string} path The path to set
 * @param {object} data The data to set
 * @return {Promise<*, Error>}
 */
export const set = _write.bind(undefined, 'set');
 
/**
 * {@link Webcom#push} as a {@link Promise}
 * @access protected
 * @param {string} path The path to push
 * @param {object} data The data to push
 * @return {Promise<Webcom, Error>}
 */
export const push = (path, data) => new Promise((resolve, reject) => {
	const pushRef = cache.base.child(path).push(data, error => error ? reject(error) : resolve(pushRef));
});
 
/**
 * {@link Webcom#update} as a {@link Promise}
 * @access protected
 * @param {string} path The path to update
 * @param {object} data The data to update
 * @return {Promise<*, Error>}
 */
export const update = _write.bind(undefined, 'update');
 
/**
 * {@link Webcom#remove} as a {@link Promise}
 * @access protected
 * @param {string} path The path to remove
 * @return {Promise<*, Error>}
 */
export const remove = (path) => new Promise((resolve, reject) => {
	cache.base.child(path).remove(error => error ? reject(error) : resolve());
});
 
/**
 * {@link Webcom#once} shortcut as a {@link Promise}
 * @access protected
 * @param {string} path The path
 * @param {string} event The event
 * @returns {Promise<Webcom/api.DataSnapshot, Error>}
 */
export const once = (path, event) => new Promise((resolve, reject) => {
	cache.base.child(path).once(eventType(event), resolve, reject);
});
 
/**
 * {@link Webcom#once}('value') as a {@link Promise}
 * @access protected
 * @param {string} path The path to get
 * @return {Promise<Webcom/api.DataSnapshot, Error>}
 */
export const get = path => once(path, 'value');
 
/**
 * List values as a {@link Promise}
 * @access protected
 * @param {string} path The path to get
 * @param {function} Type The type of the object to list
 * @param params Additional constructor parameters
 * @return {Promise<Object[], Error>}
 */
export const list = (path, Type, ...params) => {
	return get(path).then(snapData => {
		if(snapData) {
			const values = [];
			snapData.forEach(snapChild => {
				values.push(new Type(snapChild, ...params));
			});
			return values;
		}
	});
};
 
/**
 * {@link Webcom#on} shortcut
 * @access protected
 * @param {string} path The path
 * @param {string} event The event
 * @param {Webcom/api.Query~queryCallback|function} queryCallback The callback
 * @param {Webcom/api.Query~cancelCallback|function} [cancelCallback] The callback
 */
export const on = (path, event, queryCallback, cancelCallback) => {
	cache.base.child(path).on(eventType(event), queryCallback, cancelCallback);
};
 
/**
 * {@link Webcom#off} shortcut
 * @access protected
 * @param {string} path The path
 * @param {string} event The event
 * @param {function} [callback] The callback
 */
export const off = (path, event, callback) => {
	cache.base.child(path).off(eventType(event), callback);
};
 
/**
 * {@link Webcom#onDisconnect} shortcut
 * @access protected
 * @param {string} path The path
 * @return {Webcom/api.OnDisconnect}
 */
export const onDisconnect = path => cache.base.child(path).onDisconnect();
 
/**
 * Timestamp value
 * @access protected
 * @type {function}
 */
// export const ts = () => Webcom.ServerValue.TIMESTAMP;
export const ts = () => Date.now();
// HACK #DataSync: Rollback when TIMESTAMP works again (server-side bug with security rules)