Skip to content

Commit

Permalink
fix: patch useAsyncFn (#11604)
Browse files Browse the repository at this point in the history
* fix: patch useAsyncFn

* Revert "revert: changes on identifier (#11441)"

This reverts commit c198cc3.
  • Loading branch information
swkatmask committed Apr 30, 2024
1 parent fda6921 commit 6976a47
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 77 deletions.
22 changes: 14 additions & 8 deletions packages/base/src/Identifier/identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,8 @@ export class ECKeyIdentifier extends Identifier {
)
}
declare [Symbol.toStringTag]: string
#ec: undefined
static [Symbol.hasInstance](x: any): boolean {
return typeof x === 'object' && x !== null && #ec in x
return toText(x)?.startsWith('ec_key:') ?? false
}
static {
ECKeyIdentifier.prototype[Symbol.toStringTag] = 'ECKeyIdentifier'
Expand Down Expand Up @@ -184,9 +183,8 @@ export class PostIVIdentifier extends Identifier {
return new Uint8Array(decodeArrayBuffer(x))
}
declare [Symbol.toStringTag]: string
#post_iv: undefined
static [Symbol.hasInstance](x: any): boolean {
return typeof x === 'object' && x !== null && #post_iv in x
return toText(x)?.startsWith('post_iv:') ?? false
}
static {
PostIVIdentifier.prototype[Symbol.toStringTag] = 'PostIVIdentifier'
Expand Down Expand Up @@ -234,9 +232,8 @@ export class PostIdentifier extends Identifier {
return this.postID
}
declare [Symbol.toStringTag]: string
#post: undefined
static [Symbol.hasInstance](x: any): boolean {
return typeof x === 'object' && x !== null && #post in x
return toText(x)?.startsWith('post:') ?? false
}
static {
PostIdentifier.prototype[Symbol.toStringTag] = 'PostIdentifier'
Expand Down Expand Up @@ -293,9 +290,8 @@ export class ProfileIdentifier extends Identifier {
return `person:${this.network}/${this.userId}`
}
declare [Symbol.toStringTag]: string
#profile: undefined
static [Symbol.hasInstance](x: any): boolean {
return typeof x === 'object' && x !== null && #profile in x
return toText(x)?.startsWith('person:') ?? false
}
static {
ProfileIdentifier.prototype[Symbol.toStringTag] = 'ProfileIdentifier'
Expand All @@ -315,3 +311,13 @@ function banSlash(input: string | undefined | null, source?: string) {
if (!input) return
if (input.includes('/')) throw new TypeError(`Cannot contain / in a part of identifier (${source}): ${input}`)
}

function toText(x: any) {
try {
const text = x.toText()
if (typeof text === 'string') return text
return
} catch {
return
}
}
32 changes: 28 additions & 4 deletions patches/react-use@17.4.0.patch
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ diff --git a/CHANGELOG.md b/CHANGELOG.md
deleted file mode 100644
index 9b28df0e8aa4734af2c7006fd077ff17e2281db5..0000000000000000000000000000000000000000
diff --git a/esm/useAsyncFn.js b/esm/useAsyncFn.js
index 01d7307bd106229f6d791d0c588589235f404d97..6dd2699c44098e7fdb861c28b5a52c094ee847a8 100644
index 01d7307bd106229f6d791d0c588589235f404d97..872d4eea890cff495ced6fae8a91963d182164ee 100644
--- a/esm/useAsyncFn.js
+++ b/esm/useAsyncFn.js
@@ -21,7 +21,7 @@ export default function useAsyncFn(fn, deps, initialState) {
@@ -13,15 +13,16 @@ export default function useAsyncFn(fn, deps, initialState) {
args[_i] = arguments[_i];
}
var callId = ++lastCallId.current;
- if (!state.loading) {
- set(function (prevState) { return (__assign(__assign({}, prevState), { loading: true })); });
- }
+ set(function (prevState) {
+ if (prevState.loading) return prevState
+ return (__assign(__assign({}, prevState), { loading: true }));
+ });
return fn.apply(void 0, args).then(function (value) {
isMounted() && callId === lastCallId.current && set({ value: value, loading: false });
return value;
}, function (error) {
isMounted() && callId === lastCallId.current && set({ error: error, loading: false });
Expand All @@ -15,10 +27,22 @@ index 01d7307bd106229f6d791d0c588589235f404d97..6dd2699c44098e7fdb861c28b5a52c09
}, deps);
return [state, callback];
diff --git a/lib/useAsyncFn.js b/lib/useAsyncFn.js
index e06fd819ccad625d709fa9907e946a9b8bc58543..261f6b084f14ccc84265a2e9b300d1b86150b3b4 100644
index e06fd819ccad625d709fa9907e946a9b8bc58543..e79a2214916f5319687b532c5366a70ae4dff6d9 100644
--- a/lib/useAsyncFn.js
+++ b/lib/useAsyncFn.js
@@ -23,7 +23,7 @@ function useAsyncFn(fn, deps, initialState) {
@@ -15,15 +15,16 @@ function useAsyncFn(fn, deps, initialState) {
args[_i] = arguments[_i];
}
var callId = ++lastCallId.current;
- if (!state.loading) {
- set(function (prevState) { return (tslib_1.__assign(tslib_1.__assign({}, prevState), { loading: true })); });
- }
+ set(function (prevState) {
+ if (prevState.loading) return prevState
+ return (tslib_1.__assign(tslib_1.__assign({}, prevState), { loading: true }));
+ });
return fn.apply(void 0, args).then(function (value) {
isMounted() && callId === lastCallId.current && set({ value: value, loading: false });
return value;
}, function (error) {
isMounted() && callId === lastCallId.current && set({ error: error, loading: false });
Expand Down

0 comments on commit 6976a47

Please sign in to comment.