nope/lib/dispatcher/baseServices/data.ts

51 lines
1.3 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2022-01-14 20:29:13
* @modify date 2022-01-14 20:36:36
* @desc [description]
*/
import { sleep } from "../../helpers/async";
import { INopeDispatcher } from "../../types/nope";
/**
* Generate and registers a ping service.
*
* @author M.Karkowski
* @export
* @param {INopeDispatcher} dispatcher
* @return {*} The function to ping all dispatchers.
*/
export async function enablingSyncingData(dispatcher: INopeDispatcher) {
// Registers the Ping Method at the Dispatcher.
await dispatcher.connectivityManager.dispatchers.onChange.subscribe(
async (eventData) => {
// If there is added Data
if (eventData.added.length > 0) {
// And if we are the master module
// we will emit the new data.
if (dispatcher.connectivityManager.isMaster) {
// But before, wait for shure.
await sleep(0);
// Get the Data.
const data = dispatcher.dataDistributor.pullData("", {});
// Emit the Data.
dispatcher.communicator.emit("DataChanged", {
args: [],
data: data,
forced: false,
path: "",
sender: dispatcher.id,
timestamp: dispatcher.connectivityManager.now,
});
}
}
}
);
return {};
}