valyrian.js 7.2.11 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/dist/flux-store/index.d.ts +32 -0
- package/dist/flux-store/index.d.ts.map +1 -0
- package/dist/flux-store/index.js +258 -0
- package/dist/flux-store/index.js.map +7 -0
- package/dist/flux-store/index.min.js +1 -0
- package/dist/flux-store/index.min.js.map +1 -0
- package/dist/flux-store/index.mjs +237 -0
- package/dist/flux-store/index.mjs.map +7 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +42 -75
- package/dist/hooks/index.js.map +2 -2
- package/dist/hooks/index.min.js +1 -0
- package/dist/hooks/index.min.js.map +1 -0
- package/dist/hooks/index.mjs +43 -76
- package/dist/hooks/index.mjs.map +2 -2
- package/dist/index.d.ts +52 -54
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +397 -328
- package/dist/index.js.map +3 -3
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.mjs +397 -328
- package/dist/index.mjs.map +3 -3
- package/dist/native-store/index.d.ts +14 -0
- package/dist/native-store/index.d.ts.map +1 -0
- package/dist/native-store/index.js +103 -0
- package/dist/native-store/index.js.map +7 -0
- package/dist/native-store/index.min.js +1 -0
- package/dist/native-store/index.min.js.map +1 -0
- package/dist/native-store/index.mjs +82 -0
- package/dist/native-store/index.mjs.map +7 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +223 -86
- package/dist/node/index.js.map +4 -4
- package/dist/node/index.mjs +223 -86
- package/dist/node/index.mjs.map +4 -4
- package/dist/node/node.sw.js +152 -0
- package/dist/node/utils/inline.d.ts.map +1 -1
- package/dist/node/utils/node.sw.js +152 -0
- package/dist/node/utils/session-storage.d.ts +22 -0
- package/dist/node/utils/session-storage.d.ts.map +1 -0
- package/dist/node/utils/sw.d.ts.map +1 -1
- package/dist/node/utils/tree-adapter.d.ts +9 -0
- package/dist/node/utils/tree-adapter.d.ts.map +1 -1
- package/dist/pulse-store/index.d.ts +13 -0
- package/dist/pulse-store/index.d.ts.map +1 -0
- package/dist/pulse-store/index.js +143 -0
- package/dist/pulse-store/index.js.map +7 -0
- package/dist/pulse-store/index.min.js +1 -0
- package/dist/pulse-store/index.min.js.map +1 -0
- package/dist/pulse-store/index.mjs +122 -0
- package/dist/pulse-store/index.mjs.map +7 -0
- package/dist/request/index.d.ts.map +1 -1
- package/dist/request/index.js +68 -89
- package/dist/request/index.js.map +2 -2
- package/dist/request/index.min.js +1 -0
- package/dist/request/index.min.js.map +1 -0
- package/dist/request/index.mjs +68 -89
- package/dist/request/index.mjs.map +2 -2
- package/dist/router/index.d.ts +32 -31
- package/dist/router/index.d.ts.map +1 -1
- package/dist/router/index.js +256 -104
- package/dist/router/index.js.map +3 -3
- package/dist/router/index.min.js +1 -0
- package/dist/router/index.min.js.map +1 -0
- package/dist/router/index.mjs +256 -104
- package/dist/router/index.mjs.map +3 -3
- package/dist/signals/index.d.ts +6 -0
- package/dist/signals/index.d.ts.map +1 -0
- package/dist/signals/index.js +92 -0
- package/dist/signals/index.js.map +7 -0
- package/dist/signals/index.min.js +1 -0
- package/dist/signals/index.min.js.map +1 -0
- package/dist/signals/index.mjs +71 -0
- package/dist/signals/index.mjs.map +7 -0
- package/dist/suspense/index.d.ts +6 -0
- package/dist/suspense/index.d.ts.map +1 -0
- package/dist/suspense/index.js +67 -0
- package/dist/suspense/index.js.map +7 -0
- package/dist/suspense/index.min.js +1 -0
- package/dist/suspense/index.min.js.map +1 -0
- package/dist/suspense/index.mjs +46 -0
- package/dist/suspense/index.mjs.map +7 -0
- package/dist/sw/index.min.js +1 -0
- package/dist/sw/index.min.js.map +1 -0
- package/dist/translate/index.d.ts +19 -0
- package/dist/translate/index.d.ts.map +1 -0
- package/dist/translate/index.js +150 -0
- package/dist/translate/index.js.map +7 -0
- package/dist/translate/index.min.js +1 -0
- package/dist/translate/index.min.js.map +1 -0
- package/dist/translate/index.mjs +129 -0
- package/dist/translate/index.mjs.map +7 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/deep-freeze.d.ts +3 -0
- package/dist/utils/deep-freeze.d.ts.map +1 -0
- package/dist/utils/getter-setter.d.ts +3 -0
- package/dist/utils/getter-setter.d.ts.map +1 -0
- package/dist/utils/has-changed.d.ts +2 -0
- package/dist/utils/has-changed.d.ts.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +138 -0
- package/dist/utils/index.js.map +7 -0
- package/dist/utils/index.min.js +1 -0
- package/dist/utils/index.min.js.map +1 -0
- package/dist/utils/index.mjs +115 -0
- package/dist/utils/index.mjs.map +7 -0
- package/lib/flux-store/index.ts +301 -0
- package/lib/hooks/index.ts +52 -101
- package/lib/index.ts +479 -719
- package/lib/native-store/index.ts +106 -0
- package/lib/node/index.ts +5 -3
- package/lib/node/utils/icons.ts +5 -5
- package/lib/node/utils/inline.ts +17 -17
- package/lib/node/utils/node.sw.js +152 -0
- package/lib/node/utils/session-storage.ts +117 -0
- package/lib/node/utils/sw.ts +35 -11
- package/lib/node/utils/tree-adapter.ts +99 -52
- package/lib/pulse-store/index.ts +181 -0
- package/lib/request/index.ts +86 -116
- package/lib/router/index.ts +358 -170
- package/lib/signals/index.ts +98 -0
- package/lib/suspense/index.ts +57 -0
- package/lib/translate/index.ts +156 -0
- package/lib/utils/deep-freeze.ts +54 -0
- package/lib/utils/getter-setter.ts +40 -0
- package/lib/utils/has-changed.ts +43 -0
- package/lib/utils/index.ts +3 -0
- package/package.json +38 -50
- package/tsconfig.json +1 -1
- package/dist/dataset/index.d.ts +0 -24
- package/dist/dataset/index.d.ts.map +0 -1
- package/dist/dataset/index.js +0 -178
- package/dist/dataset/index.js.map +0 -7
- package/dist/dataset/index.mjs +0 -157
- package/dist/dataset/index.mjs.map +0 -7
- package/dist/node/node.sw.tpl +0 -133
- package/dist/node/utils/node.sw.tpl +0 -133
- package/dist/proxy-signal/index.d.ts +0 -23
- package/dist/proxy-signal/index.d.ts.map +0 -1
- package/dist/proxy-signal/index.js +0 -138
- package/dist/proxy-signal/index.js.map +0 -7
- package/dist/proxy-signal/index.mjs +0 -117
- package/dist/proxy-signal/index.mjs.map +0 -7
- package/dist/signal/index.d.ts +0 -20
- package/dist/signal/index.d.ts.map +0 -1
- package/dist/signal/index.js +0 -95
- package/dist/signal/index.js.map +0 -7
- package/dist/signal/index.mjs +0 -74
- package/dist/signal/index.mjs.map +0 -7
- package/dist/store/index.d.ts +0 -16
- package/dist/store/index.d.ts.map +0 -1
- package/dist/store/index.js +0 -93
- package/dist/store/index.js.map +0 -7
- package/dist/store/index.mjs +0 -72
- package/dist/store/index.mjs.map +0 -7
- package/lib/dataset/index.ts +0 -193
- package/lib/index.d.ts +0 -0
- package/lib/interfaces.ts.bak +0 -141
- package/lib/node/utils/node.sw.tpl +0 -133
- package/lib/proxy-signal/index.ts +0 -187
- package/lib/signal/index.ts +0 -161
- package/lib/store/index.ts +0 -101
|
@@ -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
|
-
let computed = ProxySignal(() => handler(signal.value));
|
|
69
|
-
let 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
|
-
let subscriptions = new Map();
|
|
86
|
-
let getters: Getters = {};
|
|
87
|
-
|
|
88
|
-
let forceUpdate = false;
|
|
89
|
-
|
|
90
|
-
let 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
|
-
let 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
|
-
let old = state[prop];
|
|
132
|
-
state[prop] = val;
|
|
133
|
-
if (prop === "value" && (forceUpdate || val !== old)) {
|
|
134
|
-
forceUpdate = false;
|
|
135
|
-
for (let [handler, computed] of subscriptions) {
|
|
136
|
-
computed.cleanup();
|
|
137
|
-
let 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 (let [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
|
-
}
|
package/lib/signal/index.ts
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import { VnodeWithDom, current, onUnmount, updateVnode, v } from "valyrian.js";
|
|
2
|
-
|
|
3
|
-
interface GetterInterface {
|
|
4
|
-
(): any;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
interface SetterInterface {
|
|
8
|
-
(value: any): void;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
interface SubscribeInterface {
|
|
12
|
-
(callback: Function): void;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface SubscriptionsInterface extends Array<Function> {}
|
|
16
|
-
|
|
17
|
-
export interface SignalInterface extends Array<any> {
|
|
18
|
-
0: GetterInterface;
|
|
19
|
-
1: SetterInterface;
|
|
20
|
-
2: SubscribeInterface;
|
|
21
|
-
3: SubscriptionsInterface;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
25
|
-
export function Signal(initialValue: any): SignalInterface {
|
|
26
|
-
// Create a copy of the current context object
|
|
27
|
-
const { vnode, component } = { ...current };
|
|
28
|
-
|
|
29
|
-
// Check if the context object has a vnode property
|
|
30
|
-
if (vnode && component) {
|
|
31
|
-
// Is first call
|
|
32
|
-
if (!vnode.components) {
|
|
33
|
-
// Set the components property to an empty array
|
|
34
|
-
vnode.components = [];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Check if the components array of the vnode object does not contain the component object
|
|
38
|
-
if (vnode.components.indexOf(component) === -1) {
|
|
39
|
-
// Set the calls property to -1
|
|
40
|
-
vnode.signal_calls = -1;
|
|
41
|
-
// Add the component to the components array
|
|
42
|
-
vnode.components.push(component);
|
|
43
|
-
|
|
44
|
-
// Check if the component object has a signals property
|
|
45
|
-
if (!component.signals) {
|
|
46
|
-
// Set the signals property of the component object to an empty array
|
|
47
|
-
component.signals = [];
|
|
48
|
-
// Add a function to the cleanup stack that removes the signals property from the component object
|
|
49
|
-
onUnmount(() => Reflect.deleteProperty(component, "signals"));
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Assign the signal variable to the signal stored at the index of the vnode object's calls property in the vnode's signals array
|
|
54
|
-
let signal: SignalInterface = component.signals[++vnode.signal_calls];
|
|
55
|
-
|
|
56
|
-
// If a signal has already been assigned to the signal variable, return it
|
|
57
|
-
if (signal) {
|
|
58
|
-
// Remove all subscriptions because we come from a new render
|
|
59
|
-
signal[3].length = 0;
|
|
60
|
-
|
|
61
|
-
// Return the signal
|
|
62
|
-
return signal;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Declare a variable to store the current value of the Signal
|
|
67
|
-
let value = initialValue;
|
|
68
|
-
|
|
69
|
-
// Create an array to store functions that have subscribed to changes to the Signal's value
|
|
70
|
-
const subscriptions: SubscriptionsInterface = [];
|
|
71
|
-
|
|
72
|
-
// Define a function that allows other parts of the code to subscribe to changes to the Signal's value
|
|
73
|
-
const subscribe = (callback: Function) => {
|
|
74
|
-
// Add the callback function to the subscriptions array if it is not already in the array
|
|
75
|
-
if (subscriptions.indexOf(callback) === -1) {
|
|
76
|
-
subscriptions.push(callback);
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
// Set the vnodes to update when the Signal's value changes
|
|
81
|
-
let vnodesToUpdate: Array<VnodeWithDom> = [];
|
|
82
|
-
|
|
83
|
-
// This is the function that will be called when the Signal's value changes
|
|
84
|
-
const updateVnodes = () => {
|
|
85
|
-
// Create a copy of the vnodesToUpdate array and filter out any duplicate vnodes
|
|
86
|
-
let vnodesToUpdateCopy = vnodesToUpdate.filter((vnode, index, self) => {
|
|
87
|
-
return self.findIndex((v) => v.dom === vnode.dom) === index;
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
// Loop through the vnodesToUpdate array
|
|
91
|
-
for (let i = 0, l = vnodesToUpdateCopy.length; i < l; i++) {
|
|
92
|
-
const vnode2 = vnodesToUpdateCopy[i];
|
|
93
|
-
// If it does, create a new vnode object based on the original vnode, its children, and its DOM and SVG properties
|
|
94
|
-
let newVnode = v(vnode2.tag, vnode2.props, ...vnode2.initialChildren) as VnodeWithDom;
|
|
95
|
-
newVnode.dom = vnode2.dom; // Set the new vnode object's DOM property to the old vnode object's DOM property
|
|
96
|
-
newVnode.isSVG = vnode2.isSVG; // Set the new vnode object's isSVG property to the old vnode object's isSVG property
|
|
97
|
-
|
|
98
|
-
// Update the vnode object
|
|
99
|
-
updateVnode(newVnode, vnode2);
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
// Define a function that returns the current value of the Signal
|
|
104
|
-
function get() {
|
|
105
|
-
// Get the current vnode from the context object
|
|
106
|
-
const { vnode: vnode2 } = current;
|
|
107
|
-
|
|
108
|
-
// If we have a current vnode, it means that a get function is being called from within a component
|
|
109
|
-
// so we subscribe the vnode to be updated when the Signal's value changes
|
|
110
|
-
if (vnode2 && vnodesToUpdate.indexOf(vnode2) === -1) {
|
|
111
|
-
// We set the initialChildren to a copy of the vnode's children array
|
|
112
|
-
// This is the case when the vnode is a component that has not been rendered yet and we need the initial children
|
|
113
|
-
// because they could have the components that are using the Signal
|
|
114
|
-
if (!vnode2.initialChildren) {
|
|
115
|
-
vnode2.initialChildren = [...vnode2.children];
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Add the vnode to the vnodesToUpdate array
|
|
119
|
-
vnodesToUpdate.push(vnode2);
|
|
120
|
-
|
|
121
|
-
// Subscribe the updateVnodes function to the Signal
|
|
122
|
-
subscribe(updateVnodes);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Return the current value of the Signal
|
|
126
|
-
return value;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// Define a function that allows the value of the Signal to be updated and notifies any subscribed functions of the change
|
|
130
|
-
const set = (newValue: any) => {
|
|
131
|
-
// If we have a current event on going, prevent the default action
|
|
132
|
-
if (current.event) {
|
|
133
|
-
current.event.preventDefault();
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Just return if the new value is the same as the current value
|
|
137
|
-
if (newValue === value) {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Update the value of the Signal
|
|
142
|
-
value = newValue;
|
|
143
|
-
|
|
144
|
-
// Call each subscribed function with the new value of the Signal as an argument
|
|
145
|
-
for (let i = 0, l = subscriptions.length; i < l; i++) {
|
|
146
|
-
subscriptions[i](value);
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
// Assign the signal variable an array containing the get, set, and subscribe functions
|
|
151
|
-
let signal: SignalInterface = [get, set, subscribe, subscriptions];
|
|
152
|
-
|
|
153
|
-
// If the context object has a vnode property, add the signal to the vnode's signals array
|
|
154
|
-
// and add the subscriptions array to the vnode's subscriptions array
|
|
155
|
-
if (vnode && component) {
|
|
156
|
-
component.signals.push(signal);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Return the signal
|
|
160
|
-
return signal;
|
|
161
|
-
}
|
package/lib/store/index.ts
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { update } from "valyrian.js";
|
|
2
|
-
|
|
3
|
-
interface StoreOptions {
|
|
4
|
-
state?: Record<string, unknown> | (() => Record<string, unknown>);
|
|
5
|
-
getters?: Record<string, Function>;
|
|
6
|
-
mutations?: Record<string, Function>;
|
|
7
|
-
actions?: Record<string, Function>;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
interface StoreInstance {
|
|
11
|
-
// eslint-disable-next-line no-unused-vars
|
|
12
|
-
new (options: StoreOptions): StoreInstance;
|
|
13
|
-
state: Record<string, any>;
|
|
14
|
-
getters?: Record<string, any>;
|
|
15
|
-
// eslint-disable-next-line no-unused-vars
|
|
16
|
-
commit: (type: string, ...payload: any[]) => void;
|
|
17
|
-
// eslint-disable-next-line no-unused-vars
|
|
18
|
-
dispatch: (type: string, ...payload: any[]) => void;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function keyExists(typeOfKey: string, object: Record<string, unknown>, key: string) {
|
|
22
|
-
if (key in object === false) {
|
|
23
|
-
throw new Error(`The ${typeOfKey} "${key}" does not exists.`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function deepFreeze(obj: any) {
|
|
28
|
-
if (typeof obj === "object" && obj !== null && !Object.isFrozen(obj)) {
|
|
29
|
-
if (Array.isArray(obj)) {
|
|
30
|
-
for (let i = 0, l = obj.length; i < l; i++) {
|
|
31
|
-
deepFreeze(obj[i]);
|
|
32
|
-
}
|
|
33
|
-
} else {
|
|
34
|
-
let props = Reflect.ownKeys(obj);
|
|
35
|
-
for (let i = 0, l = props.length; i < l; i++) {
|
|
36
|
-
deepFreeze(obj[props[i]]);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
Object.freeze(obj);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return obj;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
let updateTimeout: any;
|
|
46
|
-
function delayedUpdate() {
|
|
47
|
-
clearTimeout(updateTimeout);
|
|
48
|
-
updateTimeout = setTimeout(update);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export const Store = function Store(
|
|
52
|
-
this: StoreInstance,
|
|
53
|
-
{ state = {}, getters = {}, actions = {}, mutations = {} }: StoreOptions = {}
|
|
54
|
-
) {
|
|
55
|
-
let frozen = true;
|
|
56
|
-
|
|
57
|
-
function isUnfrozen() {
|
|
58
|
-
if (frozen) {
|
|
59
|
-
throw new Error("You need to commit a mutation to change the state");
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
let localState = typeof state === "function" ? state() : state;
|
|
64
|
-
|
|
65
|
-
this.state = new Proxy(localState || {}, {
|
|
66
|
-
get: (state, prop: string) => deepFreeze(state[prop]),
|
|
67
|
-
set: (state, prop: string, value: any) => {
|
|
68
|
-
isUnfrozen();
|
|
69
|
-
state[prop] = value;
|
|
70
|
-
return true;
|
|
71
|
-
},
|
|
72
|
-
deleteProperty: (state, prop: string) => {
|
|
73
|
-
isUnfrozen();
|
|
74
|
-
Reflect.deleteProperty(state, prop);
|
|
75
|
-
return true;
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
this.getters = new Proxy(getters, {
|
|
80
|
-
get: (getters, getter: string) => {
|
|
81
|
-
try {
|
|
82
|
-
return getters[getter](this.state, this.getters);
|
|
83
|
-
} catch (e) {
|
|
84
|
-
// Getters should fail silently
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
this.commit = (mutation, ...args) => {
|
|
90
|
-
keyExists("mutation", mutations, mutation);
|
|
91
|
-
frozen = false;
|
|
92
|
-
mutations[mutation](this.state, ...args);
|
|
93
|
-
frozen = true;
|
|
94
|
-
delayedUpdate();
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
this.dispatch = (action, ...args) => {
|
|
98
|
-
keyExists("action", actions, action);
|
|
99
|
-
return Promise.resolve(actions[action](this, ...args));
|
|
100
|
-
};
|
|
101
|
-
} as unknown as StoreInstance;
|