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

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.
@@ -1,4 +1,4 @@
1
- import { Action, ThunkDispatch, EnhancedStore, Tuple, StoreEnhancer, ThunkAction } from '@reduxjs/toolkit';
1
+ import { Action, EnhancedStore, Tuple, StoreEnhancer, ThunkDispatch, ThunkAction } from '@reduxjs/toolkit';
2
2
  import { History } from 'history';
3
3
 
4
4
  type FetchArgs = [string, BasicRequestInit];
@@ -42,8 +42,8 @@ interface RemoteProps extends BaseProps {
42
42
  pageKey?: PageKey;
43
43
  force?: boolean;
44
44
  }
45
- interface BeforeSave {
46
- <T extends PageResponse>(prevPage: SaveResponse, receivedPage: T): T;
45
+ interface BeforeSave<T = JSONMappable> {
46
+ <U extends SaveResponse<T> | GraftResponse<T>>(prevPage: Page<T>, receivedPage: U): U;
47
47
  }
48
48
  interface ApplicationRemote {
49
49
  (input: string | PageKey, options?: RemoteProps & {
@@ -69,6 +69,17 @@ declare const rootReducer: {
69
69
  fragments: typeof fragmentReducer;
70
70
  };
71
71
 
72
+ type FragmentProxy = {
73
+ __fragment: true;
74
+ };
75
+ type ProxiedContent<T> = T extends Fragment<infer U, true> ? ProxiedContent<U> & FragmentProxy : T extends Fragment<infer U, false | undefined> ? (ProxiedContent<U> & FragmentProxy) | undefined : T extends (infer U)[] ? ProxiedContent<U>[] : T extends object ? {
76
+ [K in keyof T]: ProxiedContent<T[K]>;
77
+ } : T;
78
+ type FragmentRefOrId = FragmentRef | string;
79
+ declare function useContent<T = JSONMappable>(): ProxiedContent<T>;
80
+ declare function useContent<T = JSONMappable>(fragmentRef: FragmentRefOrId): ProxiedContent<T>;
81
+ declare function unproxy<T>(proxy: T): Unproxy<T>;
82
+
72
83
  type PageKey = string;
73
84
  type RestoreStrategy = 'fromCacheOnly' | 'revisitOnly' | 'fromCacheAndRevisitInBackground';
74
85
  type NavigationAction = 'push' | 'replace' | 'none';
@@ -82,15 +93,13 @@ type JSONObject = {
82
93
  type JSONMappable = JSONValue[] | JSONObject;
83
94
  type JSONKeyable = JSONObject[] | JSONObject;
84
95
  type JSONValue = JSONPrimitive | JSONMappable;
85
- type Fragment<T> = {
96
+ type Fragment<T, Present = false> = {
86
97
  __id: string;
87
- } & {
88
98
  __fragmentType?: T;
99
+ __required?: Present extends boolean ? Present : false;
89
100
  };
90
- type Unproxied<T> = T extends Fragment<unknown> ? {
91
- __id: string;
92
- } : T extends (infer U)[] ? Unproxied<U>[] : T extends object ? {
93
- [K in keyof T]: Unproxied<T[K]>;
101
+ type Unproxy<T> = T extends FragmentProxy ? FragmentRef : T extends Fragment<unknown, unknown> ? FragmentRef : T extends (infer U)[] ? Unproxy<U>[] : T extends object ? {
102
+ [K in keyof T]: Unproxy<T[K]>;
94
103
  } : T;
95
104
  interface ParsedResponse {
96
105
  rsp: Response;
@@ -108,7 +117,7 @@ type SaveResponse<T = JSONMappable> = {
108
117
  componentIdentifier: ComponentIdentifier;
109
118
  assets: string[];
110
119
  csrfToken?: string;
111
- fragments: FragmentRef[];
120
+ fragments: FragmentPath[];
112
121
  defers: Defer[];
113
122
  slices: JSONObject;
114
123
  action: 'savePage';
@@ -123,24 +132,23 @@ type GraftResponse<T = JSONMappable> = {
123
132
  componentIdentifier: ComponentIdentifier;
124
133
  assets: string[];
125
134
  csrfToken?: string;
126
- fragments: FragmentRef[];
135
+ fragments: FragmentPath[];
127
136
  defers: Defer[];
128
137
  slices: JSONObject;
129
138
  action: 'graft';
130
139
  renderedAt: number;
131
- restoreStrategy: RestoreStrategy;
132
140
  path: Keypath;
133
141
  fragmentContext?: string;
134
142
  };
135
- type StreamMutateMessage = {
143
+ type StreamMessage = {
136
144
  data: JSONMappable;
137
145
  fragmentIds: string[];
138
146
  handler: 'append' | 'prepend' | 'save';
139
147
  options: Record<string, string>;
140
148
  };
141
149
  type StreamResponse = {
142
- data: StreamMutateMessage[];
143
- fragments: FragmentRef[];
150
+ data: StreamMessage[];
151
+ fragments: FragmentPath[];
144
152
  assets: string[];
145
153
  csrfToken?: string;
146
154
  action: 'handleStreamResponse';
@@ -148,10 +156,13 @@ type StreamResponse = {
148
156
  slices: JSONObject;
149
157
  };
150
158
  type PageResponse = GraftResponse | SaveResponse | StreamResponse;
151
- type FragmentRef = {
159
+ type FragmentPath = {
152
160
  id: string;
153
161
  path: Keypath;
154
162
  };
163
+ type FragmentRef = {
164
+ __id: string;
165
+ };
155
166
  type AllPages<T = JSONMappable> = Record<PageKey, Page<T>>;
156
167
  type AllFragments = Record<string, JSONMappable>;
157
168
  interface SuperglueState {
@@ -269,4 +280,4 @@ declare const visit: VisitCreator;
269
280
 
270
281
  declare function saveAndProcessPage(pageKey: string, page: PageResponse): SaveAndProcessPageThunk;
271
282
 
272
- export { type Visit as $, type ApplicationRemote as A, type VisitCreator as B, type ComponentIdentifier as C, type Defer as D, type RemoteCreator as E, type FetchArgs as F, type GraftResponse as G, type Handlers as H, type Dispatch as I, type JSONMappable as J, type Keypath as K, type UJSHandlers as L, type Meta as M, type NavigationContextProps as N, type HistoryState as O, type PageKey as P, type SaveAndProcessPageThunk as Q, type RestoreStrategy as R, type SuperglueStore as S, type MetaThunk as T, type Unproxied as U, type VisitMeta as V, type VisitMetaThunk as W, type DefermentThunk as X, type BasicRequestInit as Y, type BuildStore as Z, type BuildVisitAndRemote as _, type JSONValue as a, type VisitProps as a0, type Remote as a1, type RemoteProps as a2, type BeforeSave as a3, type GraftingSuccessAction as a4, type GraftingErrorAction as a5, MismatchedComponentError as a6, remote as a7, visit as a8, type NavigationProviderProps as b, type NavigateTo as c, type SaveResponse as d, type PageResponse as e, type SuperglueState as f, type SetupProps as g, type ApplicationVisit as h, type ApplicationProps as i, superglueReducer as j, type NavigationAction as k, type JSONPrimitive as l, type JSONObject as m, type JSONKeyable as n, type Fragment as o, pageReducer as p, type ParsedResponse as q, rootReducer as r, saveAndProcessPage as s, type Page as t, type StreamMutateMessage as u, type StreamResponse as v, type FragmentRef as w, type AllPages as x, type AllFragments as y, type RootState as z };
283
+ export { type BasicRequestInit as $, type ApplicationRemote as A, type AllPages as B, type ComponentIdentifier as C, type Defer as D, type AllFragments as E, type FetchArgs as F, type GraftResponse as G, type Handlers as H, type RootState as I, type JSONMappable as J, type Keypath as K, type VisitCreator as L, type Meta as M, type NavigationProviderProps as N, type RemoteCreator as O, type PageKey as P, type Dispatch as Q, type RestoreStrategy as R, type SuperglueStore as S, type UJSHandlers as T, type Unproxy as U, type VisitMeta as V, type HistoryState as W, type SaveAndProcessPageThunk as X, type MetaThunk as Y, type VisitMetaThunk as Z, type DefermentThunk as _, type JSONValue as a, type BuildStore as a0, type BuildVisitAndRemote as a1, type Visit as a2, type VisitProps as a3, type Remote as a4, type RemoteProps as a5, type BeforeSave as a6, type GraftingSuccessAction as a7, type GraftingErrorAction as a8, useContent as a9, MismatchedComponentError as aa, remote as ab, visit as ac, type NavigateTo as b, type NavigationContextProps as c, type SaveResponse as d, type PageResponse as e, type Fragment as f, type FragmentProxy as g, type SuperglueState as h, type ApplicationProps as i, type SetupProps as j, type ApplicationVisit as k, superglueReducer as l, type NavigationAction as m, type JSONPrimitive as n, type JSONObject as o, pageReducer as p, type JSONKeyable as q, rootReducer as r, saveAndProcessPage as s, type ParsedResponse as t, unproxy as u, type Page as v, type StreamMessage as w, type StreamResponse as x, type FragmentPath as y, type FragmentRef as z };
@@ -1,8 +1,8 @@
1
1
  import * as history from 'history';
2
- import { J as JSONMappable, K as Keypath, a as JSONValue, P as PageKey, A as ApplicationRemote, S as SuperglueStore, N as NavigationContextProps, b as NavigationProviderProps, c as NavigateTo, d as SaveResponse, F as FetchArgs, e as PageResponse, U as Unproxied, f as SuperglueState, g as SetupProps, h as ApplicationVisit, H as Handlers, i as ApplicationProps } from './index-O_srQ3Nm.mjs';
3
- export { y as AllFragments, x as AllPages, Y as BasicRequestInit, a3 as BeforeSave, Z as BuildStore, _ as BuildVisitAndRemote, C as ComponentIdentifier, D as Defer, X as DefermentThunk, I as Dispatch, o as Fragment, w as FragmentRef, G as GraftResponse, a5 as GraftingErrorAction, a4 as GraftingSuccessAction, O as HistoryState, n as JSONKeyable, m as JSONObject, l as JSONPrimitive, M as Meta, T as MetaThunk, k as NavigationAction, t as Page, q as ParsedResponse, a1 as Remote, E as RemoteCreator, a2 as RemoteProps, R as RestoreStrategy, z as RootState, Q as SaveAndProcessPageThunk, u as StreamMutateMessage, v as StreamResponse, L as UJSHandlers, $ as Visit, B as VisitCreator, V as VisitMeta, W as VisitMetaThunk, a0 as VisitProps, p as pageReducer, r as rootReducer, s as saveAndProcessPage, j as superglueReducer } from './index-O_srQ3Nm.mjs';
2
+ import { J as JSONMappable, K as Keypath, a as JSONValue, P as PageKey, A as ApplicationRemote, S as SuperglueStore, N as NavigationProviderProps, b as NavigateTo, c as NavigationContextProps, F as FetchArgs, d as SaveResponse, e as PageResponse, f as Fragment, U as Unproxy, g as FragmentProxy, h as SuperglueState, i as ApplicationProps, j as SetupProps, k as ApplicationVisit, H as Handlers } from './index-j0c-9ZLt.mjs';
3
+ export { E as AllFragments, B as AllPages, $ as BasicRequestInit, a6 as BeforeSave, a0 as BuildStore, a1 as BuildVisitAndRemote, C as ComponentIdentifier, D as Defer, _ as DefermentThunk, Q as Dispatch, y as FragmentPath, z as FragmentRef, G as GraftResponse, a8 as GraftingErrorAction, a7 as GraftingSuccessAction, W as HistoryState, q as JSONKeyable, o as JSONObject, n as JSONPrimitive, M as Meta, Y as MetaThunk, m as NavigationAction, v as Page, t as ParsedResponse, a4 as Remote, O as RemoteCreator, a5 as RemoteProps, R as RestoreStrategy, I as RootState, X as SaveAndProcessPageThunk, w as StreamMessage, x as StreamResponse, T as UJSHandlers, a2 as Visit, L as VisitCreator, V as VisitMeta, Z as VisitMetaThunk, a3 as VisitProps, p as pageReducer, r as rootReducer, s as saveAndProcessPage, l as superglueReducer, u as unproxy, a9 as useContent } from './index-j0c-9ZLt.mjs';
4
4
  import React from 'react';
5
- import { Subscription, Consumer, ChannelNameWithParams } from '@rails/actioncable';
5
+ import { ChannelNameWithParams, Subscription } from '@rails/actioncable';
6
6
  import { DebouncedFunc } from 'lodash';
7
7
  import * as _reduxjs_toolkit from '@reduxjs/toolkit';
8
8
 
@@ -31,7 +31,7 @@ declare class StreamActions {
31
31
  }
32
32
  declare function useStreamSource(channel: StreamSourceProps): {
33
33
  connected: boolean;
34
- subscription: Subscription<Consumer> | null;
34
+ subscription: Subscription | null;
35
35
  };
36
36
 
37
37
  declare const NavigationContext: React.Context<NavigationContextProps>;
@@ -74,27 +74,11 @@ declare const receiveResponse: _reduxjs_toolkit.ActionCreatorWithPreparedPayload
74
74
  response: PageResponse;
75
75
  }, "@@superglue/RECEIVE_RESPONSE", never, never>;
76
76
 
77
- type ProxiedContent<T> = T & {
78
- readonly [K in keyof T]: T[K] extends {
79
- __id: string;
80
- } ? unknown : T[K] extends (infer U)[] ? ProxiedContent<U>[] : T[K] extends object ? ProxiedContent<T[K]> : T[K];
77
+ type Unpack<T> = T extends Fragment<infer U, unknown> ? U : T extends FragmentProxy ? T : never;
78
+ declare function useSetFragment(): {
79
+ <T extends Fragment<unknown, unknown>>(fragmentRef: T, updater: (draft: Unproxy<Unpack<T>>) => void): void;
80
+ <T = unknown>(fragmentId: string, updater: (draft: T) => void): void;
81
81
  };
82
- type FragmentRef = {
83
- __id: string;
84
- } | string;
85
- declare function useContent<T = JSONMappable>(): ProxiedContent<T>;
86
- declare function useContent<T = JSONMappable>(fragmentRef: FragmentRef): ProxiedContent<T>;
87
- declare function useContent<T = JSONMappable>(fragmentRef: FragmentRef, options: {
88
- optional: false;
89
- }): ProxiedContent<T>;
90
- declare function useContent<T = JSONMappable>(fragmentRef: FragmentRef, options: {
91
- optional: true;
92
- }): ProxiedContent<T> | undefined;
93
- declare function unproxy<T>(proxy: T): Unproxied<T>;
94
-
95
- declare function useSetFragment(): (fragmentRef: {
96
- __id: string;
97
- } | string, updater: (draft: unknown) => void) => void;
98
82
 
99
83
  declare function useSuperglue(): SuperglueState;
100
84
 
@@ -109,4 +93,4 @@ declare const setup: ({ initialPage, baseUrl, path, store, buildVisitAndRemote,
109
93
  };
110
94
  declare function Application({ initialPage, baseUrl, path, store, buildVisitAndRemote, history, mapping, ...rest }: ApplicationProps): React.JSX.Element;
111
95
 
112
- export { Application, ApplicationProps, ApplicationRemote, ApplicationVisit, FetchArgs, GRAFTING_ERROR, GRAFTING_SUCCESS, Handlers, JSONMappable, JSONValue, Keypath, NavigateTo, NavigationContext, NavigationContextProps, NavigationProvider, NavigationProviderProps, PageKey, PageResponse, SaveResponse, SetupProps, SuperglueState, SuperglueStore, Unproxied, beforeFetch, beforeRemote, beforeVisit, copyPage, getIn, prepareStore, receiveResponse, removePage, saveResponse, setup, unproxy, urlToPageKey, useContent, useSetFragment, useStreamSource, useSuperglue };
96
+ export { Application, ApplicationProps, ApplicationRemote, ApplicationVisit, FetchArgs, Fragment, GRAFTING_ERROR, GRAFTING_SUCCESS, Handlers, JSONMappable, JSONValue, Keypath, NavigateTo, NavigationContext, NavigationContextProps, NavigationProvider, NavigationProviderProps, PageKey, PageResponse, SaveResponse, SetupProps, SuperglueState, SuperglueStore, Unproxy, beforeFetch, beforeRemote, beforeVisit, copyPage, getIn, prepareStore, receiveResponse, removePage, saveResponse, setup, urlToPageKey, useSetFragment, useStreamSource, useSuperglue };
@@ -33,7 +33,7 @@ import {
33
33
  ujsHandlers,
34
34
  unproxy,
35
35
  urlToPageKey
36
- } from "./chunk-Q76JBI63.mjs";
36
+ } from "./chunk-M74JY6QK.mjs";
37
37
 
38
38
  // node_modules/lodash/lodash.js
39
39
  var require_lodash = __commonJS({
@@ -5529,7 +5529,7 @@ import { useSelector as useSelector3 } from "react-redux";
5529
5529
  // lib/hooks/useContent.tsx
5530
5530
  import { useSelector, useStore } from "react-redux";
5531
5531
  import { useMemo, useRef } from "react";
5532
- function useContent(fragmentRef, options) {
5532
+ function useContent(fragmentRef) {
5533
5533
  const superglueState = useSuperglue();
5534
5534
  const currentPageKey = superglueState.currentPageKey;
5535
5535
  const dependencies = useRef(/* @__PURE__ */ new Set());
@@ -5554,16 +5554,11 @@ function useContent(fragmentRef, options) {
5554
5554
  });
5555
5555
  }
5556
5556
  );
5557
- const raiseOnMissing = !(options?.optional ?? false);
5558
5557
  const store = useStore();
5559
5558
  const proxy = useMemo(() => {
5560
5559
  const proxyCache = /* @__PURE__ */ new WeakMap();
5561
5560
  if (fragmentId && !sourceData) {
5562
- if (raiseOnMissing) {
5563
- throw new Error(`Fragment with id "${fragmentId}" not found`);
5564
- } else {
5565
- return void 0;
5566
- }
5561
+ return void 0;
5567
5562
  }
5568
5563
  return createProxy(
5569
5564
  sourceData,
@@ -5571,7 +5566,7 @@ function useContent(fragmentRef, options) {
5571
5566
  dependencies.current,
5572
5567
  proxyCache
5573
5568
  );
5574
- }, [sourceData, trackedFragments, options?.optional]);
5569
+ }, [sourceData, trackedFragments]);
5575
5570
  return proxy;
5576
5571
  }
5577
5572
  function unproxy2(proxy) {
@@ -5580,24 +5575,27 @@ function unproxy2(proxy) {
5580
5575
 
5581
5576
  // lib/hooks/useSetFragment.tsx
5582
5577
  import { useDispatch, useSelector as useSelector2 } from "react-redux";
5583
- import { produce } from "immer";
5578
+ import { Immer } from "immer";
5579
+ var immer = new Immer();
5580
+ immer.setAutoFreeze(false);
5584
5581
  function useSetFragment() {
5585
5582
  const dispatch = useDispatch();
5586
5583
  const fragments = useSelector2((state) => state.fragments);
5587
- return function setter(fragmentRef, updater) {
5588
- const fragmentId = typeof fragmentRef === "string" ? fragmentRef : fragmentRef.__id;
5584
+ function setter(fragmentRefOrId, updater) {
5585
+ const fragmentId = typeof fragmentRefOrId === "string" ? fragmentRefOrId : fragmentRefOrId.__id;
5589
5586
  const currentFragment = fragments[fragmentId];
5590
5587
  if (currentFragment === void 0) {
5591
5588
  throw new Error(`Fragment with id "${fragmentId}" not found`);
5592
5589
  }
5593
- const updatedFragment = produce(currentFragment, updater);
5590
+ const updatedFragment = immer.produce(currentFragment, updater);
5594
5591
  dispatch(
5595
5592
  saveFragment({
5596
5593
  fragmentId,
5597
5594
  data: updatedFragment
5598
5595
  })
5599
5596
  );
5600
- };
5597
+ }
5598
+ return setter;
5601
5599
  }
5602
5600
 
5603
5601
  // lib/hooks/index.ts
@@ -5669,7 +5667,7 @@ function useStreamSource(channel) {
5669
5667
  return () => {
5670
5668
  };
5671
5669
  }
5672
- }, [cable2, channel, currentPageKey]);
5670
+ }, [cable2, JSON.stringify(channel), currentPageKey]);
5673
5671
  return {
5674
5672
  connected,
5675
5673
  subscription: subscriptionRef.current