@mapled/react 0.1.0 → 0.1.3

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
@@ -112,7 +112,7 @@ export function Post() {
112
112
  ```
113
113
 
114
114
  - `useSingle(key, options?)` — `data` is the single's record, or `null` while it is empty.
115
- - `useRecords(collection, options?)` — `data` is `{ records, total, release }`; options are the client's: `filter`, `sort`, `limit`, `offset`, `fields`, `expand`.
115
+ - `useRecords(collection, options?)` — `data` is `{ records, total, release }`; options are the client's: `filter`, `sort`, `limit`, `offset`, `fields`, `expand`, `locale` (read in one of the project's languages; `createClient({ key, locale })` for every read).
116
116
  - `useRecord(collection, { slug } | { id } | null, options?)` — `data` is `null` when the record isn't published; `null` instead of a reference reads nothing (a router param that hasn't arrived).
117
117
  - Components that ask for the same thing share one request; an inline options object is the same read on every render.
118
118
  - Rich text values are Markdown — render them with a Markdown component, never as raw HTML.
@@ -158,7 +158,7 @@ export function LiveApp() {
158
158
  }
159
159
  ```
160
160
 
161
- Each open tab then asks Mapled for the release pointer once a minute (`live={{ interval: 30 }}` for thirty seconds, never under fifteen) while it is visible, and the hooks read again — pinned to the new release — when it moves; what was on screen stays there until the new answer arrives. It is off by default because every check is a request of the delivery key's budget (1,200 a minute).
161
+ Each open tab then asks Mapled for the release pointer once a minute (`live={{ interval: 30 }}` for thirty seconds, never under fifteen) while it is visible, and the hooks read again — pinned to the new release — when it moves; what was on screen stays there until the new answer arrives. The watch starts from the release the hooks' reads answered, so a publish that landed between them and the first check, or while a tab opened in the background waited to be shown, is heard at once. It is off by default because every check is a request of the delivery key's budget (1,200 a minute).
162
162
 
163
163
  ## Forms
164
164
 
package/dist/index.js CHANGED
@@ -71,6 +71,7 @@ export function MapledProvider({ client, live = false, children }) {
71
71
  useEffect(() => {
72
72
  let mounted = true;
73
73
  setRelease(undefined);
74
+ followed.current = undefined;
74
75
  setPreview(null);
75
76
  client.isPreview().then((on) => mounted && setPreview(on), () => mounted && setPreview(false));
76
77
  return () => {
@@ -79,11 +80,18 @@ export function MapledProvider({ client, live = false, children }) {
79
80
  }, [client]);
80
81
  const interval = typeof live === "object" ? live.interval : undefined;
81
82
  const watching = Boolean(live) && preview === false;
83
+ // the newest release the page followed: a watch restarted for a new pace starts there
84
+ const followed = useRef(undefined);
82
85
  useEffect(() => {
83
86
  // drafts have no releases to follow
84
87
  if (!watching)
85
88
  return;
86
- return client.watch((next) => setRelease(next.release), { interval });
89
+ // where the page is, the client knows from the hooks' reads (`x-mapled-release`): a publish between them and
90
+ // the watch's first answer, or while a background tab waited to be shown, is news, and the hooks read again
91
+ return client.watch((next) => {
92
+ followed.current = Math.max(next.release, followed.current ?? 0);
93
+ setRelease(next.release);
94
+ }, { interval, from: followed.current });
87
95
  }, [client, watching, interval]);
88
96
  const value = useMemo(() => ({ client, store, release, preview }), [client, store, release, preview]);
89
97
  return createElement(MapledContext.Provider, { value }, children);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapled/react",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
4
4
  "description": "React helpers for Mapled — published content in a React single-page app (Vite and the like): a provider and hooks over @mapled/client, with preview and publish notifications.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://mapled.io",
@@ -36,7 +36,7 @@
36
36
  "typecheck": "tsc --noEmit -p tsconfig.json"
37
37
  },
38
38
  "dependencies": {
39
- "@mapled/client": "^0.1.0"
39
+ "@mapled/client": "^0.3.0"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "react": ">=18"