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 CHANGED
@@ -2,7 +2,7 @@
2
2
  /*
3
3
  Project: ziko.js
4
4
  Author: Zakaria Elalaoui
5
- Date : Sat Nov 22 2025 13:34:29 GMT+0100 (UTC+01:00)
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
@@ -1181,17 +1181,143 @@ const __CACHE__ = {
1181
1181
  }
1182
1182
  };
1183
1183
 
1184
+ class ZikoUseChannel{
1185
+ constructor(name = ""){
1186
+ this.channel = new BroadcastChannel(name);
1187
+ this.EVENTS_DATAS_PAIRS = new Map();
1188
+ this.EVENTS_HANDLERS_PAIRS = new Map();
1189
+ this.LAST_RECEIVED_EVENT = "";
1190
+ this.UUID="ziko-channel"+Random.string(10);
1191
+ this.SUBSCRIBERS = new Set([this.UUID]);
1192
+ }
1193
+ get broadcast(){
1194
+ // update receiver
1195
+ return this;
1196
+ }
1197
+ emit(event, data){
1198
+ this.EVENTS_DATAS_PAIRS.set(event,data);
1199
+ this.#maintainEmit(event);
1200
+ return this;
1201
+ }
1202
+ on(event,handler=console.log){
1203
+ this.EVENTS_HANDLERS_PAIRS.set(event,handler);
1204
+ this.#maintainOn();
1205
+ return this;
1206
+ }
1207
+ #maintainOn(){
1208
+ this.channel.onmessage = (e) => {
1209
+ this.LAST_RECEIVED_EVENT=e.data.last_sended_event;
1210
+ const USER_ID=e.data.userId;
1211
+ this.SUBSCRIBERS.add(USER_ID);
1212
+ const Data=e.data.EVENTS_DATAS_PAIRS.get(this.LAST_RECEIVED_EVENT);
1213
+ const Handler=this.EVENTS_HANDLERS_PAIRS.get(this.LAST_RECEIVED_EVENT);
1214
+ if(Data && Handler)Handler(Data);
1215
+ };
1216
+ return this;
1217
+ }
1218
+ #maintainEmit(event){
1219
+ this.channel.postMessage({
1220
+ EVENTS_DATAS_PAIRS:this.EVENTS_DATAS_PAIRS,
1221
+ last_sended_event:event,
1222
+ userId:this.UUID
1223
+ });
1224
+ return this;
1225
+ }
1226
+ close(){
1227
+ this.channel.close();
1228
+ return this;
1229
+ }
1230
+ }
1231
+ const useChannel = name => new ZikoUseChannel(name);
1232
+
1233
+ // To do : remove old items
1234
+ class ZikoUseStorage{
1235
+ constructor(storage, globalKey, initialValue){
1236
+ this.cache={
1237
+ storage,
1238
+ globalKey,
1239
+ channel:useChannel(`Ziko:useStorage-${globalKey}`),
1240
+ oldItemKeys:new Set()
1241
+ };
1242
+ this.#init(initialValue);
1243
+ this.#maintain();
1244
+ }
1245
+ get items(){
1246
+ return JSON.parse(this.cache.storage[this.cache.globalKey]??null);
1247
+ }
1248
+ #maintain() {
1249
+ for(let i in this.items)Object.assign(this, { [[i]]: this.items[i] });
1250
+ }
1251
+ #init(initialValue){
1252
+ this.cache.channel=useChannel(`Ziko:useStorage-${this.cache.globalKey}`);
1253
+ this.cache.channel.on("Ziko-Storage-Updated",()=>this.#maintain());
1254
+ if(!initialValue)return;
1255
+ if(this.cache.storage[this.cache.globalKey]){
1256
+ Object.keys(this.items).forEach(key=>this.cache.oldItemKeys.add(key));
1257
+ // console.group("Ziko:useStorage")
1258
+ // console.warn(`Storage key '${this.cache.globalKey}' already exists. we will not overwrite it.`);
1259
+ // console.info(`%cWe'll keep the existing data.`,"background-color:#2222dd; color:gold;");
1260
+ // console.group("")
1261
+ }
1262
+ else this.set(initialValue);
1263
+ }
1264
+ set(data){
1265
+ this.cache.storage.setItem(this.cache.globalKey,JSON.stringify(data));
1266
+ this.cache.channel.emit("Ziko-Storage-Updated",{});
1267
+ Object.keys(data).forEach(key=>this.cache.oldItemKeys.add(key));
1268
+ this.#maintain();
1269
+ return this
1270
+ }
1271
+ add(data){
1272
+ const db={
1273
+ ...this.items,
1274
+ ...data
1275
+ };
1276
+ this.cache.storage.setItem(this.cache.globalKey,JSON.stringify(db));
1277
+ this.#maintain();
1278
+ return this;
1279
+ }
1280
+ remove(...keys){
1281
+ const db={...this.items};
1282
+ for(let i=0;i<keys.length;i++){
1283
+ delete db[keys[i]];
1284
+ delete this[keys[i]];
1285
+ }
1286
+ this.set(db);
1287
+ return this;
1288
+ }
1289
+ get(key){
1290
+ return this.items[key];
1291
+ }
1292
+ clear(){
1293
+ this.cache.storage.removeItem(this.cache.globalKey);
1294
+ this.#maintain();
1295
+ return this;
1296
+ }
1297
+
1298
+ }
1299
+ const useLocaleStorage=(key,initialValue)=>new ZikoUseStorage(localStorage,key,initialValue);
1300
+ const useSessionStorage=(key,initialValue)=>new ZikoUseStorage(sessionStorage,key,initialValue);
1301
+
1184
1302
  const __State__ = {
1185
1303
  store : new Map(),
1186
- index : undefined?.data?.__Ziko__?.__State__?.index ?? 0,
1304
+ index : 0,
1305
+ session_storage : null,
1187
1306
  register: function(state){
1188
- console.log({
1189
- // hmr : import.meta.hot?.data.__Ziko__.__State__.index,
1190
- index : this.index
1191
- });
1307
+ if(!undefined.SSR && undefined.DEV){
1308
+ if(!this.session) this.session_storage = useSessionStorage('ziko-state', {});
1309
+ const savedValue = this.session_storage.get(this.index);
1310
+ if(!savedValue) this.session_storage.add({[this.index] : state.value});
1311
+ else state.value = savedValue;
1312
+ }
1192
1313
  this.store.set(this.index++, state);
1193
- }
1194
-
1314
+ },
1315
+ update: function(index, value){
1316
+ if(!undefined.SSR && undefined.DEV){
1317
+ this.session_storage.add({[index] : value});
1318
+ }
1319
+ },
1320
+
1195
1321
  };
1196
1322
 
1197
1323
  function __init__global__(){
@@ -1449,20 +1575,8 @@ function _register_to_class_(target, mixin) {
1449
1575
 
1450
1576
  if(!globalThis.__Ziko__) __init__global__();
1451
1577
 
1452
- // HMR persistence
1453
- if (undefined?.data) {
1454
- undefined.data.__Ziko__ = undefined.data?.__Ziko__ || globalThis?.__Ziko__;
1455
- globalThis.__Ziko__ = undefined.data.__Ziko__;
1456
- // import.meta.hot.accept(n=>console.log(n));
1457
- // console.log(import.meta.hot.data.__Ziko__.__State__.store)
1458
- }
1459
-
1460
-
1461
-
1462
1578
  function useState(initialValue) {
1463
-
1464
- // console.log(import.meta.hot.data.__Ziko__.__State__.store.get(0))
1465
-
1579
+
1466
1580
  const {store, index} = __Ziko__.__State__;
1467
1581
  __Ziko__.__State__.register({
1468
1582
  value : initialValue,
@@ -1470,7 +1584,7 @@ function useState(initialValue) {
1470
1584
  paused : false
1471
1585
  });
1472
1586
 
1473
- const current = store.get(index);
1587
+ let current = store.get(index);
1474
1588
 
1475
1589
  function getValue() {
1476
1590
  return {
@@ -1482,10 +1596,13 @@ function useState(initialValue) {
1482
1596
 
1483
1597
  function setValue(newValue) {
1484
1598
  if (current.paused) return;
1485
- if (typeof newValue === "function") newValue = newValue(current.value);
1599
+ if (typeof newValue === "function") {
1600
+ newValue = newValue(current.value);
1601
+ }
1486
1602
  if (newValue !== current.value) {
1487
1603
  current.value = newValue;
1488
1604
  current.subscribers.forEach(fn => fn(current.value));
1605
+ __Ziko__.__State__.update(index, newValue);
1489
1606
  }
1490
1607
  }
1491
1608
 
@@ -2890,7 +3007,7 @@ const SVGWrapper = (content) => new UISVGWrapper(content);
2890
3007
  function define_wc(name, UIElement, props = {}, { mode = 'open'} = {}) {
2891
3008
  if (globalThis.customElements?.get(name)) {
2892
3009
  console.warn(`Custom element "${name}" is already defined`);
2893
- return; // skip redefinition
3010
+ return;
2894
3011
  }
2895
3012
  if(name.search('-') === -1){
2896
3013
  console.warn(`"${name}" is not a valid custom element name`);
@@ -2914,19 +3031,21 @@ function define_wc(name, UIElement, props = {}, { mode = 'open'} = {}) {
2914
3031
  }
2915
3032
 
2916
3033
  connectedCallback() {
2917
- this.mount();
3034
+ this.render();
2918
3035
  }
2919
3036
 
2920
- mount() {
3037
+ render() {
2921
3038
  this.shadowRoot.innerHTML = '';
2922
- this.UIElement = UIElement(this.props).mount(this.shadowRoot);
3039
+ const item = UIElement(this.props);
3040
+ if(item instanceof Array) item.forEach(n => n.mount(this.shadowRoot));
3041
+ else item.mount(this.shadowRoot);
2923
3042
  }
2924
3043
 
2925
3044
  attributeChangedCallback(name, _, newValue) {
2926
3045
  Object.assign(this.props, {
2927
3046
  [name]: this.mask[name].type(newValue)
2928
3047
  });
2929
- this.mount();
3048
+ this.render();
2930
3049
  }
2931
3050
  }
2932
3051
  );
@@ -4465,6 +4584,7 @@ const throttle=(fn,delay)=>{
4465
4584
  }
4466
4585
  };
4467
4586
 
4587
+ const sleep= ms => new Promise(res => setTimeout(res, ms));
4468
4588
  function timeout(ms, fn) {
4469
4589
  let id;
4470
4590
  const promise = new Promise((resolve) => {
@@ -4481,10 +4601,6 @@ function timeout(ms, fn) {
4481
4601
  };
4482
4602
  }
4483
4603
 
4484
- const sleep= ms => new Promise(res => setTimeout(res, ms));
4485
-
4486
- // use it with await
4487
-
4488
4604
  class TimeLoop {
4489
4605
  constructor(callback, { step = 1000, t0 = 0, t1 = Infinity, autoplay = true } = {}) {
4490
4606
  this.callback = callback;
@@ -5094,55 +5210,6 @@ const useReactive = (nested_value) => mapfun$1(
5094
5210
  nested_value
5095
5211
  );
5096
5212
 
5097
- class ZikoUseChannel{
5098
- constructor(name = ""){
5099
- this.channel = new BroadcastChannel(name);
5100
- this.EVENTS_DATAS_PAIRS = new Map();
5101
- this.EVENTS_HANDLERS_PAIRS = new Map();
5102
- this.LAST_RECEIVED_EVENT = "";
5103
- this.UUID="ziko-channel"+Random.string(10);
5104
- this.SUBSCRIBERS = new Set([this.UUID]);
5105
- }
5106
- get broadcast(){
5107
- // update receiver
5108
- return this;
5109
- }
5110
- emit(event, data){
5111
- this.EVENTS_DATAS_PAIRS.set(event,data);
5112
- this.#maintainEmit(event);
5113
- return this;
5114
- }
5115
- on(event,handler=console.log){
5116
- this.EVENTS_HANDLERS_PAIRS.set(event,handler);
5117
- this.#maintainOn();
5118
- return this;
5119
- }
5120
- #maintainOn(){
5121
- this.channel.onmessage = (e) => {
5122
- this.LAST_RECEIVED_EVENT=e.data.last_sended_event;
5123
- const USER_ID=e.data.userId;
5124
- this.SUBSCRIBERS.add(USER_ID);
5125
- const Data=e.data.EVENTS_DATAS_PAIRS.get(this.LAST_RECEIVED_EVENT);
5126
- const Handler=this.EVENTS_HANDLERS_PAIRS.get(this.LAST_RECEIVED_EVENT);
5127
- if(Data && Handler)Handler(Data);
5128
- };
5129
- return this;
5130
- }
5131
- #maintainEmit(event){
5132
- this.channel.postMessage({
5133
- EVENTS_DATAS_PAIRS:this.EVENTS_DATAS_PAIRS,
5134
- last_sended_event:event,
5135
- userId:this.UUID
5136
- });
5137
- return this;
5138
- }
5139
- close(){
5140
- this.channel.close();
5141
- return this;
5142
- }
5143
- }
5144
- const useChannel = name => new ZikoUseChannel(name);
5145
-
5146
5213
  class ZikoUseThreed {
5147
5214
  #workerContent;
5148
5215
  constructor() {
@@ -5259,75 +5326,6 @@ tags.p("Test useRoot ").style({
5259
5326
 
5260
5327
  */
5261
5328
 
5262
- // To do : remove old items
5263
- class ZikoUseStorage{
5264
- constructor(storage, globalKey, initialValue){
5265
- this.cache={
5266
- storage,
5267
- globalKey,
5268
- channel:useChannel(`Ziko:useStorage-${globalKey}`),
5269
- oldItemKeys:new Set()
5270
- };
5271
- this.#init(initialValue);
5272
- this.#maintain();
5273
- }
5274
- get items(){
5275
- return JSON.parse(this.cache.storage[this.cache.globalKey]??null);
5276
- }
5277
- #maintain() {
5278
- for(let i in this.items)Object.assign(this, { [[i]]: this.items[i] });
5279
- }
5280
- #init(initialValue){
5281
- this.cache.channel=useChannel(`Ziko:useStorage-${this.cache.globalKey}`);
5282
- this.cache.channel.on("Ziko-Storage-Updated",()=>this.#maintain());
5283
- if(!initialValue)return;
5284
- if(this.cache.storage[this.cache.globalKey]){
5285
- Object.keys(this.items).forEach(key=>this.cache.oldItemKeys.add(key));
5286
- console.group("Ziko:useStorage");
5287
- console.warn(`Storage key '${this.cache.globalKey}' already exists. we will not overwrite it.`);
5288
- console.info(`%cWe'll keep the existing data.`,"background-color:#2222dd; color:gold;");
5289
- console.group("");
5290
- }
5291
- else this.set(initialValue);
5292
- }
5293
- set(data){
5294
- this.cache.storage.setItem(this.cache.globalKey,JSON.stringify(data));
5295
- this.cache.channel.emit("Ziko-Storage-Updated",{});
5296
- Object.keys(data).forEach(key=>this.cache.oldItemKeys.add(key));
5297
- this.#maintain();
5298
- return this
5299
- }
5300
- add(data){
5301
- const db={
5302
- ...this.items,
5303
- ...data
5304
- };
5305
- this.cache.storage.setItem(this.cache.globalKey,JSON.stringify(db));
5306
- this.#maintain();
5307
- return this;
5308
- }
5309
- remove(...keys){
5310
- const db={...this.items};
5311
- for(let i=0;i<keys.length;i++){
5312
- delete db[keys[i]];
5313
- delete this[keys[i]];
5314
- }
5315
- this.set(db);
5316
- return this;
5317
- }
5318
- get(key){
5319
- return this.items[key];
5320
- }
5321
- clear(){
5322
- this.cache.storage.removeItem(this.cache.globalKey);
5323
- this.#maintain();
5324
- return this;
5325
- }
5326
-
5327
- }
5328
- const useLocaleStorage=(key,initialValue)=>new ZikoUseStorage(localStorage,key,initialValue);
5329
- const useSessionStorage=(key,initialValue)=>new ZikoUseStorage(sessionStorage,key,initialValue);
5330
-
5331
5329
  let {sqrt, cos, sin, exp, log, cosh, sinh} = Math;
5332
5330
  // Math.abs = new Proxy(Math.abs, {
5333
5331
  // apply(target, thisArg, args) {