ziko 0.49.1 → 0.49.3
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 +148 -150
- package/dist/ziko.js +148 -150
- package/dist/ziko.min.js +2 -2
- package/dist/ziko.mjs +148 -150
- package/package.json +1 -1
- package/src/__ziko__/__state__.js +16 -7
- package/src/hooks/use-state.js +7 -15
- package/src/time/delay/index.js +16 -2
- package/src/use/use-storage.js +4 -4
- package/types/time/clocks/clock.d.ts +28 -0
- package/types/time/clocks/index.d.ts +3 -0
- package/types/time/clocks/scheduler.d.ts +42 -0
- package/types/time/clocks/tick.d.ts +26 -0
- package/types/time/decorators/index.d.ts +9 -0
- package/types/time/delay/index.d.ts +12 -0
- package/types/time/index.d.ts +4 -1
- package/types/time/loop/index.d.ts +50 -0
- package/src/time/delay/sleep.js +0 -3
- package/src/time/delay/timeout.js +0 -15
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 : Tue Nov 25 2025 19:11:53 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
|
|
@@ -1185,17 +1185,143 @@
|
|
|
1185
1185
|
}
|
|
1186
1186
|
};
|
|
1187
1187
|
|
|
1188
|
+
class ZikoUseChannel{
|
|
1189
|
+
constructor(name = ""){
|
|
1190
|
+
this.channel = new BroadcastChannel(name);
|
|
1191
|
+
this.EVENTS_DATAS_PAIRS = new Map();
|
|
1192
|
+
this.EVENTS_HANDLERS_PAIRS = new Map();
|
|
1193
|
+
this.LAST_RECEIVED_EVENT = "";
|
|
1194
|
+
this.UUID="ziko-channel"+Random.string(10);
|
|
1195
|
+
this.SUBSCRIBERS = new Set([this.UUID]);
|
|
1196
|
+
}
|
|
1197
|
+
get broadcast(){
|
|
1198
|
+
// update receiver
|
|
1199
|
+
return this;
|
|
1200
|
+
}
|
|
1201
|
+
emit(event, data){
|
|
1202
|
+
this.EVENTS_DATAS_PAIRS.set(event,data);
|
|
1203
|
+
this.#maintainEmit(event);
|
|
1204
|
+
return this;
|
|
1205
|
+
}
|
|
1206
|
+
on(event,handler=console.log){
|
|
1207
|
+
this.EVENTS_HANDLERS_PAIRS.set(event,handler);
|
|
1208
|
+
this.#maintainOn();
|
|
1209
|
+
return this;
|
|
1210
|
+
}
|
|
1211
|
+
#maintainOn(){
|
|
1212
|
+
this.channel.onmessage = (e) => {
|
|
1213
|
+
this.LAST_RECEIVED_EVENT=e.data.last_sended_event;
|
|
1214
|
+
const USER_ID=e.data.userId;
|
|
1215
|
+
this.SUBSCRIBERS.add(USER_ID);
|
|
1216
|
+
const Data=e.data.EVENTS_DATAS_PAIRS.get(this.LAST_RECEIVED_EVENT);
|
|
1217
|
+
const Handler=this.EVENTS_HANDLERS_PAIRS.get(this.LAST_RECEIVED_EVENT);
|
|
1218
|
+
if(Data && Handler)Handler(Data);
|
|
1219
|
+
};
|
|
1220
|
+
return this;
|
|
1221
|
+
}
|
|
1222
|
+
#maintainEmit(event){
|
|
1223
|
+
this.channel.postMessage({
|
|
1224
|
+
EVENTS_DATAS_PAIRS:this.EVENTS_DATAS_PAIRS,
|
|
1225
|
+
last_sended_event:event,
|
|
1226
|
+
userId:this.UUID
|
|
1227
|
+
});
|
|
1228
|
+
return this;
|
|
1229
|
+
}
|
|
1230
|
+
close(){
|
|
1231
|
+
this.channel.close();
|
|
1232
|
+
return this;
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
const useChannel = name => new ZikoUseChannel(name);
|
|
1236
|
+
|
|
1237
|
+
// To do : remove old items
|
|
1238
|
+
class ZikoUseStorage{
|
|
1239
|
+
constructor(storage, globalKey, initialValue){
|
|
1240
|
+
this.cache={
|
|
1241
|
+
storage,
|
|
1242
|
+
globalKey,
|
|
1243
|
+
channel:useChannel(`Ziko:useStorage-${globalKey}`),
|
|
1244
|
+
oldItemKeys:new Set()
|
|
1245
|
+
};
|
|
1246
|
+
this.#init(initialValue);
|
|
1247
|
+
this.#maintain();
|
|
1248
|
+
}
|
|
1249
|
+
get items(){
|
|
1250
|
+
return JSON.parse(this.cache.storage[this.cache.globalKey]??null);
|
|
1251
|
+
}
|
|
1252
|
+
#maintain() {
|
|
1253
|
+
for(let i in this.items)Object.assign(this, { [[i]]: this.items[i] });
|
|
1254
|
+
}
|
|
1255
|
+
#init(initialValue){
|
|
1256
|
+
this.cache.channel=useChannel(`Ziko:useStorage-${this.cache.globalKey}`);
|
|
1257
|
+
this.cache.channel.on("Ziko-Storage-Updated",()=>this.#maintain());
|
|
1258
|
+
if(!initialValue)return;
|
|
1259
|
+
if(this.cache.storage[this.cache.globalKey]){
|
|
1260
|
+
Object.keys(this.items).forEach(key=>this.cache.oldItemKeys.add(key));
|
|
1261
|
+
// console.group("Ziko:useStorage")
|
|
1262
|
+
// console.warn(`Storage key '${this.cache.globalKey}' already exists. we will not overwrite it.`);
|
|
1263
|
+
// console.info(`%cWe'll keep the existing data.`,"background-color:#2222dd; color:gold;");
|
|
1264
|
+
// console.group("")
|
|
1265
|
+
}
|
|
1266
|
+
else this.set(initialValue);
|
|
1267
|
+
}
|
|
1268
|
+
set(data){
|
|
1269
|
+
this.cache.storage.setItem(this.cache.globalKey,JSON.stringify(data));
|
|
1270
|
+
this.cache.channel.emit("Ziko-Storage-Updated",{});
|
|
1271
|
+
Object.keys(data).forEach(key=>this.cache.oldItemKeys.add(key));
|
|
1272
|
+
this.#maintain();
|
|
1273
|
+
return this
|
|
1274
|
+
}
|
|
1275
|
+
add(data){
|
|
1276
|
+
const db={
|
|
1277
|
+
...this.items,
|
|
1278
|
+
...data
|
|
1279
|
+
};
|
|
1280
|
+
this.cache.storage.setItem(this.cache.globalKey,JSON.stringify(db));
|
|
1281
|
+
this.#maintain();
|
|
1282
|
+
return this;
|
|
1283
|
+
}
|
|
1284
|
+
remove(...keys){
|
|
1285
|
+
const db={...this.items};
|
|
1286
|
+
for(let i=0;i<keys.length;i++){
|
|
1287
|
+
delete db[keys[i]];
|
|
1288
|
+
delete this[keys[i]];
|
|
1289
|
+
}
|
|
1290
|
+
this.set(db);
|
|
1291
|
+
return this;
|
|
1292
|
+
}
|
|
1293
|
+
get(key){
|
|
1294
|
+
return this.items[key];
|
|
1295
|
+
}
|
|
1296
|
+
clear(){
|
|
1297
|
+
this.cache.storage.removeItem(this.cache.globalKey);
|
|
1298
|
+
this.#maintain();
|
|
1299
|
+
return this;
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
}
|
|
1303
|
+
const useLocaleStorage=(key,initialValue)=>new ZikoUseStorage(localStorage,key,initialValue);
|
|
1304
|
+
const useSessionStorage=(key,initialValue)=>new ZikoUseStorage(sessionStorage,key,initialValue);
|
|
1305
|
+
|
|
1188
1306
|
const __State__ = {
|
|
1189
1307
|
store : new Map(),
|
|
1190
|
-
index :
|
|
1308
|
+
index : 0,
|
|
1309
|
+
session_storage : null,
|
|
1191
1310
|
register: function(state){
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1311
|
+
if(!undefined.SSR && undefined.DEV){
|
|
1312
|
+
if(!this.session) this.session_storage = useSessionStorage('ziko-state', {});
|
|
1313
|
+
const savedValue = this.session_storage.get(this.index);
|
|
1314
|
+
if(!savedValue) this.session_storage.add({[this.index] : state.value});
|
|
1315
|
+
else state.value = savedValue;
|
|
1316
|
+
}
|
|
1196
1317
|
this.store.set(this.index++, state);
|
|
1197
|
-
}
|
|
1198
|
-
|
|
1318
|
+
},
|
|
1319
|
+
update: function(index, value){
|
|
1320
|
+
if(!undefined.SSR && undefined.DEV){
|
|
1321
|
+
this.session_storage.add({[index] : value});
|
|
1322
|
+
}
|
|
1323
|
+
},
|
|
1324
|
+
|
|
1199
1325
|
};
|
|
1200
1326
|
|
|
1201
1327
|
function __init__global__(){
|
|
@@ -1453,20 +1579,8 @@
|
|
|
1453
1579
|
|
|
1454
1580
|
if(!globalThis.__Ziko__) __init__global__();
|
|
1455
1581
|
|
|
1456
|
-
// HMR persistence
|
|
1457
|
-
if (undefined?.data) {
|
|
1458
|
-
undefined.data.__Ziko__ = undefined.data?.__Ziko__ || globalThis?.__Ziko__;
|
|
1459
|
-
globalThis.__Ziko__ = undefined.data.__Ziko__;
|
|
1460
|
-
// import.meta.hot.accept(n=>console.log(n));
|
|
1461
|
-
// console.log(import.meta.hot.data.__Ziko__.__State__.store)
|
|
1462
|
-
}
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
1582
|
function useState(initialValue) {
|
|
1467
|
-
|
|
1468
|
-
// console.log(import.meta.hot.data.__Ziko__.__State__.store.get(0))
|
|
1469
|
-
|
|
1583
|
+
|
|
1470
1584
|
const {store, index} = __Ziko__.__State__;
|
|
1471
1585
|
__Ziko__.__State__.register({
|
|
1472
1586
|
value : initialValue,
|
|
@@ -1474,7 +1588,7 @@
|
|
|
1474
1588
|
paused : false
|
|
1475
1589
|
});
|
|
1476
1590
|
|
|
1477
|
-
|
|
1591
|
+
let current = store.get(index);
|
|
1478
1592
|
|
|
1479
1593
|
function getValue() {
|
|
1480
1594
|
return {
|
|
@@ -1486,10 +1600,13 @@
|
|
|
1486
1600
|
|
|
1487
1601
|
function setValue(newValue) {
|
|
1488
1602
|
if (current.paused) return;
|
|
1489
|
-
if (typeof newValue === "function")
|
|
1603
|
+
if (typeof newValue === "function") {
|
|
1604
|
+
newValue = newValue(current.value);
|
|
1605
|
+
}
|
|
1490
1606
|
if (newValue !== current.value) {
|
|
1491
1607
|
current.value = newValue;
|
|
1492
1608
|
current.subscribers.forEach(fn => fn(current.value));
|
|
1609
|
+
__Ziko__.__State__.update(index, newValue);
|
|
1493
1610
|
}
|
|
1494
1611
|
}
|
|
1495
1612
|
|
|
@@ -2894,7 +3011,7 @@
|
|
|
2894
3011
|
function define_wc(name, UIElement, props = {}, { mode = 'open'} = {}) {
|
|
2895
3012
|
if (globalThis.customElements?.get(name)) {
|
|
2896
3013
|
console.warn(`Custom element "${name}" is already defined`);
|
|
2897
|
-
return;
|
|
3014
|
+
return;
|
|
2898
3015
|
}
|
|
2899
3016
|
if(name.search('-') === -1){
|
|
2900
3017
|
console.warn(`"${name}" is not a valid custom element name`);
|
|
@@ -2918,19 +3035,21 @@
|
|
|
2918
3035
|
}
|
|
2919
3036
|
|
|
2920
3037
|
connectedCallback() {
|
|
2921
|
-
this.
|
|
3038
|
+
this.render();
|
|
2922
3039
|
}
|
|
2923
3040
|
|
|
2924
|
-
|
|
3041
|
+
render() {
|
|
2925
3042
|
this.shadowRoot.innerHTML = '';
|
|
2926
|
-
|
|
3043
|
+
const item = UIElement(this.props);
|
|
3044
|
+
if(item instanceof Array) item.forEach(n => n.mount(this.shadowRoot));
|
|
3045
|
+
else item.mount(this.shadowRoot);
|
|
2927
3046
|
}
|
|
2928
3047
|
|
|
2929
3048
|
attributeChangedCallback(name, _, newValue) {
|
|
2930
3049
|
Object.assign(this.props, {
|
|
2931
3050
|
[name]: this.mask[name].type(newValue)
|
|
2932
3051
|
});
|
|
2933
|
-
this.
|
|
3052
|
+
this.render();
|
|
2934
3053
|
}
|
|
2935
3054
|
}
|
|
2936
3055
|
);
|
|
@@ -4469,6 +4588,7 @@
|
|
|
4469
4588
|
}
|
|
4470
4589
|
};
|
|
4471
4590
|
|
|
4591
|
+
const sleep= ms => new Promise(res => setTimeout(res, ms));
|
|
4472
4592
|
function timeout(ms, fn) {
|
|
4473
4593
|
let id;
|
|
4474
4594
|
const promise = new Promise((resolve) => {
|
|
@@ -4485,10 +4605,6 @@
|
|
|
4485
4605
|
};
|
|
4486
4606
|
}
|
|
4487
4607
|
|
|
4488
|
-
const sleep= ms => new Promise(res => setTimeout(res, ms));
|
|
4489
|
-
|
|
4490
|
-
// use it with await
|
|
4491
|
-
|
|
4492
4608
|
class TimeLoop {
|
|
4493
4609
|
constructor(callback, { step = 1000, t0 = 0, t1 = Infinity, autoplay = true } = {}) {
|
|
4494
4610
|
this.callback = callback;
|
|
@@ -5098,55 +5214,6 @@
|
|
|
5098
5214
|
nested_value
|
|
5099
5215
|
);
|
|
5100
5216
|
|
|
5101
|
-
class ZikoUseChannel{
|
|
5102
|
-
constructor(name = ""){
|
|
5103
|
-
this.channel = new BroadcastChannel(name);
|
|
5104
|
-
this.EVENTS_DATAS_PAIRS = new Map();
|
|
5105
|
-
this.EVENTS_HANDLERS_PAIRS = new Map();
|
|
5106
|
-
this.LAST_RECEIVED_EVENT = "";
|
|
5107
|
-
this.UUID="ziko-channel"+Random.string(10);
|
|
5108
|
-
this.SUBSCRIBERS = new Set([this.UUID]);
|
|
5109
|
-
}
|
|
5110
|
-
get broadcast(){
|
|
5111
|
-
// update receiver
|
|
5112
|
-
return this;
|
|
5113
|
-
}
|
|
5114
|
-
emit(event, data){
|
|
5115
|
-
this.EVENTS_DATAS_PAIRS.set(event,data);
|
|
5116
|
-
this.#maintainEmit(event);
|
|
5117
|
-
return this;
|
|
5118
|
-
}
|
|
5119
|
-
on(event,handler=console.log){
|
|
5120
|
-
this.EVENTS_HANDLERS_PAIRS.set(event,handler);
|
|
5121
|
-
this.#maintainOn();
|
|
5122
|
-
return this;
|
|
5123
|
-
}
|
|
5124
|
-
#maintainOn(){
|
|
5125
|
-
this.channel.onmessage = (e) => {
|
|
5126
|
-
this.LAST_RECEIVED_EVENT=e.data.last_sended_event;
|
|
5127
|
-
const USER_ID=e.data.userId;
|
|
5128
|
-
this.SUBSCRIBERS.add(USER_ID);
|
|
5129
|
-
const Data=e.data.EVENTS_DATAS_PAIRS.get(this.LAST_RECEIVED_EVENT);
|
|
5130
|
-
const Handler=this.EVENTS_HANDLERS_PAIRS.get(this.LAST_RECEIVED_EVENT);
|
|
5131
|
-
if(Data && Handler)Handler(Data);
|
|
5132
|
-
};
|
|
5133
|
-
return this;
|
|
5134
|
-
}
|
|
5135
|
-
#maintainEmit(event){
|
|
5136
|
-
this.channel.postMessage({
|
|
5137
|
-
EVENTS_DATAS_PAIRS:this.EVENTS_DATAS_PAIRS,
|
|
5138
|
-
last_sended_event:event,
|
|
5139
|
-
userId:this.UUID
|
|
5140
|
-
});
|
|
5141
|
-
return this;
|
|
5142
|
-
}
|
|
5143
|
-
close(){
|
|
5144
|
-
this.channel.close();
|
|
5145
|
-
return this;
|
|
5146
|
-
}
|
|
5147
|
-
}
|
|
5148
|
-
const useChannel = name => new ZikoUseChannel(name);
|
|
5149
|
-
|
|
5150
5217
|
class ZikoUseThreed {
|
|
5151
5218
|
#workerContent;
|
|
5152
5219
|
constructor() {
|
|
@@ -5263,75 +5330,6 @@
|
|
|
5263
5330
|
|
|
5264
5331
|
*/
|
|
5265
5332
|
|
|
5266
|
-
// To do : remove old items
|
|
5267
|
-
class ZikoUseStorage{
|
|
5268
|
-
constructor(storage, globalKey, initialValue){
|
|
5269
|
-
this.cache={
|
|
5270
|
-
storage,
|
|
5271
|
-
globalKey,
|
|
5272
|
-
channel:useChannel(`Ziko:useStorage-${globalKey}`),
|
|
5273
|
-
oldItemKeys:new Set()
|
|
5274
|
-
};
|
|
5275
|
-
this.#init(initialValue);
|
|
5276
|
-
this.#maintain();
|
|
5277
|
-
}
|
|
5278
|
-
get items(){
|
|
5279
|
-
return JSON.parse(this.cache.storage[this.cache.globalKey]??null);
|
|
5280
|
-
}
|
|
5281
|
-
#maintain() {
|
|
5282
|
-
for(let i in this.items)Object.assign(this, { [[i]]: this.items[i] });
|
|
5283
|
-
}
|
|
5284
|
-
#init(initialValue){
|
|
5285
|
-
this.cache.channel=useChannel(`Ziko:useStorage-${this.cache.globalKey}`);
|
|
5286
|
-
this.cache.channel.on("Ziko-Storage-Updated",()=>this.#maintain());
|
|
5287
|
-
if(!initialValue)return;
|
|
5288
|
-
if(this.cache.storage[this.cache.globalKey]){
|
|
5289
|
-
Object.keys(this.items).forEach(key=>this.cache.oldItemKeys.add(key));
|
|
5290
|
-
console.group("Ziko:useStorage");
|
|
5291
|
-
console.warn(`Storage key '${this.cache.globalKey}' already exists. we will not overwrite it.`);
|
|
5292
|
-
console.info(`%cWe'll keep the existing data.`,"background-color:#2222dd; color:gold;");
|
|
5293
|
-
console.group("");
|
|
5294
|
-
}
|
|
5295
|
-
else this.set(initialValue);
|
|
5296
|
-
}
|
|
5297
|
-
set(data){
|
|
5298
|
-
this.cache.storage.setItem(this.cache.globalKey,JSON.stringify(data));
|
|
5299
|
-
this.cache.channel.emit("Ziko-Storage-Updated",{});
|
|
5300
|
-
Object.keys(data).forEach(key=>this.cache.oldItemKeys.add(key));
|
|
5301
|
-
this.#maintain();
|
|
5302
|
-
return this
|
|
5303
|
-
}
|
|
5304
|
-
add(data){
|
|
5305
|
-
const db={
|
|
5306
|
-
...this.items,
|
|
5307
|
-
...data
|
|
5308
|
-
};
|
|
5309
|
-
this.cache.storage.setItem(this.cache.globalKey,JSON.stringify(db));
|
|
5310
|
-
this.#maintain();
|
|
5311
|
-
return this;
|
|
5312
|
-
}
|
|
5313
|
-
remove(...keys){
|
|
5314
|
-
const db={...this.items};
|
|
5315
|
-
for(let i=0;i<keys.length;i++){
|
|
5316
|
-
delete db[keys[i]];
|
|
5317
|
-
delete this[keys[i]];
|
|
5318
|
-
}
|
|
5319
|
-
this.set(db);
|
|
5320
|
-
return this;
|
|
5321
|
-
}
|
|
5322
|
-
get(key){
|
|
5323
|
-
return this.items[key];
|
|
5324
|
-
}
|
|
5325
|
-
clear(){
|
|
5326
|
-
this.cache.storage.removeItem(this.cache.globalKey);
|
|
5327
|
-
this.#maintain();
|
|
5328
|
-
return this;
|
|
5329
|
-
}
|
|
5330
|
-
|
|
5331
|
-
}
|
|
5332
|
-
const useLocaleStorage=(key,initialValue)=>new ZikoUseStorage(localStorage,key,initialValue);
|
|
5333
|
-
const useSessionStorage=(key,initialValue)=>new ZikoUseStorage(sessionStorage,key,initialValue);
|
|
5334
|
-
|
|
5335
5333
|
let {sqrt, cos, sin, exp, log, cosh, sinh} = Math;
|
|
5336
5334
|
// Math.abs = new Proxy(Math.abs, {
|
|
5337
5335
|
// apply(target, thisArg, args) {
|