ziko 0.49.4 → 0.49.6

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.
@@ -0,0 +1,73 @@
1
+ // To do : remove old items
2
+ import { useChannel } from "./use-channel.js";
3
+ class UseStorage{
4
+ constructor(storage, globalKey, initialValue){
5
+ this.cache={
6
+ storage,
7
+ globalKey,
8
+ channel:useChannel(`Ziko:useStorage-${globalKey}`),
9
+ oldItemKeys:new Set()
10
+ }
11
+ this.#init(initialValue);
12
+ this.#maintain();
13
+ }
14
+ get items(){
15
+ return JSON.parse(this.cache.storage[this.cache.globalKey]??null);
16
+ }
17
+ #maintain() {
18
+ for(let i in this.items)Object.assign(this, { [[i]]: this.items[i] });
19
+ }
20
+ #init(initialValue){
21
+ this.cache.channel=useChannel(`Ziko:useStorage-${this.cache.globalKey}`);
22
+ this.cache.channel.on("Ziko-Storage-Updated",()=>this.#maintain());
23
+ if(!initialValue)return;
24
+ if(this.cache.storage[this.cache.globalKey]){
25
+ Object.keys(this.items).forEach(key=>this.cache.oldItemKeys.add(key));
26
+ // console.group("Ziko:useStorage")
27
+ // console.warn(`Storage key '${this.cache.globalKey}' already exists. we will not overwrite it.`);
28
+ // console.info(`%cWe'll keep the existing data.`,"background-color:#2222dd; color:gold;");
29
+ // console.group("")
30
+ }
31
+ else this.set(initialValue);
32
+ }
33
+ set(data){
34
+ this.cache.storage.setItem(this.cache.globalKey,JSON.stringify(data));
35
+ this.cache.channel.emit("Ziko-Storage-Updated",{});
36
+ Object.keys(data).forEach(key=>this.cache.oldItemKeys.add(key));
37
+ this.#maintain();
38
+ return this
39
+ }
40
+ add(data){
41
+ const db={
42
+ ...this.items,
43
+ ...data
44
+ }
45
+ this.cache.storage.setItem(this.cache.globalKey,JSON.stringify(db));
46
+ this.#maintain();
47
+ return this;
48
+ }
49
+ remove(...keys){
50
+ const db={...this.items};
51
+ for(let i=0;i<keys.length;i++){
52
+ delete db[keys[i]];
53
+ delete this[keys[i]];
54
+ }
55
+ this.set(db);
56
+ return this;
57
+ }
58
+ get(key){
59
+ return this.items[key];
60
+ }
61
+ clear(){
62
+ this.cache.storage.removeItem(this.cache.globalKey);
63
+ this.#maintain();
64
+ return this;
65
+ }
66
+
67
+ }
68
+ const useLocaleStorage=(key,initialValue)=>new UseStorage(localStorage,key,initialValue);
69
+ const useSessionStorage=(key,initialValue)=>new UseStorage(sessionStorage,key,initialValue);
70
+ export{
71
+ useLocaleStorage,
72
+ useSessionStorage
73
+ }
@@ -1,4 +1,4 @@
1
- class ZikoUseThreed {
1
+ class UseThreed {
2
2
  #workerContent;
3
3
  constructor() {
4
4
  this.#workerContent = (
@@ -34,7 +34,7 @@ class ZikoUseThreed {
34
34
  }
35
35
 
36
36
  const useThread = (func, callback , close) => {
37
- const T = new ZikoUseThreed();
37
+ const T = new UseThreed();
38
38
  if (func) {
39
39
  T.call(func, callback , close);
40
40
  }
@@ -0,0 +1,4 @@
1
+ export type * from './use-channel.d.ts'
2
+ export type * from './use-state.d.ts'
3
+ export type * from './use-derived.d.ts'
4
+ export type * from './use-reactive.d.ts'
@@ -0,0 +1,12 @@
1
+ export declare class UseChannel {
2
+ emit(event: string, data: any, rooms?: string[]): this;
3
+ on(event: string, handler: (data: any) => void, rooms?: string | string[]): this;
4
+ off(event: string, handler: (data: any) => void): this;
5
+ once(event: string, handler: (data: any) => void, rooms?: string | string[]): this;
6
+ join(...rooms: string[]): this;
7
+ leave(...rooms: string[]): this;
8
+ close(): this;
9
+
10
+ }
11
+
12
+ export declare const useChannel: (name?: string) => UseChannel;
@@ -0,0 +1,14 @@
1
+ export function useDerived<T>(
2
+ deriveFn: (...values: any[]) => T,
3
+ sources: Array<
4
+ () => {
5
+ value: any;
6
+ isStateGetter: () => true;
7
+ _subscribe: (fn: (value: any) => void) => void;
8
+ }
9
+ >
10
+ ): () => {
11
+ value: T;
12
+ isStateGetter: () => true;
13
+ _subscribe: (fn: (value: T) => void) => void;
14
+ };
@@ -0,0 +1,14 @@
1
+ import { mapfun, MapfunResult } from "../math/utils/mapfun.d.ts";
2
+ import { useState } from "./use-state.d.ts";
3
+
4
+ export function useReactive<
5
+ T
6
+ >(
7
+ nested_value: T
8
+ ): MapfunResult<
9
+ (n: any) => {
10
+ get: ReturnType<typeof useState<any>>[0];
11
+ set: ReturnType<typeof useState<any>>[1];
12
+ },
13
+ T
14
+ >;
@@ -0,0 +1,25 @@
1
+ export function useState<T>(
2
+ initialValue: T
3
+ ): [
4
+ /** getter function */
5
+ () => {
6
+ value: T;
7
+ isStateGetter: () => true;
8
+ _subscribe: (fn: (value: T) => void) => void;
9
+ },
10
+
11
+ /** setter function */
12
+ (newValue: T | ((prev: T) => T)) => void,
13
+
14
+ /** controller */
15
+ {
16
+ pause: () => void;
17
+ resume: () => void;
18
+ clear: () => void;
19
+ force: (newValue: T | ((prev: T) => T)) => void;
20
+ getSubscribers: () => Set<(value: T) => void>;
21
+ }
22
+ ];
23
+
24
+ /** check if argument is a state getter */
25
+ export function isStateGetter(arg: any): boolean;
package/types/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export type * from './math'
2
- export type * from './time'
2
+ export type * from './time'
3
+ export type * from './hooks'
@@ -1 +0,0 @@
1
- export default class ZikoMath {}
@@ -1,50 +0,0 @@
1
- import { Random } from "../math/random/index.js";
2
- class ZikoUseChannel{
3
- constructor(name = ""){
4
- this.channel = new BroadcastChannel(name);
5
- this.EVENTS_DATAS_PAIRS = new Map();
6
- this.EVENTS_HANDLERS_PAIRS = new Map();
7
- this.LAST_RECEIVED_EVENT = "";
8
- this.UUID="ziko-channel"+Random.string(10);
9
- this.SUBSCRIBERS = new Set([this.UUID]);
10
- }
11
- get broadcast(){
12
- // update receiver
13
- return this;
14
- }
15
- emit(event, data){
16
- this.EVENTS_DATAS_PAIRS.set(event,data)
17
- this.#maintainEmit(event);
18
- return this;
19
- }
20
- on(event,handler=console.log){
21
- this.EVENTS_HANDLERS_PAIRS.set(event,handler);
22
- this.#maintainOn()
23
- return this;
24
- }
25
- #maintainOn(){
26
- this.channel.onmessage = (e) => {
27
- this.LAST_RECEIVED_EVENT=e.data.last_sended_event;
28
- const USER_ID=e.data.userId;
29
- this.SUBSCRIBERS.add(USER_ID)
30
- const Data=e.data.EVENTS_DATAS_PAIRS.get(this.LAST_RECEIVED_EVENT)
31
- const Handler=this.EVENTS_HANDLERS_PAIRS.get(this.LAST_RECEIVED_EVENT)
32
- if(Data && Handler)Handler(Data)
33
- };
34
- return this;
35
- }
36
- #maintainEmit(event){
37
- this.channel.postMessage({
38
- EVENTS_DATAS_PAIRS:this.EVENTS_DATAS_PAIRS,
39
- last_sended_event:event,
40
- userId:this.UUID
41
- });
42
- return this;
43
- }
44
- close(){
45
- this.channel.close();
46
- return this;
47
- }
48
- }
49
- const useChannel = name => new ZikoUseChannel(name);
50
- export{ useChannel }