react-native-drawing
Version:
A React Native library that provides a canvas to perform drawing actions
76 lines (75 loc) • 2.54 kB
JavaScript
/**
* A bidirectional manager for Acknowledgement messages
* An ACK is an special message that is provided by other process to indicate an specific status
* @template T - ArgsDTO
* @template U - ResolverData
* @template V - Token
*/
export class Ack {
constructor(codec, sendData) {
Object.defineProperty(this, "codec", {
enumerable: true,
configurable: true,
writable: true,
value: codec
});
Object.defineProperty(this, "sendData", {
enumerable: true,
configurable: true,
writable: true,
value: sendData
});
Object.defineProperty(this, "interfaceIndex", {
enumerable: true,
configurable: true,
writable: true,
value: {}
});
}
/**
* Verify if the provided data has the strcuture to be shared by the instance
*/
checkData(data) {
return data.startsWith(this.token);
}
/**
* Creates an ACK promise (resolved by another process)
* @param id - Used to identify the ACK message
* (supporting multiple messages at the same time).
* This must be sended by any way to the other process,
* to be sended back with the ack completion (like a calback)
*/
activate(id) {
// Creating interface
let ackInterface;
const ackResolved = new Promise((resolve, reject) => {
ackInterface = { resolve, reject };
});
// Storing interface
this.interfaceIndex[id] = ackInterface;
return ackResolved; // Exposing promise
}
/**
* Reestructures the message data and resolves its ACK (data received by another process)
*/
receive(encodedData) {
// Building DTO
const validEncodedData = encodedData.replace(this.token, ''); // Removing token from data
const data = this.codec.toData(validEncodedData);
const { id } = data;
// Getting message resolver interface
const ackInterface = this.interfaceIndex[id];
if (ackInterface === undefined) {
return;
}
delete this.interfaceIndex[id];
this.makeChoice(data, ackInterface);
}
/**
* Send the Ack to another process, it only should be received for a manager with the same structure of that (bidirectional)
*/
send(args) {
const encodedData = this.codec.toJSON(args);
this.sendData(this.token + encodedData); // Sending with token
}
}