ziko 0.41.1 → 0.42.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ziko",
3
- "version": "0.41.1",
3
+ "version": "0.42.0",
4
4
  "description": "A versatile JavaScript library offering a rich set of Hyperscript Based UI components, advanced mathematical utilities, interactivity ,animations, client side routing and more ...",
5
5
  "keywords": [
6
6
  "front-end",
@@ -60,7 +60,7 @@
60
60
  "./time": {},
61
61
  "./components": {}
62
62
  },
63
- "sideEffects" : false,
63
+ "sideEffects" : true,
64
64
  "bin": {
65
65
  "create-ziko-app": "starter/bin/index.js"
66
66
  },
@@ -0,0 +1,12 @@
1
+ export const __State__ = {
2
+ store : new Map(),
3
+ index : import.meta.hot?.data?.__Ziko__?.__State__?.index ?? 0,
4
+ register: function(state){
5
+ console.log({
6
+ hmr : import.meta.hot?.data.__Ziko__.__State__.index,
7
+ index : this.index
8
+ })
9
+ this.store.set(this.index++, state)
10
+ }
11
+
12
+ }
@@ -3,12 +3,14 @@ import { __UI__ } from './__ui__.js';
3
3
  import { __Config__ } from './__config__.js';
4
4
  import { __HYDRATION__, __HYDRATION_MAP__ } from './__hydration__.js';
5
5
  import { __CACHE__ } from './__cache__.js';
6
+ import { __State__ } from './__state__.js';
6
7
  export function __init__global__(){
7
8
  if ( !globalThis?.__Ziko__ ){
8
9
  globalThis.__Ziko__ = {
9
10
  __UI__,
10
11
  __HYDRATION__,
11
- __HYDRATION_MAP__,
12
+ __State__,
13
+ // __HYDRATION_MAP__,
12
14
  __Config__,
13
15
  __CACHE__,
14
16
  };
@@ -3,47 +3,6 @@ export function useDerived(deriveFn, sources) {
3
3
  const subscribers = new Set();
4
4
  let paused = false;
5
5
 
6
- function getValue() {
7
- return {
8
- value,
9
- isStateGetter: () => true,
10
- _subscribe: (fn, UIElement) => {
11
- subscribers.add(fn);
12
-
13
- const observer = new MutationObserver(() => {
14
- if (!document.body.contains(UIElement.element)) {
15
- subscribers.delete(fn);
16
- observer.disconnect();
17
- }
18
- });
19
-
20
- observer.observe(document.body, { childList: true, subtree: true });
21
- },
22
- };
23
- }
24
-
25
- function setValue(newValue) {
26
- if (paused) return;
27
- if (typeof newValue === "function") newValue = newValue(value);
28
- if (newValue !== value) {
29
- value = newValue;
30
- subscribers.forEach(fn => fn(value));
31
- }
32
- }
33
-
34
- const controller = {
35
- pause: () => { paused = true; },
36
- resume: () => { paused = false; },
37
- clear: () => { subscribers.clear(); },
38
- force: (newValue) => {
39
- if (typeof newValue === "function") newValue = newValue(value);
40
- value = newValue;
41
- subscribers.forEach(fn => fn(value));
42
- },
43
- getSubscribers: () => new Set(subscribers),
44
- };
45
-
46
- // Subscribe to source states
47
6
  sources.forEach(source => {
48
7
  const srcValue = source(); // getValue()
49
8
  srcValue._subscribe(() => {
@@ -54,8 +13,11 @@ export function useDerived(deriveFn, sources) {
54
13
  subscribers.forEach(fn => fn(value));
55
14
  }
56
15
  }
57
- }, { element: document.body }); // dummy UIElement
16
+ });
58
17
  });
59
-
60
- return [getValue, setValue, controller];
61
- }
18
+ return () => ({
19
+ value,
20
+ isStateGetter : () => true,
21
+ _subscribe: (fn) => subscribers.add(fn)
22
+ })
23
+ }
@@ -1,7 +1,17 @@
1
1
  import {mapfun} from '../math'
2
2
  import { useState } from './use-state.js'
3
3
 
4
- const useReactive = (nested_value) => mapfun(n => useState(n), nested_value)
4
+ const useReactive = (nested_value) => mapfun(
5
+ n => {
6
+ const state = useState(n)
7
+ // console.log(state)
8
+ return {
9
+ get : state[0],
10
+ set : state[1],
11
+ }
12
+ },
13
+ nested_value
14
+ )
5
15
  export{
6
16
  useReactive
7
17
  }
@@ -1,55 +1,62 @@
1
+ import { __init__global__ } from "../__ziko__/index.js";
2
+ if(!globalThis.__Ziko__) __init__global__()
3
+
4
+ // HMR persistence
5
+ if (import.meta.hot) {
6
+ import.meta.hot.data.__Ziko__ = import.meta.hot.data.__Ziko__ || globalThis.__Ziko__;
7
+ globalThis.__Ziko__ = import.meta.hot.data.__Ziko__;
8
+ // import.meta.hot.accept(n=>console.log(n));
9
+ // console.log(import.meta.hot.data.__Ziko__.__State__.store)
10
+ }
11
+
12
+
13
+
1
14
  export function useState(initialValue) {
2
- let value = initialValue;
3
- const subscribers = new Set();
4
- let paused = false;
15
+
16
+ // console.log(import.meta.hot.data.__Ziko__.__State__.store.get(0))
17
+
18
+ const {store, index} = __Ziko__.__State__
19
+ __Ziko__.__State__.register({
20
+ value : initialValue,
21
+ subscribers : new Set(),
22
+ paused : false
23
+ })
24
+
25
+ const current = store.get(index);
5
26
 
6
27
  function getValue() {
7
28
  return {
8
- value,
29
+ value: current.value,
9
30
  isStateGetter: () => true,
10
- _subscribe: (
11
- fn,
12
- // UIElement
13
- ) => {
14
- subscribers.add(fn);
15
-
16
- // const observer = new MutationObserver(() => {
17
- // if (!document.body.contains(UIElement.element)) {
18
- // subscribers.delete(fn);
19
- // observer.disconnect();
20
- // }
21
- // });
22
-
23
- // observer.observe(document.body, { childList: true, subtree: true });
24
- },
31
+ _subscribe: (fn) => current.subscribers.add(fn),
25
32
  };
26
33
  }
27
34
 
28
35
  function setValue(newValue) {
29
- if (paused) return;
30
- if (typeof newValue === "function") newValue = newValue(value);
31
- if (newValue !== value) {
32
- value = newValue;
33
- subscribers.forEach(fn => fn(value));
36
+ if (current.paused) return;
37
+ if (typeof newValue === "function") newValue = newValue(current.value);
38
+ if (newValue !== current.value) {
39
+ current.value = newValue;
40
+ current.subscribers.forEach(fn => fn(current.value));
34
41
  }
35
42
  }
36
43
 
37
44
  const controller = {
38
- pause: () => { paused = true; },
39
- resume: () => { paused = false; },
40
- clear: () => { subscribers.clear(); },
41
- force: (newValue) => { // force update even if paused
42
- if (typeof newValue === "function") newValue = newValue(value);
43
- value = newValue;
44
- subscribers.forEach(fn => fn(value));
45
+ pause: () => { current.paused = true; },
46
+ resume: () => { current.paused = false; },
47
+ clear: () => { current.subscribers.clear(); },
48
+ force: (newValue) => {
49
+ if (typeof newValue === "function") newValue = newValue(current.value);
50
+ current.value = newValue;
51
+ current.subscribers.forEach(fn => fn(current.value));
45
52
  },
46
- getSubscribers: () => new Set(subscribers),
53
+ getSubscribers: () => new Set(current.subscribers),
47
54
  };
48
55
 
49
56
  return [getValue, setValue, controller];
50
57
  }
51
58
 
52
- export const isStateGetter = (arg) => {
53
- return typeof(arg) === 'function' && arg?.()?.isStateGetter?.()
54
- }
55
59
 
60
+ export const isStateGetter = (arg) => {
61
+ return typeof arg === 'function' && arg?.()?.isStateGetter?.();
62
+ };
@@ -1,15 +1,26 @@
1
1
  class Tick {
2
- constructor(ms, fn) {
2
+ constructor(fn, ms, count = Infinity, start) {
3
3
  this.ms = ms;
4
4
  this.fn = fn;
5
+ this.count = count;
6
+ this.frame = 1;
5
7
  this.id = null;
6
8
  this.running = false;
9
+ if(start) this.start()
7
10
  }
8
11
 
9
12
  start() {
10
13
  if (!this.running) {
11
14
  this.running = true;
12
- this.id = setInterval(this.fn, this.ms);
15
+ this.frame = 1;
16
+ this.id = setInterval(() => {
17
+ if (this.frame > this.count) {
18
+ this.stop();
19
+ return;
20
+ }
21
+ this.fn.call(null, this);
22
+ this.frame++;
23
+ }, this.ms);
13
24
  }
14
25
  return this;
15
26
  }
@@ -27,8 +38,11 @@ class Tick {
27
38
  return this.running;
28
39
  }
29
40
  }
30
- const tick = (ms, fn) => new Tick(ms, fn);
31
- export{
41
+
42
+ // Helper factory
43
+ const tick = (fn, ms, count = Infinity, start = true) => new Tick(fn, ms, count, start);
44
+
45
+ export {
32
46
  Tick,
33
47
  tick
34
- }
48
+ };
@@ -1,4 +1,12 @@
1
- export function defineWC(name, UIElement, props = {}, { mode = 'open'} = {}) {
1
+ export function define_wc(name, UIElement, props = {}, { mode = 'open'} = {}) {
2
+ if (globalThis.customElements?.get(name)) {
3
+ console.warn(`Custom element "${name}" is already defined`);
4
+ return; // skip redefinition
5
+ }
6
+ if(name.search('-') === -1){
7
+ console.warn(`"${name}" is not a valid custom element name`);
8
+ return;
9
+ }
2
10
  globalThis.customElements?.define(
3
11
  name,
4
12
  class extends HTMLElement {