This commit is contained in:
bobbydigitales
2024-09-13 22:57:30 -07:00
parent 9140739879
commit f0d073bab1
30 changed files with 2091 additions and 765 deletions

View File

@@ -4,8 +4,7 @@
// email: string;
// }
const dbName: string = "ddln";
const storeNameBase: string = "posts";
const postStoreName: string = "posts";
let keyBase = "dandelion_posts_v1_"
let key = "";
@@ -19,8 +18,8 @@ type DBError = Event & {
target: { errorCode: DOMException };
};
export function openDatabase(userID:string): Promise<IDBDatabase> {
const storeName = `${storeNameBase}_${userID}`;
export function openDatabase(userID: string): Promise<IDBDatabase> {
const dbName = `user_${userID}`
return new Promise((resolve, reject) => {
const request: IDBOpenDBRequest = indexedDB.open(dbName, 1);
@@ -33,8 +32,8 @@ export function openDatabase(userID:string): Promise<IDBDatabase> {
request.onupgradeneeded = (event: IDBVersionChangeEvent) => {
const db: IDBDatabase = (event.target as IDBOpenDBRequest).result;
if (!db.objectStoreNames.contains(storeName)) {
let store = db.createObjectStore(storeName, { keyPath: "id", autoIncrement: true });
if (!db.objectStoreNames.contains(postStoreName)) {
let store = db.createObjectStore(postStoreName, { keyPath: "id", autoIncrement: true });
store.createIndex("datetimeIndex", "post_timestamp", { unique: false });
store.createIndex("postIDIndex", "data.post_id", { unique: true });
@@ -52,12 +51,11 @@ export function openDatabase(userID:string): Promise<IDBDatabase> {
export async function addData(userID: string, data: any): Promise<void> {
try {
const storeName = `${storeNameBase}_${userID}`;
const db = await openDatabase(userID);
const transaction = db.transaction(storeName, "readwrite");
const store = transaction.objectStore(storeName);
const transaction = db.transaction(postStoreName, "readwrite");
const store = transaction.objectStore(postStoreName);
const addRequest = store.add({post_timestamp: data.post_timestamp, data:data});
const addRequest = store.add({ post_timestamp: data.post_timestamp, data: data });
addRequest.onsuccess = (e: Event) => {
// console.log('Data has been added:', (e.target as IDBRequest).result);
@@ -73,23 +71,72 @@ export async function addData(userID: string, data: any): Promise<void> {
}
}
export async function deleteData(userID: string, postID: string) {
try {
const db = await openDatabase(userID);
const transaction = db.transaction(postStoreName, "readwrite");
const store = transaction.objectStore(postStoreName);
const index = store.index("postIDIndex");
const getRequest = index.getKey(postID);
getRequest.onerror = e => console.log((e.target as IDBRequest).error)
getRequest.onsuccess = e => {
const key = (e.target as IDBRequest).result;
if (key === undefined) {
console.error("Post not found");
return null;
}
const deleteRequest = store.delete(key);
deleteRequest.onerror = e => { console.error((e.target as IDBRequest).error); return false };
deleteRequest.onsuccess = () => true;
}
} catch (error) {
console.error('Error in opening database:', error);
}
}
export async function clearData(userID: string) {
try {
const db = await openDatabase(userID);
const transaction = db.transaction(postStoreName, "readwrite");
const store = transaction.objectStore(postStoreName);
const clearRequest = store.clear();
clearRequest.onsuccess = (e: Event) => {
// console.log('Data has been added:', (e.target as IDBRequest).result);
};
clearRequest.onerror = (event: Event) => {
// Use a type assertion to access the specific properties of IDBRequest error event
const errorEvent = event as IDBRequestEvent;
console.error('Error in clearing data:', errorEvent.target.error?.message);
};
} catch (error) {
console.error('Error in opening database:', error);
}
}
export async function addDataArray(userID: string, array: any[]): Promise<void> {
try {
const storeName = `${storeNameBase}_${userID}`;
const db = await openDatabase(userID);
const transaction = db.transaction(storeName, "readwrite");
const store = transaction.objectStore(storeName);
const transaction = db.transaction(postStoreName, "readwrite");
const store = transaction.objectStore(postStoreName);
let count = 0;
array.reverse();
for (let data of array) {
const addRequest = store.add({post_timestamp: data.post_timestamp, data:data});
const addRequest = store.add({ post_timestamp: data.post_timestamp, data: data });
addRequest.onsuccess = (e: Event) => {
// console.log('Data has been added:', (e.target as IDBRequest).result);
};
addRequest.onerror = (event: Event) => {
// Use a type assertion to access the specific properties of IDBRequest error event
const errorEvent = event as IDBRequestEvent;
@@ -103,17 +150,16 @@ export async function addDataArray(userID: string, array: any[]): Promise<void>
// }
}
} catch (error) {
console.error('Error in opening database:', error);
}
}
export async function getData(userID:string, lowerID:Date, upperID:Date): Promise<any | undefined> {
const storeName = `${storeNameBase}_${userID}`;
export async function getData(userID: string, lowerID: Date, upperID: Date): Promise<any | undefined> {
const db = await openDatabase(userID);
const transaction = db.transaction(storeName, "readonly");
const store = transaction.objectStore(storeName);
const transaction = db.transaction(postStoreName, "readonly");
const store = transaction.objectStore(postStoreName);
return new Promise((resolve, reject) => {
const keyRangeValue = IDBKeyRange.bound(lowerID, upperID);
@@ -145,11 +191,10 @@ export async function getData(userID:string, lowerID:Date, upperID:Date): Promis
});
}
export async function getAllData(userID:string): Promise<any | undefined> {
const storeName = `${storeNameBase}_${userID}`;
export async function getAllData(userID: string): Promise<any | undefined> {
const db = await openDatabase(userID);
const transaction = db.transaction(storeName, "readonly");
const store = transaction.objectStore(storeName);
const transaction = db.transaction(postStoreName, "readonly");
const store = transaction.objectStore(postStoreName);
return new Promise((resolve, reject) => {
const getRequest = store.getAll();

File diff suppressed because it is too large Load Diff

83
src/webRTC.ts Normal file
View File

@@ -0,0 +1,83 @@
const config = {
iceServers: [{ urls: "stun: stun.l.google.com" }],
};
let localConnection = new RTCPeerConnection();
function handleSendChannelStatusChange() {
console.log(handleSendChannelStatusChange);
}
let sendChannel = localConnection.createDataChannel("sendChannel");
sendChannel.onopen = handleSendChannelStatusChange;
sendChannel.onclose = handleSendChannelStatusChange;
let remoteConnection = new RTCPeerConnection();
remoteConnection.ondatachannel = receiveChannelCallback;
localConnection.onicecandidate = (e:any) =>
!e.candidate ||
remoteConnection.addIceCandidate(e.candidate).catch(handleAddCandidateError);
remoteConnection.onicecandidate = (e) =>
!e.candidate ||
localConnection.addIceCandidate(e.candidate).catch(handleAddCandidateError);
function handleCreateDescriptionError(error:any) {
console.log(`Unable to create an offer: ${error.toString()}`);
}
function handleLocalAddCandidateSuccess() {
console.log('handleLocalAddCandidateSuccess');
}
function handleRemoteAddCandidateSuccess() {
console.log('handleRemoteAddCandidateSuccess');
}
function handleAddCandidateError() {
console.log("Oh noes! addICECandidate failed!");
}
localConnection
.createOffer()
.then((offer) => localConnection.setLocalDescription(offer))
.then(() =>
remoteConnection.setRemoteDescription(localConnection.localDescription as RTCSessionDescriptionInit),
)
.then(() => remoteConnection.createAnswer())
.then((answer) => remoteConnection.setLocalDescription(answer))
.then(() =>
localConnection.setRemoteDescription(remoteConnection.localDescription as RTCSessionDescriptionInit),
)
.catch(handleCreateDescriptionError);
function handleReceiveChannelStatusChange(event:any) {
let receiveChannel = event.channel;
if (receiveChannel) {
console.log(
`Receive channel's status has changed to ${receiveChannel.readyState}`,
);
}
}
function receiveChannelCallback(event:any) {
let receiveChannel = event.channel;
receiveChannel.onmessage = handleReceiveMessage;
receiveChannel.onopen = handleReceiveChannelStatusChange;
receiveChannel.onclose = handleReceiveChannelStatusChange;
}
function sendMessage(message:string) {
sendChannel.send(message);
}
function handleReceiveMessage(event:any) {
console.log(event.data);
}