Skip to content

Commit

Permalink
fix: apply lots of improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite committed Sep 15, 2024
1 parent f05d215 commit bd53963
Show file tree
Hide file tree
Showing 22 changed files with 404 additions and 100 deletions.
40 changes: 38 additions & 2 deletions apps/yasumu/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/yasumu/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ tauri-plugin-shell = "2.0.0-rc.1"
tauri-plugin-process = "2.0.0-rc.0"
mailparse = "0.15.0"
smol = "2.0.2"
tanxium = "0.1.2"
tanxium = "0.1.3"
uuid = { version = "1.10.0", features = ["fast-rng"] }
14 changes: 10 additions & 4 deletions apps/yasumu/src-tauri/capabilities/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "main-capability",
"description": "Capability for main window",
"windows": [
"main"
],
"windows": ["main"],
"permissions": [
"core:app:allow-version",
"core:app:allow-name",
Expand Down Expand Up @@ -54,6 +52,14 @@
"store:allow-set",
"store:allow-save",
"store:allow-load",
"store:allow-clear",
"store:allow-delete",
"store:allow-entries",
"store:allow-has",
"store:allow-keys",
"store:allow-length",
"store:allow-reset",
"store:allow-values",
{
"identifier": "fs:allow-write-text-file",
"allow": [
Expand Down Expand Up @@ -153,4 +159,4 @@
},
"process:default"
]
}
}
80 changes: 58 additions & 22 deletions apps/yasumu/src-tauri/runtime/02_runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,50 +86,68 @@
}
}

class YasumuStoreModel implements YasumuStore {
private get store() {
return Yasumu.context.data.store;
const getStore = () => {
const val = Yasumu.context.data.store;

if (!val) {
return (Yasumu.context.data.store = {});
}

private get changes() {
return Yasumu.context.__meta.store;
return val;
};
const getChanges = () => {
const val = Yasumu.context.__meta.store;

if (!Array.isArray(val)) {
return (Yasumu.context.__meta.store = []);
}

return val;
};

class YasumuStoreModel implements YasumuStore {
public get(key: string): any {
return this.store[key] ?? this.changes.find((change) => change.key === key)?.value;
return getStore()[key] ?? getChanges().find((change) => change.key === key)?.value;
}

public set(key: string, value: any): void {
this.store[key] = value;
this.changes.push({ op: 'set', key, value });
if (value === undefined) {
throw new Error('Value cannot be undefined');
}

getStore()[key] = value;
getChanges().push({ op: 'set', key, value });
}

public remove(key: string): void {
delete this.store[key];
this.changes.push({ op: 'delete', key });
delete getStore()[key];
getChanges().push({ op: 'delete', key });
}

public has(key: string): boolean {
return key in this.store || this.changes.some((change) => change.key === key);
return key in getStore() || getChanges().some((change) => change.key === key);
}

public count(): number {
const keys = Object.keys(this.store);
return keys.length + this.changes.filter((change) => change.op === 'set' && !keys.includes(change.key)).length;
const keys = Object.keys(getStore());
return keys.length + getChanges().filter((change) => change.op === 'set' && !keys.includes(change.key)).length;
}

public clear(): void {
for (const key in this.store) {
this.changes.push({ op: 'delete', key });
delete this.store[key];
const store = getStore();
const changes = getChanges();
for (const key in store) {
changes.push({ op: 'delete', key });
delete store[key];
}
}

public entries(): [string, any][] {
const main = Object.entries(this.store);
const main = Object.entries(getStore());
const changes = getChanges();

const res = [
this.changes
changes
.filter((change) => change.op === 'set' && !main.some((v) => v[0] === change.key))
.map((v) => [v.key, v.value]),
];
Expand All @@ -142,10 +160,12 @@
context: {
data: {},
__meta: {
store: {},
requestHeaders: {},
store: [],
request: {} as any,
response: {} as any,
console: [],
},
test: [],
} satisfies YasumuContextMeta,
},
setContextData(data: string | Record<string, any>) {
if (typeof data === 'string') {
Expand All @@ -159,8 +179,24 @@
store: new YasumuStoreModel(),
};

function deeplyClean(obj: any) {
if (!obj) return;

for (const key in obj) {
if (obj[key] === undefined) {
delete obj[key];
} else if (typeof obj[key] === 'object') {
deeplyClean(obj[key]);
}
}
}

Yasumu.serialize = function () {
return JSON.stringify(this.context.__meta);
const obj = this.context.__meta;

deeplyClean(obj);

return JSON.stringify(obj);
};

Object.assign(Yasumu, context);
Expand Down
26 changes: 21 additions & 5 deletions apps/yasumu/src-tauri/runtime/03_console.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/// <reference path="./_common.ts" />
(() => {
const COMMON_OBJECTS = new Set(['Map', 'Set', 'WeakMap', 'WeakSet']);
function inspect(value: any, visited = new WeakSet(), indentLevel = 0): string {
const MAX_DEPTH = 2;

function inspect(value: any, visited = new WeakSet(), indentLevel = 0, root = true, depth = 0): string {
try {
if (typeof value === 'string') {
return `"${value}"`;
if (!root) return `"${value}"`;
return value;
}

if (typeof value === 'bigint') {
Expand All @@ -27,14 +30,27 @@
}

if (typeof value === 'object') {
if (depth > MAX_DEPTH) {
return '{ ... }';
}
if (visited.has(value)) {
return '[Circular]';
}

visited.add(value);

if (value instanceof Error) {
let msg = value.stack || String(value);

if (!value.stack) {
msg += `\n${Yasumu.utils.getStackTrace()}`;
}

return msg;
}

if (Array.isArray(value)) {
return `[${value.map((item) => inspect(item, visited, indentLevel + 2)).join(', ')}]`;
return `[${value.map((item) => inspect(item, visited, indentLevel + 2, false, depth + 1)).join(', ')}]`;
}

if (value && 'constructor' in value && value.constructor && COMMON_OBJECTS.has(value.constructor.name)) {
Expand All @@ -50,7 +66,7 @@
for (const key in value) {
if (Object.prototype.propertyIsEnumerable.call(value, key)) {
enumerableFound++;
result += `${nestedIndent}${key}: ${inspect(value[key], visited, indentLevel + 2)},\n`;
result += `${nestedIndent}${key}: ${inspect(value[key], visited, indentLevel + 2, false, depth + 1)},\n`;
}
}

Expand All @@ -71,7 +87,7 @@
}
}

const CONSOLE_METHODS: LogType = ['log', 'error', 'warn', 'info', 'clear'];
const CONSOLE_METHODS: LogType = ['log', 'warn', 'error', 'info', 'clear'];

const console = new Proxy<YasumuConsole>({} as YasumuConsole, {
get(target, prop, receiver) {
Expand Down
Loading

0 comments on commit bd53963

Please sign in to comment.