@codecks/fetch 0.1.2 → 0.1.4

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.
@@ -2,28 +2,32 @@ import { queryToKey } from "../query-helpers-DsMMOtSO.js";
2
2
  import { use, useSyncExternalStore } from "react";
3
3
 
4
4
  //#region src/_exploration/user-query-manager.ts
5
- const diffResults = (prevResult, prevParts, nextResult) => {
6
- const nextParts = [];
5
+ const diffResults = (prevTrackedData, nextStorePayload) => {
6
+ const nextPartKeys = nextStorePayload.partKeys;
7
+ const prevSet = new Set(prevTrackedData.partKeys);
8
+ const nextSet = new Set(nextPartKeys);
9
+ const added = nextPartKeys.filter((key) => !prevSet.has(key));
10
+ const removed = prevTrackedData.partKeys.filter((key) => !nextSet.has(key));
7
11
  return {
8
- added: nextParts,
9
- removed: prevParts,
10
- reconciledResult: {
11
- parts: nextParts,
12
- data: nextResult
12
+ added,
13
+ removed,
14
+ nextTrackedData: {
15
+ partKeys: nextPartKeys,
16
+ value: nextStorePayload.value
13
17
  }
14
18
  };
15
19
  };
16
20
  const createStoreQuery = (opts) => {
17
21
  const { onDispose, updateDataListener, store, q, ids, model } = opts;
18
22
  const queryListeners = new Set();
19
- let lastData = {
20
- parts: [],
21
- data: null
23
+ let trackedData = {
24
+ partKeys: [],
25
+ value: null
22
26
  };
23
- const reconcileNextResult = (nextData) => {
24
- const { added, removed, reconciledResult } = diffResults(lastData.data, lastData.parts, nextData);
25
- if (!added.length && !removed.length) {
26
- lastData = reconciledResult;
27
+ const reconcileNextResult = (storePayload) => {
28
+ const { added, removed, nextTrackedData } = diffResults(trackedData, storePayload);
29
+ if (added.length || removed.length) {
30
+ trackedData = nextTrackedData;
27
31
  updateDataListener({
28
32
  added,
29
33
  removed
@@ -34,13 +38,26 @@ const createStoreQuery = (opts) => {
34
38
  const notify = () => {
35
39
  loadData();
36
40
  };
41
+ let userQueryResult;
37
42
  const loadData = () => {
38
- const res = store.loadData(model, ids, q);
39
- if (res.state === "pending") res.promise.then(reconcileNextResult);
40
- else reconcileNextResult(res.value);
41
- return res;
43
+ const storeResult = store.loadData(model, ids, q);
44
+ if (storeResult.state === "pending") userQueryResult = {
45
+ state: "pending",
46
+ promise: storeResult.promise.then((storePayload) => {
47
+ reconcileNextResult(storePayload);
48
+ return storePayload.value;
49
+ })
50
+ };
51
+ else {
52
+ const storePayload = storeResult.payload;
53
+ reconcileNextResult(storePayload);
54
+ userQueryResult = {
55
+ state: "resolved",
56
+ value: storePayload.value
57
+ };
58
+ }
42
59
  };
43
- let data = loadData();
60
+ loadData();
44
61
  const subscribe = (listener) => {
45
62
  queryListeners.add(listener);
46
63
  return () => {
@@ -48,12 +65,12 @@ const createStoreQuery = (opts) => {
48
65
  if (queryListeners.size === 0) onDispose();
49
66
  updateDataListener({
50
67
  added: [],
51
- removed: lastData.parts
68
+ removed: trackedData.partKeys
52
69
  }, notify);
53
70
  };
54
71
  };
55
72
  const get = () => {
56
- return data;
73
+ return userQueryResult;
57
74
  };
58
75
  return {
59
76
  subscribe,
@@ -122,10 +139,12 @@ const createHooks = (store) => {
122
139
  return val.value;
123
140
  };
124
141
  const useFetch = (model, id, q) => {
125
- return useQueryManager(model, [id], q);
142
+ const res = useQueryManager(model, [id], q);
143
+ return res[id];
126
144
  };
127
145
  const useFetchFromRoot = (q) => {
128
- return useQueryManager("_root", [""], { relations: q });
146
+ const res = useQueryManager("_root", [""], { relations: q });
147
+ return res[""];
129
148
  };
130
149
  return {
131
150
  useFetch,
@@ -1,5 +1,5 @@
1
- import "../query-type-MH8IxqbW.js";
2
- import { BaseLoader, BaseRequester, MissingDataRequest, OnLoadedFn } from "../loader-types-DJdGWL4B.js";
1
+ import "../query-type-Dx5ZCv5N.js";
2
+ import { BaseLoader, BaseRequester, MissingDataRequest, OnLoadedFn } from "../loader-types-Dp0CSVTf.js";
3
3
 
4
4
  //#region src/_exploration/loader.d.ts
5
5
  type DataRequestWithKey = {
@@ -1,4 +1,5 @@
1
- import "../query-type-MH8IxqbW.js";
2
- import "../loader-types-DJdGWL4B.js";
3
- import { Store } from "../store-mAj3G67A.js";
1
+ import "../query-type-Dx5ZCv5N.js";
2
+ import "../index-DUBsesYl.js";
3
+ import "../loader-types-Dp0CSVTf.js";
4
+ import { Store } from "../store-B6JWZnAO.js";
4
5
  export { Store };
@@ -71,14 +71,15 @@ var Store = class {
71
71
  if (missingRequests.length > 0) this.loader.loadBatch(missingRequests);
72
72
  return {
73
73
  state: "resolved",
74
- value: {
74
+ payload: {
75
75
  value: resultObj,
76
76
  partKeys: allPartKeys
77
77
  }
78
78
  };
79
79
  }
80
80
  const promise = this.loader.loadBatch(missingRequests).then(() => {
81
- return this.loadData(model, ids, q, acceptDirty, iteration + 1);
81
+ const res = this.loadData(model, ids, q, acceptDirty, iteration + 1);
82
+ return res.state === "pending" ? res.promise : res.payload;
82
83
  });
83
84
  return {
84
85
  state: "pending",
@@ -1,4 +1,4 @@
1
- import { ModelDesc, RelationEntry } from "./query-type-MH8IxqbW.js";
1
+ import { ModelDesc, RelationEntry } from "./query-type-Dx5ZCv5N.js";
2
2
 
3
3
  //#region src/models/_root.d.ts
4
4
  declare const _rootDesc: ModelDesc<"_root", {}, {
@@ -1,4 +1,4 @@
1
- import { InferModelQuery, ModelDesc, ModelQuery, Nominal, RelationEntry, TypedField } from "./query-type-MH8IxqbW.js";
1
+ import { ModelDesc, Nominal, RelationEntry, TypedField } from "./query-type-Dx5ZCv5N.js";
2
2
 
3
3
  //#region src/models/AccountOnboardingStep.d.ts
4
4
  type AccountOnboardingStepId = Nominal<string, "accountOnboardingStep">;
@@ -2670,19 +2670,4 @@ declare const modelMap: {
2670
2670
  }, ["itemId"]>;
2671
2671
  };
2672
2672
  //#endregion
2673
- //#region src/loaders/loader-utils.d.ts
2674
- type ModelMap = typeof modelMap;
2675
- type DataLoader = {
2676
- fetchModel: <K extends keyof ModelMap, Id extends string, const Q extends ModelQuery<ModelMap[K], ModelMap>>(model: K, id: Id[], q: Q) => Promise<Record<Id, InferModelQuery<ModelMap[K], Q, ModelMap>>>;
2677
- };
2678
- type FetchFunction = (url: string, init?: RequestInit) => Promise<Response>;
2679
- type FetchOptions = {
2680
- fetch?: FetchFunction;
2681
- accessToken?: string;
2682
- subdomain?: string;
2683
- baseUrl?: string;
2684
- headers?: Record<string, string>;
2685
- timeout?: number;
2686
- };
2687
- //#endregion
2688
- export { DataLoader, FetchOptions, modelMap as modelMap$1 };
2673
+ export { AccountId, AccountOnboardingStepId, ActiveTimeTrackerId, ActivityId, AffiliateCodeId, AppId, AppInstallationId, AttachmentId, CardId, CardPresetId, CardSubscriptionId, CardUpvoteId, DeckId, DeckSubscriptionId, DiscordGuildId, DiscordMemberId, DiscordProjectNotificationId, DiscordSlashCommandId, FileId, IntegrationId, InvoiceId, MilestoneId, PinnedMilestoneId, ProjectId, ProjectSelectionId, ProjectTagId, QueueEntryId, QueueSelectionId, ReleaseId, ResolvableEntryId, ResolvableEntryReactionId, ResolvableId, SavedSearchId, SprintConfigId, SprintId, TimeTrackingSegmentId, UserEmailId, UserId, UserInvitationId, UserInviteCodeId, UserReportEmailId, UserReportSettingId, UserReportTokenId, UserTagId, VisionBoardId, VisionBoardQueryId, WizardId, WorkflowItemId, modelMap as modelMap$1 };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { InferModelQuery, InferRelQuery, Instance, ModelQuery, RelQuery } from "./query-type-MH8IxqbW.js";
2
- import { DataLoader, FetchOptions, modelMap$1 as modelMap } from "./loader-utils-CjrkQskt.js";
3
- import { _rootDesc$1 as _rootDesc } from "./_root-BG9fhc-C.js";
1
+ import { InferModelQuery, InferRelQuery, Instance, ModelQuery, RelQuery } from "./query-type-Dx5ZCv5N.js";
2
+ import { modelMap$1 as modelMap } from "./index-DUBsesYl.js";
3
+ import { DataLoader, FetchOptions } from "./loader-utils-Du-CTKH-.js";
4
+ import { _rootDesc$1 as _rootDesc } from "./_root-D1Faf8cg.js";
4
5
 
5
6
  //#region src/loaders/simple-loader.d.ts
6
7
  type SimpleLoaderOptions = FetchOptions;
@@ -1,4 +1,4 @@
1
- import { SerializableRelationQuery } from "./query-type-MH8IxqbW.js";
1
+ import { SerializableRelationQuery } from "./query-type-Dx5ZCv5N.js";
2
2
 
3
3
  //#region src/_exploration/loader-types.d.ts
4
4
  type OnLoadedFn = (model: string, key: string, partialInstance: Record<string, unknown>) => void;
@@ -12,10 +12,10 @@ type LoaderPayload = {
12
12
  };
13
13
  type LoaderResult = {
14
14
  state: "resolved";
15
- value: LoaderPayload;
15
+ payload: LoaderPayload;
16
16
  } | {
17
17
  state: "pending";
18
- promise: Promise<LoaderResult>;
18
+ promise: Promise<LoaderPayload>;
19
19
  };
20
20
  type MissingDataRequest = {
21
21
  type: "field";
@@ -0,0 +1,19 @@
1
+ import { InferModelQuery, ModelQuery } from "./query-type-Dx5ZCv5N.js";
2
+ import { modelMap$1 as modelMap } from "./index-DUBsesYl.js";
3
+
4
+ //#region src/loaders/loader-utils.d.ts
5
+ type ModelMap = typeof modelMap;
6
+ type DataLoader = {
7
+ fetchModel: <K extends keyof ModelMap, Id extends string, const Q extends ModelQuery<ModelMap[K], ModelMap>>(model: K, id: Id[], q: Q) => Promise<Record<Id, InferModelQuery<ModelMap[K], Q, ModelMap>>>;
8
+ };
9
+ type FetchFunction = (url: string, init?: RequestInit) => Promise<Response>;
10
+ type FetchOptions = {
11
+ fetch?: FetchFunction;
12
+ accessToken?: string;
13
+ subdomain?: string;
14
+ baseUrl?: string;
15
+ headers?: Record<string, string>;
16
+ timeout?: number;
17
+ };
18
+ //#endregion
19
+ export { DataLoader, FetchOptions };
@@ -179,4 +179,4 @@ type Instance<M extends keyof ModelMap> = {
179
179
  "~key": string;
180
180
  };
181
181
  //#endregion
182
- export { AnyDesc, InferModelQuery, InferRelQuery, Instance, ModelDesc, ModelQuery, Nominal, RelQuery, RelationEntry, SerializableRelationQuery, TypedField };
182
+ export { InferModelQuery, InferRelQuery, Instance, ModelDesc, ModelQuery, Nominal, RelQuery, RelationEntry, SerializableRelationQuery, TypedField };