@thoughtbot/superglue 2.0.0-alpha.4 → 2.0.0-alpha.7

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.
@@ -429,9 +429,208 @@ var prependToFragment = (0, import_toolkit.createAction)(
429
429
  }
430
430
  );
431
431
 
432
+ // lib/utils/proxy.ts
433
+ var ORIGINAL_TARGET = Symbol("@@originalTarget");
434
+ var ARRAY_GETTER_METHODS = /* @__PURE__ */ new Set([
435
+ Symbol.iterator,
436
+ "at",
437
+ "concat",
438
+ "entries",
439
+ "every",
440
+ "filter",
441
+ "find",
442
+ "findIndex",
443
+ "flat",
444
+ "flatMap",
445
+ "forEach",
446
+ "includes",
447
+ "indexOf",
448
+ "join",
449
+ "keys",
450
+ "lastIndexOf",
451
+ "map",
452
+ "reduce",
453
+ "reduceRight",
454
+ "slice",
455
+ "some",
456
+ "toString",
457
+ "values"
458
+ ]);
459
+ var ARRAY_SETTER_METHODS = /* @__PURE__ */ new Set([
460
+ "copyWithin",
461
+ "fill",
462
+ "pop",
463
+ "push",
464
+ "reverse",
465
+ "shift",
466
+ "sort",
467
+ "splice",
468
+ "unshift"
469
+ ]);
470
+ function isArraySetter(prop) {
471
+ return ARRAY_SETTER_METHODS.has(prop);
472
+ }
473
+ function isArrayGetter(prop) {
474
+ return ARRAY_GETTER_METHODS.has(prop);
475
+ }
476
+ function convertToInt(prop) {
477
+ if (typeof prop === "symbol") return null;
478
+ const num = Number(prop);
479
+ return Number.isInteger(num) ? num : null;
480
+ }
481
+ function isFragmentReference(value) {
482
+ return !!value && typeof value === "object" && "__id" in value && typeof value.__id === "string";
483
+ }
484
+ function createArrayProxy(arrayData, fragments, dependencies, proxyCache) {
485
+ if (proxyCache && proxyCache.has(arrayData)) {
486
+ return proxyCache.get(arrayData);
487
+ }
488
+ const proxy = new Proxy(arrayData, {
489
+ get(target, prop) {
490
+ if (prop === ORIGINAL_TARGET) {
491
+ return target;
492
+ }
493
+ if (isArrayGetter(prop)) {
494
+ const method = target[prop];
495
+ if (typeof method === "function") {
496
+ return function(...args) {
497
+ return Reflect.apply(method, proxy, args);
498
+ };
499
+ }
500
+ return method;
501
+ }
502
+ if (isArraySetter(prop)) {
503
+ throw new Error(
504
+ `Cannot mutate proxy array. Use Redux actions to update state.`
505
+ );
506
+ }
507
+ const index = convertToInt(prop);
508
+ if (index !== null && index >= 0 && index < target.length) {
509
+ const item = target[index];
510
+ if (isFragmentReference(item)) {
511
+ dependencies.add(item.__id);
512
+ const fragmentData = fragments.current[item.__id];
513
+ if (!fragmentData) {
514
+ throw new Error(`Fragment with id "${item.__id}" not found`);
515
+ }
516
+ return createProxy(fragmentData, fragments, dependencies, proxyCache);
517
+ }
518
+ if (typeof item === "object" && item !== null) {
519
+ if ("$$typeof" in item) {
520
+ return item;
521
+ } else {
522
+ return createProxy(
523
+ item,
524
+ fragments,
525
+ dependencies,
526
+ proxyCache
527
+ );
528
+ }
529
+ }
530
+ return item;
531
+ }
532
+ return Reflect.get(target, prop);
533
+ },
534
+ has(target, prop) {
535
+ if (prop === ORIGINAL_TARGET) {
536
+ return true;
537
+ }
538
+ return Reflect.has(target, prop);
539
+ },
540
+ set() {
541
+ throw new Error(
542
+ "Cannot mutate proxy array. Use Redux actions to update state."
543
+ );
544
+ },
545
+ deleteProperty() {
546
+ throw new Error(
547
+ "Cannot delete properties on proxy array. Use Redux actions to update state."
548
+ );
549
+ },
550
+ defineProperty() {
551
+ throw new Error(
552
+ "Cannot define properties on proxy array. Use Redux actions to update state."
553
+ );
554
+ }
555
+ });
556
+ if (proxyCache) {
557
+ proxyCache.set(arrayData, proxy);
558
+ }
559
+ return proxy;
560
+ }
561
+ function createObjectProxy(objectData, fragments, dependencies, proxyCache) {
562
+ if (proxyCache && proxyCache.has(objectData)) {
563
+ return proxyCache.get(objectData);
564
+ }
565
+ const proxy = new Proxy(objectData, {
566
+ get(target, prop) {
567
+ if (prop === ORIGINAL_TARGET) {
568
+ return target;
569
+ }
570
+ const value = target[prop];
571
+ if (isFragmentReference(value)) {
572
+ dependencies.add(value.__id);
573
+ const fragmentData = fragments.current[value.__id];
574
+ if (!fragmentData) {
575
+ throw new Error(`Fragment with id "${value.__id}" not found`);
576
+ }
577
+ return createProxy(fragmentData, fragments, dependencies, proxyCache);
578
+ }
579
+ if (typeof value === "object" && value !== null) {
580
+ if ("$$typeof" in value) {
581
+ return value;
582
+ } else if (Array.isArray(value)) {
583
+ return createArrayProxy(value, fragments, dependencies, proxyCache);
584
+ } else {
585
+ return createObjectProxy(value, fragments, dependencies, proxyCache);
586
+ }
587
+ }
588
+ return value;
589
+ },
590
+ has(target, prop) {
591
+ if (prop === ORIGINAL_TARGET) {
592
+ return true;
593
+ }
594
+ return Reflect.has(target, prop);
595
+ },
596
+ set() {
597
+ throw new Error(
598
+ "Cannot mutate proxy object. Use Redux actions to update state."
599
+ );
600
+ },
601
+ deleteProperty() {
602
+ throw new Error(
603
+ "Cannot delete properties on proxy object. Use Redux actions to update state."
604
+ );
605
+ },
606
+ defineProperty() {
607
+ throw new Error(
608
+ "Cannot define properties on proxy object. Use Redux actions to update state."
609
+ );
610
+ }
611
+ });
612
+ if (proxyCache) {
613
+ proxyCache.set(objectData, proxy);
614
+ }
615
+ return proxy;
616
+ }
617
+ function createProxy(content, fragments, dependencies, proxyCache) {
618
+ if (!content || typeof content !== "object") {
619
+ return content;
620
+ }
621
+ if ("$$typeof" in content) {
622
+ return content;
623
+ }
624
+ if (Array.isArray(content)) {
625
+ return createArrayProxy(content, fragments, dependencies, proxyCache);
626
+ }
627
+ return createObjectProxy(content, fragments, dependencies, proxyCache);
628
+ }
629
+
432
630
  // lib/action_creators/requests.ts
433
631
  function handleFetchErr(err, fetchArgs, dispatch) {
434
632
  dispatch(superglueError({ message: err.message }));
633
+ console.error(err);
435
634
  throw err;
436
635
  }
437
636
  function buildMeta(pageKey, page, state, rsp, fetchArgs) {
@@ -470,7 +669,7 @@ var remote = (path, {
470
669
  dispatch(beforeRemote({ currentPageKey, fetchArgs }));
471
670
  dispatch(beforeFetch({ fetchArgs }));
472
671
  return fetch(...fetchArgs).then(parseResponse).then(({ rsp, json }) => {
473
- const { superglue, pages = {} } = getState();
672
+ const { superglue, pages = {}, fragments } = getState();
474
673
  let pageKey;
475
674
  if (targetPageKey === void 0) {
476
675
  const isGet = fetchArgs[1].method === "GET";
@@ -503,7 +702,15 @@ the same page. Or if you're sure you want to proceed, use force: true.
503
702
  }
504
703
  }
505
704
  dispatch(receiveResponse({ pageKey, response: json }));
506
- const page = beforeSave(pages[pageKey], json);
705
+ const existingPage = createProxy(
706
+ pages[pageKey],
707
+ { current: fragments },
708
+ /* @__PURE__ */ new Set(),
709
+ /* @__PURE__ */ new WeakMap()
710
+ );
711
+ const page = JSON.parse(
712
+ JSON.stringify(beforeSave(existingPage, json))
713
+ );
507
714
  return dispatch(saveAndProcessPage(pageKey, page)).then(() => meta);
508
715
  }).catch((e) => handleFetchErr(e, fetchArgs, dispatch));
509
716
  };
@@ -542,7 +749,7 @@ var visit = (path, {
542
749
  );
543
750
  lastVisitController = controller;
544
751
  return fetch(...fetchArgs).then(parseResponse).then(({ rsp, json }) => {
545
- const { superglue, pages = {} } = getState();
752
+ const { superglue, pages = {}, fragments } = getState();
546
753
  const isGet = fetchArgs[1].method === "GET";
547
754
  const pageKey = calculatePageKey(rsp, isGet, currentPageKey);
548
755
  const meta = buildMeta(pageKey, json, superglue, rsp, fetchArgs);
@@ -585,7 +792,15 @@ to the same page.
585
792
  )
586
793
  };
587
794
  dispatch(receiveResponse({ pageKey, response: json }));
588
- const page = beforeSave(pages[pageKey], json);
795
+ const existingPage = createProxy(
796
+ pages[pageKey],
797
+ { current: fragments },
798
+ /* @__PURE__ */ new Set(),
799
+ /* @__PURE__ */ new WeakMap()
800
+ );
801
+ const page = JSON.parse(
802
+ JSON.stringify(beforeSave(existingPage, json))
803
+ );
589
804
  return dispatch(saveAndProcessPage(pageKey, page)).then(() => visitMeta);
590
805
  }).catch((e) => handleFetchErr(e, fetchArgs, dispatch));
591
806
  };
@@ -730,204 +945,6 @@ var handleStreamResponse = (response) => {
730
945
  };
731
946
  };
732
947
 
733
- // lib/utils/proxy.ts
734
- var ORIGINAL_TARGET = Symbol("@@originalTarget");
735
- var ARRAY_GETTER_METHODS = /* @__PURE__ */ new Set([
736
- Symbol.iterator,
737
- "at",
738
- "concat",
739
- "entries",
740
- "every",
741
- "filter",
742
- "find",
743
- "findIndex",
744
- "flat",
745
- "flatMap",
746
- "forEach",
747
- "includes",
748
- "indexOf",
749
- "join",
750
- "keys",
751
- "lastIndexOf",
752
- "map",
753
- "reduce",
754
- "reduceRight",
755
- "slice",
756
- "some",
757
- "toString",
758
- "values"
759
- ]);
760
- var ARRAY_SETTER_METHODS = /* @__PURE__ */ new Set([
761
- "copyWithin",
762
- "fill",
763
- "pop",
764
- "push",
765
- "reverse",
766
- "shift",
767
- "sort",
768
- "splice",
769
- "unshift"
770
- ]);
771
- function isArraySetter(prop) {
772
- return ARRAY_SETTER_METHODS.has(prop);
773
- }
774
- function isArrayGetter(prop) {
775
- return ARRAY_GETTER_METHODS.has(prop);
776
- }
777
- function convertToInt(prop) {
778
- if (typeof prop === "symbol") return null;
779
- const num = Number(prop);
780
- return Number.isInteger(num) ? num : null;
781
- }
782
- function isFragmentReference(value) {
783
- return !!value && typeof value === "object" && "__id" in value && typeof value.__id === "string";
784
- }
785
- function createArrayProxy(arrayData, fragments, dependencies, proxyCache) {
786
- if (proxyCache && proxyCache.has(arrayData)) {
787
- return proxyCache.get(arrayData);
788
- }
789
- const proxy = new Proxy(arrayData, {
790
- get(target, prop) {
791
- if (prop === ORIGINAL_TARGET) {
792
- return target;
793
- }
794
- if (isArrayGetter(prop)) {
795
- const method = target[prop];
796
- if (typeof method === "function") {
797
- return function(...args) {
798
- return Reflect.apply(method, proxy, args);
799
- };
800
- }
801
- return method;
802
- }
803
- if (isArraySetter(prop)) {
804
- throw new Error(
805
- `Cannot mutate proxy array. Use Redux actions to update state.`
806
- );
807
- }
808
- const index = convertToInt(prop);
809
- if (index !== null && index >= 0 && index < target.length) {
810
- const item = target[index];
811
- if (isFragmentReference(item)) {
812
- dependencies.add(item.__id);
813
- const fragmentData = fragments.current[item.__id];
814
- if (!fragmentData) {
815
- throw new Error(`Fragment with id "${item.__id}" not found`);
816
- }
817
- return createProxy(fragmentData, fragments, dependencies, proxyCache);
818
- }
819
- if (typeof item === "object" && item !== null) {
820
- if ("$$typeof" in item) {
821
- return item;
822
- } else {
823
- return createProxy(
824
- item,
825
- fragments,
826
- dependencies,
827
- proxyCache
828
- );
829
- }
830
- }
831
- return item;
832
- }
833
- return Reflect.get(target, prop);
834
- },
835
- has(target, prop) {
836
- if (prop === ORIGINAL_TARGET) {
837
- return true;
838
- }
839
- return Reflect.has(target, prop);
840
- },
841
- set() {
842
- throw new Error(
843
- "Cannot mutate proxy array. Use Redux actions to update state."
844
- );
845
- },
846
- deleteProperty() {
847
- throw new Error(
848
- "Cannot delete properties on proxy array. Use Redux actions to update state."
849
- );
850
- },
851
- defineProperty() {
852
- throw new Error(
853
- "Cannot define properties on proxy array. Use Redux actions to update state."
854
- );
855
- }
856
- });
857
- if (proxyCache) {
858
- proxyCache.set(arrayData, proxy);
859
- }
860
- return proxy;
861
- }
862
- function createObjectProxy(objectData, fragments, dependencies, proxyCache) {
863
- if (proxyCache && proxyCache.has(objectData)) {
864
- return proxyCache.get(objectData);
865
- }
866
- const proxy = new Proxy(objectData, {
867
- get(target, prop) {
868
- if (prop === ORIGINAL_TARGET) {
869
- return target;
870
- }
871
- const value = target[prop];
872
- if (isFragmentReference(value)) {
873
- dependencies.add(value.__id);
874
- const fragmentData = fragments.current[value.__id];
875
- if (!fragmentData) {
876
- throw new Error(`Fragment with id "${value.__id}" not found`);
877
- }
878
- return createProxy(fragmentData, fragments, dependencies, proxyCache);
879
- }
880
- if (typeof value === "object" && value !== null) {
881
- if ("$$typeof" in value) {
882
- return value;
883
- } else if (Array.isArray(value)) {
884
- return createArrayProxy(value, fragments, dependencies, proxyCache);
885
- } else {
886
- return createObjectProxy(value, fragments, dependencies, proxyCache);
887
- }
888
- }
889
- return value;
890
- },
891
- has(target, prop) {
892
- if (prop === ORIGINAL_TARGET) {
893
- return true;
894
- }
895
- return Reflect.has(target, prop);
896
- },
897
- set() {
898
- throw new Error(
899
- "Cannot mutate proxy object. Use Redux actions to update state."
900
- );
901
- },
902
- deleteProperty() {
903
- throw new Error(
904
- "Cannot delete properties on proxy object. Use Redux actions to update state."
905
- );
906
- },
907
- defineProperty() {
908
- throw new Error(
909
- "Cannot define properties on proxy object. Use Redux actions to update state."
910
- );
911
- }
912
- });
913
- if (proxyCache) {
914
- proxyCache.set(objectData, proxy);
915
- }
916
- return proxy;
917
- }
918
- function createProxy(content, fragments, dependencies, proxyCache) {
919
- if (!content || typeof content !== "object") {
920
- return content;
921
- }
922
- if ("$$typeof" in content) {
923
- return content;
924
- }
925
- if (Array.isArray(content)) {
926
- return createArrayProxy(content, fragments, dependencies, proxyCache);
927
- }
928
- return createObjectProxy(content, fragments, dependencies, proxyCache);
929
- }
930
-
931
948
  // lib/action_creators/index.ts
932
949
  function fetchDeferments(pageKey, defers = []) {
933
950
  pageKey = urlToPageKey(pageKey);