ziko 1.1.1 → 1.2.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/dist/ziko.cjs +137 -114
- package/dist/ziko.js +137 -114
- package/dist/ziko.min.js +2 -2
- package/dist/ziko.mjs +137 -114
- package/package.json +1 -1
- package/src/__ziko__/__state__.js +10 -10
- package/src/hooks/use-state.js +30 -28
- package/src/hooks/use-storage.js +99 -80
package/dist/ziko.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/*
|
|
3
3
|
Project: ziko.js
|
|
4
4
|
Author: Zakaria Elalaoui
|
|
5
|
-
Date :
|
|
5
|
+
Date : Thu Jul 30 2026 14:31:45 GMT+0100 (UTC+01:00)
|
|
6
6
|
Git-Repo : https://github.com/zakarialaoui10/ziko.js
|
|
7
7
|
Git-Wiki : https://github.com/zakarialaoui10/ziko.js/wiki
|
|
8
8
|
Released under MIT License
|
|
@@ -1876,115 +1876,134 @@ class UseIPC {
|
|
|
1876
1876
|
const useIPC = (name) => new UseIPC(name);
|
|
1877
1877
|
|
|
1878
1878
|
class UseStorage {
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
globalKey,
|
|
1883
|
-
channel: use_channel ? useIPC(`Ziko:useStorage-${globalKey}`) : null,
|
|
1884
|
-
oldItemKeys: new Set()
|
|
1885
|
-
};
|
|
1879
|
+
static RESERVED_KEYS = new Set([
|
|
1880
|
+
"cache", "items", "set", "add", "remove", "get", "clear", "onStorageUpdated"
|
|
1881
|
+
]);
|
|
1886
1882
|
|
|
1887
|
-
|
|
1888
|
-
|
|
1883
|
+
constructor(storage, globalKey, initialValue, use_channel = true) {
|
|
1884
|
+
this.cache = {
|
|
1885
|
+
storage,
|
|
1886
|
+
globalKey,
|
|
1887
|
+
channel: use_channel ? useIPC(`Ziko:useStorage-${globalKey}`) : null,
|
|
1888
|
+
oldItemKeys: new Set()
|
|
1889
|
+
};
|
|
1889
1890
|
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
if (!raw) return {};
|
|
1893
|
-
try {
|
|
1894
|
-
return JSON.parse(raw);
|
|
1895
|
-
} catch {
|
|
1896
|
-
return {};
|
|
1897
|
-
}
|
|
1898
|
-
}
|
|
1891
|
+
this.#init(initialValue, use_channel);
|
|
1892
|
+
}
|
|
1899
1893
|
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
}
|
|
1908
|
-
#init(initialValue, use_channel) {
|
|
1909
|
-
if (use_channel && this.cache.channel) this.cache.channel.on("Ziko-Storage-Updated", () => this.#maintain());
|
|
1910
|
-
if (!initialValue) {
|
|
1911
|
-
this.#maintain();
|
|
1912
|
-
return;
|
|
1913
|
-
}
|
|
1914
|
-
if (this.cache.storage.getItem(this.cache.globalKey)) {
|
|
1915
|
-
const existing = this.items;
|
|
1916
|
-
Object.keys(existing).forEach(k => this.cache.oldItemKeys.add(k));
|
|
1917
|
-
this.#maintain();
|
|
1918
|
-
}
|
|
1919
|
-
else this.set(initialValue);
|
|
1894
|
+
get items() {
|
|
1895
|
+
const raw = this.cache.storage.getItem(this.cache.globalKey);
|
|
1896
|
+
if (!raw) return {};
|
|
1897
|
+
try {
|
|
1898
|
+
return JSON.parse(raw);
|
|
1899
|
+
} catch {
|
|
1900
|
+
return {};
|
|
1920
1901
|
}
|
|
1902
|
+
}
|
|
1921
1903
|
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
this.#maintain();
|
|
1926
|
-
return this;
|
|
1927
|
-
}
|
|
1904
|
+
#maintain() {
|
|
1905
|
+
const currentItems = this.items;
|
|
1906
|
+
const currentKeys = new Set(Object.keys(currentItems));
|
|
1928
1907
|
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1908
|
+
// Cleanup keys that were removed
|
|
1909
|
+
this.cache.oldItemKeys.forEach(key => {
|
|
1910
|
+
if (!currentKeys.has(key)) {
|
|
1911
|
+
delete this[key];
|
|
1912
|
+
this.cache.oldItemKeys.delete(key);
|
|
1913
|
+
}
|
|
1914
|
+
});
|
|
1915
|
+
|
|
1916
|
+
// Populate keys from storage onto `this`
|
|
1917
|
+
for (const key in currentItems) {
|
|
1918
|
+
if (!UseStorage.RESERVED_KEYS.has(key)) {
|
|
1919
|
+
this[key] = currentItems[key];
|
|
1920
|
+
this.cache.oldItemKeys.add(key);
|
|
1921
|
+
}
|
|
1935
1922
|
}
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
this.cache.oldItemKeys.delete(key);
|
|
1942
|
-
});
|
|
1943
|
-
this.set(items);
|
|
1944
|
-
return this;
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
#init(initialValue, use_channel) {
|
|
1926
|
+
if (use_channel && this.cache.channel) {
|
|
1927
|
+
this.cache.channel.on("Ziko-Storage-Updated", () => this.#maintain());
|
|
1945
1928
|
}
|
|
1946
|
-
|
|
1947
|
-
|
|
1929
|
+
|
|
1930
|
+
const hasStoredData = this.cache.storage.getItem(this.cache.globalKey) !== null;
|
|
1931
|
+
|
|
1932
|
+
if (hasStoredData) {
|
|
1933
|
+
// Key already exists in storage -> Restore data to instance
|
|
1934
|
+
this.#maintain();
|
|
1935
|
+
} else if (initialValue !== undefined) {
|
|
1936
|
+
// Key doesn't exist -> Seed with initialValue
|
|
1937
|
+
this.set(initialValue);
|
|
1938
|
+
} else {
|
|
1939
|
+
// Key doesn't exist and no initialValue provided -> Default to empty
|
|
1940
|
+
this.#maintain();
|
|
1948
1941
|
}
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1942
|
+
}
|
|
1943
|
+
|
|
1944
|
+
set(data) {
|
|
1945
|
+
this.cache.storage.setItem(this.cache.globalKey, JSON.stringify(data));
|
|
1946
|
+
if (this.cache.channel) {
|
|
1947
|
+
this.cache.channel.emit("Ziko-Storage-Updated", data);
|
|
1955
1948
|
}
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1949
|
+
this.#maintain();
|
|
1950
|
+
return this;
|
|
1951
|
+
}
|
|
1952
|
+
|
|
1953
|
+
add(data) {
|
|
1954
|
+
return this.set({
|
|
1955
|
+
...this.items,
|
|
1956
|
+
...data
|
|
1957
|
+
});
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
remove(...keys) {
|
|
1961
|
+
const items = { ...this.items };
|
|
1962
|
+
keys.forEach(key => delete items[key]);
|
|
1963
|
+
return this.set(items);
|
|
1964
|
+
}
|
|
1965
|
+
|
|
1966
|
+
get(key) {
|
|
1967
|
+
return this.items[key];
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
clear() {
|
|
1971
|
+
this.cache.storage.removeItem(this.cache.globalKey);
|
|
1972
|
+
this.#maintain();
|
|
1973
|
+
return this;
|
|
1974
|
+
}
|
|
1975
|
+
|
|
1976
|
+
onStorageUpdated(callback) {
|
|
1977
|
+
if (this.cache.channel) {
|
|
1978
|
+
this.cache.channel.on("Ziko-Storage-Updated", callback);
|
|
1961
1979
|
}
|
|
1980
|
+
return this;
|
|
1981
|
+
}
|
|
1962
1982
|
}
|
|
1963
1983
|
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
new UseStorage(localStorage, key, initialValue, use_channel);
|
|
1984
|
+
const useLocalStorage = (key, initialValue, use_channel = true) =>
|
|
1985
|
+
new UseStorage(localStorage, key, initialValue, use_channel);
|
|
1967
1986
|
|
|
1968
1987
|
const useSessionStorage = (key, initialValue, use_channel = true) =>
|
|
1969
|
-
|
|
1988
|
+
new UseStorage(sessionStorage, key, initialValue, use_channel);
|
|
1970
1989
|
|
|
1971
1990
|
var __State__ = {
|
|
1972
1991
|
store : new Map(),
|
|
1973
1992
|
index : 0,
|
|
1974
1993
|
session_storage : null,
|
|
1975
1994
|
register: function(state){
|
|
1976
|
-
if(!
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
}
|
|
1982
|
-
this.store.set(this.index++, state)
|
|
1995
|
+
// if(!import.meta?.env?.SSR && import.meta?.env?.DEV){
|
|
1996
|
+
// if(!this.session) this.session_storage = useSessionStorage('ziko-state', {})
|
|
1997
|
+
// const savedValue = this.session_storage.get(this.index)
|
|
1998
|
+
// if(!savedValue) this.session_storage.add({[this.index] : state.value});
|
|
1999
|
+
// else state.value = savedValue
|
|
2000
|
+
// }
|
|
2001
|
+
// this.store.set(this.index++, state)
|
|
1983
2002
|
},
|
|
1984
2003
|
update: function(index, value){
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
2004
|
+
// if(!import.meta?.env?.SSR && import.meta?.env?.DEV){
|
|
2005
|
+
// this.session_storage.add({[index] : value})
|
|
2006
|
+
// }
|
|
1988
2007
|
},
|
|
1989
2008
|
|
|
1990
2009
|
};
|
|
@@ -2161,57 +2180,61 @@ var LifecycleMethods = /*#__PURE__*/Object.freeze({
|
|
|
2161
2180
|
unmount: unmount
|
|
2162
2181
|
});
|
|
2163
2182
|
|
|
2164
|
-
|
|
2183
|
+
// import { __init__global__ } from "../__ziko__/index.js";
|
|
2184
|
+
|
|
2185
|
+
// if (!globalThis.__Ziko__) __init__global__();
|
|
2165
2186
|
|
|
2166
2187
|
function useState(initialValue) {
|
|
2167
2188
|
|
|
2168
|
-
const
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
});
|
|
2174
|
-
|
|
2175
|
-
let current = store.get(index);
|
|
2189
|
+
const state = {
|
|
2190
|
+
value: initialValue,
|
|
2191
|
+
subscribers: new Set(),
|
|
2192
|
+
paused: false,
|
|
2193
|
+
};
|
|
2176
2194
|
|
|
2177
2195
|
function getValue() {
|
|
2178
2196
|
return {
|
|
2179
|
-
value:
|
|
2197
|
+
value: state.value,
|
|
2180
2198
|
isStateGetter: () => true,
|
|
2181
|
-
_subscribe: (fn) =>
|
|
2199
|
+
_subscribe: (fn) => {
|
|
2200
|
+
state.subscribers.add(fn);
|
|
2201
|
+
return () => state.subscribers.delete(fn);
|
|
2202
|
+
},
|
|
2182
2203
|
};
|
|
2183
2204
|
}
|
|
2184
2205
|
|
|
2185
2206
|
function setValue(newValue) {
|
|
2186
|
-
if (
|
|
2207
|
+
if (state.paused) return;
|
|
2208
|
+
|
|
2187
2209
|
if (typeof newValue === "function") {
|
|
2188
|
-
newValue = newValue(
|
|
2210
|
+
newValue = newValue(state.value);
|
|
2189
2211
|
}
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2212
|
+
|
|
2213
|
+
if (!Object.is(newValue, state.value)) {
|
|
2214
|
+
state.value = newValue;
|
|
2215
|
+
state.subscribers.forEach((fn) => fn(state.value));
|
|
2194
2216
|
}
|
|
2195
2217
|
}
|
|
2196
2218
|
|
|
2197
2219
|
const controller = {
|
|
2198
|
-
pause: () => {
|
|
2199
|
-
resume: () => {
|
|
2200
|
-
clear: () => {
|
|
2220
|
+
pause: () => { state.paused = true; },
|
|
2221
|
+
resume: () => { state.paused = false; },
|
|
2222
|
+
clear: () => { state.subscribers.clear(); },
|
|
2201
2223
|
force: (newValue) => {
|
|
2202
|
-
if (typeof newValue === "function")
|
|
2203
|
-
|
|
2204
|
-
|
|
2224
|
+
if (typeof newValue === "function") {
|
|
2225
|
+
newValue = newValue(state.value);
|
|
2226
|
+
}
|
|
2227
|
+
state.value = newValue;
|
|
2228
|
+
state.subscribers.forEach((fn) => fn(state.value));
|
|
2205
2229
|
},
|
|
2206
|
-
getSubscribers: () => new Set(
|
|
2230
|
+
getSubscribers: () => new Set(state.subscribers),
|
|
2207
2231
|
};
|
|
2208
2232
|
|
|
2209
2233
|
return [getValue, setValue, controller];
|
|
2210
2234
|
}
|
|
2211
2235
|
|
|
2212
|
-
|
|
2213
2236
|
const isStateGetter = (arg) => {
|
|
2214
|
-
return typeof arg ===
|
|
2237
|
+
return typeof arg === "function" && arg?.()?.isStateGetter?.() === true;
|
|
2215
2238
|
};
|
|
2216
2239
|
|
|
2217
2240
|
const camel2hyphencase = (text = '') => text.replace(/[A-Z]/g, match => '-' + match.toLowerCase());
|
|
@@ -5077,7 +5100,7 @@ exports.trunc = trunc;
|
|
|
5077
5100
|
exports.useDerived = useDerived;
|
|
5078
5101
|
exports.useEventEmitter = useEventEmitter;
|
|
5079
5102
|
exports.useIPC = useIPC;
|
|
5080
|
-
exports.
|
|
5103
|
+
exports.useLocalStorage = useLocalStorage;
|
|
5081
5104
|
exports.useMediaQuery = useMediaQuery;
|
|
5082
5105
|
exports.useQueryParams = useQueryParams;
|
|
5083
5106
|
exports.useReactive = useReactive;
|
package/dist/ziko.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/*
|
|
3
3
|
Project: ziko.js
|
|
4
4
|
Author: Zakaria Elalaoui
|
|
5
|
-
Date :
|
|
5
|
+
Date : Thu Jul 30 2026 14:31:45 GMT+0100 (UTC+01:00)
|
|
6
6
|
Git-Repo : https://github.com/zakarialaoui10/ziko.js
|
|
7
7
|
Git-Wiki : https://github.com/zakarialaoui10/ziko.js/wiki
|
|
8
8
|
Released under MIT License
|
|
@@ -1880,115 +1880,134 @@
|
|
|
1880
1880
|
const useIPC = (name) => new UseIPC(name);
|
|
1881
1881
|
|
|
1882
1882
|
class UseStorage {
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
globalKey,
|
|
1887
|
-
channel: use_channel ? useIPC(`Ziko:useStorage-${globalKey}`) : null,
|
|
1888
|
-
oldItemKeys: new Set()
|
|
1889
|
-
};
|
|
1883
|
+
static RESERVED_KEYS = new Set([
|
|
1884
|
+
"cache", "items", "set", "add", "remove", "get", "clear", "onStorageUpdated"
|
|
1885
|
+
]);
|
|
1890
1886
|
|
|
1891
|
-
|
|
1892
|
-
|
|
1887
|
+
constructor(storage, globalKey, initialValue, use_channel = true) {
|
|
1888
|
+
this.cache = {
|
|
1889
|
+
storage,
|
|
1890
|
+
globalKey,
|
|
1891
|
+
channel: use_channel ? useIPC(`Ziko:useStorage-${globalKey}`) : null,
|
|
1892
|
+
oldItemKeys: new Set()
|
|
1893
|
+
};
|
|
1893
1894
|
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
if (!raw) return {};
|
|
1897
|
-
try {
|
|
1898
|
-
return JSON.parse(raw);
|
|
1899
|
-
} catch {
|
|
1900
|
-
return {};
|
|
1901
|
-
}
|
|
1902
|
-
}
|
|
1895
|
+
this.#init(initialValue, use_channel);
|
|
1896
|
+
}
|
|
1903
1897
|
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
}
|
|
1912
|
-
#init(initialValue, use_channel) {
|
|
1913
|
-
if (use_channel && this.cache.channel) this.cache.channel.on("Ziko-Storage-Updated", () => this.#maintain());
|
|
1914
|
-
if (!initialValue) {
|
|
1915
|
-
this.#maintain();
|
|
1916
|
-
return;
|
|
1917
|
-
}
|
|
1918
|
-
if (this.cache.storage.getItem(this.cache.globalKey)) {
|
|
1919
|
-
const existing = this.items;
|
|
1920
|
-
Object.keys(existing).forEach(k => this.cache.oldItemKeys.add(k));
|
|
1921
|
-
this.#maintain();
|
|
1922
|
-
}
|
|
1923
|
-
else this.set(initialValue);
|
|
1898
|
+
get items() {
|
|
1899
|
+
const raw = this.cache.storage.getItem(this.cache.globalKey);
|
|
1900
|
+
if (!raw) return {};
|
|
1901
|
+
try {
|
|
1902
|
+
return JSON.parse(raw);
|
|
1903
|
+
} catch {
|
|
1904
|
+
return {};
|
|
1924
1905
|
}
|
|
1906
|
+
}
|
|
1925
1907
|
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
this.#maintain();
|
|
1930
|
-
return this;
|
|
1931
|
-
}
|
|
1908
|
+
#maintain() {
|
|
1909
|
+
const currentItems = this.items;
|
|
1910
|
+
const currentKeys = new Set(Object.keys(currentItems));
|
|
1932
1911
|
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1912
|
+
// Cleanup keys that were removed
|
|
1913
|
+
this.cache.oldItemKeys.forEach(key => {
|
|
1914
|
+
if (!currentKeys.has(key)) {
|
|
1915
|
+
delete this[key];
|
|
1916
|
+
this.cache.oldItemKeys.delete(key);
|
|
1917
|
+
}
|
|
1918
|
+
});
|
|
1919
|
+
|
|
1920
|
+
// Populate keys from storage onto `this`
|
|
1921
|
+
for (const key in currentItems) {
|
|
1922
|
+
if (!UseStorage.RESERVED_KEYS.has(key)) {
|
|
1923
|
+
this[key] = currentItems[key];
|
|
1924
|
+
this.cache.oldItemKeys.add(key);
|
|
1925
|
+
}
|
|
1939
1926
|
}
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
this.cache.oldItemKeys.delete(key);
|
|
1946
|
-
});
|
|
1947
|
-
this.set(items);
|
|
1948
|
-
return this;
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1929
|
+
#init(initialValue, use_channel) {
|
|
1930
|
+
if (use_channel && this.cache.channel) {
|
|
1931
|
+
this.cache.channel.on("Ziko-Storage-Updated", () => this.#maintain());
|
|
1949
1932
|
}
|
|
1950
|
-
|
|
1951
|
-
|
|
1933
|
+
|
|
1934
|
+
const hasStoredData = this.cache.storage.getItem(this.cache.globalKey) !== null;
|
|
1935
|
+
|
|
1936
|
+
if (hasStoredData) {
|
|
1937
|
+
// Key already exists in storage -> Restore data to instance
|
|
1938
|
+
this.#maintain();
|
|
1939
|
+
} else if (initialValue !== undefined) {
|
|
1940
|
+
// Key doesn't exist -> Seed with initialValue
|
|
1941
|
+
this.set(initialValue);
|
|
1942
|
+
} else {
|
|
1943
|
+
// Key doesn't exist and no initialValue provided -> Default to empty
|
|
1944
|
+
this.#maintain();
|
|
1952
1945
|
}
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1946
|
+
}
|
|
1947
|
+
|
|
1948
|
+
set(data) {
|
|
1949
|
+
this.cache.storage.setItem(this.cache.globalKey, JSON.stringify(data));
|
|
1950
|
+
if (this.cache.channel) {
|
|
1951
|
+
this.cache.channel.emit("Ziko-Storage-Updated", data);
|
|
1959
1952
|
}
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1953
|
+
this.#maintain();
|
|
1954
|
+
return this;
|
|
1955
|
+
}
|
|
1956
|
+
|
|
1957
|
+
add(data) {
|
|
1958
|
+
return this.set({
|
|
1959
|
+
...this.items,
|
|
1960
|
+
...data
|
|
1961
|
+
});
|
|
1962
|
+
}
|
|
1963
|
+
|
|
1964
|
+
remove(...keys) {
|
|
1965
|
+
const items = { ...this.items };
|
|
1966
|
+
keys.forEach(key => delete items[key]);
|
|
1967
|
+
return this.set(items);
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
get(key) {
|
|
1971
|
+
return this.items[key];
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
clear() {
|
|
1975
|
+
this.cache.storage.removeItem(this.cache.globalKey);
|
|
1976
|
+
this.#maintain();
|
|
1977
|
+
return this;
|
|
1978
|
+
}
|
|
1979
|
+
|
|
1980
|
+
onStorageUpdated(callback) {
|
|
1981
|
+
if (this.cache.channel) {
|
|
1982
|
+
this.cache.channel.on("Ziko-Storage-Updated", callback);
|
|
1965
1983
|
}
|
|
1984
|
+
return this;
|
|
1985
|
+
}
|
|
1966
1986
|
}
|
|
1967
1987
|
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
new UseStorage(localStorage, key, initialValue, use_channel);
|
|
1988
|
+
const useLocalStorage = (key, initialValue, use_channel = true) =>
|
|
1989
|
+
new UseStorage(localStorage, key, initialValue, use_channel);
|
|
1971
1990
|
|
|
1972
1991
|
const useSessionStorage = (key, initialValue, use_channel = true) =>
|
|
1973
|
-
|
|
1992
|
+
new UseStorage(sessionStorage, key, initialValue, use_channel);
|
|
1974
1993
|
|
|
1975
1994
|
var __State__ = {
|
|
1976
1995
|
store : new Map(),
|
|
1977
1996
|
index : 0,
|
|
1978
1997
|
session_storage : null,
|
|
1979
1998
|
register: function(state){
|
|
1980
|
-
if(!
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
}
|
|
1986
|
-
this.store.set(this.index++, state)
|
|
1999
|
+
// if(!import.meta?.env?.SSR && import.meta?.env?.DEV){
|
|
2000
|
+
// if(!this.session) this.session_storage = useSessionStorage('ziko-state', {})
|
|
2001
|
+
// const savedValue = this.session_storage.get(this.index)
|
|
2002
|
+
// if(!savedValue) this.session_storage.add({[this.index] : state.value});
|
|
2003
|
+
// else state.value = savedValue
|
|
2004
|
+
// }
|
|
2005
|
+
// this.store.set(this.index++, state)
|
|
1987
2006
|
},
|
|
1988
2007
|
update: function(index, value){
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
2008
|
+
// if(!import.meta?.env?.SSR && import.meta?.env?.DEV){
|
|
2009
|
+
// this.session_storage.add({[index] : value})
|
|
2010
|
+
// }
|
|
1992
2011
|
},
|
|
1993
2012
|
|
|
1994
2013
|
};
|
|
@@ -2165,57 +2184,61 @@
|
|
|
2165
2184
|
unmount: unmount
|
|
2166
2185
|
});
|
|
2167
2186
|
|
|
2168
|
-
|
|
2187
|
+
// import { __init__global__ } from "../__ziko__/index.js";
|
|
2188
|
+
|
|
2189
|
+
// if (!globalThis.__Ziko__) __init__global__();
|
|
2169
2190
|
|
|
2170
2191
|
function useState(initialValue) {
|
|
2171
2192
|
|
|
2172
|
-
const
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
});
|
|
2178
|
-
|
|
2179
|
-
let current = store.get(index);
|
|
2193
|
+
const state = {
|
|
2194
|
+
value: initialValue,
|
|
2195
|
+
subscribers: new Set(),
|
|
2196
|
+
paused: false,
|
|
2197
|
+
};
|
|
2180
2198
|
|
|
2181
2199
|
function getValue() {
|
|
2182
2200
|
return {
|
|
2183
|
-
value:
|
|
2201
|
+
value: state.value,
|
|
2184
2202
|
isStateGetter: () => true,
|
|
2185
|
-
_subscribe: (fn) =>
|
|
2203
|
+
_subscribe: (fn) => {
|
|
2204
|
+
state.subscribers.add(fn);
|
|
2205
|
+
return () => state.subscribers.delete(fn);
|
|
2206
|
+
},
|
|
2186
2207
|
};
|
|
2187
2208
|
}
|
|
2188
2209
|
|
|
2189
2210
|
function setValue(newValue) {
|
|
2190
|
-
if (
|
|
2211
|
+
if (state.paused) return;
|
|
2212
|
+
|
|
2191
2213
|
if (typeof newValue === "function") {
|
|
2192
|
-
newValue = newValue(
|
|
2214
|
+
newValue = newValue(state.value);
|
|
2193
2215
|
}
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2216
|
+
|
|
2217
|
+
if (!Object.is(newValue, state.value)) {
|
|
2218
|
+
state.value = newValue;
|
|
2219
|
+
state.subscribers.forEach((fn) => fn(state.value));
|
|
2198
2220
|
}
|
|
2199
2221
|
}
|
|
2200
2222
|
|
|
2201
2223
|
const controller = {
|
|
2202
|
-
pause: () => {
|
|
2203
|
-
resume: () => {
|
|
2204
|
-
clear: () => {
|
|
2224
|
+
pause: () => { state.paused = true; },
|
|
2225
|
+
resume: () => { state.paused = false; },
|
|
2226
|
+
clear: () => { state.subscribers.clear(); },
|
|
2205
2227
|
force: (newValue) => {
|
|
2206
|
-
if (typeof newValue === "function")
|
|
2207
|
-
|
|
2208
|
-
|
|
2228
|
+
if (typeof newValue === "function") {
|
|
2229
|
+
newValue = newValue(state.value);
|
|
2230
|
+
}
|
|
2231
|
+
state.value = newValue;
|
|
2232
|
+
state.subscribers.forEach((fn) => fn(state.value));
|
|
2209
2233
|
},
|
|
2210
|
-
getSubscribers: () => new Set(
|
|
2234
|
+
getSubscribers: () => new Set(state.subscribers),
|
|
2211
2235
|
};
|
|
2212
2236
|
|
|
2213
2237
|
return [getValue, setValue, controller];
|
|
2214
2238
|
}
|
|
2215
2239
|
|
|
2216
|
-
|
|
2217
2240
|
const isStateGetter = (arg) => {
|
|
2218
|
-
return typeof arg ===
|
|
2241
|
+
return typeof arg === "function" && arg?.()?.isStateGetter?.() === true;
|
|
2219
2242
|
};
|
|
2220
2243
|
|
|
2221
2244
|
const camel2hyphencase = (text = '') => text.replace(/[A-Z]/g, match => '-' + match.toLowerCase());
|
|
@@ -5081,7 +5104,7 @@
|
|
|
5081
5104
|
exports.useDerived = useDerived;
|
|
5082
5105
|
exports.useEventEmitter = useEventEmitter;
|
|
5083
5106
|
exports.useIPC = useIPC;
|
|
5084
|
-
exports.
|
|
5107
|
+
exports.useLocalStorage = useLocalStorage;
|
|
5085
5108
|
exports.useMediaQuery = useMediaQuery;
|
|
5086
5109
|
exports.useQueryParams = useQueryParams;
|
|
5087
5110
|
exports.useReactive = useReactive;
|