src/reference-variables.js
/**
	 * Subscribe a listener function for a Page Path of a Reference Variable, when
	 * the variable is available, call the function with the variable.
	 *
	 * <del> It will return a unsubscribe function for unsubscribe listener. </del>
	 *
	 * @param {string} pagePath
	 * @param {Function} listener
	 *
	 */
Subscribe a listener function for a Page Path of a Reference Variable, when
the variable is available, call the function with the variable.

 It will return a unsubscribe function for unsubscribe listener.

@param {string} pagePath
@param {Function} listener
Subscribe a listener function for a Page Path of a Reference Variable when the variable is available call the function with the variable . It will return a unsubscribe function for unsubscribe listener . @param { string } pagePath @param { Function } listener
function subscribe(pagePath, listener) {
		ensureValueIsString(['first argument (for Reference Variable key)', pagePath]);
		ensureValueIsCallable(['second argument (for Reference Variable listener)', listener]);
		let listeners = [listener];
		if (keys.has(pagePath)) {
			const {listeners: currentListeners, value} = keys.get(pagePath);
			if (value !== UNDEFINED) {
				listener(null, cloneDeep(value));
				return;
			}
			listeners = currentListeners.slice();
			listeners.push(listener);
		}
		keys.set(pagePath, {listeners, value: UNDEFINED});
	}
function subscribe ( pagePath , listener ) { ensureValueIsString ( [ 'first argument (for Reference Variable key)' , pagePath ] ) ; ensureValueIsCallable ( [ 'second argument (for Reference Variable listener)' , listener ] ) ; let listeners = [ listener ] ; if ( keys . has ( pagePath ) ) { const { listeners : currentListeners , value } = keys . get ( pagePath ) ; if ( value !== UNDEFINED ) { listener ( null , cloneDeep ( value ) ) ; return ; } listeners = currentListeners . slice ( ) ; listeners . push ( listener ) ; } keys . set ( pagePath , { listeners , value : UNDEFINED } ) ; }
Subscribe a listener function for a Page Path of a Reference Variable, when
 the variable is available, call the function with the variable.
Subscribe a listener function for a Page Path of a Reference Variable when the variable is available call the function with the variable .
// make a clone so listener can't change the value. // return () => {}; // we dont need unsubscribe function. // let isSubscribed = true; // return function unsubscribe() { // 	if (!isSubscribed) { // 		return; // 	} // 	isSubscribed = false; // 	const {listeners: currentListeners} = keys.get(pagePath); // 	listeners = currentListeners.slice(); // 	const index = currentListeners.indexOf(listener); // 	listeners.splice(index, 1); // 	keys.set(pagePath, {listeners}); // };