valyrian.js 7.2.10 → 7.2.12

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.
Files changed (64) hide show
  1. package/dist/dataset/index.d.ts +2 -2
  2. package/dist/dataset/index.d.ts.map +1 -1
  3. package/dist/dataset/index.js +21 -21
  4. package/dist/dataset/index.js.map +2 -2
  5. package/dist/dataset/index.mjs +22 -22
  6. package/dist/dataset/index.mjs.map +2 -2
  7. package/dist/hooks/index.d.ts.map +1 -1
  8. package/dist/hooks/index.js +17 -32
  9. package/dist/hooks/index.js.map +3 -3
  10. package/dist/hooks/index.mjs +17 -32
  11. package/dist/hooks/index.mjs.map +3 -3
  12. package/dist/index.d.ts +49 -53
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +378 -326
  15. package/dist/index.js.map +3 -3
  16. package/dist/index.min.js +1 -1
  17. package/dist/index.min.js.map +1 -1
  18. package/dist/index.mjs +378 -326
  19. package/dist/index.mjs.map +3 -3
  20. package/dist/node/index.js +115 -88
  21. package/dist/node/index.js.map +2 -2
  22. package/dist/node/index.mjs +115 -88
  23. package/dist/node/index.mjs.map +2 -2
  24. package/dist/node/utils/tree-adapter.d.ts +5 -0
  25. package/dist/node/utils/tree-adapter.d.ts.map +1 -1
  26. package/dist/proxy-signal/index.js +10 -10
  27. package/dist/proxy-signal/index.js.map +2 -2
  28. package/dist/proxy-signal/index.mjs +10 -10
  29. package/dist/proxy-signal/index.mjs.map +2 -2
  30. package/dist/request/index.js +16 -16
  31. package/dist/request/index.js.map +2 -2
  32. package/dist/request/index.mjs +16 -16
  33. package/dist/request/index.mjs.map +2 -2
  34. package/dist/router/index.d.ts.map +1 -1
  35. package/dist/router/index.js +21 -20
  36. package/dist/router/index.js.map +2 -2
  37. package/dist/router/index.mjs +21 -20
  38. package/dist/router/index.mjs.map +2 -2
  39. package/dist/signal/index.d.ts +7 -18
  40. package/dist/signal/index.d.ts.map +1 -1
  41. package/dist/signal/index.js +29 -48
  42. package/dist/signal/index.js.map +3 -3
  43. package/dist/signal/index.mjs +31 -50
  44. package/dist/signal/index.mjs.map +3 -3
  45. package/dist/store/index.js +2 -2
  46. package/dist/store/index.js.map +2 -2
  47. package/dist/store/index.mjs +2 -2
  48. package/dist/store/index.mjs.map +2 -2
  49. package/lib/dataset/index.ts +25 -25
  50. package/lib/hooks/index.ts +25 -54
  51. package/lib/index.ts +465 -715
  52. package/lib/node/index.ts +2 -2
  53. package/lib/node/utils/icons.ts +5 -5
  54. package/lib/node/utils/inline.ts +17 -17
  55. package/lib/node/utils/sw.ts +3 -3
  56. package/lib/node/utils/tree-adapter.ts +95 -62
  57. package/lib/proxy-signal/index.ts +10 -10
  58. package/lib/request/index.ts +16 -16
  59. package/lib/router/index.ts +21 -20
  60. package/lib/signal/index.ts +56 -131
  61. package/lib/store/index.ts +2 -2
  62. package/package.json +10 -3
  63. package/lib/index.d.ts +0 -0
  64. package/lib/interfaces.ts.bak +0 -141
@@ -30,9 +30,9 @@ var addPath = ({
30
30
  if (!method || !path || !Array.isArray(middlewares) || middlewares.length === 0) {
31
31
  throw new Error(`Invalid route input: ${method} ${path} ${middlewares}`);
32
32
  }
33
- let realpath = path.replace(/(\S)(\/+)$/, "$1");
34
- let params = (realpath.match(/:(\w+)?/gi) || []).map((param) => param.slice(1));
35
- let regexpPath = "^" + realpath.replace(/:(\w+)/gi, "([^\\/\\s]+)") + "$";
33
+ const realpath = path.replace(/(\S)(\/+)$/, "$1");
34
+ const params = (realpath.match(/:(\w+)?/gi) || []).map((param) => param.slice(1));
35
+ const regexpPath = "^" + realpath.replace(/:(\w+)/gi, "([^\\/\\s]+)") + "$";
36
36
  router.paths.push({
37
37
  method,
38
38
  path: realpath,
@@ -42,24 +42,24 @@ var addPath = ({
42
42
  });
43
43
  };
44
44
  function parseQuery(queryParts) {
45
- let parts = queryParts ? queryParts.split("&") : [];
46
- let query = {};
47
- for (let nameValue of parts) {
48
- let [name, value] = nameValue.split("=", 2);
45
+ const parts = queryParts ? queryParts.split("&") : [];
46
+ const query = {};
47
+ for (const nameValue of parts) {
48
+ const [name, value] = nameValue.split("=", 2);
49
49
  query[name] = value || "";
50
50
  }
51
51
  return query;
52
52
  }
53
53
  function searchMiddlewares(router, path) {
54
- let middlewares = [];
55
- let params = {};
56
- let matches = [];
57
- for (let item of router.paths) {
58
- let match = item.regexp.exec(path);
54
+ const middlewares = [];
55
+ const params = {};
56
+ const matches = [];
57
+ for (const item of router.paths) {
58
+ const match = item.regexp.exec(path);
59
59
  if (Array.isArray(match)) {
60
60
  middlewares.push(...item.middlewares);
61
61
  match.shift();
62
- for (let [index, key] of item.params.entries()) {
62
+ for (const [index, key] of item.params.entries()) {
63
63
  params[key] = match[index];
64
64
  }
65
65
  matches.push(...match);
@@ -110,12 +110,12 @@ var Router = class _Router {
110
110
  this.pathPrefix = pathPrefix;
111
111
  }
112
112
  add(path, ...middlewares) {
113
- let pathWithoutLastSlash = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);
113
+ const pathWithoutLastSlash = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);
114
114
  addPath({ router: this, method: "add", path: pathWithoutLastSlash, middlewares });
115
115
  return this;
116
116
  }
117
117
  use(...middlewares) {
118
- let path = getPathWithoutLastSlash(
118
+ const path = getPathWithoutLastSlash(
119
119
  `${this.pathPrefix}${typeof middlewares[0] === "string" ? middlewares.shift() : "/"}`
120
120
  );
121
121
  for (const item of middlewares) {
@@ -144,7 +144,7 @@ var Router = class _Router {
144
144
  if (!path) {
145
145
  throw new Error("router.url.required");
146
146
  }
147
- let constructedPath = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);
147
+ const constructedPath = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);
148
148
  const parts = constructedPath.split("?", 2);
149
149
  this.url = constructedPath;
150
150
  this.query = parseQuery(parts[1]);
@@ -193,16 +193,17 @@ function mountRouter(elementContainer, router) {
193
193
  localRedirect = router.go.bind(router);
194
194
  if (!isNodeJs) {
195
195
  let onPopStateGoToRoute2 = function() {
196
- let pathWithoutPrefix = getPathWithoutPrefix(document.location.pathname, router.pathPrefix);
196
+ const pathWithoutPrefix = getPathWithoutPrefix(document.location.pathname, router.pathPrefix);
197
197
  router.go(pathWithoutPrefix, void 0, true);
198
198
  };
199
199
  var onPopStateGoToRoute = onPopStateGoToRoute2;
200
200
  window.addEventListener("popstate", onPopStateGoToRoute2, false);
201
201
  onPopStateGoToRoute2();
202
202
  }
203
- directive("route", (url, vnode, oldnode) => {
204
- setAttribute("href", url, vnode, oldnode);
205
- setAttribute("onclick", router.getOnClickHandler(url), vnode, oldnode);
203
+ directive("route", (vnode) => {
204
+ const url = vnode.props["v-route"];
205
+ setAttribute("href", url, vnode);
206
+ setAttribute("onclick", router.getOnClickHandler(url), vnode);
206
207
  });
207
208
  }
208
209
  export {
@@ -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 (\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,IAAI;AAC/B,2BAAuB;AAAA,EACzB;AACA,SAAO;AACT;AAEA,IAAM,UAAU,CAAC;AAAA,EACf;AAAA,EACA;AAAA,EACA;AAAA,EAC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QAAQ,KAAK,OAAO,OAAO;AACxC,iBAAa,WAAW,OAAO,kBAAkB,GAAG,GAAG,OAAO,OAAO;AAAA,EACvE,CAAC;AACH;",
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 const realpath = path.replace(/(\\S)(\\/+)$/, \"$1\");\n\n // Find the express-like params in the path\n const 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 const 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 const parts = queryParts ? queryParts.split(\"&\") : [];\n const query: Record<string, string> = {};\n\n // Iterate over the name-value pairs and add them to the query object\n for (const nameValue of parts) {\n const [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 const middlewares: Middlewares = [];\n const params: Record<string, any> = {};\n const matches = [];\n\n // Search for middlewares\n for (const item of router.paths) {\n const 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 (const [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 const 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 const 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 const 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 const 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\", (vnode: VnodeWithDom): void => {\n const url = vnode.props[\"v-route\"];\n setAttribute(\"href\", url, vnode);\n setAttribute(\"onclick\", router.getOnClickHandler(url), vnode);\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,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,QAAM,WAAW,KAAK,QAAQ,cAAc,IAAI;AAGhD,QAAM,UAAU,SAAS,MAAM,WAAW,KAAK,CAAC,GAE7C,IAAI,CAAC,UAAU,MAAM,MAAM,CAAC,CAAC;AAGhC,QAAM,aAAa,MAAM,SAAS,QAAQ,YAAY,cAAc,IAAI;AAExE,SAAO,MAAM,KAAK;AAAA,IAChB;AAAA,IACA,MAAM;AAAA,IACN,aAAa,KAAK,WAAW;AAAA,IAC7B;AAAA,IACA,QAAQ,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,KAAa;AAC7B,WAAO,CAAC,MAAkB;AACxB,UAAI,OAAO,QAAQ,YAAY,IAAI,SAAS,GAAG;AAC7C,aAAK,GAAG,GAAG;AAAA,MACb;AACA,QAAE,eAAe;AAAA,IACnB;AAAA,EACF;AACF;AAEA,IAAI;AAEG,SAAS,SACd,KACA,iBACA,mBAAmB,OACK;AACxB,MAAI,CAAC,eAAe;AAClB,UAAM,IAAI,MAAM,2BAA2B;AAAA,EAC7C;AACA,SAAO,cAAc,KAAK,iBAAiB,gBAAgB;AAC7D;AAEO,SAAS,YAAY,kBAAgC,QAAsB;AAChF,SAAO,YAAY;AACnB,kBAAgB,OAAO,GAAG,KAAK,MAAM;AAErC,MAAI,CAAC,UAAU;AACb,QAASA,uBAAT,WAAqC;AACnC,YAAM,oBAAoB,qBAAqB,SAAS,SAAS,UAAU,OAAO,UAAU;AAC5F,MAAC,OAA6B,GAAG,mBAAmB,QAAW,IAAI;AAAA,IACrE;AAHS,8BAAAA;AAIT,WAAO,iBAAiB,YAAYA,sBAAqB,KAAK;AAC9D,IAAAA,qBAAoB;AAAA,EACtB;AAEA,YAAU,SAAS,CAAC,UAA8B;AAChD,UAAM,MAAM,MAAM,MAAM,SAAS;AACjC,iBAAa,QAAQ,KAAK,KAAK;AAC/B,iBAAa,WAAW,OAAO,kBAAkB,GAAG,GAAG,KAAK;AAAA,EAC9D,CAAC;AACH;",
6
6
  "names": ["onPopStateGoToRoute"]
7
7
  }
@@ -1,20 +1,9 @@
1
- interface GetterInterface {
2
- (): any;
3
- }
4
- interface SetterInterface {
5
- (value: any): void;
6
- }
7
- interface SubscribeInterface {
8
- (callback: Function): void;
9
- }
10
- interface SubscriptionsInterface extends Array<Function> {
11
- }
12
- export interface SignalInterface extends Array<any> {
13
- 0: GetterInterface;
14
- 1: SetterInterface;
15
- 2: SubscribeInterface;
16
- 3: SubscriptionsInterface;
17
- }
18
- export declare function Signal(initialValue: any): SignalInterface;
1
+ type getter = () => any;
2
+ type setter = (newValue: any) => void;
3
+ type unsubscribe = () => void;
4
+ type subscribe = (callback: () => void) => unsubscribe;
5
+ type subscriptions = Set<() => void>;
6
+ type signal = [getter, setter, subscribe, subscriptions];
7
+ export declare function Signal<T>(initialValue: T): signal;
19
8
  export {};
20
9
  //# sourceMappingURL=index.d.ts.map
@@ -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,EAAE,GAAG,GAAG,eAAe,CAwIzD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/signal/index.ts"],"names":[],"mappings":"AAEA,KAAK,MAAM,GAAG,MAAM,GAAG,CAAC;AACxB,KAAK,MAAM,GAAG,CAAC,QAAQ,EAAE,GAAG,KAAK,IAAI,CAAC;AACtC,KAAK,WAAW,GAAG,MAAM,IAAI,CAAC;AAC9B,KAAK,SAAS,GAAG,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,WAAW,CAAC;AACvD,KAAK,aAAa,GAAG,GAAG,CAAC,MAAM,IAAI,CAAC,CAAC;AACrC,KAAK,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,CAAC,CAAC;AAUzD,wBAAgB,MAAM,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,MAAM,CAoEjD"}
@@ -24,72 +24,53 @@ __export(signal_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(signal_exports);
26
26
  var import_valyrian = require("valyrian.js");
27
+ var componentToSignalsWeakMap = /* @__PURE__ */ new WeakMap();
27
28
  function Signal(initialValue) {
28
- const { vnode, component } = { ...import_valyrian.current };
29
- if (vnode && component) {
30
- if (!vnode.components) {
31
- vnode.components = [];
29
+ if (import_valyrian.current.component) {
30
+ if (componentToSignalsWeakMap.has(import_valyrian.current.component) === false) {
31
+ const SignalCalls2 = { signals: [], signal_calls: -1 };
32
+ componentToSignalsWeakMap.set(import_valyrian.current.component, SignalCalls2);
33
+ (0, import_valyrian.onUnmount)(() => componentToSignalsWeakMap.delete(import_valyrian.current.component));
32
34
  }
33
- if (vnode.components.indexOf(component) === -1) {
34
- vnode.signal_calls = -1;
35
- vnode.components.push(component);
36
- if (!component.signals) {
37
- component.signals = [];
38
- (0, import_valyrian.onUnmount)(() => Reflect.deleteProperty(component, "signals"));
39
- }
40
- }
41
- let signal2 = component.signals[++vnode.signal_calls];
35
+ const SignalCalls = componentToSignalsWeakMap.get(import_valyrian.current.component);
36
+ (0, import_valyrian.onCleanup)(() => SignalCalls.signal_calls = -1);
37
+ const signal2 = SignalCalls.signals[++SignalCalls.signal_calls];
42
38
  if (signal2) {
43
- signal2[3].length = 0;
44
- return signal2;
39
+ const fakeSubscribe = () => {
40
+ };
41
+ return [signal2[0], signal2[1], fakeSubscribe, signal2[3]];
45
42
  }
46
43
  }
47
44
  let value = initialValue;
48
- const subscriptions = [];
45
+ const subscribers = /* @__PURE__ */ new Set();
49
46
  const subscribe = (callback) => {
50
- if (subscriptions.indexOf(callback) === -1) {
51
- subscriptions.push(callback);
52
- }
53
- };
54
- let vnodesToUpdate = [];
55
- const updateVnodes = () => {
56
- let vnodesToUpdateCopy = vnodesToUpdate.filter((vnode2, index, self) => {
57
- return self.findIndex((v2) => v2.dom === vnode2.dom) === index;
58
- });
59
- for (let i = 0, l = vnodesToUpdateCopy.length; i < l; i++) {
60
- const vnode2 = vnodesToUpdateCopy[i];
61
- let newVnode = (0, import_valyrian.v)(vnode2.tag, vnode2.props, ...vnode2.initialChildren);
62
- newVnode.dom = vnode2.dom;
63
- newVnode.isSVG = vnode2.isSVG;
64
- (0, import_valyrian.updateVnode)(newVnode, vnode2);
65
- }
47
+ subscribers.add(callback);
48
+ return () => subscribers.delete(callback);
66
49
  };
67
- function get() {
68
- const { vnode: vnode2 } = import_valyrian.current;
69
- if (vnode2 && vnodesToUpdate.indexOf(vnode2) === -1) {
70
- if (!vnode2.initialChildren) {
71
- vnode2.initialChildren = [...vnode2.children];
72
- }
73
- vnodesToUpdate.push(vnode2);
50
+ const domToVnodesToUpdate = /* @__PURE__ */ new Map();
51
+ const updateVnodes = () => domToVnodesToUpdate.forEach((vnode) => (0, import_valyrian.updateVnode)(vnode));
52
+ const getValue = () => {
53
+ if (import_valyrian.current.vnode) {
54
+ const vnode = import_valyrian.current.vnode;
55
+ domToVnodesToUpdate.set(vnode.dom, vnode);
74
56
  subscribe(updateVnodes);
75
57
  }
76
58
  return value;
77
- }
78
- const set = (newValue) => {
59
+ };
60
+ const setValue = (newValue) => {
79
61
  if (import_valyrian.current.event) {
80
62
  import_valyrian.current.event.preventDefault();
81
63
  }
82
- if (newValue === value) {
64
+ if (value === newValue) {
83
65
  return;
84
66
  }
85
67
  value = newValue;
86
- for (let i = 0, l = subscriptions.length; i < l; i++) {
87
- subscriptions[i](value);
88
- }
68
+ subscribers.forEach((subscriber) => subscriber());
89
69
  };
90
- let signal = [get, set, subscribe, subscriptions];
91
- if (vnode && component) {
92
- component.signals.push(signal);
70
+ const signal = [getValue, setValue, subscribe, subscribers];
71
+ if (import_valyrian.current.component) {
72
+ const SignalCalls = componentToSignalsWeakMap.get(import_valyrian.current.component);
73
+ SignalCalls.signals.push(signal);
93
74
  }
94
75
  return signal;
95
76
  }
@@ -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: 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
- "names": ["signal", "vnode", "v"]
4
+ "sourcesContent": ["import { updateVnode, current, VnodeWithDom, onCleanup, POJOComponent, Component, onUnmount } from \"valyrian.js\";\n\ntype getter = () => any;\ntype setter = (newValue: any) => void;\ntype unsubscribe = () => void;\ntype subscribe = (callback: () => void) => unsubscribe;\ntype subscriptions = Set<() => void>;\ntype signal = [getter, setter, subscribe, subscriptions];\n\ntype SignalCalls = {\n signals: signal[];\n signal_calls: number;\n};\n\nconst componentToSignalsWeakMap = new WeakMap<Component | POJOComponent, SignalCalls>();\n\n// Signal is a generic function that creates a reactive state with a getter, setter, and subscribe mechanism.\nexport function Signal<T>(initialValue: T): signal {\n if (current.component) {\n if (componentToSignalsWeakMap.has(current.component) === false) {\n const SignalCalls = { signals: [], signal_calls: -1 };\n componentToSignalsWeakMap.set(current.component, SignalCalls);\n onUnmount(() => componentToSignalsWeakMap.delete(current.component as Component | POJOComponent));\n }\n\n const SignalCalls = componentToSignalsWeakMap.get(current.component) as SignalCalls;\n onCleanup(() => (SignalCalls.signal_calls = -1));\n\n const signal = SignalCalls.signals[++SignalCalls.signal_calls];\n\n if (signal) {\n // Return the signal if it already exists.\n // But without the subscribe function. This is to prevent the subscribe function from being called multiple times.\n const fakeSubscribe = (() => {}) as unknown as subscribe;\n return [signal[0], signal[1], fakeSubscribe, signal[3]];\n }\n }\n\n // The current value of the signal is stored in a closure to maintain state.\n let value: T = initialValue;\n // Subscribers is a Set of functions to be called whenever the value changes.\n const subscribers: subscriptions = new Set();\n\n // subscribe is a function that allows a subscriber to listen to changes in the signal's value.\n // It returns an unsubscribe function to stop listening to changes.\n const subscribe = (callback: () => void) => {\n subscribers.add(callback);\n return () => subscribers.delete(callback);\n };\n\n const domToVnodesToUpdate: Map<Node, VnodeWithDom> = new Map();\n const updateVnodes = () => domToVnodesToUpdate.forEach((vnode) => updateVnode(vnode));\n\n // getValue is a function that returns the current value of the signal.\n const getValue = () => {\n if (current.vnode) {\n const vnode = current.vnode as VnodeWithDom;\n domToVnodesToUpdate.set(vnode.dom, vnode);\n subscribe(updateVnodes);\n }\n return value;\n };\n\n // setValue is a function that updates the value of the signal and notifies subscribers.\n const setValue = (newValue: any) => {\n if (current.event) {\n current.event.preventDefault();\n }\n\n if (value === newValue) {\n return;\n }\n value = newValue;\n // Notify all subscribers by invoking their callback functions.\n subscribers.forEach((subscriber) => subscriber());\n };\n\n const signal: signal = [getValue, setValue, subscribe, subscribers];\n\n if (current.component) {\n const SignalCalls = componentToSignalsWeakMap.get(current.component) as SignalCalls;\n SignalCalls.signals.push(signal);\n }\n\n return signal;\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAmG;AAcnG,IAAM,4BAA4B,oBAAI,QAAgD;AAG/E,SAAS,OAAU,cAAyB;AACjD,MAAI,wBAAQ,WAAW;AACrB,QAAI,0BAA0B,IAAI,wBAAQ,SAAS,MAAM,OAAO;AAC9D,YAAMA,eAAc,EAAE,SAAS,CAAC,GAAG,cAAc,GAAG;AACpD,gCAA0B,IAAI,wBAAQ,WAAWA,YAAW;AAC5D,qCAAU,MAAM,0BAA0B,OAAO,wBAAQ,SAAsC,CAAC;AAAA,IAClG;AAEA,UAAM,cAAc,0BAA0B,IAAI,wBAAQ,SAAS;AACnE,mCAAU,MAAO,YAAY,eAAe,EAAG;AAE/C,UAAMC,UAAS,YAAY,QAAQ,EAAE,YAAY,YAAY;AAE7D,QAAIA,SAAQ;AAGV,YAAM,gBAAiB,MAAM;AAAA,MAAC;AAC9B,aAAO,CAACA,QAAO,CAAC,GAAGA,QAAO,CAAC,GAAG,eAAeA,QAAO,CAAC,CAAC;AAAA,IACxD;AAAA,EACF;AAGA,MAAI,QAAW;AAEf,QAAM,cAA6B,oBAAI,IAAI;AAI3C,QAAM,YAAY,CAAC,aAAyB;AAC1C,gBAAY,IAAI,QAAQ;AACxB,WAAO,MAAM,YAAY,OAAO,QAAQ;AAAA,EAC1C;AAEA,QAAM,sBAA+C,oBAAI,IAAI;AAC7D,QAAM,eAAe,MAAM,oBAAoB,QAAQ,CAAC,cAAU,6BAAY,KAAK,CAAC;AAGpF,QAAM,WAAW,MAAM;AACrB,QAAI,wBAAQ,OAAO;AACjB,YAAM,QAAQ,wBAAQ;AACtB,0BAAoB,IAAI,MAAM,KAAK,KAAK;AACxC,gBAAU,YAAY;AAAA,IACxB;AACA,WAAO;AAAA,EACT;AAGA,QAAM,WAAW,CAAC,aAAkB;AAClC,QAAI,wBAAQ,OAAO;AACjB,8BAAQ,MAAM,eAAe;AAAA,IAC/B;AAEA,QAAI,UAAU,UAAU;AACtB;AAAA,IACF;AACA,YAAQ;AAER,gBAAY,QAAQ,CAAC,eAAe,WAAW,CAAC;AAAA,EAClD;AAEA,QAAM,SAAiB,CAAC,UAAU,UAAU,WAAW,WAAW;AAElE,MAAI,wBAAQ,WAAW;AACrB,UAAM,cAAc,0BAA0B,IAAI,wBAAQ,SAAS;AACnE,gBAAY,QAAQ,KAAK,MAAM;AAAA,EACjC;AAEA,SAAO;AACT;",
6
+ "names": ["SignalCalls", "signal"]
7
7
  }
@@ -1,71 +1,52 @@
1
1
  // lib/signal/index.ts
2
- import { current, onUnmount, updateVnode, v } from "valyrian.js";
2
+ import { updateVnode, current, onCleanup, onUnmount } from "valyrian.js";
3
+ var componentToSignalsWeakMap = /* @__PURE__ */ new WeakMap();
3
4
  function Signal(initialValue) {
4
- const { vnode, component } = { ...current };
5
- if (vnode && component) {
6
- if (!vnode.components) {
7
- vnode.components = [];
8
- }
9
- if (vnode.components.indexOf(component) === -1) {
10
- vnode.signal_calls = -1;
11
- vnode.components.push(component);
12
- if (!component.signals) {
13
- component.signals = [];
14
- onUnmount(() => Reflect.deleteProperty(component, "signals"));
15
- }
16
- }
17
- let signal2 = component.signals[++vnode.signal_calls];
5
+ if (current.component) {
6
+ if (componentToSignalsWeakMap.has(current.component) === false) {
7
+ const SignalCalls2 = { signals: [], signal_calls: -1 };
8
+ componentToSignalsWeakMap.set(current.component, SignalCalls2);
9
+ onUnmount(() => componentToSignalsWeakMap.delete(current.component));
10
+ }
11
+ const SignalCalls = componentToSignalsWeakMap.get(current.component);
12
+ onCleanup(() => SignalCalls.signal_calls = -1);
13
+ const signal2 = SignalCalls.signals[++SignalCalls.signal_calls];
18
14
  if (signal2) {
19
- signal2[3].length = 0;
20
- return signal2;
15
+ const fakeSubscribe = () => {
16
+ };
17
+ return [signal2[0], signal2[1], fakeSubscribe, signal2[3]];
21
18
  }
22
19
  }
23
20
  let value = initialValue;
24
- const subscriptions = [];
21
+ const subscribers = /* @__PURE__ */ new Set();
25
22
  const subscribe = (callback) => {
26
- if (subscriptions.indexOf(callback) === -1) {
27
- subscriptions.push(callback);
28
- }
29
- };
30
- let vnodesToUpdate = [];
31
- const updateVnodes = () => {
32
- let vnodesToUpdateCopy = vnodesToUpdate.filter((vnode2, index, self) => {
33
- return self.findIndex((v2) => v2.dom === vnode2.dom) === index;
34
- });
35
- for (let i = 0, l = vnodesToUpdateCopy.length; i < l; i++) {
36
- const vnode2 = vnodesToUpdateCopy[i];
37
- let newVnode = v(vnode2.tag, vnode2.props, ...vnode2.initialChildren);
38
- newVnode.dom = vnode2.dom;
39
- newVnode.isSVG = vnode2.isSVG;
40
- updateVnode(newVnode, vnode2);
41
- }
23
+ subscribers.add(callback);
24
+ return () => subscribers.delete(callback);
42
25
  };
43
- function get() {
44
- const { vnode: vnode2 } = current;
45
- if (vnode2 && vnodesToUpdate.indexOf(vnode2) === -1) {
46
- if (!vnode2.initialChildren) {
47
- vnode2.initialChildren = [...vnode2.children];
48
- }
49
- vnodesToUpdate.push(vnode2);
26
+ const domToVnodesToUpdate = /* @__PURE__ */ new Map();
27
+ const updateVnodes = () => domToVnodesToUpdate.forEach((vnode) => updateVnode(vnode));
28
+ const getValue = () => {
29
+ if (current.vnode) {
30
+ const vnode = current.vnode;
31
+ domToVnodesToUpdate.set(vnode.dom, vnode);
50
32
  subscribe(updateVnodes);
51
33
  }
52
34
  return value;
53
- }
54
- const set = (newValue) => {
35
+ };
36
+ const setValue = (newValue) => {
55
37
  if (current.event) {
56
38
  current.event.preventDefault();
57
39
  }
58
- if (newValue === value) {
40
+ if (value === newValue) {
59
41
  return;
60
42
  }
61
43
  value = newValue;
62
- for (let i = 0, l = subscriptions.length; i < l; i++) {
63
- subscriptions[i](value);
64
- }
44
+ subscribers.forEach((subscriber) => subscriber());
65
45
  };
66
- let signal = [get, set, subscribe, subscriptions];
67
- if (vnode && component) {
68
- component.signals.push(signal);
46
+ const signal = [getValue, setValue, subscribe, subscribers];
47
+ if (current.component) {
48
+ const SignalCalls = componentToSignalsWeakMap.get(current.component);
49
+ SignalCalls.signals.push(signal);
69
50
  }
70
51
  return signal;
71
52
  }
@@ -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: 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,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,iB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\n\ntype getter = () => any;\ntype setter = (newValue: any) => void;\ntype unsubscribe = () => void;\ntype subscribe = (callback: () => void) => unsubscribe;\ntype subscriptions = Set<() => void>;\ntype signal = [getter, setter, subscribe, subscriptions];\n\ntype SignalCalls = {\n signals: signal[];\n signal_calls: number;\n};\n\nconst componentToSignalsWeakMap = new WeakMap<Component | POJOComponent, SignalCalls>();\n\n// Signal is a generic function that creates a reactive state with a getter, setter, and subscribe mechanism.\nexport function Signal<T>(initialValue: T): signal {\n if (current.component) {\n if (componentToSignalsWeakMap.has(current.component) === false) {\n const SignalCalls = { signals: [], signal_calls: -1 };\n componentToSignalsWeakMap.set(current.component, SignalCalls);\n onUnmount(() => componentToSignalsWeakMap.delete(current.component as Component | POJOComponent));\n }\n\n const SignalCalls = componentToSignalsWeakMap.get(current.component) as SignalCalls;\n onCleanup(() => (SignalCalls.signal_calls = -1));\n\n const signal = SignalCalls.signals[++SignalCalls.signal_calls];\n\n if (signal) {\n // Return the signal if it already exists.\n // But without the subscribe function. This is to prevent the subscribe function from being called multiple times.\n const fakeSubscribe = (() => {}) as unknown as subscribe;\n return [signal[0], signal[1], fakeSubscribe, signal[3]];\n }\n }\n\n // The current value of the signal is stored in a closure to maintain state.\n let value: T = initialValue;\n // Subscribers is a Set of functions to be called whenever the value changes.\n const subscribers: subscriptions = new Set();\n\n // subscribe is a function that allows a subscriber to listen to changes in the signal's value.\n // It returns an unsubscribe function to stop listening to changes.\n const subscribe = (callback: () => void) => {\n subscribers.add(callback);\n return () => subscribers.delete(callback);\n };\n\n const domToVnodesToUpdate: Map<Node, VnodeWithDom> = new Map();\n const updateVnodes = () => domToVnodesToUpdate.forEach((vnode) => updateVnode(vnode));\n\n // getValue is a function that returns the current value of the signal.\n const getValue = () => {\n if (current.vnode) {\n const vnode = current.vnode as VnodeWithDom;\n domToVnodesToUpdate.set(vnode.dom, vnode);\n subscribe(updateVnodes);\n }\n return value;\n };\n\n // setValue is a function that updates the value of the signal and notifies subscribers.\n const setValue = (newValue: any) => {\n if (current.event) {\n current.event.preventDefault();\n }\n\n if (value === newValue) {\n return;\n }\n value = newValue;\n // Notify all subscribers by invoking their callback functions.\n subscribers.forEach((subscriber) => subscriber());\n };\n\n const signal: signal = [getValue, setValue, subscribe, subscribers];\n\n if (current.component) {\n const SignalCalls = componentToSignalsWeakMap.get(current.component) as SignalCalls;\n SignalCalls.signals.push(signal);\n }\n\n return signal;\n}\n"],
5
+ "mappings": ";AAAA,SAAS,aAAa,SAAuB,WAAqC,iBAAiB;AAcnG,IAAM,4BAA4B,oBAAI,QAAgD;AAG/E,SAAS,OAAU,cAAyB;AACjD,MAAI,QAAQ,WAAW;AACrB,QAAI,0BAA0B,IAAI,QAAQ,SAAS,MAAM,OAAO;AAC9D,YAAMA,eAAc,EAAE,SAAS,CAAC,GAAG,cAAc,GAAG;AACpD,gCAA0B,IAAI,QAAQ,WAAWA,YAAW;AAC5D,gBAAU,MAAM,0BAA0B,OAAO,QAAQ,SAAsC,CAAC;AAAA,IAClG;AAEA,UAAM,cAAc,0BAA0B,IAAI,QAAQ,SAAS;AACnE,cAAU,MAAO,YAAY,eAAe,EAAG;AAE/C,UAAMC,UAAS,YAAY,QAAQ,EAAE,YAAY,YAAY;AAE7D,QAAIA,SAAQ;AAGV,YAAM,gBAAiB,MAAM;AAAA,MAAC;AAC9B,aAAO,CAACA,QAAO,CAAC,GAAGA,QAAO,CAAC,GAAG,eAAeA,QAAO,CAAC,CAAC;AAAA,IACxD;AAAA,EACF;AAGA,MAAI,QAAW;AAEf,QAAM,cAA6B,oBAAI,IAAI;AAI3C,QAAM,YAAY,CAAC,aAAyB;AAC1C,gBAAY,IAAI,QAAQ;AACxB,WAAO,MAAM,YAAY,OAAO,QAAQ;AAAA,EAC1C;AAEA,QAAM,sBAA+C,oBAAI,IAAI;AAC7D,QAAM,eAAe,MAAM,oBAAoB,QAAQ,CAAC,UAAU,YAAY,KAAK,CAAC;AAGpF,QAAM,WAAW,MAAM;AACrB,QAAI,QAAQ,OAAO;AACjB,YAAM,QAAQ,QAAQ;AACtB,0BAAoB,IAAI,MAAM,KAAK,KAAK;AACxC,gBAAU,YAAY;AAAA,IACxB;AACA,WAAO;AAAA,EACT;AAGA,QAAM,WAAW,CAAC,aAAkB;AAClC,QAAI,QAAQ,OAAO;AACjB,cAAQ,MAAM,eAAe;AAAA,IAC/B;AAEA,QAAI,UAAU,UAAU;AACtB;AAAA,IACF;AACA,YAAQ;AAER,gBAAY,QAAQ,CAAC,eAAe,WAAW,CAAC;AAAA,EAClD;AAEA,QAAM,SAAiB,CAAC,UAAU,UAAU,WAAW,WAAW;AAElE,MAAI,QAAQ,WAAW;AACrB,UAAM,cAAc,0BAA0B,IAAI,QAAQ,SAAS;AACnE,gBAAY,QAAQ,KAAK,MAAM;AAAA,EACjC;AAEA,SAAO;AACT;",
6
+ "names": ["SignalCalls", "signal"]
7
7
  }
@@ -36,7 +36,7 @@ function deepFreeze(obj) {
36
36
  deepFreeze(obj[i]);
37
37
  }
38
38
  } else {
39
- let props = Reflect.ownKeys(obj);
39
+ const props = Reflect.ownKeys(obj);
40
40
  for (let i = 0, l = props.length; i < l; i++) {
41
41
  deepFreeze(obj[props[i]]);
42
42
  }
@@ -57,7 +57,7 @@ var Store = function Store2({ state = {}, getters = {}, actions = {}, mutations
57
57
  throw new Error("You need to commit a mutation to change the state");
58
58
  }
59
59
  }
60
- let localState = typeof state === "function" ? state() : state;
60
+ const localState = typeof state === "function" ? state() : state;
61
61
  this.state = new Proxy(localState || {}, {
62
62
  get: (state2, prop) => deepFreeze(state2[prop]),
63
63
  set: (state2, prop, value) => {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../lib/store/index.ts"],
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(\"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B,WAAWA,OAAM,IAAI,CAAC;AAAA,IACpD,KAAK,CAACA,QAAO,MAAc,UAAe;AACxC,iBAAW;AACX,MAAAA,OAAM,IAAI,IAAI;AACd,aAAO;AAAA,IACT;AAAA,IACA,gBAAgB,CAACA,QAAO,SAAiB;AACvC,iBAAW;AACX,cAAQ,eAAeA,QAAO,IAAI;AAClC,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AAED,OAAK,UAAU,IAAI,MAAM,SAAS;AAAA,IAChC,KAAK,CAACC,UAAS,WAAmB;AAChC,UAAI;AACF,eAAOA,SAAQ,MAAM,EAAE,KAAK,OAAO,KAAK,OAAO;AAAA,MACjD,SAAS,GAAG;AAAA,MAEZ;AAAA,IACF;AAAA,EACF,CAAC;AAED,OAAK,SAAS,CAAC,aAAa,SAAS;AACnC,cAAU,YAAY,WAAW,QAAQ;AACzC,aAAS;AACT,cAAU,QAAQ,EAAE,KAAK,OAAO,GAAG,IAAI;AACvC,aAAS;AACT,kBAAc;AAAA,EAChB;AAEA,OAAK,WAAW,CAAC,WAAW,SAAS;AACnC,cAAU,UAAU,SAAS,MAAM;AACnC,WAAO,QAAQ,QAAQ,QAAQ,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;AAAA,EACvD;AACF;",
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 const 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 const 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": ";;;;;;;;;;;;;;;;;;;;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,EAC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AM,SAAS;AAAA,IAChC,KAAK,CAACC,UAAS,WAAmB;AAChC,UAAI;AACF,eAAOA,SAAQ,MAAM,EAAE,KAAK,OAAO,KAAK,OAAO;AAAA,MACjD,SAAS,GAAG;AAAA,MAEZ;AAAA,IACF;AAAA,EACF,CAAC;AAED,OAAK,SAAS,CAAC,aAAa,SAAS;AACnC,cAAU,YAAY,WAAW,QAAQ;AACzC,aAAS;AACT,cAAU,QAAQ,EAAE,KAAK,OAAO,GAAG,IAAI;AACvC,aAAS;AACT,kBAAc;AAAA,EAChB;AAEA,OAAK,WAAW,CAAC,WAAW,SAAS;AACnC,cAAU,UAAU,SAAS,MAAM;AACnC,WAAO,QAAQ,QAAQ,QAAQ,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;AAAA,EACvD;AACF;",
6
6
  "names": ["Store", "state", "getters"]
7
7
  }
@@ -12,7 +12,7 @@ function deepFreeze(obj) {
12
12
  deepFreeze(obj[i]);
13
13
  }
14
14
  } else {
15
- let props = Reflect.ownKeys(obj);
15
+ const props = Reflect.ownKeys(obj);
16
16
  for (let i = 0, l = props.length; i < l; i++) {
17
17
  deepFreeze(obj[props[i]]);
18
18
  }
@@ -33,7 +33,7 @@ var Store = function Store2({ state = {}, getters = {}, actions = {}, mutations
33
33
  throw new Error("You need to commit a mutation to change the state");
34
34
  }
35
35
  }
36
- let localState = typeof state === "function" ? state() : state;
36
+ const localState = typeof state === "function" ? state() : state;
37
37
  this.state = new Proxy(localState || {}, {
38
38
  get: (state2, prop) => deepFreeze(state2[prop]),
39
39
  set: (state2, prop, value) => {