mtxuilib 0.1.38 → 0.1.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mtxuilib",
3
3
  "private": false,
4
- "version": "0.1.38",
4
+ "version": "0.1.39",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -1 +0,0 @@
1
- export declare const ServiceWorkerMain: () => import("react/jsx-runtime").JSX.Element;
@@ -1,25 +0,0 @@
1
- "use client";
2
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
- import { MtButton } from "mtxuilib/ui-mt/Button";
4
- import { useEffect, useRef } from "react";
5
- export const ServiceWorkerMain = () => {
6
- const workerRef = useRef();
7
- useEffect(() => {
8
- if (typeof window != "undefined") {
9
- if ("serviceWorker" in navigator) {
10
- navigator.serviceWorker
11
- .register("/api.v1/sw.js", { scope: "./'" })
12
- .then((registration) => {
13
- console.log("Service worker registered successfully. Scope:", registration.scope);
14
- })
15
- .catch((error) => {
16
- console.error("Service worker registration failed:", error);
17
- });
18
- }
19
- }
20
- }, []);
21
- return (_jsxs("div", { className: "prose bg-slate-100", children: [_jsx("p", { children: "WebWorker6" }), _jsx(MtButton, { onClick: () => {
22
- console.log("fetch1");
23
- fetch("/");
24
- }, children: "fetch-test" })] }));
25
- };
@@ -1 +0,0 @@
1
- export declare function pi(n: number): number;
@@ -1,7 +0,0 @@
1
- export function pi(n) {
2
- let v = 0;
3
- for (let i = 1; i <= n; i += 4) {
4
- v += 1 / i - 1 / (i + 2);
5
- }
6
- return 4 * v;
7
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,34 +0,0 @@
1
- import { pi } from "./lib";
2
- addEventListener("message", (event) => {
3
- postMessage(pi(event.data));
4
- });
5
- const installEvent = () => {
6
- self.addEventListener("install", () => {
7
- console.log("service worker installed");
8
- });
9
- };
10
- installEvent();
11
- const activateEvent = () => {
12
- self.addEventListener("activate", () => {
13
- console.log("service worker activated");
14
- });
15
- };
16
- activateEvent();
17
- const cacheName = "v3";
18
- const cacheClone = async (e) => {
19
- const res = await fetch(e.request);
20
- const resClone = res.clone();
21
- const cache = await caches.open(cacheName);
22
- await cache.put(e.request, resClone);
23
- return res;
24
- };
25
- const fetchEvent = () => {
26
- console.log("setup service worker fetch event");
27
- self.addEventListener("fetch", (e) => {
28
- console.log("fetch event@serviceworker", e);
29
- e.respondWith(cacheClone(e)
30
- .catch(() => caches.match(e.request))
31
- .then((res) => res));
32
- });
33
- };
34
- fetchEvent();
package/dist/lib/url.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export declare const isAbsoluteUrl: (url: string) => boolean;
2
- export declare function urlJoin(...args: any): string;
package/dist/lib/url.js DELETED
@@ -1,67 +0,0 @@
1
- export const isAbsoluteUrl = (url) => {
2
- return url.startsWith("http://") || url.startsWith("https://");
3
- };
4
- function normalize(strArray) {
5
- const resultArray = [];
6
- if (strArray.length === 0) {
7
- return '';
8
- }
9
- strArray = strArray.filter((part) => part !== '');
10
- if (typeof strArray[0] !== 'string') {
11
- throw new TypeError('Url must be a string. Received ' + strArray[0]);
12
- }
13
- if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) {
14
- strArray[0] = strArray.shift() + strArray[0];
15
- }
16
- if (strArray[0] === '/' && strArray.length > 1) {
17
- strArray[0] = strArray.shift() + strArray[0];
18
- }
19
- if (strArray[0].match(/^file:\/\/\//)) {
20
- strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1:///');
21
- }
22
- else {
23
- strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://');
24
- }
25
- for (let i = 0; i < strArray.length; i++) {
26
- let component = strArray[i];
27
- if (typeof component !== 'string') {
28
- throw new TypeError('Url must be a string. Received ' + component);
29
- }
30
- if (i > 0) {
31
- component = component.replace(/^[\/]+/, '');
32
- }
33
- if (i < strArray.length - 1) {
34
- component = component.replace(/[\/]+$/, '');
35
- }
36
- else {
37
- component = component.replace(/[\/]+$/, '/');
38
- }
39
- if (component === '') {
40
- continue;
41
- }
42
- resultArray.push(component);
43
- }
44
- let str = '';
45
- for (let i = 0; i < resultArray.length; i++) {
46
- const part = resultArray[i];
47
- if (i === 0) {
48
- str += part;
49
- continue;
50
- }
51
- const prevPart = resultArray[i - 1];
52
- if (prevPart && prevPart.endsWith('?') || prevPart.endsWith('#')) {
53
- str += part;
54
- continue;
55
- }
56
- str += '/' + part;
57
- }
58
- str = str.replace(/\/(\?|&|#[^!])/g, '$1');
59
- const [beforeHash, afterHash] = str.split('#');
60
- const parts = beforeHash.split(/(?:\?|&)+/).filter(Boolean);
61
- str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&') + (afterHash && afterHash.length > 0 ? '#' + afterHash : '');
62
- return str;
63
- }
64
- export function urlJoin(...args) {
65
- const parts = Array.from(Array.isArray(args[0]) ? args[0] : args);
66
- return normalize(parts);
67
- }
@@ -1,3 +0,0 @@
1
- export type UseScriptStatus = "idle" | "loading" | "ready" | "error";
2
- declare function useScript(src: string): UseScriptStatus;
3
- export default useScript;
@@ -1,66 +0,0 @@
1
- 'use client';
2
- import * as React from "react";
3
- const cachedScriptStatuses = {};
4
- function getScriptNode(src) {
5
- const node = document.querySelector(`script[src="${src}"]`);
6
- const status = node?.getAttribute("data-status");
7
- return {
8
- node,
9
- status,
10
- };
11
- }
12
- function useScript(src) {
13
- const [status, setStatus] = React.useState(() => {
14
- if (typeof window === "undefined") {
15
- return "loading";
16
- }
17
- return cachedScriptStatuses[src] ?? "loading";
18
- });
19
- React.useEffect(() => {
20
- const cachedScriptStatus = cachedScriptStatuses[src];
21
- if (cachedScriptStatus === "ready" || cachedScriptStatus === "error") {
22
- setStatus(cachedScriptStatus);
23
- return;
24
- }
25
- const script = getScriptNode(src);
26
- let scriptNode = script.node;
27
- if (!scriptNode) {
28
- scriptNode = document.createElement("script");
29
- scriptNode.src = src;
30
- scriptNode.async = true;
31
- scriptNode.setAttribute("data-status", "loading");
32
- document.body.appendChild(scriptNode);
33
- const setAttributeFromEvent = (event) => {
34
- const scriptStatus = event.type === "load" ? "ready" : "error";
35
- scriptNode?.setAttribute("data-status", scriptStatus);
36
- };
37
- scriptNode.addEventListener("load", setAttributeFromEvent);
38
- scriptNode.addEventListener("error", setAttributeFromEvent);
39
- }
40
- else {
41
- setStatus(script.status ?? cachedScriptStatus ?? "loading");
42
- }
43
- const setStateFromEvent = (event) => {
44
- const newStatus = event.type === "load" ? "ready" : "error";
45
- setStatus(newStatus);
46
- cachedScriptStatuses[src] = newStatus;
47
- };
48
- scriptNode.addEventListener("load", setStateFromEvent);
49
- scriptNode.addEventListener("error", setStateFromEvent);
50
- return () => {
51
- if (scriptNode) {
52
- scriptNode.removeEventListener("load", setStateFromEvent);
53
- scriptNode.removeEventListener("error", setStateFromEvent);
54
- }
55
- if (scriptNode) {
56
- try {
57
- scriptNode.remove();
58
- }
59
- catch (error) {
60
- }
61
- }
62
- };
63
- }, [src]);
64
- return status;
65
- }
66
- export default useScript;
File without changes