@bitstillery/mithril 3.0.3 → 3.1.2

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.
Files changed (3) hide show
  1. package/index.ts +1 -1
  2. package/package.json +10 -2
  3. package/state.ts +80 -217
package/index.ts CHANGED
@@ -102,7 +102,7 @@ setSignalRedrawCallback((sig: Signal<any>) => {
102
102
 
103
103
  // Export signals API
104
104
  export {signal, computed, effect, Signal, ComputedSignal, state, watch, registerState, getRegisteredStates}
105
- export type {State, StateOptions, Unwatch} from './state'
105
+ export type {State, StateOptions, StateSignals, Unwatch} from './state'
106
106
 
107
107
  // Export Store class
108
108
  export {Store} from './store'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bitstillery/mithril",
3
- "version": "3.0.3",
3
+ "version": "3.1.2",
4
4
  "description": "Mithril + Signals, Store and SSR",
5
5
  "license": "MIT",
6
6
  "author": "Bitstillery",
@@ -44,6 +44,9 @@
44
44
  "default": "./htm.ts"
45
45
  }
46
46
  },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
47
50
  "scripts": {
48
51
  "lint": "bun run lint:ts",
49
52
  "lint:ts": "bun run lint:ts-format && bun run lint:ts-syntax && bun run lint:ts-types",
@@ -52,7 +55,11 @@
52
55
  "lint:ts-types": "tsgo --noEmit",
53
56
  "test": "bun test",
54
57
  "prepublishOnly": "bun run lint:ts",
55
- "pack:dry-run": "npm pack --dry-run"
58
+ "pack:dry-run": "npm pack --dry-run",
59
+ "release": "standard-version && npm publish",
60
+ "release:patch": "standard-version --release-as patch && npm publish",
61
+ "release:minor": "standard-version --release-as minor && npm publish",
62
+ "release:major": "standard-version --release-as major && npm publish"
56
63
  },
57
64
  "dependencies": {
58
65
  "htm": "^3.1.1"
@@ -71,6 +78,7 @@
71
78
  "marked-gfm-heading-id": "^4.0.0",
72
79
  "oxfmt": "^0.32.0",
73
80
  "oxlint": "^1.30.0",
81
+ "standard-version": "^9.5.0",
74
82
  "tippy.js": "^6.3.7"
75
83
  }
76
84
  }
package/state.ts CHANGED
@@ -204,6 +204,16 @@ export function state<T extends Record<string, any>>(initial: T, name?: string,
204
204
  return stateCache.get(obj)
205
205
  }
206
206
 
207
+ const linkArrayParentSignal = (value: any, sig: Signal<any> | ComputedSignal<any>) => {
208
+ if (!value || typeof value !== 'object') return
209
+ if ((value as any).__isState === true && Array.isArray((value as any).__signals)) {
210
+ arrayParentSignalMap.set(value, sig as Signal<any>)
211
+ ;(value as any)._parentSignal = sig as Signal<any>
212
+ } else if (Array.isArray(value)) {
213
+ arrayParentSignalMap.set(value, sig as Signal<any>)
214
+ }
215
+ }
216
+
207
217
  // Handle arrays
208
218
  if (Array.isArray(obj)) {
209
219
  // Arrays don't get their own signalMap - they use the parent's
@@ -294,7 +304,19 @@ export function state<T extends Record<string, any>>(initial: T, name?: string,
294
304
  // Search methods also need unwrapped values for comparison
295
305
  const searchMethods = ['includes', 'indexOf', 'lastIndexOf']
296
306
  // Methods that return new arrays or strings need unwrapped values
297
- const returnMethods = ['slice', 'concat', 'flat', 'flatMap', 'join', 'toString', 'toLocaleString']
307
+ const returnMethods = [
308
+ 'slice',
309
+ 'concat',
310
+ 'flat',
311
+ 'flatMap',
312
+ 'join',
313
+ 'toString',
314
+ 'toLocaleString',
315
+ // ES2023 copy-with-mutate methods - bind to proxy so iteration unwraps signals
316
+ 'toSorted',
317
+ 'toReversed',
318
+ 'toSpliced',
319
+ ]
298
320
  // Iterator methods need unwrapped values
299
321
  const iteratorMethods = ['entries', 'keys', 'values']
300
322
  if (
@@ -554,7 +576,41 @@ export function state<T extends Record<string, any>>(initial: T, name?: string,
554
576
  // Each nested state gets its own signalMap (unless parentSignalMap is explicitly provided)
555
577
  // This prevents nested states from sharing the parent's signalMap
556
578
  const nestedSignalMap = parentSignalMap || new Map<string, Signal<any> | ComputedSignal<any>>()
557
- const wrapped = new Proxy(obj, {
579
+ let wrapped: any
580
+ const getChildContext = (): InitContext | undefined =>
581
+ context ? {...context, rootState: stateRootMap.get(wrapped) ?? wrapped} : undefined
582
+
583
+ const createPropertySignal = (originalValue: any): Signal<any> | ComputedSignal<any> => {
584
+ if (typeof originalValue === 'function') {
585
+ return createStateComputed(wrapped, () => originalValue.call(wrapped), !!context?.deferComputed)
586
+ }
587
+ if (isGetSetDescriptor(originalValue)) {
588
+ if (typeof originalValue.get === 'function') {
589
+ return createStateComputed(wrapped, () => originalValue.get.call(wrapped), !!context?.deferComputed)
590
+ }
591
+ return signal(undefined)
592
+ }
593
+ if (typeof originalValue === 'object' && originalValue !== null) {
594
+ const nestedState = initializeSignals(originalValue, undefined, getChildContext())
595
+ const sig = signal(nestedState)
596
+ if (nestedState && (nestedState as any).__isState) {
597
+ stateRootMap.set(nestedState, stateRootMap.get(wrapped) ?? wrapped)
598
+ }
599
+ linkArrayParentSignal(nestedState, sig)
600
+ return sig
601
+ }
602
+ return toSignal(originalValue)
603
+ }
604
+
605
+ const ensurePropertySignal = (target: any, prop: string | symbol, key: string) => {
606
+ if (!nestedSignalMap.has(key)) {
607
+ const originalValue = Reflect.get(target, prop)
608
+ if (originalValue === undefined) return undefined
609
+ nestedSignalMap.set(key, createPropertySignal(originalValue))
610
+ }
611
+ return nestedSignalMap.get(key)
612
+ }
613
+ wrapped = new Proxy(obj, {
558
614
  get(target, prop) {
559
615
  if (prop === '__originalKeys') return originalKeys
560
616
  if (prop === '__isState') return true
@@ -581,143 +637,25 @@ export function state<T extends Record<string, any>>(initial: T, name?: string,
581
637
  if (propStr.startsWith('$') && propStr.length > 1) {
582
638
  const key = propStr.slice(1) // Remove $ prefix
583
639
 
584
- // Ensure signal exists - initialize if needed
585
- // Use the same initialization logic as regular property access
586
- if (!nestedSignalMap.has(key)) {
587
- // First try to get from target (original object)
588
- const originalValue = Reflect.get(target, key)
589
- if (originalValue !== undefined) {
590
- if (typeof originalValue === 'function') {
591
- const computedSig = createStateComputed(
592
- wrapped,
593
- () => originalValue.call(wrapped),
594
- !!context?.deferComputed,
595
- )
596
- nestedSignalMap.set(key, computedSig)
597
- } else if (isGetSetDescriptor(originalValue)) {
598
- // Get/set descriptor -> computed signal from get function
599
- if (typeof originalValue.get === 'function') {
600
- const computedSig = createStateComputed(
601
- wrapped,
602
- () => originalValue.get.call(wrapped),
603
- !!context?.deferComputed,
604
- )
605
- nestedSignalMap.set(key, computedSig)
606
- } else {
607
- // Only setter, no getter - treat as regular signal with undefined initial value
608
- const sig = signal(undefined)
609
- nestedSignalMap.set(key, sig)
610
- }
611
- } else if (typeof originalValue === 'object' && originalValue !== null) {
612
- // Get the already-wrapped state from the wrapped object
613
- // Don't call initializeSignals again as it would create a new wrapped array
614
- const nestedState = (wrapped as any)[key]
615
- if (nestedState === undefined) {
616
- // Fallback: initialize if not already wrapped
617
- const childContext = context
618
- ? {...context, rootState: stateRootMap.get(wrapped) ?? wrapped}
619
- : undefined
620
- const initialized = initializeSignals(originalValue, undefined, childContext)
621
- const sig = signal(initialized)
622
- if (Array.isArray(initialized)) {
623
- arrayParentSignalMap.set(initialized, sig)
624
- }
625
- nestedSignalMap.set(key, sig)
626
- } else {
627
- const sig = signal(nestedState)
628
- // Store parent signal reference for arrays
629
- if (Array.isArray(nestedState)) {
630
- arrayParentSignalMap.set(nestedState, sig)
631
- }
632
- nestedSignalMap.set(key, sig)
633
- }
634
- } else {
635
- const sig = toSignal(originalValue)
636
- nestedSignalMap.set(key, sig)
637
- }
638
- } else {
639
- // Property doesn't exist - return undefined
640
- return undefined
641
- }
640
+ const sig = ensurePropertySignal(target, key, key)
641
+ if (!sig) return undefined
642
+
643
+ // Re-link array-backed values so mutations (splice, push) notify subscribers.
644
+ // Needed when $prop is accessed before .prop (e.g. watcher setup before render).
645
+ if (sig && !(sig instanceof ComputedSignal)) {
646
+ linkArrayParentSignal((sig as Signal<any>).peek(), sig)
642
647
  }
643
648
 
644
649
  // Return raw signal object (not the value)
645
- return nestedSignalMap.get(key)
650
+ return sig
646
651
  }
647
652
 
648
653
  const key = propStr
649
654
 
650
- // Check if we have a signal for this property
651
- if (!nestedSignalMap.has(key)) {
652
- // Try to get from target first (original object properties)
653
- const originalValue = Reflect.get(target, prop)
654
- if (originalValue !== undefined) {
655
- // Initialize signal for this property
656
- if (typeof originalValue === 'function') {
657
- // Function property -> computed signal
658
- const computedSig = createStateComputed(
659
- wrapped,
660
- () => originalValue.call(wrapped),
661
- !!context?.deferComputed,
662
- )
663
- nestedSignalMap.set(key, computedSig)
664
- } else if (isGetSetDescriptor(originalValue)) {
665
- // Get/set descriptor -> computed signal from get function
666
- if (typeof originalValue.get === 'function') {
667
- const computedSig = createStateComputed(
668
- wrapped,
669
- () => originalValue.get.call(wrapped),
670
- !!context?.deferComputed,
671
- )
672
- nestedSignalMap.set(key, computedSig)
673
- } else {
674
- // Only setter, no getter - treat as regular signal with undefined initial value
675
- const sig = signal(undefined)
676
- nestedSignalMap.set(key, sig)
677
- }
678
- } else if (typeof originalValue === 'object' && originalValue !== null) {
679
- // Nested object -> recursive state with its own signalMap
680
- const childContext = context
681
- ? {...context, rootState: stateRootMap.get(wrapped) ?? wrapped}
682
- : undefined
683
- const nestedState = initializeSignals(originalValue, undefined, childContext)
684
- const sig = signal(nestedState)
685
- // Store parent signal reference for arrays
686
- if (Array.isArray(nestedState)) {
687
- arrayParentSignalMap.set(nestedState, sig)
688
- }
689
- nestedSignalMap.set(key, sig)
690
- } else {
691
- // Primitive value -> signal
692
- const sig = toSignal(originalValue)
693
- nestedSignalMap.set(key, sig)
694
- }
695
- } else {
696
- // Property doesn't exist in original object
697
- // Check if it's a computed property that was added dynamically
698
- // For now, return undefined
699
- }
700
- }
701
-
702
- const sig = nestedSignalMap.get(key)
655
+ const sig = ensurePropertySignal(target, prop, key)
703
656
  if (sig) {
704
657
  // Access signal.value to track component dependency
705
- const value = sig.value
706
- // Always ensure parent signal is stored for arrays (in case it wasn't stored during initialization)
707
- // Check for wrapped arrays by looking for __isState and __signals properties
708
- // Array.isArray() may return false for Proxies, so we check __isState instead
709
- if (value && typeof value === 'object') {
710
- if ((value as any).__isState === true && Array.isArray((value as any).__signals)) {
711
- // This is a wrapped array - store parent signal
712
- arrayParentSignalMap.set(value, sig as Signal<any>)
713
- // Also store directly on the Proxy as a fallback
714
- ;(value as any)._parentSignal = sig as Signal<any>
715
- } else if (Array.isArray(value)) {
716
- // Regular array (shouldn't happen but just in case)
717
- arrayParentSignalMap.set(value, sig as Signal<any>)
718
- }
719
- }
720
- return value
658
+ return sig.value
721
659
  }
722
660
 
723
661
  // Fallback to original property
@@ -786,47 +724,22 @@ export function state<T extends Record<string, any>>(initial: T, name?: string,
786
724
  const sig = nestedSignalMap.get(key)
787
725
  if (sig && !(sig instanceof ComputedSignal)) {
788
726
  if (typeof value === 'object' && value !== null) {
789
- // Nested object -> recursive state with its own signalMap
790
- const childContext = context
791
- ? {...context, rootState: stateRootMap.get(wrapped) ?? wrapped}
792
- : undefined
793
- const nestedState = initializeSignals(value, undefined, childContext)
794
- // Store parent signal reference for arrays
795
- if (Array.isArray(nestedState)) {
796
- arrayParentSignalMap.set(nestedState, sig as Signal<any>)
727
+ const nestedState = initializeSignals(value, undefined, getChildContext())
728
+ if (nestedState && (nestedState as any).__isState) {
729
+ stateRootMap.set(nestedState, stateRootMap.get(wrapped) ?? wrapped)
797
730
  }
731
+ linkArrayParentSignal(nestedState, sig)
798
732
  ;(sig as Signal<any>).value = nestedState
799
733
  } else {
800
734
  ;(sig as Signal<any>).value = value
801
735
  }
802
736
  } else {
803
737
  // Replace computed with regular signal
804
- if (typeof value === 'object' && value !== null && Array.isArray(value)) {
805
- const childContext = context
806
- ? {...context, rootState: stateRootMap.get(wrapped) ?? wrapped}
807
- : undefined
808
- const nestedState = initializeSignals(value, undefined, childContext)
809
- const sig = signal(nestedState)
810
- arrayParentSignalMap.set(nestedState, sig)
811
- nestedSignalMap.set(key, sig)
812
- } else {
813
- nestedSignalMap.set(key, toSignal(value))
814
- }
738
+ nestedSignalMap.set(key, createPropertySignal(value))
815
739
  }
816
740
  } else {
817
741
  // Create new signal
818
- if (typeof value === 'object' && value !== null) {
819
- const childContext = context ? {...context, rootState: stateRootMap.get(wrapped) ?? wrapped} : undefined
820
- const nestedState = initializeSignals(value, undefined, childContext)
821
- const sig = signal(nestedState)
822
- // Store parent signal reference for arrays
823
- if (Array.isArray(nestedState)) {
824
- arrayParentSignalMap.set(nestedState, sig)
825
- }
826
- nestedSignalMap.set(key, sig)
827
- } else {
828
- nestedSignalMap.set(key, toSignal(value))
829
- }
742
+ nestedSignalMap.set(key, createPropertySignal(value))
830
743
  }
831
744
 
832
745
  return true
@@ -890,6 +803,11 @@ export function state<T extends Record<string, any>>(initial: T, name?: string,
890
803
 
891
804
  stateRootMap.set(wrapped, context?.rootState ?? wrapped)
892
805
  stateCache.set(obj, wrapped)
806
+ for (const key of originalKeys) {
807
+ if (!nestedSignalMap.has(key)) {
808
+ nestedSignalMap.set(key, createPropertySignal((obj as any)[key]))
809
+ }
810
+ }
893
811
  return wrapped
894
812
  }
895
813
 
@@ -900,61 +818,6 @@ export function state<T extends Record<string, any>>(initial: T, name?: string,
900
818
  stateDeferredFlags.set(wrapped, {allowed: false})
901
819
  }
902
820
 
903
- // Pre-initialize all signals from the initial object so they're available immediately
904
- // This ensures $s.$property works even if $s.property hasn't been accessed yet
905
- if (typeof initial === 'object' && initial !== null && !Array.isArray(initial)) {
906
- for (const key in initial) {
907
- if (Object.prototype.hasOwnProperty.call(initial, key)) {
908
- if (!signalMap.has(key)) {
909
- const value = initial[key]
910
- if (typeof value === 'function') {
911
- const computedSig = createStateComputed(wrapped, () => value.call(wrapped), deferComputed)
912
- signalMap.set(key, computedSig)
913
- } else if (isGetSetDescriptor(value)) {
914
- // Get/set descriptor -> computed signal from get function
915
- if (typeof value.get === 'function') {
916
- const computedSig = createStateComputed(wrapped, () => value.get.call(wrapped), deferComputed)
917
- signalMap.set(key, computedSig)
918
- } else {
919
- // Only setter, no getter - treat as regular signal with undefined initial value
920
- const sig = signal(undefined)
921
- signalMap.set(key, sig)
922
- }
923
- } else if (typeof value === 'object' && value !== null) {
924
- // Get the already-wrapped state from stateCache (bypass Proxy to get the actual wrapped value)
925
- // This ensures we get the same wrapped array that was created during initializeSignals
926
- const preInitContext = initContext ? {...initContext, rootState: wrapped} : undefined
927
- const nestedState = stateCache.has(value)
928
- ? stateCache.get(value)
929
- : initializeSignals(value, undefined, preInitContext)
930
- if (nestedState && (nestedState as any).__isState) stateRootMap.set(nestedState, wrapped)
931
- const sig = signal(nestedState)
932
- // Always store parent signal reference for arrays
933
- // Check for wrapped arrays by looking for __isState and __signals properties
934
- // Array.isArray() may return false for Proxies, so we check __isState instead
935
- if (
936
- nestedState &&
937
- typeof nestedState === 'object' &&
938
- (nestedState as any).__isState === true &&
939
- Array.isArray((nestedState as any).__signals)
940
- ) {
941
- arrayParentSignalMap.set(nestedState, sig)
942
- // Also store directly on the Proxy as a fallback
943
- ;(nestedState as any)._parentSignal = sig
944
- } else if (Array.isArray(nestedState)) {
945
- // Fallback for regular arrays (shouldn't happen but just in case)
946
- arrayParentSignalMap.set(nestedState, sig)
947
- }
948
- signalMap.set(key, sig)
949
- } else {
950
- const sig = toSignal(value)
951
- signalMap.set(key, sig)
952
- }
953
- }
954
- }
955
- }
956
- }
957
-
958
821
  // Register state for SSR serialization when name is provided (required for hydration)
959
822
  if (name && typeof name === 'string' && name.trim() !== '') {
960
823
  registerState(name, wrapped, initial)
@@ -969,7 +832,7 @@ export function state<T extends Record<string, any>>(initial: T, name?: string,
969
832
  * - Nested objects: $prop => Signal<State<T[K]>>
970
833
  * - Functions: $prop => ComputedSignal (computed from getter)
971
834
  */
972
- type StateSignals<T extends Record<string, any>> = {
835
+ export type StateSignals<T extends Record<string, any>> = {
973
836
  [K in keyof T as K extends string ? `$${K}` : never]: T[K] extends (...args: any[]) => any
974
837
  ? ComputedSignal<any>
975
838
  : T[K] extends object