valyrian.js 7.2.9 → 7.2.11
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/dist/dataset/index.d.ts +2 -2
- package/dist/dataset/index.d.ts.map +1 -1
- package/dist/dataset/index.js +11 -4
- package/dist/dataset/index.js.map +2 -2
- package/dist/dataset/index.mjs +10 -4
- package/dist/dataset/index.mjs.map +2 -2
- package/dist/hooks/index.js +1 -0
- package/dist/hooks/index.js.map +2 -2
- package/dist/hooks/index.mjs.map +2 -2
- package/dist/index.d.ts +17 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +2 -2
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.mjs +3 -1
- package/dist/index.mjs.map +2 -2
- package/dist/node/index.js +22 -11
- package/dist/node/index.js.map +2 -2
- package/dist/node/index.mjs +21 -11
- package/dist/node/index.mjs.map +2 -2
- package/dist/node/utils/inline.d.ts +2 -2
- package/dist/node/utils/inline.d.ts.map +1 -1
- package/dist/node/utils/tree-adapter.d.ts +3 -3
- package/dist/node/utils/tree-adapter.d.ts.map +1 -1
- package/dist/proxy-signal/index.js +1 -0
- package/dist/proxy-signal/index.js.map +1 -1
- package/dist/request/index.js +1 -0
- package/dist/request/index.js.map +1 -1
- package/dist/router/index.d.ts +2 -2
- package/dist/router/index.d.ts.map +1 -1
- package/dist/router/index.js +5 -3
- package/dist/router/index.js.map +3 -3
- package/dist/router/index.mjs +4 -3
- package/dist/router/index.mjs.map +3 -3
- package/dist/signal/index.d.ts.map +1 -1
- package/dist/signal/index.js +3 -2
- package/dist/signal/index.js.map +2 -2
- package/dist/signal/index.mjs +2 -2
- package/dist/signal/index.mjs.map +2 -2
- package/dist/store/index.js +1 -0
- package/dist/store/index.js.map +1 -1
- package/dist/sw/index.d.ts +1 -1
- package/dist/sw/index.d.ts.map +1 -1
- package/dist/sw/index.js +1 -0
- package/dist/sw/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/lib/dataset/index.ts +15 -7
- package/lib/hooks/index.ts +2 -2
- package/lib/index.d.ts +0 -0
- package/lib/index.ts +22 -31
- package/lib/interfaces.ts.bak +141 -0
- package/lib/node/utils/inline.ts +17 -2
- package/lib/node/utils/tree-adapter.ts +15 -11
- package/lib/router/index.ts +16 -4
- package/lib/signal/index.ts +5 -5
- package/package.json +1 -1
- package/tsconfig.json +22 -8
- package/dist/interfaces.d.ts +0 -96
- package/dist/interfaces.d.ts.map +0 -1
- package/lib/interfaces.ts +0 -98
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inline.d.ts","sourceRoot":"","sources":["../../../lib/node/utils/inline.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"inline.d.ts","sourceRoot":"","sources":["../../../lib/node/utils/inline.ts"],"names":[],"mappings":"AASA,wBAAsB,MAAM,CAC1B,IAAI,EAAE,MAAM,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,EACjE,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM;SADX,MAAM;SAAQ,MAAM,GAAG,IAAI;UAAQ,MAAM;eAsHhE;yBAvHqB,MAAM"}
|
|
@@ -18,8 +18,8 @@ export declare class Node implements Node {
|
|
|
18
18
|
set nodeValue(text: string);
|
|
19
19
|
get nodeValue(): string;
|
|
20
20
|
parent_node: Node | null;
|
|
21
|
-
get parentNode(): Node;
|
|
22
|
-
set parentNode(node: Node);
|
|
21
|
+
get parentNode(): Node | null;
|
|
22
|
+
set parentNode(node: Node | null);
|
|
23
23
|
constructor();
|
|
24
24
|
appendChild<T extends Node>(node: T): T;
|
|
25
25
|
insertBefore<T extends Node>(node: T, child: Node | null): T;
|
|
@@ -27,7 +27,7 @@ export declare class Node implements Node {
|
|
|
27
27
|
removeChild<T extends Node>(child: T): T;
|
|
28
28
|
cloneNode(deep?: boolean | undefined): Node;
|
|
29
29
|
setAttribute(name: string, value: any): void;
|
|
30
|
-
getAttribute(name: string): string;
|
|
30
|
+
getAttribute(name: string): string | null | undefined;
|
|
31
31
|
removeAttribute(name: string): void;
|
|
32
32
|
getElementById(id: string): Node | null;
|
|
33
33
|
addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions | undefined): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tree-adapter.d.ts","sourceRoot":"","sources":["../../../lib/node/utils/tree-adapter.ts"],"names":[],"mappings":"AAEA,UAAU,UAAW,SAAQ,KAAK,CAAC,IAAI,GAAG,OAAO,GAAG,IAAI,GAAG,gBAAgB,CAAC;CAAG;AAE/E,qBAAa,IAAK,YAAW,IAAI;IAE/B,UAAU,EAAE,UAAU,CAAM;IAC5B,OAAO,EAAE,MAAM,CAAM;IAErB,QAAQ,EAAG,MAAM,CAAC;IAClB,IAAI,QAAQ,IAAI,MAAM,CAErB;IACD,IAAI,QAAQ,CAAC,IAAI,EAAE,MAAM,EAExB;IACD,IAAI,OAAO,IAAI,MAAM,CAEpB;IACD,IAAI,OAAO,CAAC,IAAI,EAAE,MAAM,EAEvB;IAED,SAAS,EAAG,MAAM,CAAC;IACnB,IAAI,QAAQ,IAAI,MAAM,CAErB;IACD,IAAI,QAAQ,CAAC,IAAI,EAAE,MAAM,EAExB;IAED,UAAU,SAAM;IAChB,UAAU,EAAE,IAAI,EAAE,CAAM;IACxB,IAAI,WAAW,CAAC,IAAI,QAAA,EAEnB;IACD,IAAI,WAAW,WAEd;IACD,IAAI,SAAS,CAAC,IAAI,QAAA,EAEjB;IACD,IAAI,SAAS,WAEZ;IAGD,WAAW,EAAE,IAAI,GAAG,IAAI,CAAQ;IAChC,IAAI,UAAU,
|
|
1
|
+
{"version":3,"file":"tree-adapter.d.ts","sourceRoot":"","sources":["../../../lib/node/utils/tree-adapter.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,GAAG;CAGzB;AAoBD,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CA+B9C;AAED,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,SAAI,UAoCjE;AAmQD,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,gBAAgB,CAsBzE;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,UAI7C;AAED,eAAO,MAAM,QAAQ,UAAiB,CAAC"}
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../lib/proxy-signal/index.ts"],
|
|
4
4
|
"sourcesContent": ["import { update } from \"valyrian.js\";\n\n/* eslint-disable no-use-before-define */\ninterface Cleanup {\n (): void;\n}\n\ninterface Subscription {\n // eslint-disable-next-line no-unused-vars\n (value: ProxySignal[\"value\"]): void | Cleanup;\n}\n\ninterface Subscriptions extends Map<Subscription, Cleanup> {}\n\ninterface Getter {\n // eslint-disable-next-line no-unused-vars\n (value: ProxySignal[\"value\"]): any;\n}\n\ninterface Getters {\n [key: string | symbol]: Getter;\n}\n\ninterface ProxySignal {\n // Works as a getter of the value\n (): ProxySignal[\"value\"];\n // Works as a subscription to the value\n // eslint-disable-next-line no-unused-vars\n (value: Subscription): ProxySignal;\n // Works as a setter with a path and a handler\n // eslint-disable-next-line no-unused-vars\n (path: string, handler: (valueAtPathPosition: any) => any): ProxySignal[\"value\"];\n // Works as a setter with a path and a value\n // eslint-disable-next-line no-unused-vars\n (path: string, value: any): ProxySignal[\"value\"];\n // Works as a setter with a value\n // eslint-disable-next-line no-unused-vars\n (value: any): ProxySignal[\"value\"];\n // Gets the current value of the signal.\n value: any;\n // Cleanup function to be called to remove all subscriptions.\n cleanup: () => void;\n // Creates a getter on the signal.\n // eslint-disable-next-line no-unused-vars\n getter: (name: string, handler: Getter) => any;\n // To access the getters on the signal.\n [key: string | number | symbol]: any;\n}\n\nfunction makeUnsubscribe(\n subscriptions: Subscriptions,\n computed: ProxySignal,\n handler: Subscription,\n cleanup?: Cleanup\n) {\n if (typeof cleanup === \"function\") {\n computed.cleanup = cleanup;\n }\n computed.unsubscribe = () => {\n subscriptions.delete(handler);\n computed?.cleanup();\n };\n}\n\nfunction createSubscription(signal: ProxySignal, subscriptions: Subscriptions, handler: Subscription) {\n if (subscriptions.has(handler) === false) {\n // eslint-disable-next-line no-use-before-define\n let computed = ProxySignal(() => handler(signal.value));\n let cleanup = computed(); // Execute to register itself\n makeUnsubscribe(subscriptions, computed, handler, cleanup);\n subscriptions.set(handler, computed);\n }\n\n return subscriptions.get(handler);\n}\n\nlet updateTimeout: any;\nfunction delayedUpdate() {\n clearTimeout(updateTimeout);\n updateTimeout = setTimeout(update);\n}\n\n// eslint-disable-next-line sonarjs/cognitive-complexity\nexport function ProxySignal(value: any): ProxySignal {\n let subscriptions = new Map();\n let getters: Getters = {};\n\n let forceUpdate = false;\n\n let signal: ProxySignal = new Proxy(\n // eslint-disable-next-line no-unused-vars\n function (valOrPath?: any | Subscription, handler?: (valueAtPathPosition: any) => any) {\n // Works as a getter\n if (typeof valOrPath === \"undefined\") {\n return signal.value;\n }\n\n // Works as a subscription\n if (typeof valOrPath === \"function\") {\n return createSubscription(signal, subscriptions, valOrPath);\n }\n\n // Works as a setter with a path\n if (typeof valOrPath === \"string\" && typeof handler !== \"undefined\") {\n let parsed = valOrPath.split(\".\");\n let result = signal.value;\n let next;\n while (parsed.length) {\n next = parsed.shift() as string;\n if (parsed.length > 0) {\n if (typeof result[next] !== \"object\") {\n result[next] = {};\n }\n result = result[next];\n } else {\n result[next] = typeof handler === \"function\" ? handler(result[next]) : handler;\n }\n }\n forceUpdate = true;\n signal.value = signal.value;\n return signal.value;\n }\n\n // Works as a setter with a value\n signal.value = valOrPath;\n return signal.value;\n } as ProxySignal,\n {\n set(state, prop, val) {\n if (prop === \"value\" || prop === \"unsubscribe\" || prop === \"cleanup\") {\n let old = state[prop];\n state[prop] = val;\n if (prop === \"value\" && (forceUpdate || val !== old)) {\n forceUpdate = false;\n for (let [handler, computed] of subscriptions) {\n computed.cleanup();\n let cleanup = handler(val);\n makeUnsubscribe(subscriptions, computed, handler, cleanup);\n }\n delayedUpdate();\n }\n return true;\n }\n return false;\n },\n get(state, prop) {\n if (prop === \"value\") {\n return typeof state.value === \"function\" ? state.value() : state.value;\n }\n\n if (prop === \"cleanup\" || prop === \"unsubscribe\" || prop === \"getter\") {\n return state[prop];\n }\n\n if (prop in getters) {\n return getters[prop](state.value);\n }\n }\n }\n );\n\n Object.defineProperties(signal, {\n value: { value, writable: true, enumerable: true },\n cleanup: {\n value() {\n // eslint-disable-next-line no-unused-vars\n for (let [handler, computed] of subscriptions) {\n computed.unsubscribe();\n }\n },\n writable: true,\n enumerable: true\n },\n getter: {\n value(name: string, handler: Getter) {\n if (name in getters) {\n throw new Error(\"Named computed already exists.\");\n }\n\n getters[name] = handler;\n },\n enumerable: true\n }\n });\n\n return signal;\n}\n"],
|
|
5
|
-
"mappings": "
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAuB;AAiDvB,SAAS,gBACP,eACA,UACA,SACA,SACA;AACA,MAAI,OAAO,YAAY,YAAY;AACjC,aAAS,UAAU;AAAA,EACrB;AACA,WAAS,cAAc,MAAM;AAC3B,kBAAc,OAAO,OAAO;AAC5B,cAAU,QAAQ;AAAA,EACpB;AACF;AAEA,SAAS,mBAAmB,QAAqB,eAA8B,SAAuB;AACpG,MAAI,cAAc,IAAI,OAAO,MAAM,OAAO;AAExC,QAAI,WAAW,YAAY,MAAM,QAAQ,OAAO,KAAK,CAAC;AACtD,QAAI,UAAU,SAAS;AACvB,oBAAgB,eAAe,UAAU,SAAS,OAAO;AACzD,kBAAc,IAAI,SAAS,QAAQ;AAAA,EACrC;AAEA,SAAO,cAAc,IAAI,OAAO;AAClC;AAEA,IAAI;AACJ,S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AgB,eAAe,UAAU,SAAS,OAAO;AAAA,YAC3D;AACA,0BAAc;AAAA,UAChB;AACA,iBAAO;AAAA,QACT;AACA,eAAO;AAAA,MACT;AAAA,MACA,IAAI,OAAO,MAAM;AACf,YAAI,SAAS,SAAS;AACpB,iBAAO,OAAO,MAAM,UAAU,aAAa,MAAM,MAAM,IAAI,MAAM;AAAA,QACnE;AAEA,YAAI,SAAS,aAAa,SAAS,iBAAiB,SAAS,UAAU;AACrE,iBAAO,MAAM,IAAI;AAAA,QACnB;AAEA,YAAI,QAAQ,SAAS;AACnB,iBAAO,QAAQ,IAAI,EAAE,MAAM,KAAK;AAAA,QAClC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO,iBAAiB,QAAQ;AAAA,IAC9B,OAAO,EAAE,OAAO,UAAU,MAAM,YAAY,KAAK;AAAA,IACjD,SAAS;AAAA,MACP,QAAQ;AAEN,iBAAS,CAAC,SAAS,QAAQ,KAAK,eAAe;AAC7C,mBAAS,YAAY;AAAA,QACvB;AAAA,MACF;AAAA,MACA,UAAU;AAAA,MACV,YAAY;AAAA,IACd;AAAA,IACA,QAAQ;AAAA,MACN,MAAM,MAAc,SAAiB;AACnC,YAAI,QAAQ,SAAS;AACnB,gBAAM,IAAI,MAAM,gCAAgC;AAAA,QAClD;AAEA,gBAAQ,IAAI,IAAI;AAAA,MAClB;AAAA,MACA,YAAY;AAAA,IACd;AAAA,EACF,CAAC;AAED,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/request/index.js
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../lib/request/index.ts"],
|
|
4
4
|
"sourcesContent": ["import { isNodeJs } from \"valyrian.js\";\n\ninterface UrlOptions {\n base: string; // Used to prefix the url for scoped requests.\n node: string | null; // Used to redirect local requests to node server for server side rendering.\n api: string | null; // Used to redirect api requests to node server for server side rendering.\n}\n\ninterface RequestOptions {\n allowedMethods?: string[];\n urls?: UrlOptions;\n [key: string | number | symbol]: any;\n}\n\ninterface RequestOptionsWithUrls extends RequestOptions {\n urls: UrlOptions;\n allowedMethods: string[];\n}\n\ninterface SendOptions extends RequestOptionsWithUrls, RequestInit {\n allowedMethods: string[];\n method: string;\n headers: Record<string, string>;\n resolveWithFullResponse?: boolean;\n}\n\nexport interface RequestInterface {\n // eslint-disable-next-line no-unused-vars\n (method: string, url: string, data?: Record<string, any>, options?: Partial<SendOptions>): any | Response;\n // eslint-disable-next-line no-unused-vars\n new: (baseUrl: string, options?: RequestOptions) => RequestInterface;\n // eslint-disable-next-line no-unused-vars\n setOptions: (key: string, value: any) => void;\n // eslint-disable-next-line no-unused-vars\n getOptions: (key?: string) => RequestOptions | void;\n // eslint-disable-next-line no-unused-vars\n get: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;\n // eslint-disable-next-line no-unused-vars\n post: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;\n // eslint-disable-next-line no-unused-vars\n put: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;\n // eslint-disable-next-line no-unused-vars\n patch: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;\n // eslint-disable-next-line no-unused-vars\n delete: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;\n // eslint-disable-next-line no-unused-vars\n head: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;\n // eslint-disable-next-line no-unused-vars\n options: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;\n [key: string | number | symbol]: any;\n}\n\n// This method is used to serialize an object into a query string.\nfunction serialize(obj: Record<string, any>, prefix: string = \"\"): string {\n return Object.keys(obj)\n .map((prop: string) => {\n let k = prefix ? `${prefix}[${prop}]` : prop;\n return typeof obj[prop] === \"object\"\n ? serialize(obj[prop], k)\n : `${encodeURIComponent(k)}=${encodeURIComponent(obj[prop])}`;\n })\n .join(\"&\");\n}\n\nfunction parseUrl(url: string, options: RequestOptionsWithUrls) {\n let u = /^https?/gi.test(url) ? url : options.urls.base + url;\n\n let parts = u.split(\"?\");\n u = parts[0].trim().replace(/^\\/\\//, \"/\").replace(/\\/$/, \"\").trim();\n\n if (parts[1]) {\n u += `?${parts[1]}`;\n }\n\n if (isNodeJs && typeof options.urls.node === \"string\") {\n options.urls.node = options.urls.node;\n\n if (typeof options.urls.api === \"string\") {\n options.urls.api = options.urls.api.replace(/\\/$/gi, \"\").trim();\n u = u.replace(options.urls.api, options.urls.node);\n }\n\n if (!/^https?/gi.test(u)) {\n u = options.urls.node + u;\n }\n }\n\n return u;\n}\n\nconst defaultOptions: RequestOptions = { allowedMethods: [\"get\", \"post\", \"put\", \"patch\", \"delete\", \"head\", \"options\"] };\n\n// eslint-disable-next-line sonarjs/cognitive-complexity\nfunction Requester(baseUrl = \"\", options: RequestOptions = defaultOptions) {\n let url = baseUrl.replace(/\\/$/gi, \"\").trim();\n if (!options.urls) {\n options.urls = {\n base: \"\",\n node: null,\n api: null\n };\n }\n\n if (!options.allowedMethods) {\n options.allowedMethods = defaultOptions.allowedMethods;\n }\n\n let opts: RequestOptionsWithUrls = {\n ...(options as RequestOptionsWithUrls),\n urls: {\n node: options.urls.node || null,\n api: options.urls.api || null,\n base: options.urls.base ? options.urls.base + url : url\n }\n };\n\n const request = async function request(method: string, url: string, data?: Record<string, any>, options = {}) {\n let innerOptions: SendOptions = {\n method: method.toUpperCase(),\n headers: {},\n resolveWithFullResponse: false,\n ...opts,\n ...options\n } as SendOptions;\n\n if (!innerOptions.headers.Accept) {\n innerOptions.headers.Accept = \"application/json\";\n }\n\n let acceptType = innerOptions.headers.Accept;\n let contentType = innerOptions.headers[\"Content-Type\"] || innerOptions.headers[\"content-type\"] || \"\";\n\n if (innerOptions.allowedMethods.indexOf(method) === -1) {\n throw new Error(\"Method not allowed\");\n }\n\n if (data) {\n if (innerOptions.method === \"GET\" && typeof data === \"object\") {\n url += `?${serialize(data)}`;\n }\n\n if (innerOptions.method !== \"GET\") {\n if (/json/gi.test(contentType)) {\n innerOptions.body = JSON.stringify(data);\n } else {\n let formData;\n if (data instanceof FormData) {\n formData = data;\n } else {\n formData = new FormData();\n for (let i in data) {\n formData.append(i, data[i]);\n }\n }\n innerOptions.body = formData;\n }\n }\n }\n\n let response = await fetch(parseUrl(url, opts), innerOptions);\n let body = null;\n if (!response.ok) {\n let err = new Error(response.statusText) as Error & { response?: any; body?: any };\n err.response = response;\n if (/text/gi.test(acceptType)) {\n err.body = await response.text();\n }\n\n if (/json/gi.test(acceptType)) {\n try {\n err.body = await response.json();\n } catch (error) {\n // ignore\n }\n }\n\n throw err;\n }\n\n if (innerOptions.resolveWithFullResponse) {\n return response;\n }\n\n if (/text/gi.test(acceptType)) {\n body = await response.text();\n return body;\n }\n\n if (/json/gi.test(acceptType)) {\n try {\n body = await response.json();\n return body;\n } catch (error) {\n // ignore\n }\n }\n\n return response;\n } as unknown as RequestInterface;\n\n request.new = (baseUrl: string, options?: RequestOptions) => Requester(baseUrl, { ...opts, ...(options || {}) });\n\n request.setOption = (key: string, value: any) => {\n let result = opts;\n\n let parsed = key.split(\".\");\n let next;\n\n while (parsed.length) {\n next = parsed.shift() as string;\n\n let nextIsArray = next.indexOf(\"[\") > -1;\n if (nextIsArray) {\n let idx = next.replace(/\\D/gi, \"\");\n next = next.split(\"[\")[0];\n parsed.unshift(idx);\n }\n\n if (parsed.length > 0 && typeof result[next] !== \"object\") {\n result[next] = nextIsArray ? [] : {};\n }\n\n if (parsed.length === 0 && typeof value !== \"undefined\") {\n result[next] = value;\n }\n\n result = result[next];\n }\n\n return result;\n };\n\n request.getOptions = (key?: string) => {\n if (!key) {\n return opts;\n }\n\n let result = opts;\n let parsed = key.split(\".\");\n let next;\n\n while (parsed.length) {\n next = parsed.shift() as string;\n\n let nextIsArray = next.indexOf(\"[\") > -1;\n if (nextIsArray) {\n let idx = next.replace(/\\D/gi, \"\");\n next = next.split(\"[\")[0];\n parsed.unshift(idx);\n }\n\n if (parsed.length > 0 && typeof result[next] !== \"object\") {\n return null;\n }\n\n if (parsed.length === 0) {\n return result[next];\n }\n\n result = result[next];\n }\n };\n\n opts.allowedMethods.forEach(\n (method) =>\n (request[method] = (url: string, data?: Record<string, any>, options?: Record<string, any>) =>\n request(method, url, data, options))\n );\n\n return request;\n}\n\nexport const request = Requester();\n"],
|
|
5
|
-
"mappings": "
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAyB;AAqDzB,SAAS,UAAU,KAA0B,SAAiB,IAAY;AACxE,SAAO,OAAO,KAAK,GAAG,EACnB,IAAI,CAAC,SAAiB;AACrB,QAAI,IAAI,SAAS,GAAG,MAAM,IAAI,IAAI,MAAM;AACxC,WAAO,OAAO,IAAI,IAAI,MAAM,WACxB,UAAU,IAAI,IAAI,GAAG,CAAC,IACtB,GAAG,mBAAmB,CAAC,CAAC,IAAI,mBAAmB,IAAI,IAAI,CAAC,CAAC;AAAA,EAC/D,CAAC,EACA,KAAK,GAAG;AACb;AAEA,SAAS,SAAS,KAAa,SAAiC;AAC9D,MAAI,IAAI,YAAY,KAAK,GAAG,IAAI,MAAM,QAAQ,KAAK,OAAO;AAE1D,MAAI,QAAQ,EAAE,MAAM,GAAG;AACvB,MAAI,MAAM,CAAC,EAAE,KAAK,EAAE,QAAQ,SAAS,GAAG,EAAE,QAAQ,OAAO,EAAE,EAAE,KAAK;AAElE,MAAI,MAAM,CAAC,GAAG;AACZ,SAAK,IAAI,MAAM,CAAC,CAAC;AAAA,EACnB;AAEA,MAAI,4BAAY,OAAO,QAAQ,KAAK,SAAS,UAAU;AACrD,YAAQ,KAAK,OAAO,QAAQ,KAAK;AAEjC,QAAI,OAAO,QAAQ,KAAK,QAAQ,UAAU;AACxC,cAAQ,KAAK,MAAM,QAAQ,KAAK,IAAI,QAAQ,SAAS,EAAE,EAAE,KAAK;AAC9D,UAAI,EAAE,QAAQ,QAAQ,KAAK,KAAK,QAAQ,KAAK,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S,OAAO,IAAI;AAAA,IACtB;AAAA,EACF;AAEA,OAAK,eAAe;AAAA,IAClB,CAAC,WACEA,SAAQ,MAAM,IAAI,CAACC,MAAa,MAA4BC,aAC3DF,SAAQ,QAAQC,MAAK,MAAMC,QAAO;AAAA,EACxC;AAEA,SAAOF;AACT;AAEO,IAAM,UAAU,UAAU;",
|
|
6
6
|
"names": ["request", "url", "options", "baseUrl"]
|
|
7
7
|
}
|
package/dist/router/index.d.ts
CHANGED
|
@@ -48,10 +48,10 @@ export declare class Router implements RouterInterface {
|
|
|
48
48
|
add(path: string, ...middlewares: Middlewares): Router;
|
|
49
49
|
use(...middlewares: Middlewares | Router[] | string[]): Router;
|
|
50
50
|
routes(): string[];
|
|
51
|
-
go(path: string, parentComponent?: Component, preventPushState?: boolean): Promise<string | void>;
|
|
51
|
+
go(path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface, preventPushState?: boolean): Promise<string | void>;
|
|
52
52
|
getOnClickHandler(url: string): (e: MouseEvent) => void;
|
|
53
53
|
}
|
|
54
|
-
export declare function redirect(url: string, parentComponent?: Component, preventPushState?: boolean): string | void
|
|
54
|
+
export declare function redirect(url: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface, preventPushState?: boolean): Promise<string | void>;
|
|
55
55
|
export declare function mountRouter(elementContainer: string | any, router: Router): void;
|
|
56
56
|
export {};
|
|
57
57
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/router/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,aAAa,EACb,uBAAuB,EASxB,MAAM,aAAa,CAAC;AAErB,UAAU,OAAO;IACf,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,E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,MAAW;IAInC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,WAAW,EAAE,WAAW,GAAG,MAAM;IAMtD,GAAG,CAAC,GAAG,WAAW,EAAE,WAAW,GAAG,MAAM,EAAE,GAAG,MAAM,EAAE,GAAG,MAAM;IA2B9D,MAAM,IAAI,MAAM,EAAE;IAIZ,EAAE,CACN,IAAI,EAAE,MAAM,EACZ,eAAe,CAAC,EAAE,SAAS,GAAG,aAAa,GAAG,uBAAuB,EACrE,gBAAgB,UAAQ,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAwCzB,iBAAiB,CAAC,GAAG,EAAE,MAAM,OAChB,UAAU;CAOxB;AAID,wBAAgB,QAAQ,CACtB,GAAG,EAAE,MAAM,EACX,eAAe,CAAC,EAAE,SAAS,GAAG,aAAa,GAAG,uBAAuB,EACrE,gBAAgB,UAAQ,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAKxB;AAED,wBAAgB,WAAW,CAAC,gBAAgB,EAAE,MAAM,GAAG,GAAG,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAiBhF"}
|
package/dist/router/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
@@ -209,12 +210,13 @@ function mountRouter(elementContainer, router) {
|
|
|
209
210
|
router.container = elementContainer;
|
|
210
211
|
localRedirect = router.go.bind(router);
|
|
211
212
|
if (!import_valyrian.isNodeJs) {
|
|
212
|
-
let
|
|
213
|
+
let onPopStateGoToRoute2 = function() {
|
|
213
214
|
let pathWithoutPrefix = getPathWithoutPrefix(document.location.pathname, router.pathPrefix);
|
|
214
215
|
router.go(pathWithoutPrefix, void 0, true);
|
|
215
216
|
};
|
|
216
|
-
|
|
217
|
-
|
|
217
|
+
var onPopStateGoToRoute = onPopStateGoToRoute2;
|
|
218
|
+
window.addEventListener("popstate", onPopStateGoToRoute2, false);
|
|
219
|
+
onPopStateGoToRoute2();
|
|
218
220
|
}
|
|
219
221
|
(0, import_valyrian.directive)("route", (url, vnode, oldnode) => {
|
|
220
222
|
(0, import_valyrian.setAttribute)("href", url, vnode, oldnode);
|
package/dist/router/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../lib/router/index.ts"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable no-use-before-define */\nimport {\n Component,\n POJOComponent,\n VnodeComponentInterface,\n VnodeWithDom,\n directive,\n isComponent,\n isNodeJs,\n isVnodeComponent,\n mount,\n setAttribute,\n v\n} from \"valyrian.js\";\n\ninterface Request {\n params: Record<string, any>;\n query: Record<string, any>;\n url: string;\n path: string;\n matches: string[];\n // eslint-disable-next-line no-unused-vars\n redirect: (path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface) => false;\n}\n\ninterface Middleware {\n // eslint-disable-next-line no-unused-vars\n (req: Request, res?: any):\n | Promise<any | Component | POJOComponent | VnodeComponentInterface>\n | any\n | Component\n | POJOComponent\n | VnodeComponentInterface;\n}\n\ninterface Middlewares extends Array<Middleware> {}\n\ninterface Path {\n method: string;\n path: string;\n middlewares: Middlewares;\n params: string[];\n regexp: RegExp;\n}\n\ninterface RouterInterface {\n paths: Path[];\n container: Element | string | null;\n query: Record<string, string | number>;\n options: Record<string, any>;\n url: string;\n path: string;\n params: Record<string, string | number | any>;\n matches: string[];\n pathPrefix: string;\n // eslint-disable-next-line no-unused-vars\n add(method: string, ...args: Middlewares): Router;\n // eslint-disable-next-line no-unused-vars\n use(...args: (string | Middleware | Router)[]): Router;\n\n routes(): string[];\n // eslint-disable-next-line no-unused-vars\n go(path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface): Promise<string | void>;\n}\n\ninterface RedirectFunction {\n // eslint-disable-next-line no-unused-vars\n (url: string, parentComponent?: Component, preventPushState?: boolean): string | void;\n}\n\nfunction flat(array: any) {\n return Array.isArray(array) ? array.flat(Infinity) : [array];\n}\n\nfunction getPathWithoutPrefix(path: string, prefix: string) {\n return getPathWithoutLastSlash(path.replace(new RegExp(`^${prefix}`), \"\"));\n}\n\nfunction getPathWithoutLastSlash(path: string) {\n let pathWithoutLastSlash = path.replace(/\\/$/, \"\");\n if (pathWithoutLastSlash === \"\") {\n pathWithoutLastSlash = \"/\";\n }\n return pathWithoutLastSlash;\n}\n\nconst addPath = ({\n router,\n method,\n path,\n middlewares\n}: {\n router: Router;\n method: string;\n path: string;\n middlewares: Middleware[];\n}): void => {\n if (!method || !path || !Array.isArray(middlewares) || middlewares.length === 0) {\n throw new Error(`Invalid route input: ${method} ${path} ${middlewares}`);\n }\n\n // Trim trailing slashes from the path\n let realpath = path.replace(/(\\S)(\\/+)$/, \"$1\");\n\n // Find the express-like params in the path\n let params = (realpath.match(/:(\\w+)?/gi) || [])\n // Set the names of the params found\n .map((param) => param.slice(1));\n\n // Generate a regular expression to match the path\n let regexpPath = \"^\" + realpath.replace(/:(\\w+)/gi, \"([^\\\\/\\\\s]+)\") + \"$\";\n\n router.paths.push({\n method,\n path: realpath,\n middlewares: flat(middlewares),\n params,\n regexp: new RegExp(regexpPath, \"i\")\n });\n};\n\n// Parse a query string into an object\nfunction parseQuery(queryParts?: string): Record<string, string> {\n // Split the query string into an array of name-value pairs\n let parts = queryParts ? queryParts.split(\"&\") : [];\n let query: Record<string, string> = {};\n\n // Iterate over the name-value pairs and add them to the query object\n for (let nameValue of parts) {\n let [name, value] = nameValue.split(\"=\", 2);\n query[name] = value || \"\";\n }\n\n return query;\n}\n\n// Search for middlewares that match a given path\nfunction searchMiddlewares(router: RouterInterface, path: string): Middlewares {\n let middlewares: Middlewares = [];\n let params: Record<string, any> = {};\n let matches = [];\n\n // Search for middlewares\n for (let item of router.paths) {\n let match = item.regexp.exec(path);\n\n // If we found middlewares\n if (Array.isArray(match)) {\n middlewares.push(...item.middlewares);\n match.shift();\n\n // Parse params\n for (let [index, key] of item.params.entries()) {\n params[key] = match[index];\n }\n\n // Add remaining matches to the array\n matches.push(...match);\n\n if (item.method === \"add\") {\n router.path = getPathWithoutPrefix(item.path, router.pathPrefix);\n break;\n }\n }\n }\n\n router.params = params;\n router.matches = matches;\n\n return middlewares;\n}\n\nasync function searchComponent(\n router: RouterInterface,\n middlewares: Middlewares\n): Promise<Component | VnodeComponentInterface | false | void> {\n // Define request object with default values\n const request: Request = {\n params: router.params,\n query: router.query,\n url: router.url,\n path: router.path,\n matches: router.matches,\n redirect: (path: string, parentComponent?: Component) => {\n router.go(path, parentComponent);\n // Return false to stop the middleware chain\n return false;\n }\n };\n\n // Initialize response variable\n let response;\n\n // Iterate through middlewares\n for (const middleware of middlewares) {\n // Invoke middleware and update response\n response = await middleware(request, response);\n\n // Return response if it's a valid component\n if (response !== undefined && (isComponent(response) || isVnodeComponent(response))) {\n return response;\n }\n\n // Return false if response is explicitly false to stop the middleware chain\n if (response === false) {\n return false;\n }\n }\n}\n\nexport class Router implements RouterInterface {\n paths: Path[] = [];\n container: Element | string | null = null;\n query: Record<string, string | number> = {};\n options: Record<string, any> = {};\n url: string = \"\";\n path: string = \"\";\n params: Record<string, string | number | any> = {};\n matches: string[] = [];\n pathPrefix: string = \"\";\n\n constructor(pathPrefix: string = \"\") {\n this.pathPrefix = pathPrefix;\n }\n\n add(path: string, ...middlewares: Middlewares): Router {\n let pathWithoutLastSlash = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);\n addPath({ router: this, method: \"add\", path: pathWithoutLastSlash, middlewares });\n return this;\n }\n\n use(...middlewares: Middlewares | Router[] | string[]): Router {\n let path = getPathWithoutLastSlash(\n `${this.pathPrefix}${typeof middlewares[0] === \"string\" ? middlewares.shift() : \"/\"}`\n );\n\n for (const item of middlewares) {\n if (item instanceof Router) {\n const subrouter = item as Router;\n for (const subpath of subrouter.paths) {\n addPath({\n router: this,\n method: subpath.method,\n path: `${path}${subpath.path}`.replace(/^\\/\\//, \"/\"),\n middlewares: subpath.middlewares\n });\n }\n continue;\n }\n\n if (typeof item === \"function\") {\n addPath({ router: this, method: \"use\", path: `${path}.*`, middlewares: [item as Middleware] });\n }\n }\n\n return this;\n }\n\n routes(): string[] {\n return this.paths.filter((path) => path.method === \"add\").map((path) => path.path);\n }\n\n async go(path: string, parentComponent?: Component, preventPushState = false): Promise<string | void> {\n if (!path) {\n throw new Error(\"router.url.required\");\n }\n\n let constructedPath = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);\n const parts = constructedPath.split(\"?\", 2);\n this.url = constructedPath;\n this.query = parseQuery(parts[1]);\n\n const middlewares = searchMiddlewares(this as RouterInterface, parts[0].replace(/(.+)\\/$/, \"$1\").split(\"#\")[0]);\n let component = await searchComponent(this as RouterInterface, middlewares);\n\n if (component === false) {\n return;\n }\n\n if (!component) {\n throw new Error(`The url ${constructedPath} requested wasn't found`);\n }\n\n if (isComponent(parentComponent) || isVnodeComponent(parentComponent)) {\n const childComponent = isVnodeComponent(component) ? component : v(component as Component, {});\n if (isVnodeComponent(parentComponent)) {\n parentComponent.children.push(childComponent);\n component = parentComponent;\n } else {\n component = v(parentComponent, {}, childComponent) as VnodeComponentInterface;\n }\n }\n\n if (!isNodeJs && !preventPushState) {\n window.history.pushState(null, \"\", constructedPath);\n }\n\n if (this.container) {\n return mount(this.container, component);\n }\n }\n\n getOnClickHandler(url: string) {\n return (e: MouseEvent) => {\n if (typeof url === \"string\" && url.length > 0) {\n this.go(url);\n }\n e.preventDefault();\n };\n }\n}\n\nlet localRedirect: RedirectFunction;\n\nexport function redirect(url: string, parentComponent?: Component, preventPushState = false): string | void {\n if (!localRedirect) {\n throw new Error(\"router.redirect.not.found\");\n }\n return localRedirect(url, parentComponent, preventPushState);\n}\n\nexport function mountRouter(elementContainer: string | any, router: Router): void {\n router.container = elementContainer;\n localRedirect = router.go.bind(router);\n\n if (!isNodeJs) {\n function onPopStateGoToRoute(): void {\n let pathWithoutPrefix = getPathWithoutPrefix(document.location.pathname, router.pathPrefix);\n (router as unknown as Router).go(pathWithoutPrefix, undefined, true);\n }\n window.addEventListener(\"popstate\", onPopStateGoToRoute, false);\n onPopStateGoToRoute();\n }\n\n directive(\"route\", (url: string, vnode: VnodeWithDom, oldnode?: VnodeWithDom): void => {\n setAttribute(\"href\", url, vnode, oldnode);\n setAttribute(\"onclick\", router.getOnClickHandler(url), vnode, oldnode);\n });\n}\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
4
|
+
"sourcesContent": ["/* eslint-disable no-use-before-define */\nimport {\n Component,\n POJOComponent,\n VnodeComponentInterface,\n VnodeWithDom,\n directive,\n isComponent,\n isNodeJs,\n isVnodeComponent,\n mount,\n setAttribute,\n v\n} from \"valyrian.js\";\n\ninterface Request {\n params: Record<string, any>;\n query: Record<string, any>;\n url: string;\n path: string;\n matches: string[];\n // eslint-disable-next-line no-unused-vars\n redirect: (path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface) => false;\n}\n\ninterface Middleware {\n // eslint-disable-next-line no-unused-vars\n (req: Request, res?: any):\n | Promise<any | Component | POJOComponent | VnodeComponentInterface>\n | any\n | Component\n | POJOComponent\n | VnodeComponentInterface;\n}\n\ninterface Middlewares extends Array<Middleware> {}\n\ninterface Path {\n method: string;\n path: string;\n middlewares: Middlewares;\n params: string[];\n regexp: RegExp;\n}\n\ninterface RouterInterface {\n paths: Path[];\n container: Element | string | null;\n query: Record<string, string | number>;\n options: Record<string, any>;\n url: string;\n path: string;\n params: Record<string, string | number | any>;\n matches: string[];\n pathPrefix: string;\n // eslint-disable-next-line no-unused-vars\n add(method: string, ...args: Middlewares): Router;\n // eslint-disable-next-line no-unused-vars\n use(...args: (string | Middleware | Router)[]): Router;\n\n routes(): string[];\n // eslint-disable-next-line no-unused-vars\n go(path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface): Promise<string | void>;\n}\n\ninterface RedirectFunction {\n // eslint-disable-next-line no-unused-vars\n (\n path: string,\n parentComponent?: Component | POJOComponent | VnodeComponentInterface,\n preventPushState?: boolean\n ): Promise<string | void>;\n}\n\nfunction flat(array: any) {\n return Array.isArray(array) ? array.flat(Infinity) : [array];\n}\n\nfunction getPathWithoutPrefix(path: string, prefix: string) {\n return getPathWithoutLastSlash(path.replace(new RegExp(`^${prefix}`), \"\"));\n}\n\nfunction getPathWithoutLastSlash(path: string) {\n let pathWithoutLastSlash = path.replace(/\\/$/, \"\");\n if (pathWithoutLastSlash === \"\") {\n pathWithoutLastSlash = \"/\";\n }\n return pathWithoutLastSlash;\n}\n\nconst addPath = ({\n router,\n method,\n path,\n middlewares\n}: {\n router: Router;\n method: string;\n path: string;\n middlewares: Middleware[];\n}): void => {\n if (!method || !path || !Array.isArray(middlewares) || middlewares.length === 0) {\n throw new Error(`Invalid route input: ${method} ${path} ${middlewares}`);\n }\n\n // Trim trailing slashes from the path\n let realpath = path.replace(/(\\S)(\\/+)$/, \"$1\");\n\n // Find the express-like params in the path\n let params = (realpath.match(/:(\\w+)?/gi) || [])\n // Set the names of the params found\n .map((param) => param.slice(1));\n\n // Generate a regular expression to match the path\n let regexpPath = \"^\" + realpath.replace(/:(\\w+)/gi, \"([^\\\\/\\\\s]+)\") + \"$\";\n\n router.paths.push({\n method,\n path: realpath,\n middlewares: flat(middlewares),\n params,\n regexp: new RegExp(regexpPath, \"i\")\n });\n};\n\n// Parse a query string into an object\nfunction parseQuery(queryParts?: string): Record<string, string> {\n // Split the query string into an array of name-value pairs\n let parts = queryParts ? queryParts.split(\"&\") : [];\n let query: Record<string, string> = {};\n\n // Iterate over the name-value pairs and add them to the query object\n for (let nameValue of parts) {\n let [name, value] = nameValue.split(\"=\", 2);\n query[name] = value || \"\";\n }\n\n return query;\n}\n\n// Search for middlewares that match a given path\nfunction searchMiddlewares(router: RouterInterface, path: string): Middlewares {\n let middlewares: Middlewares = [];\n let params: Record<string, any> = {};\n let matches = [];\n\n // Search for middlewares\n for (let item of router.paths) {\n let match = item.regexp.exec(path);\n\n // If we found middlewares\n if (Array.isArray(match)) {\n middlewares.push(...item.middlewares);\n match.shift();\n\n // Parse params\n for (let [index, key] of item.params.entries()) {\n params[key] = match[index];\n }\n\n // Add remaining matches to the array\n matches.push(...match);\n\n if (item.method === \"add\") {\n router.path = getPathWithoutPrefix(item.path, router.pathPrefix);\n break;\n }\n }\n }\n\n router.params = params;\n router.matches = matches;\n\n return middlewares;\n}\n\nasync function searchComponent(\n router: RouterInterface,\n middlewares: Middlewares\n): Promise<Component | VnodeComponentInterface | false | void> {\n // Define request object with default values\n const request: Request = {\n params: router.params,\n query: router.query,\n url: router.url,\n path: router.path,\n matches: router.matches,\n redirect: (path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface) => {\n router.go(path, parentComponent);\n // Return false to stop the middleware chain\n return false;\n }\n };\n\n // Initialize response variable\n let response;\n\n // Iterate through middlewares\n for (const middleware of middlewares) {\n // Invoke middleware and update response\n response = await middleware(request, response);\n\n // Return response if it's a valid component\n if (response !== undefined && (isComponent(response) || isVnodeComponent(response))) {\n return response;\n }\n\n // Return false if response is explicitly false to stop the middleware chain\n if (response === false) {\n return false;\n }\n }\n}\n\nexport class Router implements RouterInterface {\n paths: Path[] = [];\n container: Element | string | null = null;\n query: Record<string, string | number> = {};\n options: Record<string, any> = {};\n url: string = \"\";\n path: string = \"\";\n params: Record<string, string | number | any> = {};\n matches: string[] = [];\n pathPrefix: string = \"\";\n\n constructor(pathPrefix: string = \"\") {\n this.pathPrefix = pathPrefix;\n }\n\n add(path: string, ...middlewares: Middlewares): Router {\n let pathWithoutLastSlash = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);\n addPath({ router: this, method: \"add\", path: pathWithoutLastSlash, middlewares });\n return this;\n }\n\n use(...middlewares: Middlewares | Router[] | string[]): Router {\n let path = getPathWithoutLastSlash(\n `${this.pathPrefix}${typeof middlewares[0] === \"string\" ? middlewares.shift() : \"/\"}`\n );\n\n for (const item of middlewares) {\n if (item instanceof Router) {\n const subrouter = item as Router;\n for (const subpath of subrouter.paths) {\n addPath({\n router: this,\n method: subpath.method,\n path: `${path}${subpath.path}`.replace(/^\\/\\//, \"/\"),\n middlewares: subpath.middlewares\n });\n }\n continue;\n }\n\n if (typeof item === \"function\") {\n addPath({ router: this, method: \"use\", path: `${path}.*`, middlewares: [item as Middleware] });\n }\n }\n\n return this;\n }\n\n routes(): string[] {\n return this.paths.filter((path) => path.method === \"add\").map((path) => path.path);\n }\n\n async go(\n path: string,\n parentComponent?: Component | POJOComponent | VnodeComponentInterface,\n preventPushState = false\n ): Promise<string | void> {\n if (!path) {\n throw new Error(\"router.url.required\");\n }\n\n let constructedPath = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);\n const parts = constructedPath.split(\"?\", 2);\n this.url = constructedPath;\n this.query = parseQuery(parts[1]);\n\n const middlewares = searchMiddlewares(this as RouterInterface, parts[0].replace(/(.+)\\/$/, \"$1\").split(\"#\")[0]);\n let component = await searchComponent(this as RouterInterface, middlewares);\n\n if (component === false) {\n return;\n }\n\n if (!component) {\n throw new Error(`The url ${constructedPath} requested wasn't found`);\n }\n\n if (isComponent(parentComponent) || isVnodeComponent(parentComponent)) {\n const childComponent = isVnodeComponent(component) ? component : v(component as Component, {});\n if (isVnodeComponent(parentComponent)) {\n parentComponent.children.push(childComponent);\n component = parentComponent;\n } else {\n component = v(parentComponent, {}, childComponent) as VnodeComponentInterface;\n }\n }\n\n if (!isNodeJs && !preventPushState) {\n window.history.pushState(null, \"\", constructedPath);\n }\n\n if (this.container) {\n return mount(this.container, component);\n }\n }\n\n getOnClickHandler(url: string) {\n return (e: MouseEvent) => {\n if (typeof url === \"string\" && url.length > 0) {\n this.go(url);\n }\n e.preventDefault();\n };\n }\n}\n\nlet localRedirect: RedirectFunction;\n\nexport function redirect(\n url: string,\n parentComponent?: Component | POJOComponent | VnodeComponentInterface,\n preventPushState = false\n): Promise<string | void> {\n if (!localRedirect) {\n throw new Error(\"router.redirect.not.found\");\n }\n return localRedirect(url, parentComponent, preventPushState);\n}\n\nexport function mountRouter(elementContainer: string | any, router: Router): void {\n router.container = elementContainer;\n localRedirect = router.go.bind(router);\n\n if (!isNodeJs) {\n function onPopStateGoToRoute(): void {\n let pathWithoutPrefix = getPathWithoutPrefix(document.location.pathname, router.pathPrefix);\n (router as unknown as Router).go(pathWithoutPrefix, undefined, true);\n }\n window.addEventListener(\"popstate\", onPopStateGoToRoute, false);\n onPopStateGoToRoute();\n }\n\n directive(\"route\", (url: string, vnode: VnodeWithDom, oldnode?: VnodeWithDom): void => {\n setAttribute(\"href\", url, vnode, oldnode);\n setAttribute(\"onclick\", router.getOnClickHandler(url), vnode, oldnode);\n });\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,sBAYO;AA6DP,SAAS,KAAK,OAAY;AACxB,SAAO,MAAM,QAAQ,KAAK,IAAI,MAAM,KAAK,QAAQ,IAAI,CAAC,KAAK;AAC7D;AAEA,SAAS,qBAAqB,MAAc,QAAgB;AAC1D,SAAO,wBAAwB,KAAK,QAAQ,IAAI,OAAO,IAAI,MAAM,EAAE,GAAG,EAAE,CAAC;AAC3E;AAEA,SAAS,wBAAwB,MAAc;AAC7C,MAAI,uBAAuB,KAAK,QAAQ,OAAO,EAAE;AACjD,MAAI,yBAAyB,IAAI;AAC/B,2BAAuB;AAAA,EACzB;AACA,SAAO;AACT;AAEA,IAAM,UAAU,CAAC;AAAA,EACf;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAKY;AACV,MAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,MAAM,QAAQ,WAAW,KAAK,YAAY,WAAW,GAAG;AAC/E,UAAM,IAAI,MAAM,wBAAwB,MAAM,IAAI,IAAI,IAAI,WAAW,EAAE;AAAA,EACzE;AAGA,MAAI,WAAW,KAAK,QAAQ,cAAc,IAAI;AAG9C,MAAI,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|
|
|
192
192
|
router.container = elementContainer;
|
|
193
193
|
localRedirect = router.go.bind(router);
|
|
194
194
|
if (!isNodeJs) {
|
|
195
|
-
let
|
|
195
|
+
let onPopStateGoToRoute2 = function() {
|
|
196
196
|
let pathWithoutPrefix = getPathWithoutPrefix(document.location.pathname, router.pathPrefix);
|
|
197
197
|
router.go(pathWithoutPrefix, void 0, true);
|
|
198
198
|
};
|
|
199
|
-
|
|
200
|
-
|
|
199
|
+
var onPopStateGoToRoute = onPopStateGoToRoute2;
|
|
200
|
+
window.addEventListener("popstate", onPopStateGoToRoute2, false);
|
|
201
|
+
onPopStateGoToRoute2();
|
|
201
202
|
}
|
|
202
203
|
directive("route", (url, vnode, oldnode) => {
|
|
203
204
|
setAttribute("href", url, vnode, oldnode);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../lib/router/index.ts"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable no-use-before-define */\nimport {\n Component,\n POJOComponent,\n VnodeComponentInterface,\n VnodeWithDom,\n directive,\n isComponent,\n isNodeJs,\n isVnodeComponent,\n mount,\n setAttribute,\n v\n} from \"valyrian.js\";\n\ninterface Request {\n params: Record<string, any>;\n query: Record<string, any>;\n url: string;\n path: string;\n matches: string[];\n // eslint-disable-next-line no-unused-vars\n redirect: (path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface) => false;\n}\n\ninterface Middleware {\n // eslint-disable-next-line no-unused-vars\n (req: Request, res?: any):\n | Promise<any | Component | POJOComponent | VnodeComponentInterface>\n | any\n | Component\n | POJOComponent\n | VnodeComponentInterface;\n}\n\ninterface Middlewares extends Array<Middleware> {}\n\ninterface Path {\n method: string;\n path: string;\n middlewares: Middlewares;\n params: string[];\n regexp: RegExp;\n}\n\ninterface RouterInterface {\n paths: Path[];\n container: Element | string | null;\n query: Record<string, string | number>;\n options: Record<string, any>;\n url: string;\n path: string;\n params: Record<string, string | number | any>;\n matches: string[];\n pathPrefix: string;\n // eslint-disable-next-line no-unused-vars\n add(method: string, ...args: Middlewares): Router;\n // eslint-disable-next-line no-unused-vars\n use(...args: (string | Middleware | Router)[]): Router;\n\n routes(): string[];\n // eslint-disable-next-line no-unused-vars\n go(path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface): Promise<string | void>;\n}\n\ninterface RedirectFunction {\n // eslint-disable-next-line no-unused-vars\n (url: string, parentComponent?: Component, preventPushState?: boolean): string | void;\n}\n\nfunction flat(array: any) {\n return Array.isArray(array) ? array.flat(Infinity) : [array];\n}\n\nfunction getPathWithoutPrefix(path: string, prefix: string) {\n return getPathWithoutLastSlash(path.replace(new RegExp(`^${prefix}`), \"\"));\n}\n\nfunction getPathWithoutLastSlash(path: string) {\n let pathWithoutLastSlash = path.replace(/\\/$/, \"\");\n if (pathWithoutLastSlash === \"\") {\n pathWithoutLastSlash = \"/\";\n }\n return pathWithoutLastSlash;\n}\n\nconst addPath = ({\n router,\n method,\n path,\n middlewares\n}: {\n router: Router;\n method: string;\n path: string;\n middlewares: Middleware[];\n}): void => {\n if (!method || !path || !Array.isArray(middlewares) || middlewares.length === 0) {\n throw new Error(`Invalid route input: ${method} ${path} ${middlewares}`);\n }\n\n // Trim trailing slashes from the path\n let realpath = path.replace(/(\\S)(\\/+)$/, \"$1\");\n\n // Find the express-like params in the path\n let params = (realpath.match(/:(\\w+)?/gi) || [])\n // Set the names of the params found\n .map((param) => param.slice(1));\n\n // Generate a regular expression to match the path\n let regexpPath = \"^\" + realpath.replace(/:(\\w+)/gi, \"([^\\\\/\\\\s]+)\") + \"$\";\n\n router.paths.push({\n method,\n path: realpath,\n middlewares: flat(middlewares),\n params,\n regexp: new RegExp(regexpPath, \"i\")\n });\n};\n\n// Parse a query string into an object\nfunction parseQuery(queryParts?: string): Record<string, string> {\n // Split the query string into an array of name-value pairs\n let parts = queryParts ? queryParts.split(\"&\") : [];\n let query: Record<string, string> = {};\n\n // Iterate over the name-value pairs and add them to the query object\n for (let nameValue of parts) {\n let [name, value] = nameValue.split(\"=\", 2);\n query[name] = value || \"\";\n }\n\n return query;\n}\n\n// Search for middlewares that match a given path\nfunction searchMiddlewares(router: RouterInterface, path: string): Middlewares {\n let middlewares: Middlewares = [];\n let params: Record<string, any> = {};\n let matches = [];\n\n // Search for middlewares\n for (let item of router.paths) {\n let match = item.regexp.exec(path);\n\n // If we found middlewares\n if (Array.isArray(match)) {\n middlewares.push(...item.middlewares);\n match.shift();\n\n // Parse params\n for (let [index, key] of item.params.entries()) {\n params[key] = match[index];\n }\n\n // Add remaining matches to the array\n matches.push(...match);\n\n if (item.method === \"add\") {\n router.path = getPathWithoutPrefix(item.path, router.pathPrefix);\n break;\n }\n }\n }\n\n router.params = params;\n router.matches = matches;\n\n return middlewares;\n}\n\nasync function searchComponent(\n router: RouterInterface,\n middlewares: Middlewares\n): Promise<Component | VnodeComponentInterface | false | void> {\n // Define request object with default values\n const request: Request = {\n params: router.params,\n query: router.query,\n url: router.url,\n path: router.path,\n matches: router.matches,\n redirect: (path: string, parentComponent?: Component) => {\n router.go(path, parentComponent);\n // Return false to stop the middleware chain\n return false;\n }\n };\n\n // Initialize response variable\n let response;\n\n // Iterate through middlewares\n for (const middleware of middlewares) {\n // Invoke middleware and update response\n response = await middleware(request, response);\n\n // Return response if it's a valid component\n if (response !== undefined && (isComponent(response) || isVnodeComponent(response))) {\n return response;\n }\n\n // Return false if response is explicitly false to stop the middleware chain\n if (response === false) {\n return false;\n }\n }\n}\n\nexport class Router implements RouterInterface {\n paths: Path[] = [];\n container: Element | string | null = null;\n query: Record<string, string | number> = {};\n options: Record<string, any> = {};\n url: string = \"\";\n path: string = \"\";\n params: Record<string, string | number | any> = {};\n matches: string[] = [];\n pathPrefix: string = \"\";\n\n constructor(pathPrefix: string = \"\") {\n this.pathPrefix = pathPrefix;\n }\n\n add(path: string, ...middlewares: Middlewares): Router {\n let pathWithoutLastSlash = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);\n addPath({ router: this, method: \"add\", path: pathWithoutLastSlash, middlewares });\n return this;\n }\n\n use(...middlewares: Middlewares | Router[] | string[]): Router {\n let path = getPathWithoutLastSlash(\n `${this.pathPrefix}${typeof middlewares[0] === \"string\" ? middlewares.shift() : \"/\"}`\n );\n\n for (const item of middlewares) {\n if (item instanceof Router) {\n const subrouter = item as Router;\n for (const subpath of subrouter.paths) {\n addPath({\n router: this,\n method: subpath.method,\n path: `${path}${subpath.path}`.replace(/^\\/\\//, \"/\"),\n middlewares: subpath.middlewares\n });\n }\n continue;\n }\n\n if (typeof item === \"function\") {\n addPath({ router: this, method: \"use\", path: `${path}.*`, middlewares: [item as Middleware] });\n }\n }\n\n return this;\n }\n\n routes(): string[] {\n return this.paths.filter((path) => path.method === \"add\").map((path) => path.path);\n }\n\n async go(path: string, parentComponent?: Component, preventPushState = false): Promise<string | void> {\n if (!path) {\n throw new Error(\"router.url.required\");\n }\n\n let constructedPath = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);\n const parts = constructedPath.split(\"?\", 2);\n this.url = constructedPath;\n this.query = parseQuery(parts[1]);\n\n const middlewares = searchMiddlewares(this as RouterInterface, parts[0].replace(/(.+)\\/$/, \"$1\").split(\"#\")[0]);\n let component = await searchComponent(this as RouterInterface, middlewares);\n\n if (component === false) {\n return;\n }\n\n if (!component) {\n throw new Error(`The url ${constructedPath} requested wasn't found`);\n }\n\n if (isComponent(parentComponent) || isVnodeComponent(parentComponent)) {\n const childComponent = isVnodeComponent(component) ? component : v(component as Component, {});\n if (isVnodeComponent(parentComponent)) {\n parentComponent.children.push(childComponent);\n component = parentComponent;\n } else {\n component = v(parentComponent, {}, childComponent) as VnodeComponentInterface;\n }\n }\n\n if (!isNodeJs && !preventPushState) {\n window.history.pushState(null, \"\", constructedPath);\n }\n\n if (this.container) {\n return mount(this.container, component);\n }\n }\n\n getOnClickHandler(url: string) {\n return (e: MouseEvent) => {\n if (typeof url === \"string\" && url.length > 0) {\n this.go(url);\n }\n e.preventDefault();\n };\n }\n}\n\nlet localRedirect: RedirectFunction;\n\nexport function redirect(url: string, parentComponent?: Component, preventPushState = false): string | void {\n if (!localRedirect) {\n throw new Error(\"router.redirect.not.found\");\n }\n return localRedirect(url, parentComponent, preventPushState);\n}\n\nexport function mountRouter(elementContainer: string | any, router: Router): void {\n router.container = elementContainer;\n localRedirect = router.go.bind(router);\n\n if (!isNodeJs) {\n function onPopStateGoToRoute(): void {\n let pathWithoutPrefix = getPathWithoutPrefix(document.location.pathname, router.pathPrefix);\n (router as unknown as Router).go(pathWithoutPrefix, undefined, true);\n }\n window.addEventListener(\"popstate\", onPopStateGoToRoute, false);\n onPopStateGoToRoute();\n }\n\n directive(\"route\", (url: string, vnode: VnodeWithDom, oldnode?: VnodeWithDom): void => {\n setAttribute(\"href\", url, vnode, oldnode);\n setAttribute(\"onclick\", router.getOnClickHandler(url), vnode, oldnode);\n });\n}\n"],
|
|
5
|
-
"mappings": ";AACA;AAAA,EAKE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;
|
|
6
|
-
"names": []
|
|
4
|
+
"sourcesContent": ["/* eslint-disable no-use-before-define */\nimport {\n Component,\n POJOComponent,\n VnodeComponentInterface,\n VnodeWithDom,\n directive,\n isComponent,\n isNodeJs,\n isVnodeComponent,\n mount,\n setAttribute,\n v\n} from \"valyrian.js\";\n\ninterface Request {\n params: Record<string, any>;\n query: Record<string, any>;\n url: string;\n path: string;\n matches: string[];\n // eslint-disable-next-line no-unused-vars\n redirect: (path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface) => false;\n}\n\ninterface Middleware {\n // eslint-disable-next-line no-unused-vars\n (req: Request, res?: any):\n | Promise<any | Component | POJOComponent | VnodeComponentInterface>\n | any\n | Component\n | POJOComponent\n | VnodeComponentInterface;\n}\n\ninterface Middlewares extends Array<Middleware> {}\n\ninterface Path {\n method: string;\n path: string;\n middlewares: Middlewares;\n params: string[];\n regexp: RegExp;\n}\n\ninterface RouterInterface {\n paths: Path[];\n container: Element | string | null;\n query: Record<string, string | number>;\n options: Record<string, any>;\n url: string;\n path: string;\n params: Record<string, string | number | any>;\n matches: string[];\n pathPrefix: string;\n // eslint-disable-next-line no-unused-vars\n add(method: string, ...args: Middlewares): Router;\n // eslint-disable-next-line no-unused-vars\n use(...args: (string | Middleware | Router)[]): Router;\n\n routes(): string[];\n // eslint-disable-next-line no-unused-vars\n go(path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface): Promise<string | void>;\n}\n\ninterface RedirectFunction {\n // eslint-disable-next-line no-unused-vars\n (\n path: string,\n parentComponent?: Component | POJOComponent | VnodeComponentInterface,\n preventPushState?: boolean\n ): Promise<string | void>;\n}\n\nfunction flat(array: any) {\n return Array.isArray(array) ? array.flat(Infinity) : [array];\n}\n\nfunction getPathWithoutPrefix(path: string, prefix: string) {\n return getPathWithoutLastSlash(path.replace(new RegExp(`^${prefix}`), \"\"));\n}\n\nfunction getPathWithoutLastSlash(path: string) {\n let pathWithoutLastSlash = path.replace(/\\/$/, \"\");\n if (pathWithoutLastSlash === \"\") {\n pathWithoutLastSlash = \"/\";\n }\n return pathWithoutLastSlash;\n}\n\nconst addPath = ({\n router,\n method,\n path,\n middlewares\n}: {\n router: Router;\n method: string;\n path: string;\n middlewares: Middleware[];\n}): void => {\n if (!method || !path || !Array.isArray(middlewares) || middlewares.length === 0) {\n throw new Error(`Invalid route input: ${method} ${path} ${middlewares}`);\n }\n\n // Trim trailing slashes from the path\n let realpath = path.replace(/(\\S)(\\/+)$/, \"$1\");\n\n // Find the express-like params in the path\n let params = (realpath.match(/:(\\w+)?/gi) || [])\n // Set the names of the params found\n .map((param) => param.slice(1));\n\n // Generate a regular expression to match the path\n let regexpPath = \"^\" + realpath.replace(/:(\\w+)/gi, \"([^\\\\/\\\\s]+)\") + \"$\";\n\n router.paths.push({\n method,\n path: realpath,\n middlewares: flat(middlewares),\n params,\n regexp: new RegExp(regexpPath, \"i\")\n });\n};\n\n// Parse a query string into an object\nfunction parseQuery(queryParts?: string): Record<string, string> {\n // Split the query string into an array of name-value pairs\n let parts = queryParts ? queryParts.split(\"&\") : [];\n let query: Record<string, string> = {};\n\n // Iterate over the name-value pairs and add them to the query object\n for (let nameValue of parts) {\n let [name, value] = nameValue.split(\"=\", 2);\n query[name] = value || \"\";\n }\n\n return query;\n}\n\n// Search for middlewares that match a given path\nfunction searchMiddlewares(router: RouterInterface, path: string): Middlewares {\n let middlewares: Middlewares = [];\n let params: Record<string, any> = {};\n let matches = [];\n\n // Search for middlewares\n for (let item of router.paths) {\n let match = item.regexp.exec(path);\n\n // If we found middlewares\n if (Array.isArray(match)) {\n middlewares.push(...item.middlewares);\n match.shift();\n\n // Parse params\n for (let [index, key] of item.params.entries()) {\n params[key] = match[index];\n }\n\n // Add remaining matches to the array\n matches.push(...match);\n\n if (item.method === \"add\") {\n router.path = getPathWithoutPrefix(item.path, router.pathPrefix);\n break;\n }\n }\n }\n\n router.params = params;\n router.matches = matches;\n\n return middlewares;\n}\n\nasync function searchComponent(\n router: RouterInterface,\n middlewares: Middlewares\n): Promise<Component | VnodeComponentInterface | false | void> {\n // Define request object with default values\n const request: Request = {\n params: router.params,\n query: router.query,\n url: router.url,\n path: router.path,\n matches: router.matches,\n redirect: (path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface) => {\n router.go(path, parentComponent);\n // Return false to stop the middleware chain\n return false;\n }\n };\n\n // Initialize response variable\n let response;\n\n // Iterate through middlewares\n for (const middleware of middlewares) {\n // Invoke middleware and update response\n response = await middleware(request, response);\n\n // Return response if it's a valid component\n if (response !== undefined && (isComponent(response) || isVnodeComponent(response))) {\n return response;\n }\n\n // Return false if response is explicitly false to stop the middleware chain\n if (response === false) {\n return false;\n }\n }\n}\n\nexport class Router implements RouterInterface {\n paths: Path[] = [];\n container: Element | string | null = null;\n query: Record<string, string | number> = {};\n options: Record<string, any> = {};\n url: string = \"\";\n path: string = \"\";\n params: Record<string, string | number | any> = {};\n matches: string[] = [];\n pathPrefix: string = \"\";\n\n constructor(pathPrefix: string = \"\") {\n this.pathPrefix = pathPrefix;\n }\n\n add(path: string, ...middlewares: Middlewares): Router {\n let pathWithoutLastSlash = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);\n addPath({ router: this, method: \"add\", path: pathWithoutLastSlash, middlewares });\n return this;\n }\n\n use(...middlewares: Middlewares | Router[] | string[]): Router {\n let path = getPathWithoutLastSlash(\n `${this.pathPrefix}${typeof middlewares[0] === \"string\" ? middlewares.shift() : \"/\"}`\n );\n\n for (const item of middlewares) {\n if (item instanceof Router) {\n const subrouter = item as Router;\n for (const subpath of subrouter.paths) {\n addPath({\n router: this,\n method: subpath.method,\n path: `${path}${subpath.path}`.replace(/^\\/\\//, \"/\"),\n middlewares: subpath.middlewares\n });\n }\n continue;\n }\n\n if (typeof item === \"function\") {\n addPath({ router: this, method: \"use\", path: `${path}.*`, middlewares: [item as Middleware] });\n }\n }\n\n return this;\n }\n\n routes(): string[] {\n return this.paths.filter((path) => path.method === \"add\").map((path) => path.path);\n }\n\n async go(\n path: string,\n parentComponent?: Component | POJOComponent | VnodeComponentInterface,\n preventPushState = false\n ): Promise<string | void> {\n if (!path) {\n throw new Error(\"router.url.required\");\n }\n\n let constructedPath = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);\n const parts = constructedPath.split(\"?\", 2);\n this.url = constructedPath;\n this.query = parseQuery(parts[1]);\n\n const middlewares = searchMiddlewares(this as RouterInterface, parts[0].replace(/(.+)\\/$/, \"$1\").split(\"#\")[0]);\n let component = await searchComponent(this as RouterInterface, middlewares);\n\n if (component === false) {\n return;\n }\n\n if (!component) {\n throw new Error(`The url ${constructedPath} requested wasn't found`);\n }\n\n if (isComponent(parentComponent) || isVnodeComponent(parentComponent)) {\n const childComponent = isVnodeComponent(component) ? component : v(component as Component, {});\n if (isVnodeComponent(parentComponent)) {\n parentComponent.children.push(childComponent);\n component = parentComponent;\n } else {\n component = v(parentComponent, {}, childComponent) as VnodeComponentInterface;\n }\n }\n\n if (!isNodeJs && !preventPushState) {\n window.history.pushState(null, \"\", constructedPath);\n }\n\n if (this.container) {\n return mount(this.container, component);\n }\n }\n\n getOnClickHandler(url: string) {\n return (e: MouseEvent) => {\n if (typeof url === \"string\" && url.length > 0) {\n this.go(url);\n }\n e.preventDefault();\n };\n }\n}\n\nlet localRedirect: RedirectFunction;\n\nexport function redirect(\n url: string,\n parentComponent?: Component | POJOComponent | VnodeComponentInterface,\n preventPushState = false\n): Promise<string | void> {\n if (!localRedirect) {\n throw new Error(\"router.redirect.not.found\");\n }\n return localRedirect(url, parentComponent, preventPushState);\n}\n\nexport function mountRouter(elementContainer: string | any, router: Router): void {\n router.container = elementContainer;\n localRedirect = router.go.bind(router);\n\n if (!isNodeJs) {\n function onPopStateGoToRoute(): void {\n let pathWithoutPrefix = getPathWithoutPrefix(document.location.pathname, router.pathPrefix);\n (router as unknown as Router).go(pathWithoutPrefix, undefined, true);\n }\n window.addEventListener(\"popstate\", onPopStateGoToRoute, false);\n onPopStateGoToRoute();\n }\n\n directive(\"route\", (url: string, vnode: VnodeWithDom, oldnode?: VnodeWithDom): void => {\n setAttribute(\"href\", url, vnode, oldnode);\n setAttribute(\"onclick\", router.getOnClickHandler(url), vnode, oldnode);\n });\n}\n"],
|
|
5
|
+
"mappings": ";AACA;AAAA,EAKE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AA6DP,SAAS,KAAK,OAAY;AACxB,SAAO,MAAM,QAAQ,KAAK,IAAI,MAAM,KAAK,QAAQ,IAAI,CAAC,KAAK;AAC7D;AAEA,SAAS,qBAAqB,MAAc,QAAgB;AAC1D,SAAO,wBAAwB,KAAK,QAAQ,IAAI,OAAO,IAAI,MAAM,EAAE,GAAG,EAAE,CAAC;AAC3E;AAEA,SAAS,wBAAwB,MAAc;AAC7C,MAAI,uBAAuB,KAAK,QAAQ,OAAO,EAAE;AACjD,MAAI,yBAAyB,IA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AYA,sBAAqB,KAAK;AAC9D,IAAAA,qBAAoB;AAAA,EACtB;AAEA,YAAU,SAAS,CAAC,KAAa,OAAqB,YAAiC;AACrF,iBAAa,QAAQ,KAAK,OAAO,OAAO;AACxC,iBAAa,WAAW,OAAO,kBAAkB,GAAG,GAAG,OAAO,OAAO;AAAA,EACvE,CAAC;AACH;",
|
|
6
|
+
"names": ["onPopStateGoToRoute"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/signal/index.ts"],"names":[],"mappings":"AAEA,UAAU,eAAe;IACvB,IAAI,GAAG,CAAC;CACT;AAED,UAAU,eAAe;IACvB,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC;CACpB;AAED,UAAU,kBAAkB;IAC1B,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAAC;CAC5B;AAED,UAAU,sBAAuB,SAAQ,KAAK,CAAC,QAAQ,CAAC;CAAG;AAE3D,MAAM,WAAW,eAAgB,SAAQ,KAAK,CAAC,GAAG,CAAC;IACjD,CAAC,EAAE,eAAe,CAAC;IACnB,CAAC,EAAE,eAAe,CAAC;IACnB,CAAC,EAAE,kBAAkB,CAAC;IACtB,CAAC,EAAE,sBAAsB,CAAC;CAC3B;AAGD,wBAAgB,MAAM,CAAC,YAAY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/signal/index.ts"],"names":[],"mappings":"AAEA,UAAU,eAAe;IACvB,IAAI,GAAG,CAAC;CACT;AAED,UAAU,eAAe;IACvB,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC;CACpB;AAED,UAAU,kBAAkB;IAC1B,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAAC;CAC5B;AAED,UAAU,sBAAuB,SAAQ,KAAK,CAAC,QAAQ,CAAC;CAAG;AAE3D,MAAM,WAAW,eAAgB,SAAQ,KAAK,CAAC,GAAG,CAAC;IACjD,CAAC,EAAE,eAAe,CAAC;IACnB,CAAC,EAAE,eAAe,CAAC;IACnB,CAAC,EAAE,kBAAkB,CAAC;IACtB,CAAC,EAAE,sBAAsB,CAAC;CAC3B;AAGD,wBAAgB,MAAM,CAAC,YAAY,EAAE,GAAG,GAAG,eAAe,CAwIzD"}
|
package/dist/signal/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
@@ -25,7 +26,7 @@ module.exports = __toCommonJS(signal_exports);
|
|
|
25
26
|
var import_valyrian = require("valyrian.js");
|
|
26
27
|
function Signal(initialValue) {
|
|
27
28
|
const { vnode, component } = { ...import_valyrian.current };
|
|
28
|
-
if (vnode) {
|
|
29
|
+
if (vnode && component) {
|
|
29
30
|
if (!vnode.components) {
|
|
30
31
|
vnode.components = [];
|
|
31
32
|
}
|
|
@@ -87,7 +88,7 @@ function Signal(initialValue) {
|
|
|
87
88
|
}
|
|
88
89
|
};
|
|
89
90
|
let signal = [get, set, subscribe, subscriptions];
|
|
90
|
-
if (vnode) {
|
|
91
|
+
if (vnode && component) {
|
|
91
92
|
component.signals.push(signal);
|
|
92
93
|
}
|
|
93
94
|
return signal;
|
package/dist/signal/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../lib/signal/index.ts"],
|
|
4
|
-
"sourcesContent": ["import { VnodeWithDom, current, onUnmount, updateVnode, v } from \"valyrian.js\";\n\ninterface GetterInterface {\n (): any;\n}\n\ninterface SetterInterface {\n (value: any): void;\n}\n\ninterface SubscribeInterface {\n (callback: Function): void;\n}\n\ninterface SubscriptionsInterface extends Array<Function> {}\n\nexport interface SignalInterface extends Array<any> {\n 0: GetterInterface;\n 1: SetterInterface;\n 2: SubscribeInterface;\n 3: SubscriptionsInterface;\n}\n\n// eslint-disable-next-line sonarjs/cognitive-complexity\nexport function Signal(initialValue): SignalInterface {\n // Create a copy of the current context object\n const { vnode, component } = { ...current };\n\n // Check if the context object has a vnode property\n if (vnode) {\n // Is first call\n if (!vnode.components) {\n // Set the components property to an empty array\n vnode.components = [];\n }\n\n // Check if the components array of the vnode object does not contain the component object\n if (vnode.components.indexOf(component) === -1) {\n // Set the calls property to -1\n vnode.signal_calls = -1;\n // Add the component to the components array\n vnode.components.push(component);\n\n // Check if the component object has a signals property\n if (!component.signals) {\n // Set the signals property of the component object to an empty array\n component.signals = [];\n // Add a function to the cleanup stack that removes the signals property from the component object\n onUnmount(() => Reflect.deleteProperty(component, \"signals\"));\n }\n }\n\n // Assign the signal variable to the signal stored at the index of the vnode object's calls property in the vnode's signals array\n let signal: SignalInterface = component.signals[++vnode.signal_calls];\n\n // If a signal has already been assigned to the signal variable, return it\n if (signal) {\n // Remove all subscriptions because we come from a new render\n signal[3].length = 0;\n\n // Return the signal\n return signal;\n }\n }\n\n // Declare a variable to store the current value of the Signal\n let value = initialValue;\n\n // Create an array to store functions that have subscribed to changes to the Signal's value\n const subscriptions: SubscriptionsInterface = [];\n\n // Define a function that allows other parts of the code to subscribe to changes to the Signal's value\n const subscribe = (callback) => {\n // Add the callback function to the subscriptions array if it is not already in the array\n if (subscriptions.indexOf(callback) === -1) {\n subscriptions.push(callback);\n }\n };\n\n // Set the vnodes to update when the Signal's value changes\n let vnodesToUpdate: Array<VnodeWithDom> = [];\n\n // This is the function that will be called when the Signal's value changes\n const updateVnodes = () => {\n // Create a copy of the vnodesToUpdate array and filter out any duplicate vnodes\n let vnodesToUpdateCopy = vnodesToUpdate.filter((vnode, index, self) => {\n return self.findIndex((v) => v.dom === vnode.dom) === index;\n });\n\n // Loop through the vnodesToUpdate array\n for (let i = 0, l = vnodesToUpdateCopy.length; i < l; i++) {\n const vnode2 = vnodesToUpdateCopy[i];\n // If it does, create a new vnode object based on the original vnode, its children, and its DOM and SVG properties\n let newVnode = v(vnode2.tag, vnode2.props, ...vnode2.initialChildren) as VnodeWithDom;\n newVnode.dom = vnode2.dom; // Set the new vnode object's DOM property to the old vnode object's DOM property\n newVnode.isSVG = vnode2.isSVG; // Set the new vnode object's isSVG property to the old vnode object's isSVG property\n\n // Update the vnode object\n updateVnode(newVnode, vnode2);\n }\n };\n\n // Define a function that returns the current value of the Signal\n function get() {\n // Get the current vnode from the context object\n const { vnode: vnode2 } = current;\n\n // If we have a current vnode, it means that a get function is being called from within a component\n // so we subscribe the vnode to be updated when the Signal's value changes\n if (vnode2 && vnodesToUpdate.indexOf(vnode2) === -1) {\n // We set the initialChildren to a copy of the vnode's children array\n // This is the case when the vnode is a component that has not been rendered yet and we need the initial children\n // because they could have the components that are using the Signal\n if (!vnode2.initialChildren) {\n vnode2.initialChildren = [...vnode2.children];\n }\n\n // Add the vnode to the vnodesToUpdate array\n vnodesToUpdate.push(vnode2);\n\n // Subscribe the updateVnodes function to the Signal\n subscribe(updateVnodes);\n }\n\n // Return the current value of the Signal\n return value;\n }\n\n // Define a function that allows the value of the Signal to be updated and notifies any subscribed functions of the change\n const set = (newValue) => {\n // If we have a current event on going, prevent the default action\n if (current.event) {\n current.event.preventDefault();\n }\n\n // Just return if the new value is the same as the current value\n if (newValue === value) {\n return;\n }\n\n // Update the value of the Signal\n value = newValue;\n\n // Call each subscribed function with the new value of the Signal as an argument\n for (let i = 0, l = subscriptions.length; i < l; i++) {\n subscriptions[i](value);\n }\n };\n\n // Assign the signal variable an array containing the get, set, and subscribe functions\n let signal: SignalInterface = [get, set, subscribe, subscriptions];\n\n // If the context object has a vnode property, add the signal to the vnode's signals array\n // and add the subscriptions array to the vnode's subscriptions array\n if (vnode) {\n component.signals.push(signal);\n }\n\n // Return the signal\n return signal;\n}\n"],
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import { VnodeWithDom, current, onUnmount, updateVnode, v } from \"valyrian.js\";\n\ninterface GetterInterface {\n (): any;\n}\n\ninterface SetterInterface {\n (value: any): void;\n}\n\ninterface SubscribeInterface {\n (callback: Function): void;\n}\n\ninterface SubscriptionsInterface extends Array<Function> {}\n\nexport interface SignalInterface extends Array<any> {\n 0: GetterInterface;\n 1: SetterInterface;\n 2: SubscribeInterface;\n 3: SubscriptionsInterface;\n}\n\n// eslint-disable-next-line sonarjs/cognitive-complexity\nexport function Signal(initialValue: any): SignalInterface {\n // Create a copy of the current context object\n const { vnode, component } = { ...current };\n\n // Check if the context object has a vnode property\n if (vnode && component) {\n // Is first call\n if (!vnode.components) {\n // Set the components property to an empty array\n vnode.components = [];\n }\n\n // Check if the components array of the vnode object does not contain the component object\n if (vnode.components.indexOf(component) === -1) {\n // Set the calls property to -1\n vnode.signal_calls = -1;\n // Add the component to the components array\n vnode.components.push(component);\n\n // Check if the component object has a signals property\n if (!component.signals) {\n // Set the signals property of the component object to an empty array\n component.signals = [];\n // Add a function to the cleanup stack that removes the signals property from the component object\n onUnmount(() => Reflect.deleteProperty(component, \"signals\"));\n }\n }\n\n // Assign the signal variable to the signal stored at the index of the vnode object's calls property in the vnode's signals array\n let signal: SignalInterface = component.signals[++vnode.signal_calls];\n\n // If a signal has already been assigned to the signal variable, return it\n if (signal) {\n // Remove all subscriptions because we come from a new render\n signal[3].length = 0;\n\n // Return the signal\n return signal;\n }\n }\n\n // Declare a variable to store the current value of the Signal\n let value = initialValue;\n\n // Create an array to store functions that have subscribed to changes to the Signal's value\n const subscriptions: SubscriptionsInterface = [];\n\n // Define a function that allows other parts of the code to subscribe to changes to the Signal's value\n const subscribe = (callback: Function) => {\n // Add the callback function to the subscriptions array if it is not already in the array\n if (subscriptions.indexOf(callback) === -1) {\n subscriptions.push(callback);\n }\n };\n\n // Set the vnodes to update when the Signal's value changes\n let vnodesToUpdate: Array<VnodeWithDom> = [];\n\n // This is the function that will be called when the Signal's value changes\n const updateVnodes = () => {\n // Create a copy of the vnodesToUpdate array and filter out any duplicate vnodes\n let vnodesToUpdateCopy = vnodesToUpdate.filter((vnode, index, self) => {\n return self.findIndex((v) => v.dom === vnode.dom) === index;\n });\n\n // Loop through the vnodesToUpdate array\n for (let i = 0, l = vnodesToUpdateCopy.length; i < l; i++) {\n const vnode2 = vnodesToUpdateCopy[i];\n // If it does, create a new vnode object based on the original vnode, its children, and its DOM and SVG properties\n let newVnode = v(vnode2.tag, vnode2.props, ...vnode2.initialChildren) as VnodeWithDom;\n newVnode.dom = vnode2.dom; // Set the new vnode object's DOM property to the old vnode object's DOM property\n newVnode.isSVG = vnode2.isSVG; // Set the new vnode object's isSVG property to the old vnode object's isSVG property\n\n // Update the vnode object\n updateVnode(newVnode, vnode2);\n }\n };\n\n // Define a function that returns the current value of the Signal\n function get() {\n // Get the current vnode from the context object\n const { vnode: vnode2 } = current;\n\n // If we have a current vnode, it means that a get function is being called from within a component\n // so we subscribe the vnode to be updated when the Signal's value changes\n if (vnode2 && vnodesToUpdate.indexOf(vnode2) === -1) {\n // We set the initialChildren to a copy of the vnode's children array\n // This is the case when the vnode is a component that has not been rendered yet and we need the initial children\n // because they could have the components that are using the Signal\n if (!vnode2.initialChildren) {\n vnode2.initialChildren = [...vnode2.children];\n }\n\n // Add the vnode to the vnodesToUpdate array\n vnodesToUpdate.push(vnode2);\n\n // Subscribe the updateVnodes function to the Signal\n subscribe(updateVnodes);\n }\n\n // Return the current value of the Signal\n return value;\n }\n\n // Define a function that allows the value of the Signal to be updated and notifies any subscribed functions of the change\n const set = (newValue: any) => {\n // If we have a current event on going, prevent the default action\n if (current.event) {\n current.event.preventDefault();\n }\n\n // Just return if the new value is the same as the current value\n if (newValue === value) {\n return;\n }\n\n // Update the value of the Signal\n value = newValue;\n\n // Call each subscribed function with the new value of the Signal as an argument\n for (let i = 0, l = subscriptions.length; i < l; i++) {\n subscriptions[i](value);\n }\n };\n\n // Assign the signal variable an array containing the get, set, and subscribe functions\n let signal: SignalInterface = [get, set, subscribe, subscriptions];\n\n // If the context object has a vnode property, add the signal to the vnode's signals array\n // and add the subscriptions array to the vnode's subscriptions array\n if (vnode && component) {\n component.signals.push(signal);\n }\n\n // Return the signal\n return signal;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAiE;AAwB1D,SAAS,OAAO,cAAoC;AAEzD,QAAM,EAAE,OAAO,UAAU,IAAI,EAAE,GAAG,wBAAQ;AAG1C,MAAI,SAAS,WAAW;AAEtB,QAAI,CAAC,MAAM,YAAY;AAErB,YAAM,aAAa,CAAC;AAAA,IACtB;AAGA,QAAI,MAAM,WAAW,QAAQ,SAAS,MAAM,IAAI;AAE9C,YAAM,eAAe;AAErB,YAAM,WAAW,KAAK,SAAS;AAG/B,UAAI,CAAC,UAAU,SAAS;AAEtB,kBAAU,UAAU,CAAC;AAErB,uCAAU,MAAM,QAAQ,eAAe,WAAW,SAAS,CAAC;AAAA,MAC9D;AAAA,IACF;AAGA,QAAIA,UAA0B,UAAU,QAAQ,EAAE,MAAM,YAAY;AAGpE,QAAIA,SAAQ;AAEV,MAAAA,QAAO,CAAC,EAAE,SAAS;AAGnB,aAAOA;AAAA,IACT;AAAA,EACF;AAGA,MAAI,QAAQ;AAGZ,QAAM,gBAAwC,CAAC;AAG/C,QAAM,YAAY,CAAC,aAAuB;AAExC,QAAI,cAAc,QAAQ,QAAQ,MAAM,IAAI;AAC1C,oBAAc,KAAK,QAAQ;AAAA,IAC7B;AAAA,EACF;AAGA,MAAI,iBAAsC,CAAC;AAG3C,QAAM,eAAe,MAAM;AAEzB,QAAI,qBAAqB,eAAe,OAAO,CAACC,QAAO,OAAO,SAAS;AACrE,aAAO,KAAK,UAAU,CAACC,OAAMA,GAAE,QAAQD,OAAM,GAAG,MAAM;AAAA,IACxD,CAAC;AAGD,aAAS,IAAI,GAAG,IAAI,mBAAmB,QAAQ,IAAI,GAAG,KAAK;AACzD,YAAM,SAAS,mBAAmB,CAAC;AAEnC,UAAI,eAAW,mBAAE,OAAO,KAAK,OAAO,OAAO,GAAG,OAAO,eAAe;AACpE,eAAS,MAAM,OAAO;AACtB,eAAS,QAAQ,OAAO;AAGxB,uCAAY,UAAU,MAAM;AAAA,IAC9B;AAAA,EACF;AAGA,WAAS,MAAM;AAEb,UAAM,EAAE,OAAO,OAAO,IAAI;AAI1B,QAAI,UAAU,eAAe,QAAQ,MAAM,MAAM,IAAI;AAInD,UAAI,CAAC,OAAO,iBAAiB;AAC3B,eAAO,kBAAkB,CAAC,GAAG,OAAO,QAAQ;AAAA,MAC9C;AAGA,qBAAe,KAAK,MAAM;AAG1B,gBAAU,YAAY;AAAA,IACxB;AAGA,WAAO;AAAA,EACT;AAGA,QAAM,MAAM,CAAC,aAAkB;AAE7B,QAAI,wBAAQ,OAAO;AACjB,8BAAQ,MAAM,eAAe;AAAA,IAC/B;AAGA,QAAI,aAAa,OAAO;AACtB;AAAA,IACF;AAGA,YAAQ;AAGR,aAAS,IAAI,GAAG,IAAI,cAAc,QAAQ,IAAI,GAAG,KAAK;AACpD,oBAAc,CAAC,EAAE,KAAK;AAAA,IACxB;AAAA,EACF;AAGA,MAAI,SAA0B,CAAC,KAAK,KAAK,WAAW,aAAa;AAIjE,MAAI,SAAS,WAAW;AACtB,cAAU,QAAQ,KAAK,MAAM;AAAA,EAC/B;AAGA,SAAO;AACT;",
|
|
6
6
|
"names": ["signal", "vnode", "v"]
|
|
7
7
|
}
|
package/dist/signal/index.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { current, onUnmount, updateVnode, v } from "valyrian.js";
|
|
3
3
|
function Signal(initialValue) {
|
|
4
4
|
const { vnode, component } = { ...current };
|
|
5
|
-
if (vnode) {
|
|
5
|
+
if (vnode && component) {
|
|
6
6
|
if (!vnode.components) {
|
|
7
7
|
vnode.components = [];
|
|
8
8
|
}
|
|
@@ -64,7 +64,7 @@ function Signal(initialValue) {
|
|
|
64
64
|
}
|
|
65
65
|
};
|
|
66
66
|
let signal = [get, set, subscribe, subscriptions];
|
|
67
|
-
if (vnode) {
|
|
67
|
+
if (vnode && component) {
|
|
68
68
|
component.signals.push(signal);
|
|
69
69
|
}
|
|
70
70
|
return signal;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../lib/signal/index.ts"],
|
|
4
|
-
"sourcesContent": ["import { VnodeWithDom, current, onUnmount, updateVnode, v } from \"valyrian.js\";\n\ninterface GetterInterface {\n (): any;\n}\n\ninterface SetterInterface {\n (value: any): void;\n}\n\ninterface SubscribeInterface {\n (callback: Function): void;\n}\n\ninterface SubscriptionsInterface extends Array<Function> {}\n\nexport interface SignalInterface extends Array<any> {\n 0: GetterInterface;\n 1: SetterInterface;\n 2: SubscribeInterface;\n 3: SubscriptionsInterface;\n}\n\n// eslint-disable-next-line sonarjs/cognitive-complexity\nexport function Signal(initialValue): SignalInterface {\n // Create a copy of the current context object\n const { vnode, component } = { ...current };\n\n // Check if the context object has a vnode property\n if (vnode) {\n // Is first call\n if (!vnode.components) {\n // Set the components property to an empty array\n vnode.components = [];\n }\n\n // Check if the components array of the vnode object does not contain the component object\n if (vnode.components.indexOf(component) === -1) {\n // Set the calls property to -1\n vnode.signal_calls = -1;\n // Add the component to the components array\n vnode.components.push(component);\n\n // Check if the component object has a signals property\n if (!component.signals) {\n // Set the signals property of the component object to an empty array\n component.signals = [];\n // Add a function to the cleanup stack that removes the signals property from the component object\n onUnmount(() => Reflect.deleteProperty(component, \"signals\"));\n }\n }\n\n // Assign the signal variable to the signal stored at the index of the vnode object's calls property in the vnode's signals array\n let signal: SignalInterface = component.signals[++vnode.signal_calls];\n\n // If a signal has already been assigned to the signal variable, return it\n if (signal) {\n // Remove all subscriptions because we come from a new render\n signal[3].length = 0;\n\n // Return the signal\n return signal;\n }\n }\n\n // Declare a variable to store the current value of the Signal\n let value = initialValue;\n\n // Create an array to store functions that have subscribed to changes to the Signal's value\n const subscriptions: SubscriptionsInterface = [];\n\n // Define a function that allows other parts of the code to subscribe to changes to the Signal's value\n const subscribe = (callback) => {\n // Add the callback function to the subscriptions array if it is not already in the array\n if (subscriptions.indexOf(callback) === -1) {\n subscriptions.push(callback);\n }\n };\n\n // Set the vnodes to update when the Signal's value changes\n let vnodesToUpdate: Array<VnodeWithDom> = [];\n\n // This is the function that will be called when the Signal's value changes\n const updateVnodes = () => {\n // Create a copy of the vnodesToUpdate array and filter out any duplicate vnodes\n let vnodesToUpdateCopy = vnodesToUpdate.filter((vnode, index, self) => {\n return self.findIndex((v) => v.dom === vnode.dom) === index;\n });\n\n // Loop through the vnodesToUpdate array\n for (let i = 0, l = vnodesToUpdateCopy.length; i < l; i++) {\n const vnode2 = vnodesToUpdateCopy[i];\n // If it does, create a new vnode object based on the original vnode, its children, and its DOM and SVG properties\n let newVnode = v(vnode2.tag, vnode2.props, ...vnode2.initialChildren) as VnodeWithDom;\n newVnode.dom = vnode2.dom; // Set the new vnode object's DOM property to the old vnode object's DOM property\n newVnode.isSVG = vnode2.isSVG; // Set the new vnode object's isSVG property to the old vnode object's isSVG property\n\n // Update the vnode object\n updateVnode(newVnode, vnode2);\n }\n };\n\n // Define a function that returns the current value of the Signal\n function get() {\n // Get the current vnode from the context object\n const { vnode: vnode2 } = current;\n\n // If we have a current vnode, it means that a get function is being called from within a component\n // so we subscribe the vnode to be updated when the Signal's value changes\n if (vnode2 && vnodesToUpdate.indexOf(vnode2) === -1) {\n // We set the initialChildren to a copy of the vnode's children array\n // This is the case when the vnode is a component that has not been rendered yet and we need the initial children\n // because they could have the components that are using the Signal\n if (!vnode2.initialChildren) {\n vnode2.initialChildren = [...vnode2.children];\n }\n\n // Add the vnode to the vnodesToUpdate array\n vnodesToUpdate.push(vnode2);\n\n // Subscribe the updateVnodes function to the Signal\n subscribe(updateVnodes);\n }\n\n // Return the current value of the Signal\n return value;\n }\n\n // Define a function that allows the value of the Signal to be updated and notifies any subscribed functions of the change\n const set = (newValue) => {\n // If we have a current event on going, prevent the default action\n if (current.event) {\n current.event.preventDefault();\n }\n\n // Just return if the new value is the same as the current value\n if (newValue === value) {\n return;\n }\n\n // Update the value of the Signal\n value = newValue;\n\n // Call each subscribed function with the new value of the Signal as an argument\n for (let i = 0, l = subscriptions.length; i < l; i++) {\n subscriptions[i](value);\n }\n };\n\n // Assign the signal variable an array containing the get, set, and subscribe functions\n let signal: SignalInterface = [get, set, subscribe, subscriptions];\n\n // If the context object has a vnode property, add the signal to the vnode's signals array\n // and add the subscriptions array to the vnode's subscriptions array\n if (vnode) {\n component.signals.push(signal);\n }\n\n // Return the signal\n return signal;\n}\n"],
|
|
5
|
-
"mappings": ";AAAA,SAAuB,SAAS,WAAW,aAAa,SAAS;AAwB1D,SAAS,OAAO,
|
|
4
|
+
"sourcesContent": ["import { VnodeWithDom, current, onUnmount, updateVnode, v } from \"valyrian.js\";\n\ninterface GetterInterface {\n (): any;\n}\n\ninterface SetterInterface {\n (value: any): void;\n}\n\ninterface SubscribeInterface {\n (callback: Function): void;\n}\n\ninterface SubscriptionsInterface extends Array<Function> {}\n\nexport interface SignalInterface extends Array<any> {\n 0: GetterInterface;\n 1: SetterInterface;\n 2: SubscribeInterface;\n 3: SubscriptionsInterface;\n}\n\n// eslint-disable-next-line sonarjs/cognitive-complexity\nexport function Signal(initialValue: any): SignalInterface {\n // Create a copy of the current context object\n const { vnode, component } = { ...current };\n\n // Check if the context object has a vnode property\n if (vnode && component) {\n // Is first call\n if (!vnode.components) {\n // Set the components property to an empty array\n vnode.components = [];\n }\n\n // Check if the components array of the vnode object does not contain the component object\n if (vnode.components.indexOf(component) === -1) {\n // Set the calls property to -1\n vnode.signal_calls = -1;\n // Add the component to the components array\n vnode.components.push(component);\n\n // Check if the component object has a signals property\n if (!component.signals) {\n // Set the signals property of the component object to an empty array\n component.signals = [];\n // Add a function to the cleanup stack that removes the signals property from the component object\n onUnmount(() => Reflect.deleteProperty(component, \"signals\"));\n }\n }\n\n // Assign the signal variable to the signal stored at the index of the vnode object's calls property in the vnode's signals array\n let signal: SignalInterface = component.signals[++vnode.signal_calls];\n\n // If a signal has already been assigned to the signal variable, return it\n if (signal) {\n // Remove all subscriptions because we come from a new render\n signal[3].length = 0;\n\n // Return the signal\n return signal;\n }\n }\n\n // Declare a variable to store the current value of the Signal\n let value = initialValue;\n\n // Create an array to store functions that have subscribed to changes to the Signal's value\n const subscriptions: SubscriptionsInterface = [];\n\n // Define a function that allows other parts of the code to subscribe to changes to the Signal's value\n const subscribe = (callback: Function) => {\n // Add the callback function to the subscriptions array if it is not already in the array\n if (subscriptions.indexOf(callback) === -1) {\n subscriptions.push(callback);\n }\n };\n\n // Set the vnodes to update when the Signal's value changes\n let vnodesToUpdate: Array<VnodeWithDom> = [];\n\n // This is the function that will be called when the Signal's value changes\n const updateVnodes = () => {\n // Create a copy of the vnodesToUpdate array and filter out any duplicate vnodes\n let vnodesToUpdateCopy = vnodesToUpdate.filter((vnode, index, self) => {\n return self.findIndex((v) => v.dom === vnode.dom) === index;\n });\n\n // Loop through the vnodesToUpdate array\n for (let i = 0, l = vnodesToUpdateCopy.length; i < l; i++) {\n const vnode2 = vnodesToUpdateCopy[i];\n // If it does, create a new vnode object based on the original vnode, its children, and its DOM and SVG properties\n let newVnode = v(vnode2.tag, vnode2.props, ...vnode2.initialChildren) as VnodeWithDom;\n newVnode.dom = vnode2.dom; // Set the new vnode object's DOM property to the old vnode object's DOM property\n newVnode.isSVG = vnode2.isSVG; // Set the new vnode object's isSVG property to the old vnode object's isSVG property\n\n // Update the vnode object\n updateVnode(newVnode, vnode2);\n }\n };\n\n // Define a function that returns the current value of the Signal\n function get() {\n // Get the current vnode from the context object\n const { vnode: vnode2 } = current;\n\n // If we have a current vnode, it means that a get function is being called from within a component\n // so we subscribe the vnode to be updated when the Signal's value changes\n if (vnode2 && vnodesToUpdate.indexOf(vnode2) === -1) {\n // We set the initialChildren to a copy of the vnode's children array\n // This is the case when the vnode is a component that has not been rendered yet and we need the initial children\n // because they could have the components that are using the Signal\n if (!vnode2.initialChildren) {\n vnode2.initialChildren = [...vnode2.children];\n }\n\n // Add the vnode to the vnodesToUpdate array\n vnodesToUpdate.push(vnode2);\n\n // Subscribe the updateVnodes function to the Signal\n subscribe(updateVnodes);\n }\n\n // Return the current value of the Signal\n return value;\n }\n\n // Define a function that allows the value of the Signal to be updated and notifies any subscribed functions of the change\n const set = (newValue: any) => {\n // If we have a current event on going, prevent the default action\n if (current.event) {\n current.event.preventDefault();\n }\n\n // Just return if the new value is the same as the current value\n if (newValue === value) {\n return;\n }\n\n // Update the value of the Signal\n value = newValue;\n\n // Call each subscribed function with the new value of the Signal as an argument\n for (let i = 0, l = subscriptions.length; i < l; i++) {\n subscriptions[i](value);\n }\n };\n\n // Assign the signal variable an array containing the get, set, and subscribe functions\n let signal: SignalInterface = [get, set, subscribe, subscriptions];\n\n // If the context object has a vnode property, add the signal to the vnode's signals array\n // and add the subscriptions array to the vnode's subscriptions array\n if (vnode && component) {\n component.signals.push(signal);\n }\n\n // Return the signal\n return signal;\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,SAAuB,SAAS,WAAW,aAAa,SAAS;AAwB1D,SAAS,OAAO,cAAoC;AAEzD,QAAM,EAAE,OAAO,UAAU,IAAI,EAAE,GAAG,QAAQ;AAG1C,MAAI,SAAS,WAAW;AAEtB,QAAI,CAAC,MAAM,YAAY;AAErB,YAAM,aAAa,CAAC;AAAA,IACtB;AAGA,QAAI,MAAM,WAAW,QAAQ,SAAS,MAAM,IAAI;AAE9C,YAAM,eAAe;AAErB,YAAM,WAAW,KAAK,SAAS;AAG/B,UAAI,CAAC,UAAU,SAAS;AAEtB,kBAAU,UAAU,CAAC;AAErB,kBAAU,MAAM,QAAQ,eAAe,WAAW,SAAS,CAAC;AAAA,MAC9D;AAAA,IACF;AAGA,QAAIA,UAA0B,UAAU,QAAQ,EAAE,MAAM,YAAY;AAGpE,QAAIA,SAAQ;AAEV,MAAAA,QAAO,CAAC,EAAE,SAAS;AAGnB,aAAO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K,KAAK,WAAW,aAAa;AAIjE,MAAI,SAAS,WAAW;AACtB,cAAU,QAAQ,KAAK,MAAM;AAAA,EAC/B;AAGA,SAAO;AACT;",
|
|
6
6
|
"names": ["signal", "vnode", "v"]
|
|
7
7
|
}
|
package/dist/store/index.js
CHANGED
package/dist/store/index.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../lib/store/index.ts"],
|
|
4
4
|
"sourcesContent": ["import { update } from \"valyrian.js\";\n\ninterface StoreOptions {\n state?: Record<string, unknown> | (() => Record<string, unknown>);\n getters?: Record<string, Function>;\n mutations?: Record<string, Function>;\n actions?: Record<string, Function>;\n}\n\ninterface StoreInstance {\n // eslint-disable-next-line no-unused-vars\n new (options: StoreOptions): StoreInstance;\n state: Record<string, any>;\n getters?: Record<string, any>;\n // eslint-disable-next-line no-unused-vars\n commit: (type: string, ...payload: any[]) => void;\n // eslint-disable-next-line no-unused-vars\n dispatch: (type: string, ...payload: any[]) => void;\n}\n\nfunction keyExists(typeOfKey: string, object: Record<string, unknown>, key: string) {\n if (key in object === false) {\n throw new Error(`The ${typeOfKey} \"${key}\" does not exists.`);\n }\n}\n\nfunction deepFreeze(obj: any) {\n if (typeof obj === \"object\" && obj !== null && !Object.isFrozen(obj)) {\n if (Array.isArray(obj)) {\n for (let i = 0, l = obj.length; i < l; i++) {\n deepFreeze(obj[i]);\n }\n } else {\n let props = Reflect.ownKeys(obj);\n for (let i = 0, l = props.length; i < l; i++) {\n deepFreeze(obj[props[i]]);\n }\n }\n Object.freeze(obj);\n }\n\n return obj;\n}\n\nlet updateTimeout: any;\nfunction delayedUpdate() {\n clearTimeout(updateTimeout);\n updateTimeout = setTimeout(update);\n}\n\nexport const Store = function Store(\n this: StoreInstance,\n { state = {}, getters = {}, actions = {}, mutations = {} }: StoreOptions = {}\n) {\n let frozen = true;\n\n function isUnfrozen() {\n if (frozen) {\n throw new Error(\"You need to commit a mutation to change the state\");\n }\n }\n\n let localState = typeof state === \"function\" ? state() : state;\n\n this.state = new Proxy(localState || {}, {\n get: (state, prop: string) => deepFreeze(state[prop]),\n set: (state, prop: string, value: any) => {\n isUnfrozen();\n state[prop] = value;\n return true;\n },\n deleteProperty: (state, prop: string) => {\n isUnfrozen();\n Reflect.deleteProperty(state, prop);\n return true;\n }\n });\n\n this.getters = new Proxy(getters, {\n get: (getters, getter: string) => {\n try {\n return getters[getter](this.state, this.getters);\n } catch (e) {\n // Getters should fail silently\n }\n }\n });\n\n this.commit = (mutation, ...args) => {\n keyExists(\"mutation\", mutations, mutation);\n frozen = false;\n mutations[mutation](this.state, ...args);\n frozen = true;\n delayedUpdate();\n };\n\n this.dispatch = (action, ...args) => {\n keyExists(\"action\", actions, action);\n return Promise.resolve(actions[action](this, ...args));\n };\n} as unknown as StoreInstance;\n"],
|
|
5
|
-
"mappings": "
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAuB;AAoBvB,SAAS,UAAU,WAAmB,QAAiC,KAAa;AAClF,MAAI,OAAO,WAAW,OAAO;AAC3B,UAAM,IAAI,MAAM,OAAO,SAAS,KAAK,GAAG,oBAAoB;AAAA,EAC9D;AACF;AAEA,SAAS,WAAW,KAAU;AAC5B,MAAI,OAAO,QAAQ,YAAY,QAAQ,QAAQ,CAAC,OAAO,SAAS,GAAG,GAAG;AACpE,QAAI,MAAM,QAAQ,GAAG,GAAG;AACtB,eAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,IAAI,GAAG,KAAK;AAC1C,mBAAW,IAAI,CAAC,CAAC;AAAA,MACnB;AAAA,IACF,OAAO;AACL,UAAI,QAAQ,QAAQ,QAAQ,GAAG;AAC/B,eAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,IAAI,GAAG,KAAK;AAC5C,mBAAW,IAAI,MAAM,CAAC,CAAC,CAAC;AAAA,MAC1B;AAAA,IACF;AACA,WAAO,OAAO,GAAG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GAAG,IAAI,CAAC;AAAA,EACvD;AACF;",
|
|
6
6
|
"names": ["Store", "state", "getters"]
|
|
7
7
|
}
|
package/dist/sw/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function registerSw(file?: string, options?: RegistrationOptions): Promise<ServiceWorkerContainer>;
|
|
1
|
+
export declare function registerSw(file?: string, options?: RegistrationOptions): Promise<ServiceWorkerContainer | undefined>;
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/sw/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/sw/index.ts"],"names":[],"mappings":"AAEA,wBAAsB,UAAU,CAAC,IAAI,SAAY,EAAE,OAAO,GAAE,mBAAoC
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/sw/index.ts"],"names":[],"mappings":"AAEA,wBAAsB,UAAU,CAAC,IAAI,SAAY,EAAE,OAAO,GAAE,mBAAoC,+CAM/F"}
|
package/dist/sw/index.js
CHANGED
package/dist/sw/index.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../lib/sw/index.ts"],
|
|
4
4
|
"sourcesContent": ["import { isNodeJs } from \"valyrian.js\";\n\nexport async function registerSw(file = \"./sw.js\", options: RegistrationOptions = { scope: \"/\" }) {\n if (isNodeJs) {\n return;\n }\n await navigator.serviceWorker.register(file, options);\n return navigator.serviceWorker;\n}\n"],
|
|
5
|
-
"mappings": "
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAyB;AAEzB,eAAsB,WAAW,OAAO,WAAW,UAA+B,EAAE,OAAO,IAAI,GAAG;AAChG,MAAI,0BAAU;AACZ;AAAA,EACF;AACA,QAAM,UAAU,cAAc,SAAS,MAAM,OAAO;AACpD,SAAO,UAAU;AACnB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":"5.2.2"}
|