forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: yykoypj <[email protected]>
- Loading branch information
Showing
1,597 changed files
with
86,857 additions
and
33,231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
function use<T>(promise: PromiseLike<T>): T { | ||
const internal: PromiseLike<T> & { | ||
status?: 'pending' | 'fulfilled' | 'rejected'; | ||
value?: T; | ||
reason?: any; | ||
} = promise; | ||
if (internal.status === 'fulfilled') { | ||
return internal.value as T; | ||
} | ||
if (internal.status === 'rejected') { | ||
throw internal.reason; | ||
} else if (internal.status === 'pending') { | ||
throw internal; | ||
} else { | ||
internal.status = 'pending'; | ||
internal.then( | ||
(result) => { | ||
internal.status = 'fulfilled'; | ||
internal.value = result; | ||
}, | ||
(reason) => { | ||
internal.status = 'rejected'; | ||
internal.reason = reason; | ||
}, | ||
); | ||
throw internal; | ||
} | ||
} | ||
|
||
export default use; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react'; | ||
|
||
export const DarkContext = React.createContext(false); | ||
|
||
export default function useDark() { | ||
return React.useContext(DarkContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export default class FetchCache { | ||
private cache: Map<string, PromiseLike<any>> = new Map(); | ||
|
||
get(key: string) { | ||
return this.cache.get(key); | ||
} | ||
|
||
set(key: string, value: PromiseLike<any>) { | ||
this.cache.set(key, value); | ||
} | ||
|
||
promise<T>(key: string, promiseFn: () => PromiseLike<T>): PromiseLike<T> { | ||
const cached = this.get(key); | ||
if (cached) { | ||
return cached; | ||
} | ||
const promise = promiseFn(); | ||
this.set(key, promise); | ||
return promise; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import fetch from 'cross-fetch'; | ||
import use from '../use'; | ||
import FetchCache from './cache'; | ||
|
||
const cache = new FetchCache(); | ||
|
||
const useFetch = <T>(options: string | { request: () => PromiseLike<T>; key: string }) => { | ||
let request; | ||
let key; | ||
if (typeof options === 'string') { | ||
request = () => fetch(options).then((res) => res.json()); | ||
key = options; | ||
} else { | ||
request = options.request; | ||
key = options.key; | ||
} | ||
return use(cache.promise<T>(key, request)); | ||
}; | ||
|
||
export default useFetch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.