gyosjs 0.4.0 → 0.4.1

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/README.md CHANGED
@@ -34,7 +34,7 @@ GyosJS is not trying to replace a full SPA framework. If your app is primarily c
34
34
  Use the browser auto-init build when you want HTML-first usage with minimal setup.
35
35
 
36
36
  ```html
37
- <script src="https://cdn.jsdelivr.net/npm/gyosjs@0.4.0/dist/gyos.auto.min.js"></script>
37
+ <script src="https://cdn.jsdelivr.net/npm/gyosjs@0.4.1/dist/gyos.auto.min.js"></script>
38
38
  ```
39
39
 
40
40
  That build attaches `window.Gyos`, starts the router when `g-boost` exists, injects transition styles, and mounts scopes when the page is ready.
@@ -121,6 +121,8 @@ With `g-boost`, same-origin links and forms can be intercepted and swapped into
121
121
 
122
122
  ## Development
123
123
 
124
+ Use Node.js 22 for dependency installation and tests. Library builds and package consumption remain checked on Node.js 18.20 with `npm run check:package`.
125
+
124
126
  ```bash
125
127
  npm install
126
128
  npm run build
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * GyosJS v0.4.0
2
+ * GyosJS v0.4.1
3
3
  * (c) 2026
4
4
  * @license MIT
5
5
  */
@@ -183,10 +183,35 @@ function hasStructuralParent(el) {
183
183
  * count.value++; // Triggers effect
184
184
  * ```
185
185
  */
186
- /**
187
- * Current effect being tracked
188
- */
189
186
  let currentEffect = null;
187
+ function track(source) {
188
+ if (!currentEffect)
189
+ return;
190
+ source.subscribers.add(currentEffect);
191
+ currentEffect.__dependencies?.add(source.subscribers);
192
+ currentEffect.__versions?.set(source, source.version);
193
+ }
194
+ function dependenciesChanged(versions) {
195
+ return untrack(() => {
196
+ for (const [source, version] of versions) {
197
+ source.refresh?.();
198
+ if (source.version !== version)
199
+ return true;
200
+ }
201
+ return false;
202
+ });
203
+ }
204
+ // Invalidate derived caches synchronously; only user effects are deferred.
205
+ function notifySubscribers(subscribers) {
206
+ for (const sub of Array.from(subscribers)) {
207
+ if (sub.__invalidate)
208
+ sub();
209
+ else if (isBatching)
210
+ batchedEffects.add(sub);
211
+ else
212
+ queueEffect(sub);
213
+ }
214
+ }
190
215
  // Simple counter for debug-friendly IDs
191
216
  let signalIdCounter = 0;
192
217
  const WARN_SUBSCRIBERS = () => typeof window !== 'undefined' && window.GYOS_DEBUG_WARN_SUBSCRIBERS;
@@ -224,6 +249,9 @@ function flushEffectQueue() {
224
249
  }
225
250
  });
226
251
  isFlushingQueue = false;
252
+ // A plain subscribe callback can enqueue more work without entering effect().
253
+ if (effectQueue.size > 0)
254
+ queueMicrotask(flushEffectQueue);
227
255
  }
228
256
  /**
229
257
  * Queue an effect for execution in next microtask
@@ -282,17 +310,14 @@ function signal(initialValue, debugOrOptions) {
282
310
  const debugId = ++signalIdCounter;
283
311
  let value = initialValue;
284
312
  const subscribers = new Set();
313
+ const source = { subscribers, version: 0 };
285
314
  let warnedSubscribers = false;
286
315
  const read = () => {
287
316
  // Track dependency
288
317
  if (currentEffect) {
289
- // IMPORTANT: Only add if not already subscribed (prevent duplicates)
290
- if (!subscribers.has(currentEffect)) {
291
- subscribers.add(currentEffect);
292
- // Store the subscribers Set in the effect's dependencies for cleanup
293
- if (currentEffect.__dependencies) {
294
- currentEffect.__dependencies.add(subscribers);
295
- }
318
+ const wasSubscribed = subscribers.has(currentEffect);
319
+ track(source);
320
+ if (!wasSubscribed) {
296
321
  if (DEBUG_VERBOSE()) {
297
322
  console.log('[Signal]', formatDebugLabel(debugId, debugLabel), 'Added subscriber, total:', subscribers.size);
298
323
  }
@@ -311,18 +336,13 @@ function signal(initialValue, debugOrOptions) {
311
336
  // For objects/arrays, check if it's actually the same reference
312
337
  // Deep equality is expensive, let user handle it
313
338
  value = newValue;
339
+ source.version++;
314
340
  DEBUG_VERBOSE() && console.log('[Signal]', formatDebugLabel(debugId, debugLabel), 'Value changed to:', newValue);
315
341
  notify();
316
342
  };
317
343
  const notify = () => {
318
344
  DEBUG_VERBOSE() && console.log('[Signal]', formatDebugLabel(debugId, debugLabel), 'Notifying', subscribers.size, 'subscribers');
319
- if (isBatching) {
320
- subscribers.forEach(sub => batchedEffects.add(sub));
321
- }
322
- else {
323
- // Always queue effects to prevent cascading updates
324
- subscribers.forEach(sub => queueEffect(sub));
325
- }
345
+ notifySubscribers(subscribers);
326
346
  };
327
347
  // Create callable signal (like Angular/SolidJS)
328
348
  // Call without args = read, call with arg = write
@@ -357,8 +377,16 @@ function signal(initialValue, debugOrOptions) {
357
377
  },
358
378
  subscribe: {
359
379
  value: function (fn) {
360
- subscribers.add(fn);
361
- return () => subscribers.delete(fn);
380
+ let active = true;
381
+ const callback = () => { if (active)
382
+ untrack(fn); };
383
+ subscribers.add(callback);
384
+ return () => {
385
+ active = false;
386
+ subscribers.delete(callback);
387
+ effectQueue.delete(callback);
388
+ batchedEffects.delete(callback);
389
+ };
362
390
  },
363
391
  enumerable: true,
364
392
  configurable: true
@@ -418,49 +446,60 @@ function computed(fn) {
418
446
  let value;
419
447
  let isDirty = true;
420
448
  let isFirstRun = true;
449
+ let computing = false;
421
450
  const subscribers = new Set();
422
- let cleanup;
451
+ const dependencies = new Set();
452
+ const versions = new Map();
453
+ const source = { subscribers, version: 0, refresh: () => compute() };
454
+ const invalidate = () => {
455
+ // A later batched write must also move already queued consumers into
456
+ // the batch, even when an earlier ordinary write dirtied this cache.
457
+ if (isDirty && !isBatching)
458
+ return;
459
+ isDirty = true;
460
+ notifySubscribers(subscribers);
461
+ };
462
+ invalidate.__invalidate = true;
463
+ invalidate.__dependencies = dependencies;
464
+ invalidate.__versions = versions;
423
465
  const compute = () => {
424
466
  if (!isDirty && !isFirstRun)
425
467
  return;
426
- // Cleanup previous dependencies
427
- if (cleanup)
428
- cleanup();
429
- // Track dependencies in an effect
430
- cleanup = effect(() => {
431
- try {
432
- const newValue = fn();
433
- // Only update if changed
434
- if (isFirstRun || value !== newValue) {
435
- value = newValue;
436
- isFirstRun = false;
437
- isDirty = false;
438
- // Notify subscribers
439
- if (!isBatching) {
440
- subscribers.forEach(sub => queueEffect(sub));
441
- }
442
- else {
443
- subscribers.forEach(sub => batchedEffects.add(sub));
444
- }
445
- }
468
+ if (computing)
469
+ throw new Error('[Computed] Circular dependency');
470
+ computing = true;
471
+ const previousEffect = currentEffect;
472
+ try {
473
+ // A dirty upstream computed may still resolve to its previous value.
474
+ if (!isFirstRun && !dependenciesChanged(versions)) {
475
+ isDirty = false;
476
+ return;
446
477
  }
447
- catch (error) {
448
- console.error('[Computed] Error in computation:', error);
449
- throw error;
478
+ dependencies.forEach(dependency => dependency.delete(invalidate));
479
+ dependencies.clear();
480
+ versions.clear();
481
+ currentEffect = invalidate;
482
+ const nextValue = fn();
483
+ if (isFirstRun || !Object.is(value, nextValue)) {
484
+ value = nextValue;
485
+ source.version++;
450
486
  }
451
- });
452
- isDirty = false;
487
+ isFirstRun = false;
488
+ isDirty = false;
489
+ }
490
+ catch (error) {
491
+ // Preserve error isolation and the last successful value.
492
+ console.error('[Computed] Error in computation:', error);
493
+ isDirty = false;
494
+ }
495
+ finally {
496
+ currentEffect = previousEffect;
497
+ computing = false;
498
+ }
453
499
  };
454
500
  const read = () => {
455
- if (isDirty || isFirstRun)
456
- compute();
457
- // Track dependency
458
- if (currentEffect && !subscribers.has(currentEffect)) {
459
- subscribers.add(currentEffect);
460
- if (currentEffect.__dependencies) {
461
- currentEffect.__dependencies.add(subscribers);
462
- }
463
- }
501
+ compute();
502
+ track(source);
464
503
  return value;
465
504
  };
466
505
  // Create callable computed (read-only)
@@ -485,8 +524,14 @@ function computed(fn) {
485
524
  },
486
525
  subscribe: {
487
526
  value: function (fn) {
488
- subscribers.add(fn);
489
- return () => subscribers.delete(fn);
527
+ // Establish dependencies even if subscribe() is the first access.
528
+ let first = true;
529
+ return effect(() => {
530
+ read();
531
+ if (!first)
532
+ untrack(fn);
533
+ first = false;
534
+ });
490
535
  },
491
536
  enumerable: true,
492
537
  configurable: true
@@ -537,11 +582,15 @@ function computed(fn) {
537
582
  */
538
583
  function effect(fn) {
539
584
  let cleanup;
540
- let dependencies = new Set();
585
+ const dependencies = new Set();
586
+ const versions = new Map();
541
587
  let active = true;
588
+ let initialized = false;
542
589
  const execute = () => {
543
590
  if (!active)
544
591
  return;
592
+ if (initialized && !dependenciesChanged(versions))
593
+ return;
545
594
  // Prevent recursive execution - if already running, skip
546
595
  if (runningEffects.has(execute)) {
547
596
  DEBUG() && console.warn('[Effect] Skipped recursive execution');
@@ -556,10 +605,13 @@ function effect(fn) {
556
605
  });
557
606
  DEBUG() && console.log('[Effect] Cleaned up dependencies, count:', dependencies.size);
558
607
  dependencies.clear();
608
+ versions.clear();
559
609
  // Clean up previous run
560
610
  if (cleanup) {
611
+ const previousCleanup = cleanup;
612
+ cleanup = undefined;
561
613
  try {
562
- cleanup();
614
+ untrack(previousCleanup);
563
615
  }
564
616
  catch (error) {
565
617
  console.error('[Effect] Error in cleanup:', error);
@@ -570,6 +622,7 @@ function effect(fn) {
570
622
  currentEffect = execute;
571
623
  // Store dependencies for cleanup
572
624
  execute.__dependencies = dependencies;
625
+ execute.__versions = versions;
573
626
  try {
574
627
  const nextCleanup = fn();
575
628
  cleanup = typeof nextCleanup === 'function' ? nextCleanup : undefined;
@@ -579,6 +632,7 @@ function effect(fn) {
579
632
  // Don't throw - allow other effects to continue
580
633
  }
581
634
  finally {
635
+ initialized = true;
582
636
  currentEffect = prevEffect;
583
637
  effectDepth--;
584
638
  runningEffects.delete(execute);
@@ -595,14 +649,17 @@ function effect(fn) {
595
649
  if (!active)
596
650
  return;
597
651
  active = false;
652
+ effectQueue.delete(execute);
653
+ batchedEffects.delete(execute);
598
654
  // Remove from all dependencies
599
655
  dependencies.forEach(depSet => {
600
656
  depSet.delete(execute);
601
657
  });
602
658
  dependencies.clear();
659
+ versions.clear();
603
660
  if (cleanup) {
604
661
  try {
605
- cleanup();
662
+ untrack(cleanup);
606
663
  }
607
664
  catch (error) {
608
665
  console.error('[Effect] Error in final cleanup:', error);
@@ -662,6 +719,8 @@ function batch(fn) {
662
719
  const effects = Array.from(batchedEffects);
663
720
  batchedEffects.clear();
664
721
  effects.forEach(effect => {
722
+ // This batch also settles any earlier queued write to this observer.
723
+ effectQueue.delete(effect);
665
724
  try {
666
725
  effect();
667
726
  }
@@ -8706,7 +8765,7 @@ const Gyos = {
8706
8765
  portalCreate,
8707
8766
  portalDestroy,
8708
8767
  setCspNonce,
8709
- version: '0.4.0'
8768
+ version: '0.4.1'
8710
8769
  };
8711
8770
 
8712
8771
  const standardExpressionRuntime = {