add functions to manage and add chat
This commit is contained in:
Vendored
+57
-33
@@ -331,71 +331,95 @@
|
|||||||
/* @__PURE__ */ createElement("div", { class: "flex-column" }, /* @__PURE__ */ createElement("input", { "bind:value": addressInput }), /* @__PURE__ */ createElement("button", { "on:click": connect, "toggle:disabled": isConnected }, "Connect"), /* @__PURE__ */ createElement("button", { "on:click": disconnect }, "Disconnect"), /* @__PURE__ */ createElement("hr", null), /* @__PURE__ */ createElement("span", { "subscribe:innerText": connectedAddress }))
|
/* @__PURE__ */ createElement("div", { class: "flex-column" }, /* @__PURE__ */ createElement("input", { "bind:value": addressInput }), /* @__PURE__ */ createElement("button", { "on:click": connect, "toggle:disabled": isConnected }, "Connect"), /* @__PURE__ */ createElement("button", { "on:click": disconnect }, "Disconnect"), /* @__PURE__ */ createElement("hr", null), /* @__PURE__ */ createElement("span", { "subscribe:innerText": connectedAddress }))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// src/typeSafety.ts
|
||||||
|
var DATA_VERSION = "v2";
|
||||||
|
|
||||||
// src/Model/storageModel.ts
|
// src/Model/storageModel.ts
|
||||||
var StorageModel = class {
|
var StorageModel = class _StorageModel {
|
||||||
storageEntryTree = {};
|
storageEntryTree = {};
|
||||||
// basic
|
// basic
|
||||||
store = (directoryNames, key, value) => {
|
store = (key, value) => {
|
||||||
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
|
localStorage.setItem(key, value);
|
||||||
localStorage.setItem(unifiedPath, value);
|
const pathComponents = _StorageModel.keyToPathComponents(key);
|
||||||
this.updateTree(...directoryNames, key);
|
this.updateTree(...pathComponents);
|
||||||
};
|
};
|
||||||
restore = (directoryNames, key) => {
|
restore = (key) => {
|
||||||
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
|
return localStorage.getItem(key);
|
||||||
return localStorage.getItem(unifiedPath);
|
|
||||||
};
|
};
|
||||||
remove = (directoryNames, key) => {
|
remove = (key) => {
|
||||||
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
|
localStorage.removeItem(key);
|
||||||
localStorage.removeItem(unifiedPath);
|
|
||||||
};
|
};
|
||||||
list = (...directoryNames) => {
|
list = (key) => {
|
||||||
|
const pathComponents = _StorageModel.keyToPathComponents(key);
|
||||||
let currentParent = this.storageEntryTree;
|
let currentParent = this.storageEntryTree;
|
||||||
for (const directoryName of directoryNames) {
|
for (const component of pathComponents) {
|
||||||
currentParent = currentParent[directoryName];
|
const nextParent = currentParent[component];
|
||||||
|
if (nextParent == void 0) return [];
|
||||||
|
currentParent = nextParent;
|
||||||
}
|
}
|
||||||
return [...Object.keys(currentParent)];
|
return [...Object.keys(currentParent)];
|
||||||
};
|
};
|
||||||
// array
|
// stringifiable
|
||||||
storeArray = (directoryNames, key, value) => {
|
storeStringifiable = (key, value) => {
|
||||||
const valueString = stringify(value);
|
const valueString = stringify(value);
|
||||||
this.store(directoryNames, key, valueString);
|
this.store(key, valueString);
|
||||||
};
|
};
|
||||||
restoreArray = (directoryNames, key) => {
|
restoreStringifiable = (key) => {
|
||||||
const valueString = this.restore(directoryNames, key);
|
const valueString = this.restore(key);
|
||||||
if (!valueString) return null;
|
if (!valueString) return null;
|
||||||
const parsed = parse(valueString);
|
return parse(valueString);
|
||||||
if (!Array.isArray(parsed)) return null;
|
|
||||||
return parsed;
|
|
||||||
};
|
};
|
||||||
// init
|
// init
|
||||||
constructor() {
|
constructor() {
|
||||||
for (const key of Object.keys(localStorage)) {
|
for (const key of Object.keys(localStorage)) {
|
||||||
const directoryNames = this.keyToDirectoryNames(key);
|
const components = _StorageModel.keyToPathComponents(key);
|
||||||
this.updateTree(...directoryNames);
|
this.updateTree(...components);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// utility
|
// utility
|
||||||
updateTree = (...pathParts) => {
|
updateTree = (...pathComponents) => {
|
||||||
let currentParent = this.storageEntryTree;
|
let currentParent = this.storageEntryTree;
|
||||||
for (const pathPart of pathParts) {
|
for (const pathPart of pathComponents) {
|
||||||
if (!currentParent[pathPart]) {
|
if (!currentParent[pathPart]) {
|
||||||
currentParent[pathPart] = {};
|
currentParent[pathPart] = {};
|
||||||
}
|
}
|
||||||
currentParent = currentParent[pathPart];
|
currentParent = currentParent[pathPart];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
directoryNamesToKey = (...directoryNames) => {
|
static pathComponentsToKey = (...pathComponents) => {
|
||||||
return directoryNames.join("/");
|
return pathComponents.join("/");
|
||||||
};
|
};
|
||||||
keyToDirectoryNames = (key) => {
|
static keyToPathComponents = (key) => {
|
||||||
return key.split("/");
|
return key.split("/");
|
||||||
};
|
};
|
||||||
|
static join = (...items) => {
|
||||||
|
let allComponents = [];
|
||||||
|
for (const item of items) {
|
||||||
|
const parts = this.keyToPathComponents(item);
|
||||||
|
allComponents.push(...parts);
|
||||||
|
}
|
||||||
|
return _StorageModel.pathComponentsToKey(...allComponents);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
var storageKeys = {
|
||||||
|
// connection
|
||||||
|
socketAddress: `${DATA_VERSION}/connection/socket-address`,
|
||||||
|
// settings
|
||||||
|
userName: `${DATA_VERSION}/settings/user-name`,
|
||||||
|
firstDayOfWeek: `${DATA_VERSION}/settings/first-day-of-week`,
|
||||||
|
// history
|
||||||
|
previousAddresses: `${DATA_VERSION}/history/previous-addresses`,
|
||||||
|
previousObjectCategories: `${DATA_VERSION}/history/object-categories`,
|
||||||
|
previousObjectStatuses: `${DATA_VERSION}/history/object-statuses`,
|
||||||
|
previousObjectFilters: `${DATA_VERSION}/history/object-filters`,
|
||||||
|
// chat etc
|
||||||
|
chatInfo: (id) => `${DATA_VERSION}/chat/${id}/info`,
|
||||||
|
chatMessages: (id) => `${DATA_VERSION}/chat/${id}/messages`,
|
||||||
|
chatObjects: (id) => `${DATA_VERSION}/chat/${id}/objects`,
|
||||||
|
chatOutbox: (id) => `${DATA_VERSION}/chat/${id}/outbox`
|
||||||
};
|
};
|
||||||
|
|
||||||
// src/index.tsx
|
// src/index.tsx
|
||||||
var storageModel = new StorageModel();
|
var storageModel = new StorageModel();
|
||||||
console.log(storageModel.list("a"));
|
console.log(storageModel.list("z"));
|
||||||
console.log(storageModel.list("b"));
|
|
||||||
console.log(storageModel.list("b", "c"));
|
|
||||||
console.log(JSON.stringify(storageModel.storageEntryTree, null, 4));
|
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -41,6 +41,17 @@ export class ChatModel {
|
|||||||
return StorageModel.join(this.objectDirPath, id);
|
return StorageModel.join(this.objectDirPath, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set
|
||||||
|
setPrimaryChannel(primaryChannel: string): void {
|
||||||
|
this.info.primaryChannel = primaryChannel;
|
||||||
|
this.storeInfo();
|
||||||
|
}
|
||||||
|
|
||||||
|
setSecondaryChannels(secondaryChannels: string[]): void {
|
||||||
|
this.info.secondaryChannels = secondaryChannels;
|
||||||
|
this.storeInfo();
|
||||||
|
}
|
||||||
|
|
||||||
// store & add
|
// store & add
|
||||||
storeInfo(): void {
|
storeInfo(): void {
|
||||||
this.storageModel.storeStringifiable(this.infoPath, this.info);
|
this.storageModel.storeStringifiable(this.infoPath, this.info);
|
||||||
@@ -105,6 +116,15 @@ export class ChatModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// creation methods
|
// creation methods
|
||||||
|
export function createChat(storageModel: StorageModel, primaryChannel: string): ChatModel {
|
||||||
|
const id: string = v4();
|
||||||
|
|
||||||
|
const chatModel = new ChatModel(storageModel, id);
|
||||||
|
chatModel.setPrimaryChannel(primaryChannel);
|
||||||
|
|
||||||
|
return chatModel;
|
||||||
|
}
|
||||||
|
|
||||||
export function generateChatInfo(primaryChannel: string): ChatInfo {
|
export function generateChatInfo(primaryChannel: string): ChatInfo {
|
||||||
return {
|
return {
|
||||||
primaryChannel,
|
primaryChannel,
|
||||||
|
|||||||
Reference in New Issue
Block a user