valyrian.js 7.2.12 → 8.0.1

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 (168) hide show
  1. package/README.md +6 -6
  2. package/dist/flux-store/index.d.ts +32 -0
  3. package/dist/flux-store/index.d.ts.map +1 -0
  4. package/dist/flux-store/index.js +267 -0
  5. package/dist/flux-store/index.js.map +7 -0
  6. package/dist/flux-store/index.min.js +1 -0
  7. package/dist/flux-store/index.min.js.map +1 -0
  8. package/dist/flux-store/index.mjs +246 -0
  9. package/dist/flux-store/index.mjs.map +7 -0
  10. package/dist/hooks/index.d.ts.map +1 -1
  11. package/dist/hooks/index.js +35 -51
  12. package/dist/hooks/index.js.map +3 -3
  13. package/dist/hooks/index.min.js +1 -0
  14. package/dist/hooks/index.min.js.map +1 -0
  15. package/dist/hooks/index.mjs +36 -52
  16. package/dist/hooks/index.mjs.map +3 -3
  17. package/dist/index.d.ts +18 -14
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +107 -88
  20. package/dist/index.js.map +3 -3
  21. package/dist/index.min.js +1 -1
  22. package/dist/index.min.js.map +1 -1
  23. package/dist/index.mjs +107 -88
  24. package/dist/index.mjs.map +3 -3
  25. package/dist/native-store/index.d.ts +14 -0
  26. package/dist/native-store/index.d.ts.map +1 -0
  27. package/dist/native-store/index.js +103 -0
  28. package/dist/native-store/index.js.map +7 -0
  29. package/dist/native-store/index.min.js +1 -0
  30. package/dist/native-store/index.min.js.map +1 -0
  31. package/dist/native-store/index.mjs +82 -0
  32. package/dist/native-store/index.mjs.map +7 -0
  33. package/dist/node/index.d.ts +1 -1
  34. package/dist/node/index.d.ts.map +1 -1
  35. package/dist/node/index.js +125 -10
  36. package/dist/node/index.js.map +4 -4
  37. package/dist/node/index.mjs +125 -10
  38. package/dist/node/index.mjs.map +4 -4
  39. package/dist/node/node.sw.js +152 -0
  40. package/dist/node/utils/icons.d.ts +4 -5
  41. package/dist/node/utils/icons.d.ts.map +1 -1
  42. package/dist/node/utils/inline.d.ts +1 -1
  43. package/dist/node/utils/inline.d.ts.map +1 -1
  44. package/dist/node/utils/node.sw.js +152 -0
  45. package/dist/node/utils/session-storage.d.ts +22 -0
  46. package/dist/node/utils/session-storage.d.ts.map +1 -0
  47. package/dist/node/utils/sw.d.ts.map +1 -1
  48. package/dist/node/utils/tree-adapter.d.ts +5 -1
  49. package/dist/node/utils/tree-adapter.d.ts.map +1 -1
  50. package/dist/pulse-store/index.d.ts +16 -0
  51. package/dist/pulse-store/index.d.ts.map +1 -0
  52. package/dist/pulse-store/index.js +143 -0
  53. package/dist/pulse-store/index.js.map +7 -0
  54. package/dist/pulse-store/index.min.js +1 -0
  55. package/dist/pulse-store/index.min.js.map +1 -0
  56. package/dist/pulse-store/index.mjs +122 -0
  57. package/dist/pulse-store/index.mjs.map +7 -0
  58. package/dist/request/index.d.ts +11 -11
  59. package/dist/request/index.d.ts.map +1 -1
  60. package/dist/request/index.js +63 -84
  61. package/dist/request/index.js.map +2 -2
  62. package/dist/request/index.min.js +1 -0
  63. package/dist/request/index.min.js.map +1 -0
  64. package/dist/request/index.mjs +63 -84
  65. package/dist/request/index.mjs.map +2 -2
  66. package/dist/router/index.d.ts +36 -33
  67. package/dist/router/index.d.ts.map +1 -1
  68. package/dist/router/index.js +247 -96
  69. package/dist/router/index.js.map +3 -3
  70. package/dist/router/index.min.js +1 -0
  71. package/dist/router/index.min.js.map +1 -0
  72. package/dist/router/index.mjs +247 -96
  73. package/dist/router/index.mjs.map +3 -3
  74. package/dist/signals/index.d.ts +6 -0
  75. package/dist/signals/index.d.ts.map +1 -0
  76. package/dist/signals/index.js +92 -0
  77. package/dist/signals/index.js.map +7 -0
  78. package/dist/signals/index.min.js +1 -0
  79. package/dist/signals/index.min.js.map +1 -0
  80. package/dist/signals/index.mjs +71 -0
  81. package/dist/signals/index.mjs.map +7 -0
  82. package/dist/suspense/index.d.ts +6 -0
  83. package/dist/suspense/index.d.ts.map +1 -0
  84. package/dist/suspense/index.js +67 -0
  85. package/dist/suspense/index.js.map +7 -0
  86. package/dist/suspense/index.min.js +1 -0
  87. package/dist/suspense/index.min.js.map +1 -0
  88. package/dist/suspense/index.mjs +46 -0
  89. package/dist/suspense/index.mjs.map +7 -0
  90. package/dist/sw/index.min.js +1 -0
  91. package/dist/sw/index.min.js.map +1 -0
  92. package/dist/translate/index.d.ts +19 -0
  93. package/dist/translate/index.d.ts.map +1 -0
  94. package/dist/translate/index.js +150 -0
  95. package/dist/translate/index.js.map +7 -0
  96. package/dist/translate/index.min.js +1 -0
  97. package/dist/translate/index.min.js.map +1 -0
  98. package/dist/translate/index.mjs +129 -0
  99. package/dist/translate/index.mjs.map +7 -0
  100. package/dist/tsconfig.tsbuildinfo +1 -1
  101. package/dist/utils/deep-freeze.d.ts +3 -0
  102. package/dist/utils/deep-freeze.d.ts.map +1 -0
  103. package/dist/utils/getter-setter.d.ts +3 -0
  104. package/dist/utils/getter-setter.d.ts.map +1 -0
  105. package/dist/utils/has-changed.d.ts +2 -0
  106. package/dist/utils/has-changed.d.ts.map +1 -0
  107. package/dist/utils/index.d.ts +4 -0
  108. package/dist/utils/index.d.ts.map +1 -0
  109. package/dist/utils/index.js +138 -0
  110. package/dist/utils/index.js.map +7 -0
  111. package/dist/utils/index.min.js +1 -0
  112. package/dist/utils/index.min.js.map +1 -0
  113. package/dist/utils/index.mjs +115 -0
  114. package/dist/utils/index.mjs.map +7 -0
  115. package/lib/flux-store/index.ts +312 -0
  116. package/lib/hooks/index.ts +39 -57
  117. package/lib/index.ts +135 -118
  118. package/lib/native-store/index.ts +106 -0
  119. package/lib/node/index.ts +3 -1
  120. package/lib/node/utils/icons.ts +4 -4
  121. package/lib/node/utils/inline.ts +2 -0
  122. package/lib/node/utils/node.sw.js +152 -0
  123. package/lib/node/utils/session-storage.ts +117 -0
  124. package/lib/node/utils/sw.ts +34 -10
  125. package/lib/node/utils/tree-adapter.ts +19 -1
  126. package/lib/pulse-store/index.ts +188 -0
  127. package/lib/request/index.ts +92 -122
  128. package/lib/router/index.ts +353 -164
  129. package/lib/signals/index.ts +98 -0
  130. package/lib/suspense/index.ts +57 -0
  131. package/lib/translate/index.ts +156 -0
  132. package/lib/utils/deep-freeze.ts +54 -0
  133. package/lib/utils/getter-setter.ts +40 -0
  134. package/lib/utils/has-changed.ts +43 -0
  135. package/lib/utils/index.ts +3 -0
  136. package/package.json +40 -57
  137. package/tsconfig.json +5 -4
  138. package/dist/dataset/index.d.ts +0 -24
  139. package/dist/dataset/index.d.ts.map +0 -1
  140. package/dist/dataset/index.js +0 -178
  141. package/dist/dataset/index.js.map +0 -7
  142. package/dist/dataset/index.mjs +0 -157
  143. package/dist/dataset/index.mjs.map +0 -7
  144. package/dist/node/node.sw.tpl +0 -133
  145. package/dist/node/utils/node.sw.tpl +0 -133
  146. package/dist/proxy-signal/index.d.ts +0 -23
  147. package/dist/proxy-signal/index.d.ts.map +0 -1
  148. package/dist/proxy-signal/index.js +0 -138
  149. package/dist/proxy-signal/index.js.map +0 -7
  150. package/dist/proxy-signal/index.mjs +0 -117
  151. package/dist/proxy-signal/index.mjs.map +0 -7
  152. package/dist/signal/index.d.ts +0 -9
  153. package/dist/signal/index.d.ts.map +0 -1
  154. package/dist/signal/index.js +0 -76
  155. package/dist/signal/index.js.map +0 -7
  156. package/dist/signal/index.mjs +0 -55
  157. package/dist/signal/index.mjs.map +0 -7
  158. package/dist/store/index.d.ts +0 -16
  159. package/dist/store/index.d.ts.map +0 -1
  160. package/dist/store/index.js +0 -93
  161. package/dist/store/index.js.map +0 -7
  162. package/dist/store/index.mjs +0 -72
  163. package/dist/store/index.mjs.map +0 -7
  164. package/lib/dataset/index.ts +0 -193
  165. package/lib/node/utils/node.sw.tpl +0 -133
  166. package/lib/proxy-signal/index.ts +0 -187
  167. package/lib/signal/index.ts +0 -86
  168. package/lib/store/index.ts +0 -101
@@ -1,72 +0,0 @@
1
- // lib/store/index.ts
2
- import { update } from "valyrian.js";
3
- function keyExists(typeOfKey, object, key) {
4
- if (key in object === false) {
5
- throw new Error(`The ${typeOfKey} "${key}" does not exists.`);
6
- }
7
- }
8
- function deepFreeze(obj) {
9
- if (typeof obj === "object" && obj !== null && !Object.isFrozen(obj)) {
10
- if (Array.isArray(obj)) {
11
- for (let i = 0, l = obj.length; i < l; i++) {
12
- deepFreeze(obj[i]);
13
- }
14
- } else {
15
- const props = Reflect.ownKeys(obj);
16
- for (let i = 0, l = props.length; i < l; i++) {
17
- deepFreeze(obj[props[i]]);
18
- }
19
- }
20
- Object.freeze(obj);
21
- }
22
- return obj;
23
- }
24
- var updateTimeout;
25
- function delayedUpdate() {
26
- clearTimeout(updateTimeout);
27
- updateTimeout = setTimeout(update);
28
- }
29
- var Store = function Store2({ state = {}, getters = {}, actions = {}, mutations = {} } = {}) {
30
- let frozen = true;
31
- function isUnfrozen() {
32
- if (frozen) {
33
- throw new Error("You need to commit a mutation to change the state");
34
- }
35
- }
36
- const localState = typeof state === "function" ? state() : state;
37
- this.state = new Proxy(localState || {}, {
38
- get: (state2, prop) => deepFreeze(state2[prop]),
39
- set: (state2, prop, value) => {
40
- isUnfrozen();
41
- state2[prop] = value;
42
- return true;
43
- },
44
- deleteProperty: (state2, prop) => {
45
- isUnfrozen();
46
- Reflect.deleteProperty(state2, prop);
47
- return true;
48
- }
49
- });
50
- this.getters = new Proxy(getters, {
51
- get: (getters2, getter) => {
52
- try {
53
- return getters2[getter](this.state, this.getters);
54
- } catch (e) {
55
- }
56
- }
57
- });
58
- this.commit = (mutation, ...args) => {
59
- keyExists("mutation", mutations, mutation);
60
- frozen = false;
61
- mutations[mutation](this.state, ...args);
62
- frozen = true;
63
- delayedUpdate();
64
- };
65
- this.dispatch = (action, ...args) => {
66
- keyExists("action", actions, action);
67
- return Promise.resolve(actions[action](this, ...args));
68
- };
69
- };
70
- export {
71
- Store
72
- };
@@ -1,7 +0,0 @@
1
- {
2
- "version": 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 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,SAAS,cAAc;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,YAAM,QAAQ,QAAQ,QAAQ,GAAG;AACjC,eAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,IAAI,GAAG,KAAK;AAC5C,mBAAW,IAAI,MAAM,CAAC,CAAC,CAAC;AAAA,MAC1B;AAAA,IACF;AACA,WAAO,OAAO,GAAG;AAAA,EACnB;AAEA,SAAO;AACT;AAEA,IAAI;AACJ,SAAS,gBAAgB;AACvB,eAAa,aAAa;AAC1B,kBAAgB,WAAW,MAAM;AACnC;AAEO,IAAM,QAAQ,SAASA,OAE5B,EAAE,QAAQ,CAAC,GAAG,UAAU,CAAC,GAAG,UAAU,CAAC,GAAG,YAAY,CAAC,EAAE,IAAkB,CAAC,GAC5E;AACA,MAAI,SAAS;AAEb,WAAS,aAAa;AACpB,QAAI,QAAQ;AACV,YAAM,IAAI,MAAM,mDAAmD;AAAA,IACrE;AAAA,EACF;AAEA,QAAM,aAAa,OAAO,UAAU,aAAa,MAAM,IAAI;AAE3D,OAAK,QAAQ,IAAI,MAAM,cAAc,CAAC,GAAG;AAAA,IACvC,KAAK,CAACC,QAAO,SAAiB,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;",
6
- "names": ["Store", "state", "getters"]
7
- }
@@ -1,193 +0,0 @@
1
- import { VnodeWithDom, createElement, directive, patch, updateAttributes } from "valyrian.js";
2
-
3
- interface DataSetInterface<T> {
4
- data: T[];
5
- // eslint-disable-next-line no-unused-vars
6
- reset: (data: T[]) => void;
7
- // eslint-disable-next-line no-unused-vars
8
- add: (...data: T[]) => void;
9
- // eslint-disable-next-line no-unused-vars
10
- update: (index: number, data: T) => void;
11
- // eslint-disable-next-line no-unused-vars
12
- delete: (index: number) => void;
13
- }
14
- interface DataSetHandler<T> {
15
- // eslint-disable-next-line no-unused-vars
16
- (data: T, index: number): VnodeWithDom;
17
- }
18
-
19
- function deepFreeze(obj: any) {
20
- if (typeof obj === "object" && obj !== null && !Object.isFrozen(obj)) {
21
- if (Array.isArray(obj)) {
22
- for (let i = 0, l = obj.length; i < l; i++) {
23
- deepFreeze(obj[i]);
24
- }
25
- } else {
26
- const props = Reflect.ownKeys(obj);
27
- for (let i = 0, l = props.length; i < l; i++) {
28
- deepFreeze(obj[props[i]]);
29
- }
30
- }
31
- Object.freeze(obj);
32
- }
33
-
34
- return obj;
35
- }
36
-
37
- export class DataSet<T> implements DataSetInterface<T> {
38
- #vnode: VnodeWithDom | null = null;
39
- // eslint-disable-next-line no-unused-vars
40
- #handler: DataSetHandler<T> | null = null;
41
- #data: T[] = [];
42
- #isFrozen = false;
43
- #dataProxy: T[] | null = null;
44
-
45
- get data() {
46
- if (this.#dataProxy === null) {
47
- throw new Error("DataSet is not initialized");
48
- }
49
-
50
- return this.#dataProxy;
51
- }
52
-
53
- set data(data: T[]) {
54
- throw new Error("You need to use the reset method to set the data");
55
- }
56
-
57
- #setData(data: T[]) {
58
- if (this.#isFrozen) {
59
- this.#data = deepFreeze([...data]);
60
- } else {
61
- this.#data = data;
62
- }
63
- this.#dataProxy = new Proxy(this.#data as T[], {
64
- set: () => {
65
- throw new Error("You need to use the add, update or delete methods to change the data");
66
- },
67
- get(target: any, prop: string) {
68
- return target[prop];
69
- },
70
- deleteProperty: () => {
71
- throw new Error("You need to use the add, update or delete methods to change the data");
72
- }
73
- }) as T[];
74
- }
75
-
76
- constructor(data: T[] = [], shouldFreeze = true) {
77
- this.#isFrozen = shouldFreeze;
78
- this.#setData(data);
79
- }
80
-
81
- setVnodeAndHandler(vnode: VnodeWithDom, handler: DataSetHandler<T>) {
82
- this.#vnode = vnode;
83
- this.#handler = handler;
84
- this.reset(this.#data);
85
- }
86
-
87
- reset(data: T[]) {
88
- this.#setData(data);
89
- if (this.#vnode === null || this.#handler === null) {
90
- return;
91
- }
92
-
93
- const vnode = this.#vnode;
94
- const handler = this.#handler;
95
-
96
- if (data.length === 0) {
97
- vnode.children = [];
98
- vnode.dom.textContent = "";
99
- return;
100
- }
101
-
102
- const childrenLength = vnode.children.length;
103
- for (let i = 0, l = data.length; i < l; i++) {
104
- const child = handler(this.data[i], i);
105
-
106
- if (i < childrenLength) {
107
- const oldChild = vnode.children[i];
108
- child.isSVG = oldChild.isSVG;
109
- child.dom = oldChild.dom;
110
- updateAttributes(child as VnodeWithDom, null);
111
- vnode.children[i] = child;
112
- patch(child as VnodeWithDom);
113
- continue;
114
- }
115
-
116
- child.isSVG = vnode.isSVG || child.tag === "svg";
117
- child.dom = createElement(child.tag as string, child.isSVG);
118
- vnode.dom.appendChild(child.dom);
119
- updateAttributes(child as VnodeWithDom, null);
120
- vnode.children.push(child);
121
- patch(child as VnodeWithDom);
122
- }
123
-
124
- for (let i = data.length; i < childrenLength; i++) {
125
- vnode.dom.removeChild(vnode.children[i].dom);
126
- }
127
- vnode.children.length = data.length;
128
- }
129
-
130
- add(...data: T[]) {
131
- if (this.#data) {
132
- const oldLength = this.#data.length;
133
- if (this.#isFrozen) {
134
- this.#setData([...this.#data, ...data]);
135
- } else {
136
- this.#data.push(...data);
137
- }
138
-
139
- if (this.#vnode === null || this.#handler === null) {
140
- return;
141
- }
142
-
143
- const vnode = this.#vnode;
144
- const handler = this.#handler;
145
-
146
- for (let i = 0, ii = oldLength, l = data.length; i < l; i++, ii++) {
147
- const child = handler(this.#data[i], ii);
148
- child.isSVG = vnode.isSVG || child.tag === "svg";
149
- child.dom = createElement(child.tag as string, child.isSVG);
150
- vnode.dom.appendChild(child.dom);
151
- updateAttributes(child as VnodeWithDom, null);
152
- vnode.children.push(child);
153
- patch(child as VnodeWithDom);
154
- }
155
- }
156
- }
157
-
158
- delete(index: number) {
159
- if (this.#data && this.#vnode) {
160
- const child = this.#vnode.children[index];
161
- if (this.#isFrozen) {
162
- this.#setData(this.data.filter((_, i) => i !== index));
163
- } else {
164
- this.#data.splice(index, 1);
165
- }
166
-
167
- this.#vnode.dom.removeChild(child.dom);
168
- this.#vnode.children.splice(index, 1);
169
- }
170
- }
171
-
172
- update(index: number, item: Partial<T>) {
173
- if (this.#data && this.#vnode && this.#handler) {
174
- const child = this.#vnode.children[index];
175
- if (this.#isFrozen) {
176
- this.#setData(this.#data.map((d, i) => (i === index ? { ...d, ...item } : d)));
177
- } else {
178
- this.#data[index] = { ...this.#data[index], ...item };
179
- }
180
- const newChild = this.#handler(this.#data[index], index);
181
- newChild.isSVG = this.#vnode.isSVG || newChild.tag === "svg";
182
- newChild.dom = child.dom;
183
- this.#vnode.children[index] = newChild;
184
- updateAttributes(newChild as VnodeWithDom, null);
185
- patch(newChild as VnodeWithDom);
186
- }
187
- }
188
- }
189
-
190
- directive("with-dataset", (dataSet: DataSet<any>, vnode: VnodeWithDom) => {
191
- dataSet.setVnodeAndHandler(vnode, vnode.children[0]);
192
- return false;
193
- });
@@ -1,133 +0,0 @@
1
- let Log = console.log;
2
-
3
- let config = {
4
- version: "v1::",
5
- name: "Valyrian.js",
6
- urls: ["/"]
7
- };
8
-
9
- let cacheName = config.version + config.name;
10
-
11
- async function fetchRequest(event) {
12
- Log("WORKER: fetchevent for " + event.request.url);
13
- let response;
14
- try {
15
- // IMPORTANT: Clone the request. A request is a stream and
16
- // can only be consumed once. Since we are consuming this
17
- // once by cache and once by the browser for fetch, we need
18
- // to clone the response.
19
- let fetchRequest = event.request.clone();
20
- response = await fetch(fetchRequest);
21
- if (response && response.status < 300 && response.type === "basic") {
22
- try {
23
- // IMPORTANT: Clone the response. A response is a stream
24
- // and because we want the browser to consume the response
25
- // as well as the cache consuming the response, we need
26
- // to clone it so we have two streams.
27
- let responseToCache = response.clone();
28
- let cache = await caches.open(cacheName);
29
- cache.put(event.request, responseToCache);
30
-
31
- Log("WORKER: fetch response stored in cache.", event.request.url);
32
- } catch (err) {
33
- Log("WORKER: fetch response could not be stored in cache.", err);
34
- }
35
-
36
- return response;
37
- }
38
- } catch (error) {
39
- Log("WORKER: fetch request failed.", error);
40
- }
41
-
42
- let cachedResponse;
43
- try {
44
- cachedResponse = await caches.match(event.request);
45
- if (cachedResponse) {
46
- Log("WORKER: fetch request failed, responding with cache.");
47
- return cachedResponse;
48
- }
49
- } catch (error) {
50
- Log("WORKER: cache request failed.", error);
51
- }
52
-
53
- Log(
54
- "WORKER: fetch request failed in both cache and network, responding with service unavailable."
55
- );
56
- return (
57
- response ||
58
- new Response("<h1>Service Unavailable</h1>", {
59
- status: 503,
60
- statusText: "Service Unavailable",
61
- headers: new Headers({
62
- "Content-Type": "text/html"
63
- })
64
- })
65
- );
66
- }
67
-
68
- self.addEventListener("fetch", (event) => {
69
- // DevTools opening will trigger these o-i-c requests, which this SW can't handle.
70
- // https://github.com/paulirish/caltrainschedule.io/issues/49
71
- if (
72
- event.request.cache === "only-if-cached" &&
73
- event.request.mode !== "same-origin"
74
- ) {
75
- return;
76
- }
77
-
78
- Log("WORKER: fetch event in progress.", event.request.url);
79
-
80
- // We only handle Get requests all others let them pass
81
- if (event.request.method !== "GET") {
82
- return;
83
- }
84
-
85
- event.respondWith(fetchRequest(event));
86
- });
87
-
88
- self.addEventListener("install", (event) => {
89
- Log("WORKER: Version install", cacheName);
90
- event.waitUntil(
91
- caches
92
- .open(cacheName)
93
- .then((cache) => cache.addAll(config.urls))
94
- // IMPORTANT: `skipWaiting()` forces the waiting ServiceWorker to become the
95
- // active ServiceWorker, triggering the `onactivate` event.
96
- // Together with `Clients.claim()` this allows a worker to take effect
97
- // immediately in the client(s).
98
- .then(() => self.skipWaiting())
99
- );
100
- });
101
-
102
- // IMPORTANT: `onactivate` is usually called after a worker was installed and the page
103
- // got refreshed. Since we call `skipWaiting()` in `oninstall`, `onactivate` is
104
- // called immediately.
105
- self.addEventListener("activate", (event) => {
106
- self.clients
107
- .matchAll({
108
- includeUncontrolled: true
109
- })
110
- .then((clientList) => {
111
- urls = clientList.map((client) => client.url);
112
- Log("WORKER: Matching clients:", urls.join(", "));
113
- });
114
-
115
- event.waitUntil(
116
- caches
117
- .keys()
118
- .then((keys) =>
119
- Promise.all(
120
- keys
121
- // Filter by keys that don't start with the latest version prefix.
122
- .filter((key) => !key.startsWith(cacheName))
123
- // Return a promise that's fulfilled when each outdated cache is deleted.
124
- .map((key) => caches.delete(key))
125
- )
126
- )
127
-
128
- // IMPORTANT: `claim()` sets this worker as the active worker for all clients that
129
- // match the workers scope and triggers an `oncontrollerchange` event for
130
- // the clients.
131
- .then(() => self.clients.claim())
132
- );
133
- });
@@ -1,187 +0,0 @@
1
- import { update } from "valyrian.js";
2
-
3
- /* eslint-disable no-use-before-define */
4
- interface Cleanup {
5
- (): void;
6
- }
7
-
8
- interface Subscription {
9
- // eslint-disable-next-line no-unused-vars
10
- (value: ProxySignal["value"]): void | Cleanup;
11
- }
12
-
13
- interface Subscriptions extends Map<Subscription, Cleanup> {}
14
-
15
- interface Getter {
16
- // eslint-disable-next-line no-unused-vars
17
- (value: ProxySignal["value"]): any;
18
- }
19
-
20
- interface Getters {
21
- [key: string | symbol]: Getter;
22
- }
23
-
24
- interface ProxySignal {
25
- // Works as a getter of the value
26
- (): ProxySignal["value"];
27
- // Works as a subscription to the value
28
- // eslint-disable-next-line no-unused-vars
29
- (value: Subscription): ProxySignal;
30
- // Works as a setter with a path and a handler
31
- // eslint-disable-next-line no-unused-vars
32
- (path: string, handler: (valueAtPathPosition: any) => any): ProxySignal["value"];
33
- // Works as a setter with a path and a value
34
- // eslint-disable-next-line no-unused-vars
35
- (path: string, value: any): ProxySignal["value"];
36
- // Works as a setter with a value
37
- // eslint-disable-next-line no-unused-vars
38
- (value: any): ProxySignal["value"];
39
- // Gets the current value of the signal.
40
- value: any;
41
- // Cleanup function to be called to remove all subscriptions.
42
- cleanup: () => void;
43
- // Creates a getter on the signal.
44
- // eslint-disable-next-line no-unused-vars
45
- getter: (name: string, handler: Getter) => any;
46
- // To access the getters on the signal.
47
- [key: string | number | symbol]: any;
48
- }
49
-
50
- function makeUnsubscribe(
51
- subscriptions: Subscriptions,
52
- computed: ProxySignal,
53
- handler: Subscription,
54
- cleanup?: Cleanup
55
- ) {
56
- if (typeof cleanup === "function") {
57
- computed.cleanup = cleanup;
58
- }
59
- computed.unsubscribe = () => {
60
- subscriptions.delete(handler);
61
- computed?.cleanup();
62
- };
63
- }
64
-
65
- function createSubscription(signal: ProxySignal, subscriptions: Subscriptions, handler: Subscription) {
66
- if (subscriptions.has(handler) === false) {
67
- // eslint-disable-next-line no-use-before-define
68
- const computed = ProxySignal(() => handler(signal.value));
69
- const cleanup = computed(); // Execute to register itself
70
- makeUnsubscribe(subscriptions, computed, handler, cleanup);
71
- subscriptions.set(handler, computed);
72
- }
73
-
74
- return subscriptions.get(handler);
75
- }
76
-
77
- let updateTimeout: any;
78
- function delayedUpdate() {
79
- clearTimeout(updateTimeout);
80
- updateTimeout = setTimeout(update);
81
- }
82
-
83
- // eslint-disable-next-line sonarjs/cognitive-complexity
84
- export function ProxySignal(value: any): ProxySignal {
85
- const subscriptions = new Map();
86
- const getters: Getters = {};
87
-
88
- let forceUpdate = false;
89
-
90
- const signal: ProxySignal = new Proxy(
91
- // eslint-disable-next-line no-unused-vars
92
- function (valOrPath?: any | Subscription, handler?: (valueAtPathPosition: any) => any) {
93
- // Works as a getter
94
- if (typeof valOrPath === "undefined") {
95
- return signal.value;
96
- }
97
-
98
- // Works as a subscription
99
- if (typeof valOrPath === "function") {
100
- return createSubscription(signal, subscriptions, valOrPath);
101
- }
102
-
103
- // Works as a setter with a path
104
- if (typeof valOrPath === "string" && typeof handler !== "undefined") {
105
- const parsed = valOrPath.split(".");
106
- let result = signal.value;
107
- let next;
108
- while (parsed.length) {
109
- next = parsed.shift() as string;
110
- if (parsed.length > 0) {
111
- if (typeof result[next] !== "object") {
112
- result[next] = {};
113
- }
114
- result = result[next];
115
- } else {
116
- result[next] = typeof handler === "function" ? handler(result[next]) : handler;
117
- }
118
- }
119
- forceUpdate = true;
120
- signal.value = signal.value;
121
- return signal.value;
122
- }
123
-
124
- // Works as a setter with a value
125
- signal.value = valOrPath;
126
- return signal.value;
127
- } as ProxySignal,
128
- {
129
- set(state, prop, val) {
130
- if (prop === "value" || prop === "unsubscribe" || prop === "cleanup") {
131
- const old = state[prop];
132
- state[prop] = val;
133
- if (prop === "value" && (forceUpdate || val !== old)) {
134
- forceUpdate = false;
135
- for (const [handler, computed] of subscriptions) {
136
- computed.cleanup();
137
- const cleanup = handler(val);
138
- makeUnsubscribe(subscriptions, computed, handler, cleanup);
139
- }
140
- delayedUpdate();
141
- }
142
- return true;
143
- }
144
- return false;
145
- },
146
- get(state, prop) {
147
- if (prop === "value") {
148
- return typeof state.value === "function" ? state.value() : state.value;
149
- }
150
-
151
- if (prop === "cleanup" || prop === "unsubscribe" || prop === "getter") {
152
- return state[prop];
153
- }
154
-
155
- if (prop in getters) {
156
- return getters[prop](state.value);
157
- }
158
- }
159
- }
160
- );
161
-
162
- Object.defineProperties(signal, {
163
- value: { value, writable: true, enumerable: true },
164
- cleanup: {
165
- value() {
166
- // eslint-disable-next-line no-unused-vars
167
- for (const [handler, computed] of subscriptions) {
168
- computed.unsubscribe();
169
- }
170
- },
171
- writable: true,
172
- enumerable: true
173
- },
174
- getter: {
175
- value(name: string, handler: Getter) {
176
- if (name in getters) {
177
- throw new Error("Named computed already exists.");
178
- }
179
-
180
- getters[name] = handler;
181
- },
182
- enumerable: true
183
- }
184
- });
185
-
186
- return signal;
187
- }
@@ -1,86 +0,0 @@
1
- import { updateVnode, current, VnodeWithDom, onCleanup, POJOComponent, Component, onUnmount } from "valyrian.js";
2
-
3
- type getter = () => any;
4
- type setter = (newValue: any) => void;
5
- type unsubscribe = () => void;
6
- type subscribe = (callback: () => void) => unsubscribe;
7
- type subscriptions = Set<() => void>;
8
- type signal = [getter, setter, subscribe, subscriptions];
9
-
10
- type SignalCalls = {
11
- signals: signal[];
12
- signal_calls: number;
13
- };
14
-
15
- const componentToSignalsWeakMap = new WeakMap<Component | POJOComponent, SignalCalls>();
16
-
17
- // Signal is a generic function that creates a reactive state with a getter, setter, and subscribe mechanism.
18
- export function Signal<T>(initialValue: T): signal {
19
- if (current.component) {
20
- if (componentToSignalsWeakMap.has(current.component) === false) {
21
- const SignalCalls = { signals: [], signal_calls: -1 };
22
- componentToSignalsWeakMap.set(current.component, SignalCalls);
23
- onUnmount(() => componentToSignalsWeakMap.delete(current.component as Component | POJOComponent));
24
- }
25
-
26
- const SignalCalls = componentToSignalsWeakMap.get(current.component) as SignalCalls;
27
- onCleanup(() => (SignalCalls.signal_calls = -1));
28
-
29
- const signal = SignalCalls.signals[++SignalCalls.signal_calls];
30
-
31
- if (signal) {
32
- // Return the signal if it already exists.
33
- // But without the subscribe function. This is to prevent the subscribe function from being called multiple times.
34
- const fakeSubscribe = (() => {}) as unknown as subscribe;
35
- return [signal[0], signal[1], fakeSubscribe, signal[3]];
36
- }
37
- }
38
-
39
- // The current value of the signal is stored in a closure to maintain state.
40
- let value: T = initialValue;
41
- // Subscribers is a Set of functions to be called whenever the value changes.
42
- const subscribers: subscriptions = new Set();
43
-
44
- // subscribe is a function that allows a subscriber to listen to changes in the signal's value.
45
- // It returns an unsubscribe function to stop listening to changes.
46
- const subscribe = (callback: () => void) => {
47
- subscribers.add(callback);
48
- return () => subscribers.delete(callback);
49
- };
50
-
51
- const domToVnodesToUpdate: Map<Node, VnodeWithDom> = new Map();
52
- const updateVnodes = () => domToVnodesToUpdate.forEach((vnode) => updateVnode(vnode));
53
-
54
- // getValue is a function that returns the current value of the signal.
55
- const getValue = () => {
56
- if (current.vnode) {
57
- const vnode = current.vnode as VnodeWithDom;
58
- domToVnodesToUpdate.set(vnode.dom, vnode);
59
- subscribe(updateVnodes);
60
- }
61
- return value;
62
- };
63
-
64
- // setValue is a function that updates the value of the signal and notifies subscribers.
65
- const setValue = (newValue: any) => {
66
- if (current.event) {
67
- current.event.preventDefault();
68
- }
69
-
70
- if (value === newValue) {
71
- return;
72
- }
73
- value = newValue;
74
- // Notify all subscribers by invoking their callback functions.
75
- subscribers.forEach((subscriber) => subscriber());
76
- };
77
-
78
- const signal: signal = [getValue, setValue, subscribe, subscribers];
79
-
80
- if (current.component) {
81
- const SignalCalls = componentToSignalsWeakMap.get(current.component) as SignalCalls;
82
- SignalCalls.signals.push(signal);
83
- }
84
-
85
- return signal;
86
- }