Skip to content

Commit

Permalink
use typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
MaHaWo committed Aug 26, 2024
1 parent dfd3d6f commit 90c0e5e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
26 changes: 0 additions & 26 deletions src/lib/stores/childrenData.js

This file was deleted.

53 changes: 53 additions & 0 deletions src/lib/stores/childrenData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { writable } from 'svelte/store';

interface ChildData {
token: string;
}

interface ChildrenList {
[usertoken: string]: {
[childtoken: string]: ChildData;
};
}

const childrenlist: ChildrenList = {};

const childrenData = writable(childrenlist);

async function addChildrenData(data: ChildData, childtoken: string, usertoken: string) {
childrenData.update((childrenlist) => {
if (!(usertoken in childrenlist)) {
throw new Error(`User token ${usertoken} not found`);
}

if (!(childtoken in childrenlist[usertoken])) {
throw new Error(`Child token ${childtoken} not found for user token ${usertoken}`);
}

data['token'] = usertoken;
if (usertoken in childrenlist) {
childrenlist[usertoken][childtoken] = data;
} else {
childrenlist[usertoken][childtoken] = data;
}
return childrenlist;
});
}

async function removeChildrenData(childtoken: string, usertoken: string) {
childrenData.update((childrenlist) => {
if (!(usertoken in childrenlist)) {
throw new Error(`User token ${usertoken} not found`);
}

if (!(childtoken in childrenlist[usertoken])) {
throw new Error(`Child token ${childtoken} not found for user token ${usertoken}`);
}

delete childrenlist[usertoken][childtoken];

return childrenlist;
});
}

export { addChildrenData, childrenData, removeChildrenData };

0 comments on commit 90c0e5e

Please sign in to comment.