@sanity/sdk 0.0.0-alpha.20 → 0.0.0-alpha.22
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/dist/index.js +38 -9
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/auth/authStore.ts +1 -0
- package/src/document/documentStore.ts +0 -1
- package/src/resources/createResource.ts +74 -10
package/dist/index.js
CHANGED
|
@@ -20,7 +20,12 @@ function getEnv(key) {
|
|
|
20
20
|
if (typeof window < "u" && window.ENV)
|
|
21
21
|
return window.ENV?.[key];
|
|
22
22
|
}
|
|
23
|
-
const resourceCache = /* @__PURE__ */ new WeakMap()
|
|
23
|
+
const resourceCache = /* @__PURE__ */ new WeakMap(), SHARED_IDENTITY = {
|
|
24
|
+
id: "shared",
|
|
25
|
+
resourceId: "shared.shared",
|
|
26
|
+
projectId: "shared",
|
|
27
|
+
dataset: "shared"
|
|
28
|
+
};
|
|
24
29
|
function createResourceState(initialState, devToolsOptions) {
|
|
25
30
|
const store = createStore$1()(devtools(() => initialState, devToolsOptions));
|
|
26
31
|
return {
|
|
@@ -35,26 +40,50 @@ function createResourceState(initialState, devToolsOptions) {
|
|
|
35
40
|
};
|
|
36
41
|
}
|
|
37
42
|
function initializeResource(instance, resource) {
|
|
38
|
-
const fullName = resource.
|
|
43
|
+
const fullName = resource.isShared ? `${resource.name}-shared` : `${resource.name}-${instance.identity.resourceId}`, initialState = resource.getInitialState(instance), state = createResourceState(initialState, {
|
|
39
44
|
name: fullName,
|
|
40
45
|
enabled: !!getEnv("DEV")
|
|
41
46
|
}), dispose = resource.initialize?.call({ instance, state }, instance) ?? (() => {
|
|
42
47
|
});
|
|
43
48
|
return { state, dispose };
|
|
44
49
|
}
|
|
50
|
+
const instanceSharedResources = /* @__PURE__ */ new Map();
|
|
45
51
|
function getOrCreateResource(instance, resource) {
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
const initializedResources = resourceCache.get(
|
|
52
|
+
const identityKey = resource.isShared ? SHARED_IDENTITY : instance.identity, fullName = resource.isShared ? `${resource.name}-shared` : `${resource.name}-${instance.identity.resourceId}`;
|
|
53
|
+
resource.isShared && (instanceSharedResources.has(instance.identity) || instanceSharedResources.set(instance.identity, /* @__PURE__ */ new Set()), instanceSharedResources.get(instance.identity).add(fullName)), resourceCache.has(identityKey) || resourceCache.set(identityKey, /* @__PURE__ */ new Map());
|
|
54
|
+
const initializedResources = resourceCache.get(identityKey), cached = initializedResources.get(fullName);
|
|
49
55
|
if (cached) return cached;
|
|
50
56
|
const result = initializeResource(instance, resource);
|
|
51
57
|
return initializedResources.set(fullName, result), result;
|
|
52
58
|
}
|
|
53
59
|
function disposeResources(identity) {
|
|
54
60
|
const resources = resourceCache.get(identity);
|
|
55
|
-
if (resources)
|
|
61
|
+
if (resources) {
|
|
56
62
|
for (const resource of resources.values())
|
|
57
63
|
resource.dispose();
|
|
64
|
+
resourceCache.delete(identity);
|
|
65
|
+
}
|
|
66
|
+
const usedSharedResources = instanceSharedResources.get(identity);
|
|
67
|
+
if (usedSharedResources) {
|
|
68
|
+
const sharedResources = resourceCache.get(SHARED_IDENTITY);
|
|
69
|
+
if (sharedResources) {
|
|
70
|
+
for (const fullName of usedSharedResources) {
|
|
71
|
+
const resource = sharedResources.get(fullName);
|
|
72
|
+
if (resource) {
|
|
73
|
+
usedSharedResources.delete(fullName);
|
|
74
|
+
let stillInUse = !1;
|
|
75
|
+
for (const [otherIdentity, otherUsed] of instanceSharedResources.entries())
|
|
76
|
+
if (otherIdentity !== identity && otherUsed.has(fullName)) {
|
|
77
|
+
stillInUse = !0;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
stillInUse || (resource.dispose(), sharedResources.delete(fullName));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
sharedResources.size === 0 && resourceCache.delete(SHARED_IDENTITY);
|
|
84
|
+
}
|
|
85
|
+
instanceSharedResources.delete(identity);
|
|
86
|
+
}
|
|
58
87
|
}
|
|
59
88
|
function createAction(resource, actionDefinition) {
|
|
60
89
|
return (dependencies, ...args) => {
|
|
@@ -247,6 +276,7 @@ const subscribeToStorageEventsAndSetToken = createInternalAction(
|
|
|
247
276
|
let tokenRefresherRunning = !1;
|
|
248
277
|
const authStore = {
|
|
249
278
|
name: "Auth",
|
|
279
|
+
isShared: !0,
|
|
250
280
|
getInitialState(instance) {
|
|
251
281
|
const {
|
|
252
282
|
apiHost,
|
|
@@ -4073,8 +4103,7 @@ const _resolveDocument = createAction(documentStore, () => function(doc) {
|
|
|
4073
4103
|
switchMap(
|
|
4074
4104
|
(client) => client.observable.request({
|
|
4075
4105
|
uri: `/projects/${projectId}/datasets/${dataset}/acl`,
|
|
4076
|
-
tag: "acl.get"
|
|
4077
|
-
withCredentials: !0
|
|
4106
|
+
tag: "acl.get"
|
|
4078
4107
|
})
|
|
4079
4108
|
),
|
|
4080
4109
|
tap$1((datasetAcl) => state.set("setGrants", { grants: createGrantsLookup(datasetAcl) }))
|
|
@@ -4847,7 +4876,7 @@ const API_VERSION = "vX", usersStore = {
|
|
|
4847
4876
|
resolveUsers,
|
|
4848
4877
|
setOptions
|
|
4849
4878
|
});
|
|
4850
|
-
var version = "0.0.0-alpha.
|
|
4879
|
+
var version = "0.0.0-alpha.22";
|
|
4851
4880
|
const CORE_SDK_VERSION = getEnv("PKG_VERSION") || `${version}-development`;
|
|
4852
4881
|
export {
|
|
4853
4882
|
AuthStateType,
|