-
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.
Add character count remaining display
- Loading branch information
1 parent
69534f9
commit ef06513
Showing
14 changed files
with
198 additions
and
66 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,3 @@ | ||
import Endpoint from "endpoint/Endpoint" | ||
|
||
export default Endpoint("/manifest/form/lengths", "get") |
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,8 @@ | ||
import EndpointFormInputLengths from "endpoint/manifest/EndpointFormInputLengths" | ||
import Manifest from "model/Manifest" | ||
|
||
export default Manifest({ | ||
get () { | ||
return EndpointFormInputLengths.query() | ||
}, | ||
}) |
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,43 @@ | ||
import type { ErrorResponse, Response } from "api.fluff4.me" | ||
import Time from "utility/Time" | ||
|
||
interface ManifestDefinition<T> { | ||
get (): Promise<Response<T> | ErrorResponse<Response<T>>> | ||
} | ||
|
||
interface Manifest<T> { | ||
manifest: T | undefined | ||
getManifest (force?: boolean): Promise<T> | ||
isFresh (manifest?: T): manifest is T | ||
} | ||
|
||
function Manifest<T> (definition: ManifestDefinition<T>): Manifest<T> { | ||
let manifestTime: number | undefined | ||
let promise: Promise<T> | undefined | ||
|
||
const result: Manifest<T> = { | ||
manifest: undefined, | ||
isFresh (manifest?: T): manifest is T { | ||
return !!manifest && Date.now() - (manifestTime ?? 0) < Time.minutes(5) | ||
}, | ||
async getManifest (force?: boolean) { | ||
// don't re-request the tag manifest if it was requested less than 5 minutes ago | ||
if (!force && result.isFresh(result.manifest)) | ||
return result.manifest | ||
|
||
return promise ??= (async () => { | ||
const response = await definition.get() | ||
if (response instanceof Error) | ||
throw response | ||
|
||
result.manifest = response.data | ||
manifestTime = Date.now() | ||
promise = undefined | ||
return result.manifest | ||
})() | ||
}, | ||
} | ||
return result | ||
} | ||
|
||
export default Manifest |
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 |
---|---|---|
@@ -1,73 +1,54 @@ | ||
import type { ManifestGlobalTags, Tag } from "api.fluff4.me" | ||
import type { Tag } from "api.fluff4.me" | ||
import EndpointTagManifest from "endpoint/tag/EndpointTagManifest" | ||
import Time from "utility/Time" | ||
import Manifest from "model/Manifest" | ||
|
||
namespace Tags { | ||
const Tags = Object.assign( | ||
Manifest({ | ||
get () { | ||
return EndpointTagManifest.query() | ||
}, | ||
}), | ||
{ resolve }, | ||
) | ||
|
||
let manifestTime: number | undefined | ||
export let manifest: ManifestGlobalTags | undefined | ||
let promise: Promise<ManifestGlobalTags> | undefined | ||
|
||
function manifestIsFresh (manifest?: ManifestGlobalTags): manifest is ManifestGlobalTags { | ||
return !!manifest && Date.now() - (manifestTime ?? 0) < Time.minutes(5) | ||
} | ||
|
||
export async function getManifest (force = false) { | ||
// don't re-request the tag manifest if it was requested less than 5 minutes ago | ||
if (!force && manifestIsFresh(manifest)) | ||
return manifest | ||
|
||
return promise ??= (async () => { | ||
const response = await EndpointTagManifest.query() | ||
if (response instanceof Error) | ||
throw response | ||
|
||
manifest = response.data | ||
manifestTime = Date.now() | ||
promise = undefined | ||
return manifest | ||
})() | ||
} | ||
export default Tags | ||
|
||
export async function resolve (tag: string): Promise<Tag | undefined> | ||
export async function resolve (category: string, name: string): Promise<Tag | undefined> | ||
export async function resolve (tags?: string[] | null): Promise<Tag[]> | ||
export async function resolve (tags?: string[] | null | string, name?: string) { | ||
if (!tags?.length) | ||
return [] | ||
export async function resolve (tag: string): Promise<Tag | undefined> | ||
export async function resolve (category: string, name: string): Promise<Tag | undefined> | ||
export async function resolve (tags?: string[] | null): Promise<Tag[]> | ||
export async function resolve (tags?: string[] | null | string, name?: string) { | ||
if (!tags?.length) | ||
return [] | ||
|
||
if (Array.isArray(tags)) | ||
return resolveInternal(tags) | ||
if (Array.isArray(tags)) | ||
return resolveInternal(tags) | ||
|
||
const tag = name ? `${tags}: ${name}` : tags | ||
const [result] = await resolveInternal([tag]) | ||
return result as Tag | undefined | ||
} | ||
const tag = name ? `${tags}: ${name}` : tags | ||
const [result] = await resolveInternal([tag]) | ||
return result as Tag | undefined | ||
} | ||
|
||
|
||
async function resolveInternal (tags: string[]) { | ||
const result: Tag[] = [] | ||
async function resolveInternal (tags: string[]) { | ||
const result: Tag[] = [] | ||
|
||
let manifest = await getManifest() | ||
let manifest = await Tags.getManifest() | ||
resolveTags() | ||
if (result.length !== tags.length && !Tags.isFresh()) { | ||
manifest = await Tags.getManifest(true) | ||
resolveTags() | ||
if (result.length !== tags.length && !manifestIsFresh()) { | ||
manifest = await getManifest(true) | ||
resolveTags() | ||
} | ||
} | ||
|
||
return result | ||
return result | ||
|
||
function resolveTags () { | ||
result.splice(0, Infinity) | ||
for (const tagString of tags) { | ||
const tag = manifest.tags[tagString] | ||
if (!tag) | ||
continue | ||
function resolveTags () { | ||
result.splice(0, Infinity) | ||
for (const tagString of tags) { | ||
const tag = manifest.tags[tagString] | ||
if (!tag) | ||
continue | ||
|
||
result.push(tag) | ||
} | ||
result.push(tag) | ||
} | ||
} | ||
} | ||
|
||
export default Tags |
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 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 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
Oops, something went wrong.