@stackframe/stack-shared 2.3.2 → 2.3.5
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/utils/caches.d.ts +2 -0
- package/dist/utils/caches.js +7 -2
- package/dist/utils/globals.d.ts +3 -0
- package/dist/utils/globals.js +14 -0
- package/dist/utils/objects.d.ts +1 -1
- package/dist/utils/promises.d.ts +1 -3
- package/dist/utils/promises.js +5 -21
- package/dist/utils/results.d.ts +1 -1
- package/dist/utils/results.js +8 -8
- package/package.json +1 -1
package/dist/utils/caches.d.ts
CHANGED
|
@@ -34,6 +34,7 @@ export declare class AsyncCache<D extends any[], T> {
|
|
|
34
34
|
});
|
|
35
35
|
readonly getOrWait: (key: D, cacheStrategy: CacheStrategy) => ReactPromise<T>;
|
|
36
36
|
readonly forceSetCachedValue: (key: D, value: T) => void;
|
|
37
|
+
readonly forceSetCachedValueAsync: (key: D, value: Promise<T>) => ReactPromise<boolean>;
|
|
37
38
|
readonly refresh: (key: D) => Promise<T>;
|
|
38
39
|
readonly invalidate: (key: D) => Promise<T>;
|
|
39
40
|
readonly onChange: (key: D, callback: (value: T, oldValue: T | undefined) => void) => {
|
|
@@ -75,6 +76,7 @@ declare class AsyncValueCache<T> {
|
|
|
75
76
|
private _setAsync;
|
|
76
77
|
private _refetch;
|
|
77
78
|
forceSetCachedValue(value: T): void;
|
|
79
|
+
forceSetCachedValueAsync(value: Promise<T>): ReactPromise<boolean>;
|
|
78
80
|
refresh(): Promise<T>;
|
|
79
81
|
invalidate(): Promise<T>;
|
|
80
82
|
onChange(callback: (value: T, oldValue: T | undefined) => void): {
|
package/dist/utils/caches.js
CHANGED
|
@@ -46,6 +46,7 @@ export class AsyncCache {
|
|
|
46
46
|
getIfCached = this._createKeyed("getIfCached");
|
|
47
47
|
getOrWait = this._createKeyed("getOrWait");
|
|
48
48
|
forceSetCachedValue = this._createKeyed("forceSetCachedValue");
|
|
49
|
+
forceSetCachedValueAsync = this._createKeyed("forceSetCachedValueAsync");
|
|
49
50
|
refresh = this._createKeyed("refresh");
|
|
50
51
|
invalidate = this._createKeyed("invalidate");
|
|
51
52
|
onChange = this._createKeyed("onChange");
|
|
@@ -88,7 +89,9 @@ class AsyncValueCache {
|
|
|
88
89
|
this._store.set(value);
|
|
89
90
|
}
|
|
90
91
|
_setAsync(value) {
|
|
91
|
-
|
|
92
|
+
const promise = pending(value);
|
|
93
|
+
this._pendingPromise = promise;
|
|
94
|
+
return pending(this._store.setAsync(promise));
|
|
92
95
|
}
|
|
93
96
|
_refetch(cacheStrategy) {
|
|
94
97
|
if (cacheStrategy === "read-write" && this._pendingPromise) {
|
|
@@ -98,12 +101,14 @@ class AsyncValueCache {
|
|
|
98
101
|
if (cacheStrategy === "never") {
|
|
99
102
|
return promise;
|
|
100
103
|
}
|
|
101
|
-
this._pendingPromise = promise;
|
|
102
104
|
return pending(this._setAsync(promise).then(() => promise));
|
|
103
105
|
}
|
|
104
106
|
forceSetCachedValue(value) {
|
|
105
107
|
this._set(value);
|
|
106
108
|
}
|
|
109
|
+
forceSetCachedValueAsync(value) {
|
|
110
|
+
return this._setAsync(value);
|
|
111
|
+
}
|
|
107
112
|
async refresh() {
|
|
108
113
|
return await this.getOrWait("write-only");
|
|
109
114
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const globalVar = typeof globalThis !== 'undefined' ? globalThis :
|
|
2
|
+
typeof window !== 'undefined' ? window :
|
|
3
|
+
typeof global !== 'undefined' ? global :
|
|
4
|
+
typeof self !== 'undefined' ? self :
|
|
5
|
+
{};
|
|
6
|
+
export { globalVar, };
|
|
7
|
+
const stackGlobalsSymbol = Symbol.for('__stack-globals');
|
|
8
|
+
globalVar[stackGlobalsSymbol] ??= {};
|
|
9
|
+
export function createGlobal(key, init) {
|
|
10
|
+
if (!globalVar[stackGlobalsSymbol][key]) {
|
|
11
|
+
globalVar[stackGlobalsSymbol][key] = init();
|
|
12
|
+
}
|
|
13
|
+
return globalVar[stackGlobalsSymbol][key];
|
|
14
|
+
}
|
package/dist/utils/objects.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export type DeepPartial<T> = T extends object ? {
|
|
|
8
8
|
*/
|
|
9
9
|
export declare function deepPlainEquals<T>(obj1: T, obj2: unknown): obj2 is T;
|
|
10
10
|
export declare function typedEntries<T extends {}>(obj: T): [keyof T, T[keyof T]][];
|
|
11
|
-
export declare function typedFromEntries<
|
|
11
|
+
export declare function typedFromEntries<K extends PropertyKey, V>(entries: [K, V][]): Record<K, V>;
|
|
12
12
|
export declare function typedKeys<T extends {}>(obj: T): (keyof T)[];
|
|
13
13
|
export declare function typedValues<T extends {}>(obj: T): T[keyof T][];
|
|
14
14
|
export declare function typedAssign<T extends {}, U extends {}>(target: T, source: U): asserts target is T & U;
|
package/dist/utils/promises.d.ts
CHANGED
|
@@ -11,9 +11,7 @@ export declare function resolved<T>(value: T): ReactPromise<T>;
|
|
|
11
11
|
/**
|
|
12
12
|
* Like Promise.resolve(...), but also adds the status and value properties for use with React's `use` hook.
|
|
13
13
|
*/
|
|
14
|
-
export declare function rejected<T>(reason: unknown
|
|
15
|
-
disableErrorWrapping?: boolean;
|
|
16
|
-
}): ReactPromise<T>;
|
|
14
|
+
export declare function rejected<T>(reason: unknown): ReactPromise<T>;
|
|
17
15
|
export declare function neverResolve(): ReactPromise<never>;
|
|
18
16
|
export declare function pending<T>(promise: Promise<T>, options?: {
|
|
19
17
|
disableErrorWrapping?: boolean;
|
package/dist/utils/promises.js
CHANGED
|
@@ -41,11 +41,10 @@ export function resolved(value) {
|
|
|
41
41
|
/**
|
|
42
42
|
* Like Promise.resolve(...), but also adds the status and value properties for use with React's `use` hook.
|
|
43
43
|
*/
|
|
44
|
-
export function rejected(reason
|
|
45
|
-
|
|
46
|
-
return Object.assign(Promise.reject(actualReason), {
|
|
44
|
+
export function rejected(reason) {
|
|
45
|
+
return Object.assign(Promise.reject(reason), {
|
|
47
46
|
status: "rejected",
|
|
48
|
-
reason:
|
|
47
|
+
reason: reason,
|
|
49
48
|
});
|
|
50
49
|
}
|
|
51
50
|
export function neverResolve() {
|
|
@@ -58,27 +57,12 @@ export function pending(promise, options = {}) {
|
|
|
58
57
|
return value;
|
|
59
58
|
}, actualReason => {
|
|
60
59
|
res.status = "rejected";
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
throw reason;
|
|
60
|
+
res.reason = actualReason;
|
|
61
|
+
throw actualReason;
|
|
64
62
|
});
|
|
65
63
|
res.status = "pending";
|
|
66
64
|
return res;
|
|
67
65
|
}
|
|
68
|
-
function createReactPromiseErrorWrapper(error) {
|
|
69
|
-
return new ReactPromiseErrorWrapper(error);
|
|
70
|
-
}
|
|
71
|
-
class ReactPromiseErrorWrapper extends StackAssertionError {
|
|
72
|
-
error;
|
|
73
|
-
wrappedErrorMessage;
|
|
74
|
-
constructor(error) {
|
|
75
|
-
const wrappedErrorMessage = error instanceof ReactPromiseErrorWrapper ? error.wrappedErrorMessage : `${error}`;
|
|
76
|
-
super(`Error occured while creating a ReactPromise: ${wrappedErrorMessage}\n\nSee the \`cause\` property for the original error. This error is a wrapper around the original error to preserve the stack trace (which would go lost when using Stack's pending(...) or rejected(...) functions).`, { cause: error });
|
|
77
|
-
this.error = error;
|
|
78
|
-
this.wrappedErrorMessage = wrappedErrorMessage;
|
|
79
|
-
this.name = "ReactPromiseErrorWrapper";
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
66
|
export async function wait(ms) {
|
|
83
67
|
return await new Promise(resolve => setTimeout(resolve, ms));
|
|
84
68
|
}
|
package/dist/utils/results.d.ts
CHANGED
|
@@ -58,7 +58,7 @@ declare function pending(): AsyncResult<never, never, void> & {
|
|
|
58
58
|
declare function pending<P>(progress: P): AsyncResult<never, never, P> & {
|
|
59
59
|
status: "pending";
|
|
60
60
|
};
|
|
61
|
-
declare function promiseToResult<T
|
|
61
|
+
declare function promiseToResult<T>(promise: Promise<T>): Promise<Result<T>>;
|
|
62
62
|
declare function fromThrowing<T>(fn: () => T): Result<T, unknown>;
|
|
63
63
|
declare function mapResult<T, U, E = unknown, P = unknown>(result: Result<T, E>, fn: (data: T) => U): Result<U, E>;
|
|
64
64
|
declare function mapResult<T, U, E = unknown, P = unknown>(result: AsyncResult<T, E, P>, fn: (data: T) => U): AsyncResult<U, E, P>;
|
package/dist/utils/results.js
CHANGED
|
@@ -53,14 +53,14 @@ function pending(progress) {
|
|
|
53
53
|
progress: progress,
|
|
54
54
|
};
|
|
55
55
|
}
|
|
56
|
-
function promiseToResult(promise) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
error
|
|
63
|
-
}
|
|
56
|
+
async function promiseToResult(promise) {
|
|
57
|
+
try {
|
|
58
|
+
const value = await promise;
|
|
59
|
+
return Result.ok(value);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
return Result.error(error);
|
|
63
|
+
}
|
|
64
64
|
}
|
|
65
65
|
function fromThrowing(fn) {
|
|
66
66
|
try {
|