Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
Fy- committed Mar 22, 2024
1 parent 6cb4c94 commit 098e206
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/fws-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fy-/fws-js",
"version": "0.0.15",
"version": "0.0.21",
"description": "FWJS is a framework for building web applications FWS - https://fy.to/@G",
"scripts": {
"build": "tsc && node copyPackage.js"
Expand Down
3 changes: 2 additions & 1 deletion packages/fws-js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './fws-wrapper'
export * from './i18n-backend'
export * from './rest';
export * from "./helpers";
export * from "./helpers";
export * from "./upload";
106 changes: 106 additions & 0 deletions packages/fws-js/src/upload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
export type FileUploadState = 'queue' | 'uploading' | 'success' | 'error';

export interface FileUpload {
file: File;
progress: number;
state: FileUploadState;
cancelController?: AbortController;
}

export class Uploader {
public files: FileUpload[] = [];

addFile(file: File) {
this.files.push({
file,
progress: 0,
state: 'queue',
});
}

clearFiles() {
for (let i = 0; i < this.files.length; i++) {
if (this.files[i].state === 'queue') {
this.cancelUpload(i);
}
}
this.files = [];
}

addFiles(files: FileList) {
for (let i = 0; i < files.length; i++) {
this.addFile(files[i]);
}
}

startUpload(endpoint: string = '/Blob/Upload', callback: any = null) {
let uploadCount = 0;

this.files.forEach((upload, index) => {
if (upload.state === 'queue') {
this.uploadFile(index, endpoint, () => {
uploadCount++;
if (uploadCount === this.files.length) {
if (callback) {
callback();
}
}
});
} else {
uploadCount++;
if (uploadCount === this.files.length && callback) {
callback();
}
}
});
}
private uploadFile(index: number, endpoint: string, uploadCompleteCallback: () => void) {
const fileUpload = this.files[index];
const formData = new FormData();
formData.append('file', fileUpload.file);

const xhr = new XMLHttpRequest();
const cancelController = new AbortController();
fileUpload.cancelController = { abort: () => xhr.abort(), signal: cancelController.signal };

xhr.upload.addEventListener('progress', (event) => {
if (event.lengthComputable) {
const progress = Math.round((event.loaded / event.total) * 100);
fileUpload.progress = progress;
}
});

xhr.addEventListener('load', () => {
if (xhr.status >= 200 && xhr.status < 300) {
fileUpload.state = 'success';
} else {
fileUpload.state = 'error';
}
fileUpload.progress = 100;
uploadCompleteCallback();
});

xhr.addEventListener('error', () => {
fileUpload.state = 'error';
fileUpload.progress = 100;
uploadCompleteCallback();
});

fileUpload.state = 'uploading';
xhr.open('POST', endpoint);
xhr.send(formData);
}


cancelUpload(index: number) {
const fileUpload = this.files[index];
if (fileUpload.cancelController) {
fileUpload.cancelController.abort();
fileUpload.state = 'error';
}
}

getFileUploads() {
return this.files;
}
}
2 changes: 1 addition & 1 deletion packages/fws-vue/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fy-/fws-vue",
"version": "0.0.943",
"version": "0.0.945",
"author": "Florian 'Fy' Gasquez <m@fy.to>",
"license": "MIT",
"repository": {
Expand Down
3 changes: 3 additions & 0 deletions packages/fws-vue/src/components/fws/UserFlow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ const userFlow = async (params: paramsType = { initial: false }) => {
hasOauth.value = true;
}
});
if (!hasOauth.value && responseFields.value.length > 0) {
showEmail.value = true;
}
setTimeout(() => {
if (inputs.value.length > 0 && inputs.value[inputs.value.length - 1])
inputs.value[inputs.value.length - 1].focus();
Expand Down
10 changes: 5 additions & 5 deletions packages/fws-vue/src/composables/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function useRest(): <ResultType extends APIResult>(
if (isServerRendered()) {
const hasResult = restStore.getResult(requestHash);
if (hasResult !== undefined) {
const result = { ...hasResult } as ResultType;
const result = hasResult as ResultType;
restStore.removeResult(requestHash);
if (result.result === "error") {
eventBus.emit("rest-error", result);
Expand All @@ -56,10 +56,10 @@ export function useRest(): <ResultType extends APIResult>(
try {
const restResult: ResultType = await rest(url, method, params);
if (getMode() === "ssr") {
restStore.addResult(requestHash, restResult);
}
if (restResult.result === "error") {
eventBus.emit("rest-error", restResult);
restStore.addResult(
requestHash,
JSON.parse(JSON.stringify(restResult)),
);
}
return Promise.resolve(restResult);
} catch (error) {
Expand Down
4 changes: 3 additions & 1 deletion packages/fws-vue/src/composables/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export interface SSRResult {
}

export function isServerRendered() {
return getInitialState().isSSR;
const state = getInitialState();
if (state && state.isSSR) return true;
return false;
}

export function initVueClient(router: Router, pinia: Pinia) {
Expand Down
7 changes: 5 additions & 2 deletions packages/fws-vue/src/stores/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineStore } from "pinia";
import { APIResult } from "../composables/rest";

type SharedState = {
results: Record<number, APIResult | undefined>;
results: Record<number, any | undefined>;
};

export const useRestStore = defineStore({
Expand All @@ -11,9 +11,12 @@ export const useRestStore = defineStore({
results: {},
}),
actions: {
addResult(id: number, result: APIResult) {
addResult(id: number, result: any) {
this.results[id] = result;
},
hasResult(id: number) {
return this.results[id] !== undefined;
},
getResult(id: number) {
return this.results[id];
},
Expand Down
13 changes: 5 additions & 8 deletions packages/playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ const links = computed(() => {
const _links: any[] = [
{ name: translate("nav_home"), url: "/" },
{
name: translate("nav_docs"),
url: "/docs",
},
{
name: translate("nav_fws_vue"),
url: "/fws-vue",
name: translate("nav_js_workers"),
url: "/js_workers",
},
{
name: translate("nav_klb_vue"),
url: "/klb-vue",
name: translate("nav_docs"),
url: "/docs",
},
];
/*
if (!isAuth.value) {
Expand Down
10 changes: 5 additions & 5 deletions packages/playground/src/pages/HomePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ onMounted(async () => {
<div
v-for="[key, time] in sortedStats"
:key="key"
class="flex items-center gap-3 mb-2"
class="flex items-center gap-3 border-b border-fv-neutral-100 py-1.5"
>
<h3
class="font-bold whitespace-nowrap w-[270px] text-lg mb-2 uppercase"
class="font-bold whitespace-nowrap w-[270px] text-lg uppercase"
>
{{ key }}
</h3>

<div class="flex-1 rounded-none bg-fv-neutral-700 relative noise">
<div class="flex-1 bg-fv-neutral-700 relative noise rounded-full overflow-hidden ">
<div
class="absolute leading-none text-sm text-white text-center inset-x-0 top-[5px] font-medium"
>
Expand Down Expand Up @@ -136,7 +136,7 @@ onMounted(async () => {
<div
v-for="(t, j) in thoth"
:key="t.key"
class="border-l border-b border-fv-neutral-400 border-r py-2 px-3"
class="border-l border-b border-fv-neutral-100 border-r py-2 px-3"
:class="{
'rounded-b-xl ': j == thoth.length - 1,
'rounded-t-xl border-t': j == 0,
Expand Down Expand Up @@ -190,7 +190,7 @@ onMounted(async () => {
</div>
</div>
</section>
<section class="py-10 px-3">
<section class="py-10 px-3" v-if="0">
<h2 class="font-bold text-2xl lg:text-3xl mb-4">FWS Stats</h2>
<div
v-if="stats"
Expand Down
49 changes: 49 additions & 0 deletions packages/playground/src/pages/JSProcessPage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useTranslation, useRest, useEventBus } from "@fy-/fws-vue";
import { getLocales } from "@fy-/fws-js";
const translate = useTranslation();
const maxValue = ref(0);
const rest = useRest();
const stats = ref<any>();
const getProcess = async () => {
const data = await rest(`JS`, "GET");
if (data && data.result == "success") {
stats.value = data.data;
}
};
const eventBus = useEventBus();
onMounted(async () => {
eventBus.emit("main-loading", true);
await getProcess();
eventBus.emit("main-loading", false);
});
</script>
<template>
<div class="container xl:max-w-6xl mx-auto px-4 mt-8">
<h1 class="text-3xl mb-6">
JS Workers
<small class="text-sm italic"
>(🌐 requests, πŸ”» restarts, 🐌 queue size)</small
>
</h1>
<div v-for="(v, k) in stats" :key="v.Name">
<h3 class="text-xl font-bold">{{ k }}</h3>
<div class="p-6 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
<div
class="flex flex-row items-center justify-between bg-gray-100 p-3 rounded-full py-2"
v-for="p in v"
>
<div class="text-sm font-bold">
{{ p.IsReady ? "βœ…" : "☠️" }} {{ p.Name.replace(k, "") }}
</div>
<div class="text-sm">{{ p.Latency }}ms</div>
<div class="text-sm">{{ p.RequestsHandled }} 🌐</div>
<div class="text-sm">{{ p.Restarts }} πŸ”»</div>
<div class="text-sm">{{ p.InputQueue }} 🐌</div>
</div>
</div>
</div>
</div>
</template>
5 changes: 5 additions & 0 deletions packages/playground/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export default [
name: "docs",
component: () => import("./pages/doc/IndexDoc.vue"),
},
{
path: "/js_workers",
name: "/js_workers",
component: () => import("./pages/JSProcessPage.vue"),
},
{
path: "/fws-vue",
name: "fws-vue",
Expand Down

0 comments on commit 098e206

Please sign in to comment.