@sanity/schema 4.20.0-next.73 → 4.20.0-next.75
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/lib/_internal.d.ts +19 -1
- package/lib/_internal.js +65 -8
- package/lib/_internal.js.map +1 -1
- package/package.json +6 -6
package/lib/_internal.d.ts
CHANGED
|
@@ -217,7 +217,18 @@ export declare class DescriptorConverter {
|
|
|
217
217
|
*
|
|
218
218
|
* This is automatically cached in a weak map.
|
|
219
219
|
*/
|
|
220
|
-
get(
|
|
220
|
+
get(
|
|
221
|
+
schema: Schema,
|
|
222
|
+
opts?: {
|
|
223
|
+
/**
|
|
224
|
+
* If present, this will use an idle scheduler which records duration into this array.
|
|
225
|
+
* This option will be ignored if the `scheduler` option is passed in.
|
|
226
|
+
**/
|
|
227
|
+
pauseDurations?: number[]
|
|
228
|
+
/** An explicit scheduler to do the work. */
|
|
229
|
+
scheduler?: Scheduler
|
|
230
|
+
},
|
|
231
|
+
): Promise<SetSynchronization<RegistryType>>
|
|
221
232
|
}
|
|
222
233
|
|
|
223
234
|
export declare function extractSchema(
|
|
@@ -294,6 +305,13 @@ export declare function resolveSearchConfig(type: any, maxDepth?: number): any
|
|
|
294
305
|
|
|
295
306
|
export declare function resolveSearchConfigForBaseFieldPaths(type: any, maxDepth?: number): any
|
|
296
307
|
|
|
308
|
+
/** The scheduler is capable of executing work in different ways. */
|
|
309
|
+
declare type Scheduler = {
|
|
310
|
+
map<T, U>(arr: T[], fn: (val: T) => U): Promise<U[]>
|
|
311
|
+
forEach<T>(arr: T[], fn: (val: T) => void): Promise<void>
|
|
312
|
+
forEachIter<T>(iter: Iterable<T>, fn: (val: T) => void): Promise<void>
|
|
313
|
+
}
|
|
314
|
+
|
|
297
315
|
/**
|
|
298
316
|
* @beta
|
|
299
317
|
*/
|
package/lib/_internal.js
CHANGED
|
@@ -15,7 +15,63 @@ import omit from "lodash/omit.js";
|
|
|
15
15
|
import leven from "leven";
|
|
16
16
|
import inspect from "object-inspect";
|
|
17
17
|
import { createReferenceTypeNode } from "groq-js";
|
|
18
|
-
const
|
|
18
|
+
const MAX_IDLE_WORK = 8.333333333333334;
|
|
19
|
+
class IdleScheduler {
|
|
20
|
+
#durations = [];
|
|
21
|
+
#lastAwake;
|
|
22
|
+
constructor(durations) {
|
|
23
|
+
this.#lastAwake = performance.now(), this.#durations = durations;
|
|
24
|
+
}
|
|
25
|
+
async map(arr, fn) {
|
|
26
|
+
const result = [];
|
|
27
|
+
for (const val of arr) {
|
|
28
|
+
const pause = this._tryPause();
|
|
29
|
+
pause && await pause, result.push(fn(val));
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
async forEach(arr, fn) {
|
|
34
|
+
for (const val of arr) {
|
|
35
|
+
const pause = this._tryPause();
|
|
36
|
+
pause && await pause, fn(val);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async forEachIter(iter, fn) {
|
|
40
|
+
for (const val of iter) {
|
|
41
|
+
const pause = this._tryPause();
|
|
42
|
+
pause && await pause, fn(val);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** Should be invoked at the end to also measure the last pause. */
|
|
46
|
+
end() {
|
|
47
|
+
this.#durations.push(performance.now() - this.#lastAwake);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Yields control back to the UI.
|
|
51
|
+
*/
|
|
52
|
+
_tryPause() {
|
|
53
|
+
const elapsed = performance.now() - this.#lastAwake;
|
|
54
|
+
if (!(elapsed < MAX_IDLE_WORK))
|
|
55
|
+
return this.#durations.push(elapsed), new Promise((resolve) => {
|
|
56
|
+
const done = () => {
|
|
57
|
+
this.#lastAwake = performance.now(), resolve();
|
|
58
|
+
};
|
|
59
|
+
typeof requestIdleCallback == "function" ? requestIdleCallback(done, { timeout: 1 }) : typeof requestAnimationFrame == "function" ? requestAnimationFrame(done) : setTimeout(done, 0);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const SYNC_SCHEDULER = {
|
|
64
|
+
async map(arr, fn) {
|
|
65
|
+
return arr.map(fn);
|
|
66
|
+
},
|
|
67
|
+
async forEach(arr, fn) {
|
|
68
|
+
return arr.forEach(fn);
|
|
69
|
+
},
|
|
70
|
+
async forEachIter(iter, fn) {
|
|
71
|
+
for (const val of iter)
|
|
72
|
+
fn(val);
|
|
73
|
+
}
|
|
74
|
+
}, MAX_DEPTH_UKNOWN = 5;
|
|
19
75
|
class DescriptorConverter {
|
|
20
76
|
cache = /* @__PURE__ */ new WeakMap();
|
|
21
77
|
/**
|
|
@@ -23,15 +79,16 @@ class DescriptorConverter {
|
|
|
23
79
|
*
|
|
24
80
|
* This is automatically cached in a weak map.
|
|
25
81
|
*/
|
|
26
|
-
async get(schema) {
|
|
82
|
+
async get(schema, opts) {
|
|
27
83
|
let value = this.cache.get(schema);
|
|
28
84
|
if (value) return value;
|
|
29
|
-
|
|
85
|
+
let idleScheduler;
|
|
86
|
+
const scheduler = opts?.scheduler || (opts?.pauseDurations ? idleScheduler = new IdleScheduler(opts.pauseDurations) : SYNC_SCHEDULER), options = {
|
|
30
87
|
fields: /* @__PURE__ */ new Map(),
|
|
31
88
|
duplicateFields: /* @__PURE__ */ new Map(),
|
|
32
89
|
arrayElements: /* @__PURE__ */ new Map(),
|
|
33
90
|
duplicateArrayElements: /* @__PURE__ */ new Map()
|
|
34
|
-
}, namedTypes = schema.getLocalTypeNames()
|
|
91
|
+
}, namedTypes = await scheduler.map(schema.getLocalTypeNames(), (name) => {
|
|
35
92
|
const typeDef = convertTypeDef(schema.get(name), name, options);
|
|
36
93
|
return { name, typeDef };
|
|
37
94
|
}), rewriteMap = /* @__PURE__ */ new Map();
|
|
@@ -40,13 +97,13 @@ class DescriptorConverter {
|
|
|
40
97
|
for (const [arrayElem, key] of options.duplicateArrayElements.entries())
|
|
41
98
|
rewriteMap.set(arrayElem, { __type: "hoisted", key });
|
|
42
99
|
const builder = new SetBuilder({ rewriteMap });
|
|
43
|
-
|
|
100
|
+
return await scheduler.forEachIter(options.duplicateFields.entries(), ([fieldDef, key]) => {
|
|
44
101
|
builder.addObject("sanity.schema.hoisted", { key, value: { ...fieldDef } });
|
|
45
|
-
|
|
102
|
+
}), await scheduler.forEachIter(options.duplicateArrayElements.entries(), ([arrayElem, key]) => {
|
|
46
103
|
builder.addObject("sanity.schema.hoisted", { key, value: { ...arrayElem } });
|
|
47
|
-
|
|
104
|
+
}), await scheduler.forEach(namedTypes, (namedType) => {
|
|
48
105
|
builder.addObject("sanity.schema.namedType", namedType);
|
|
49
|
-
|
|
106
|
+
}), schema.parent && builder.addSet(await this.get(schema.parent, { scheduler })), value = builder.build("sanity.schema.registry"), this.cache.set(schema, value), idleScheduler && idleScheduler.end(), value;
|
|
50
107
|
}
|
|
51
108
|
}
|
|
52
109
|
function convertCommonTypeDef(schemaType, path, opts) {
|