mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-04-01 21:13:09 +00:00
rename frontend to web
This commit is contained in:
17
web/projects/ui/src/app/util/animations.ts
Normal file
17
web/projects/ui/src/app/util/animations.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { animate, style, transition, trigger } from '@angular/animations'
|
||||
|
||||
const TRANSITION = '{{duration}}ms {{delay}}ms ease-in-out'
|
||||
const DURATION = { params: { duration: 300, delay: 0 } }
|
||||
|
||||
export const heightCollapse = trigger('heightCollapse', [
|
||||
transition(
|
||||
':enter',
|
||||
[style({ height: 0 }), animate(TRANSITION, style({ height: '*' }))],
|
||||
DURATION,
|
||||
),
|
||||
transition(
|
||||
':leave',
|
||||
[style({ height: '*' }), animate(TRANSITION, style({ height: 0 }))],
|
||||
DURATION,
|
||||
),
|
||||
])
|
||||
11
web/projects/ui/src/app/util/clearnetAddress.ts
Normal file
11
web/projects/ui/src/app/util/clearnetAddress.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { DomainInfo } from '../services/patch-db/data-model'
|
||||
|
||||
export function getClearnetAddress(
|
||||
protocol: string,
|
||||
domainInfo: DomainInfo | null,
|
||||
path = '',
|
||||
) {
|
||||
if (!domainInfo) return ''
|
||||
const subdomain = domainInfo.subdomain ? `${domainInfo.subdomain}.` : ''
|
||||
return `${protocol}://${subdomain}${domainInfo.domain}${path}`
|
||||
}
|
||||
161
web/projects/ui/src/app/util/config-utilities.ts
Normal file
161
web/projects/ui/src/app/util/config-utilities.ts
Normal file
@@ -0,0 +1,161 @@
|
||||
import { DefaultString } from '@start9labs/start-sdk/lib/config/configTypes'
|
||||
|
||||
export class Range {
|
||||
min?: number
|
||||
max?: number
|
||||
minInclusive!: boolean
|
||||
maxInclusive!: boolean
|
||||
|
||||
static from(s: string = '(*,*)'): Range {
|
||||
const r = new Range()
|
||||
r.minInclusive = s.startsWith('[')
|
||||
r.maxInclusive = s.endsWith(']')
|
||||
const [minStr, maxStr] = s.split(',').map(a => a.trim())
|
||||
r.min = minStr === '(*' ? undefined : Number(minStr.slice(1))
|
||||
r.max = maxStr === '*)' ? undefined : Number(maxStr.slice(0, -1))
|
||||
return r
|
||||
}
|
||||
|
||||
checkIncludes(n: number) {
|
||||
if (
|
||||
this.hasMin() &&
|
||||
(this.min > n || (!this.minInclusive && this.min == n))
|
||||
) {
|
||||
throw new Error(this.minMessage())
|
||||
}
|
||||
if (
|
||||
this.hasMax() &&
|
||||
(this.max < n || (!this.maxInclusive && this.max == n))
|
||||
) {
|
||||
throw new Error(this.maxMessage())
|
||||
}
|
||||
}
|
||||
|
||||
private hasMin(): this is Range & { min: number } {
|
||||
return this.min !== undefined
|
||||
}
|
||||
|
||||
private hasMax(): this is Range & { max: number } {
|
||||
return this.max !== undefined
|
||||
}
|
||||
|
||||
private minMessage(): string {
|
||||
return `greater than${this.minInclusive ? ' or equal to' : ''} ${this.min}`
|
||||
}
|
||||
|
||||
private maxMessage(): string {
|
||||
return `less than${this.maxInclusive ? ' or equal to' : ''} ${this.max}`
|
||||
}
|
||||
}
|
||||
|
||||
export function getDefaultString(defaultSpec: DefaultString): string {
|
||||
if (typeof defaultSpec === 'string') {
|
||||
return defaultSpec
|
||||
} else {
|
||||
let s = ''
|
||||
for (let i = 0; i < defaultSpec.len; i++) {
|
||||
s = s + getRandomCharInSet(defaultSpec.charset)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
// a,g,h,A-Z,,,,-
|
||||
function getRandomCharInSet(charset: string): string {
|
||||
const set = stringToCharSet(charset)
|
||||
let charIdx = Math.floor(Math.random() * set.len)
|
||||
for (let range of set.ranges) {
|
||||
if (range.len > charIdx) {
|
||||
return String.fromCharCode(range.start.charCodeAt(0) + charIdx)
|
||||
}
|
||||
charIdx -= range.len
|
||||
}
|
||||
throw new Error('unreachable')
|
||||
}
|
||||
|
||||
function stringToCharSet(charset: string): CharSet {
|
||||
let set: CharSet = { ranges: [], len: 0 }
|
||||
let start: string | null = null
|
||||
let end: string | null = null
|
||||
let in_range = false
|
||||
for (let char of charset) {
|
||||
switch (char) {
|
||||
case ',':
|
||||
if (start !== null && end !== null) {
|
||||
if (start!.charCodeAt(0) > end!.charCodeAt(0)) {
|
||||
throw new Error('start > end of charset')
|
||||
}
|
||||
const len = end.charCodeAt(0) - start.charCodeAt(0) + 1
|
||||
set.ranges.push({
|
||||
start,
|
||||
end,
|
||||
len,
|
||||
})
|
||||
set.len += len
|
||||
start = null
|
||||
end = null
|
||||
in_range = false
|
||||
} else if (start !== null && !in_range) {
|
||||
set.len += 1
|
||||
set.ranges.push({ start, end: start, len: 1 })
|
||||
start = null
|
||||
} else if (start !== null && in_range) {
|
||||
end = ','
|
||||
} else if (start === null && end === null && !in_range) {
|
||||
start = ','
|
||||
} else {
|
||||
throw new Error('unexpected ","')
|
||||
}
|
||||
break
|
||||
case '-':
|
||||
if (start === null) {
|
||||
start = '-'
|
||||
} else if (!in_range) {
|
||||
in_range = true
|
||||
} else if (in_range && end === null) {
|
||||
end = '-'
|
||||
} else {
|
||||
throw new Error('unexpected "-"')
|
||||
}
|
||||
break
|
||||
default:
|
||||
if (start === null) {
|
||||
start = char
|
||||
} else if (in_range && end === null) {
|
||||
end = char
|
||||
} else {
|
||||
throw new Error(`unexpected "${char}"`)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (start !== null && end !== null) {
|
||||
if (start!.charCodeAt(0) > end!.charCodeAt(0)) {
|
||||
throw new Error('start > end of charset')
|
||||
}
|
||||
const len = end.charCodeAt(0) - start.charCodeAt(0) + 1
|
||||
set.ranges.push({
|
||||
start,
|
||||
end,
|
||||
len,
|
||||
})
|
||||
set.len += len
|
||||
} else if (start !== null) {
|
||||
set.len += 1
|
||||
set.ranges.push({
|
||||
start,
|
||||
end: start,
|
||||
len: 1,
|
||||
})
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
interface CharSet {
|
||||
ranges: {
|
||||
start: string
|
||||
end: string
|
||||
len: number
|
||||
}[]
|
||||
len: number
|
||||
}
|
||||
9
web/projects/ui/src/app/util/configBuilderToSpec.ts
Normal file
9
web/projects/ui/src/app/util/configBuilderToSpec.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Config } from '@start9labs/start-sdk/lib/config/builder/config'
|
||||
|
||||
export async function configBuilderToSpec(
|
||||
builder:
|
||||
| Config<Record<string, unknown>, unknown>
|
||||
| Config<Record<string, unknown>, never>,
|
||||
) {
|
||||
return builder.build({} as any)
|
||||
}
|
||||
252
web/projects/ui/src/app/util/countries.json
Normal file
252
web/projects/ui/src/app/util/countries.json
Normal file
@@ -0,0 +1,252 @@
|
||||
{
|
||||
"AD": "Andorra",
|
||||
"AE": "United Arab Emirates",
|
||||
"AF": "Afghanistan",
|
||||
"AG": "Antigua and Barbuda",
|
||||
"AI": "Anguilla",
|
||||
"AL": "Albania",
|
||||
"AM": "Armenia",
|
||||
"AO": "Angola",
|
||||
"AQ": "Antarctica",
|
||||
"AR": "Argentina",
|
||||
"AS": "American Samoa",
|
||||
"AT": "Austria",
|
||||
"AU": "Australia",
|
||||
"AW": "Aruba",
|
||||
"AX": "Aland Islands",
|
||||
"AZ": "Azerbaijan",
|
||||
"BA": "Bosnia and Herzegovina",
|
||||
"BB": "Barbados",
|
||||
"BD": "Bangladesh",
|
||||
"BE": "Belgium",
|
||||
"BF": "Burkina Faso",
|
||||
"BG": "Bulgaria",
|
||||
"BH": "Bahrain",
|
||||
"BI": "Burundi",
|
||||
"BJ": "Benin",
|
||||
"BL": "Saint Barthelemy",
|
||||
"BM": "Bermuda",
|
||||
"BN": "Brunei",
|
||||
"BO": "Bolivia",
|
||||
"BQ": "Bonaire, Saint Eustatius and Saba ",
|
||||
"BR": "Brazil",
|
||||
"BS": "Bahamas",
|
||||
"BT": "Bhutan",
|
||||
"BV": "Bouvet Island",
|
||||
"BW": "Botswana",
|
||||
"BY": "Belarus",
|
||||
"BZ": "Belize",
|
||||
"CA": "Canada",
|
||||
"CC": "Cocos Islands",
|
||||
"CD": "Democratic Republic of the Congo",
|
||||
"CF": "Central African Republic",
|
||||
"CG": "Republic of the Congo",
|
||||
"CH": "Switzerland",
|
||||
"CI": "Ivory Coast",
|
||||
"CK": "Cook Islands",
|
||||
"CL": "Chile",
|
||||
"CM": "Cameroon",
|
||||
"CN": "China",
|
||||
"CO": "Colombia",
|
||||
"CR": "Costa Rica",
|
||||
"CU": "Cuba",
|
||||
"CV": "Cape Verde",
|
||||
"CW": "Curacao",
|
||||
"CX": "Christmas Island",
|
||||
"CY": "Cyprus",
|
||||
"CZ": "Czech Republic",
|
||||
"DE": "Germany",
|
||||
"DJ": "Djibouti",
|
||||
"DK": "Denmark",
|
||||
"DM": "Dominica",
|
||||
"DO": "Dominican Republic",
|
||||
"DZ": "Algeria",
|
||||
"EC": "Ecuador",
|
||||
"EE": "Estonia",
|
||||
"EG": "Egypt",
|
||||
"EH": "Western Sahara",
|
||||
"ER": "Eritrea",
|
||||
"ES": "Spain",
|
||||
"ET": "Ethiopia",
|
||||
"FI": "Finland",
|
||||
"FJ": "Fiji",
|
||||
"FK": "Falkland Islands",
|
||||
"FM": "Micronesia",
|
||||
"FO": "Faroe Islands",
|
||||
"FR": "France",
|
||||
"GA": "Gabon",
|
||||
"GB": "United Kingdom",
|
||||
"GD": "Grenada",
|
||||
"GE": "Georgia",
|
||||
"GF": "French Guiana",
|
||||
"GG": "Guernsey",
|
||||
"GH": "Ghana",
|
||||
"GI": "Gibraltar",
|
||||
"GL": "Greenland",
|
||||
"GM": "Gambia",
|
||||
"GN": "Guinea",
|
||||
"GP": "Guadeloupe",
|
||||
"GQ": "Equatorial Guinea",
|
||||
"GR": "Greece",
|
||||
"GS": "South Georgia and the South Sandwich Islands",
|
||||
"GT": "Guatemala",
|
||||
"GU": "Guam",
|
||||
"GW": "Guinea-Bissau",
|
||||
"GY": "Guyana",
|
||||
"HK": "Hong Kong",
|
||||
"HM": "Heard Island and McDonald Islands",
|
||||
"HN": "Honduras",
|
||||
"HR": "Croatia",
|
||||
"HT": "Haiti",
|
||||
"HU": "Hungary",
|
||||
"ID": "Indonesia",
|
||||
"IE": "Ireland",
|
||||
"IL": "Israel",
|
||||
"IM": "Isle of Man",
|
||||
"IN": "India",
|
||||
"IO": "British Indian Ocean Territory",
|
||||
"IQ": "Iraq",
|
||||
"IR": "Iran",
|
||||
"IS": "Iceland",
|
||||
"IT": "Italy",
|
||||
"JE": "Jersey",
|
||||
"JM": "Jamaica",
|
||||
"JO": "Jordan",
|
||||
"JP": "Japan",
|
||||
"KE": "Kenya",
|
||||
"KG": "Kyrgyzstan",
|
||||
"KH": "Cambodia",
|
||||
"KI": "Kiribati",
|
||||
"KM": "Comoros",
|
||||
"KN": "Saint Kitts and Nevis",
|
||||
"KP": "North Korea",
|
||||
"KR": "South Korea",
|
||||
"KW": "Kuwait",
|
||||
"KY": "Cayman Islands",
|
||||
"KZ": "Kazakhstan",
|
||||
"LA": "Laos",
|
||||
"LB": "Lebanon",
|
||||
"LC": "Saint Lucia",
|
||||
"LI": "Liechtenstein",
|
||||
"LK": "Sri Lanka",
|
||||
"LR": "Liberia",
|
||||
"LS": "Lesotho",
|
||||
"LT": "Lithuania",
|
||||
"LU": "Luxembourg",
|
||||
"LV": "Latvia",
|
||||
"LY": "Libya",
|
||||
"MA": "Morocco",
|
||||
"MC": "Monaco",
|
||||
"MD": "Moldova",
|
||||
"ME": "Montenegro",
|
||||
"MF": "Saint Martin",
|
||||
"MG": "Madagascar",
|
||||
"MH": "Marshall Islands",
|
||||
"MK": "Macedonia",
|
||||
"ML": "Mali",
|
||||
"MM": "Myanmar",
|
||||
"MN": "Mongolia",
|
||||
"MO": "Macao",
|
||||
"MP": "Northern Mariana Islands",
|
||||
"MQ": "Martinique",
|
||||
"MR": "Mauritania",
|
||||
"MS": "Montserrat",
|
||||
"MT": "Malta",
|
||||
"MU": "Mauritius",
|
||||
"MV": "Maldives",
|
||||
"MW": "Malawi",
|
||||
"MX": "Mexico",
|
||||
"MY": "Malaysia",
|
||||
"MZ": "Mozambique",
|
||||
"NA": "Namibia",
|
||||
"NC": "New Caledonia",
|
||||
"NE": "Niger",
|
||||
"NF": "Norfolk Island",
|
||||
"NG": "Nigeria",
|
||||
"NI": "Nicaragua",
|
||||
"NL": "Netherlands",
|
||||
"NO": "Norway",
|
||||
"NP": "Nepal",
|
||||
"NR": "Nauru",
|
||||
"NU": "Niue",
|
||||
"NZ": "New Zealand",
|
||||
"OM": "Oman",
|
||||
"PA": "Panama",
|
||||
"PE": "Peru",
|
||||
"PF": "French Polynesia",
|
||||
"PG": "Papua New Guinea",
|
||||
"PH": "Philippines",
|
||||
"PK": "Pakistan",
|
||||
"PL": "Poland",
|
||||
"PM": "Saint Pierre and Miquelon",
|
||||
"PN": "Pitcairn",
|
||||
"PR": "Puerto Rico",
|
||||
"PS": "Palestinian Territory",
|
||||
"PT": "Portugal",
|
||||
"PW": "Palau",
|
||||
"PY": "Paraguay",
|
||||
"QA": "Qatar",
|
||||
"RE": "Reunion",
|
||||
"RO": "Romania",
|
||||
"RS": "Serbia",
|
||||
"RU": "Russia",
|
||||
"RW": "Rwanda",
|
||||
"SA": "Saudi Arabia",
|
||||
"SB": "Solomon Islands",
|
||||
"SC": "Seychelles",
|
||||
"SD": "Sudan",
|
||||
"SE": "Sweden",
|
||||
"SG": "Singapore",
|
||||
"SH": "Saint Helena",
|
||||
"SI": "Slovenia",
|
||||
"SJ": "Svalbard and Jan Mayen",
|
||||
"SK": "Slovakia",
|
||||
"SL": "Sierra Leone",
|
||||
"SM": "San Marino",
|
||||
"SN": "Senegal",
|
||||
"SO": "Somalia",
|
||||
"SR": "Suriname",
|
||||
"SS": "South Sudan",
|
||||
"ST": "Sao Tome and Principe",
|
||||
"SV": "El Salvador",
|
||||
"SX": "Sint Maarten",
|
||||
"SY": "Syria",
|
||||
"SZ": "Swaziland",
|
||||
"TC": "Turks and Caicos Islands",
|
||||
"TD": "Chad",
|
||||
"TF": "French Southern Territories",
|
||||
"TG": "Togo",
|
||||
"TH": "Thailand",
|
||||
"TJ": "Tajikistan",
|
||||
"TK": "Tokelau",
|
||||
"TL": "East Timor",
|
||||
"TM": "Turkmenistan",
|
||||
"TN": "Tunisia",
|
||||
"TO": "Tonga",
|
||||
"TR": "Turkey",
|
||||
"TT": "Trinidad and Tobago",
|
||||
"TV": "Tuvalu",
|
||||
"TW": "Taiwan",
|
||||
"TZ": "Tanzania",
|
||||
"UA": "Ukraine",
|
||||
"UG": "Uganda",
|
||||
"UM": "United States Minor Outlying Islands",
|
||||
"US": "United States",
|
||||
"UY": "Uruguay",
|
||||
"UZ": "Uzbekistan",
|
||||
"VA": "Vatican",
|
||||
"VC": "Saint Vincent and the Grenadines",
|
||||
"VE": "Venezuela",
|
||||
"VG": "British Virgin Islands",
|
||||
"VI": "U.S. Virgin Islands",
|
||||
"VN": "Vietnam",
|
||||
"VU": "Vanuatu",
|
||||
"WF": "Wallis and Futuna",
|
||||
"WS": "Samoa",
|
||||
"XK": "Kosovo",
|
||||
"YE": "Yemen",
|
||||
"YT": "Mayotte",
|
||||
"ZA": "South Africa",
|
||||
"ZM": "Zambia",
|
||||
"ZW": "Zimbabwe"
|
||||
}
|
||||
17
web/projects/ui/src/app/util/dry-update.ts
Normal file
17
web/projects/ui/src/app/util/dry-update.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Emver } from '@start9labs/shared'
|
||||
import { DataModel } from '../services/patch-db/data-model'
|
||||
|
||||
export function dryUpdate(
|
||||
{ id, version }: { id: string; version: string },
|
||||
pkgs: DataModel['package-data'],
|
||||
emver: Emver,
|
||||
): string[] {
|
||||
return Object.values(pkgs)
|
||||
.filter(
|
||||
pkg =>
|
||||
Object.keys(pkg.installed?.['current-dependencies'] || {}).some(
|
||||
pkgId => pkgId === id,
|
||||
) && !emver.satisfies(version, pkg.manifest.dependencies[id].version),
|
||||
)
|
||||
.map(pkg => pkg.manifest.title)
|
||||
}
|
||||
19
web/projects/ui/src/app/util/get-package-data.ts
Normal file
19
web/projects/ui/src/app/util/get-package-data.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { PatchDB } from 'patch-db-client'
|
||||
import {
|
||||
DataModel,
|
||||
PackageDataEntry,
|
||||
} from 'src/app/services/patch-db/data-model'
|
||||
import { firstValueFrom } from 'rxjs'
|
||||
|
||||
export async function getPackage(
|
||||
patch: PatchDB<DataModel>,
|
||||
id: string,
|
||||
): Promise<PackageDataEntry | undefined> {
|
||||
return firstValueFrom(patch.watch$('package-data', id))
|
||||
}
|
||||
|
||||
export async function getAllPackages(
|
||||
patch: PatchDB<DataModel>,
|
||||
): Promise<DataModel['package-data']> {
|
||||
return firstValueFrom(patch.watch$('package-data'))
|
||||
}
|
||||
35
web/projects/ui/src/app/util/get-package-info.ts
Normal file
35
web/projects/ui/src/app/util/get-package-info.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { PackageDataEntry } from '../services/patch-db/data-model'
|
||||
import {
|
||||
DependencyStatus,
|
||||
HealthStatus,
|
||||
PrimaryRendering,
|
||||
PrimaryStatus,
|
||||
renderPkgStatus,
|
||||
} from '../services/pkg-status-rendering.service'
|
||||
import { PkgInfo } from '../types/pkg-info'
|
||||
import { packageLoadingProgress } from './package-loading-progress'
|
||||
import { PkgDependencyErrors } from '../services/dep-error.service'
|
||||
|
||||
export function getPackageInfo(
|
||||
entry: PackageDataEntry,
|
||||
depErrors: PkgDependencyErrors,
|
||||
): PkgInfo {
|
||||
const statuses = renderPkgStatus(entry, depErrors)
|
||||
const primaryRendering = PrimaryRendering[statuses.primary]
|
||||
|
||||
return {
|
||||
entry,
|
||||
primaryRendering,
|
||||
primaryStatus: statuses.primary,
|
||||
installProgress: packageLoadingProgress(entry['install-progress']),
|
||||
error:
|
||||
statuses.health === HealthStatus.Failure ||
|
||||
statuses.dependency === DependencyStatus.Warning,
|
||||
warning: statuses.primary === PrimaryStatus.NeedsConfig,
|
||||
transitioning:
|
||||
primaryRendering.showDots ||
|
||||
statuses.health === HealthStatus.Waiting ||
|
||||
statuses.health === HealthStatus.Loading ||
|
||||
statuses.health === HealthStatus.Starting,
|
||||
}
|
||||
}
|
||||
11
web/projects/ui/src/app/util/get-project-id.ts
Normal file
11
web/projects/ui/src/app/util/get-project-id.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { ActivatedRoute } from '@angular/router'
|
||||
|
||||
export function getProjectId({ snapshot }: ActivatedRoute): string {
|
||||
const projectId = snapshot.paramMap.get('projectId')
|
||||
|
||||
if (!projectId) {
|
||||
throw new Error('projectId is missing from route params')
|
||||
}
|
||||
|
||||
return projectId
|
||||
}
|
||||
9
web/projects/ui/src/app/util/get-server-info.ts
Normal file
9
web/projects/ui/src/app/util/get-server-info.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { PatchDB } from 'patch-db-client'
|
||||
import { DataModel, ServerInfo } from 'src/app/services/patch-db/data-model'
|
||||
import { firstValueFrom } from 'rxjs'
|
||||
|
||||
export async function getServerInfo(
|
||||
patch: PatchDB<DataModel>,
|
||||
): Promise<ServerInfo> {
|
||||
return firstValueFrom(patch.watch$('server-info'))
|
||||
}
|
||||
7
web/projects/ui/src/app/util/has-deps.ts
Normal file
7
web/projects/ui/src/app/util/has-deps.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { PackageDataEntry } from '../services/patch-db/data-model'
|
||||
|
||||
export function hasCurrentDeps(pkg: PackageDataEntry): boolean {
|
||||
return !!Object.keys(pkg.installed?.['current-dependents'] || {}).filter(
|
||||
depId => depId !== pkg.manifest.id,
|
||||
).length
|
||||
}
|
||||
3
web/projects/ui/src/app/util/mask.ts
Normal file
3
web/projects/ui/src/app/util/mask.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export function mask(val: string, max: number = Infinity): string {
|
||||
return '●'.repeat(Math.min(max, val.length))
|
||||
}
|
||||
50
web/projects/ui/src/app/util/package-loading-progress.ts
Normal file
50
web/projects/ui/src/app/util/package-loading-progress.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { isEmptyObject } from '@start9labs/shared'
|
||||
import { ProgressData } from 'src/app/types/progress-data'
|
||||
import { InstallProgress } from '../services/patch-db/data-model'
|
||||
|
||||
export function packageLoadingProgress(
|
||||
loadData?: InstallProgress,
|
||||
): ProgressData | null {
|
||||
if (!loadData || isEmptyObject(loadData)) {
|
||||
return null
|
||||
}
|
||||
|
||||
let {
|
||||
downloaded,
|
||||
validated,
|
||||
unpacked,
|
||||
size,
|
||||
'download-complete': downloadComplete,
|
||||
'validation-complete': validationComplete,
|
||||
'unpack-complete': unpackComplete,
|
||||
} = loadData
|
||||
|
||||
// only permit 100% when "complete" == true
|
||||
size = size || 0
|
||||
downloaded = downloadComplete ? size : Math.max(downloaded - 1, 0)
|
||||
validated = validationComplete ? size : Math.max(validated - 1, 0)
|
||||
unpacked = unpackComplete ? size : Math.max(unpacked - 1, 0)
|
||||
|
||||
const downloadWeight = 1
|
||||
const validateWeight = 0.2
|
||||
const unpackWeight = 0.7
|
||||
|
||||
const numerator = Math.floor(
|
||||
downloadWeight * downloaded +
|
||||
validateWeight * validated +
|
||||
unpackWeight * unpacked,
|
||||
)
|
||||
|
||||
const denominator = Math.floor(
|
||||
size * (downloadWeight + validateWeight + unpackWeight),
|
||||
)
|
||||
const totalProgress = Math.floor((100 * numerator) / denominator)
|
||||
|
||||
return {
|
||||
totalProgress,
|
||||
downloadProgress: Math.floor((100 * downloaded) / size),
|
||||
validateProgress: Math.floor((100 * validated) / size),
|
||||
unpackProgress: Math.floor((100 * unpacked) / size),
|
||||
isComplete: downloadComplete && validationComplete && unpackComplete,
|
||||
}
|
||||
}
|
||||
96
web/projects/ui/src/app/util/rxjs.util.ts
Normal file
96
web/projects/ui/src/app/util/rxjs.util.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import {
|
||||
Observable,
|
||||
from,
|
||||
interval,
|
||||
race,
|
||||
OperatorFunction,
|
||||
Observer,
|
||||
take,
|
||||
map,
|
||||
} from 'rxjs'
|
||||
|
||||
export function fromAsync$<S, T>(
|
||||
async: (s: S) => Promise<T>,
|
||||
s: S,
|
||||
): Observable<T>
|
||||
export function fromAsync$<T>(async: () => Promise<T>): Observable<T>
|
||||
export function fromAsync$<S, T>(
|
||||
async: (s: S) => Promise<T>,
|
||||
s?: S,
|
||||
): Observable<T> {
|
||||
return from(async(s as S))
|
||||
}
|
||||
|
||||
export function fromAsyncP<T>(async: () => Promise<T>): Promise<T>
|
||||
export function fromAsyncP<S, T>(
|
||||
async: (s: S) => Promise<T>,
|
||||
s?: S,
|
||||
): Promise<T> {
|
||||
return async(s as S)
|
||||
}
|
||||
|
||||
// emits + completes after ms
|
||||
export function emitAfter$(ms: number): Observable<number> {
|
||||
return interval(ms).pipe(take(1))
|
||||
}
|
||||
|
||||
// throws unless source observable emits withing timeout
|
||||
export function throwIn<T>(timeout: number): OperatorFunction<T, T> {
|
||||
return o =>
|
||||
race(
|
||||
o,
|
||||
emitAfter$(timeout).pipe(
|
||||
map(() => {
|
||||
throw new Error('timeout')
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
// o.pipe(squash) : Observable<void> regardless of o emission type.
|
||||
export const squash = map(() => {})
|
||||
|
||||
/*
|
||||
The main purpose of fromSync$ is to normalize error handling during a sequence
|
||||
of actions beginning with a standard synchronous action and followed by a pipe.
|
||||
For example, imagine we have `f(s: S): T` which might throw, and we wish to define the following:
|
||||
```
|
||||
function observableF(t: T): Observable<any> {
|
||||
const s = f(t)
|
||||
return someFunctionReturningAnObservable(s)
|
||||
}
|
||||
```
|
||||
|
||||
For the caller, `observableF(t).pipe(...).subscribe({ error: e => console.error('observable errored!') })`
|
||||
might throw an error from `f` which does not result in 'observable errored!' being logged.
|
||||
We could fix this with...
|
||||
```
|
||||
function observableF(t: T): Observable<any> {
|
||||
try {
|
||||
const s = f(t)
|
||||
return someFunctionReturningAnObservable(s)
|
||||
} catch(e) {
|
||||
return throwError(e)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
or we could use fromSync as below
|
||||
```
|
||||
function observableF(t: T): Observable<any> {
|
||||
return fromSync$(f, t).concatMap(someFunctionReturningAnObservable)
|
||||
}
|
||||
```
|
||||
*/
|
||||
export function fromSync$<S, T>(sync: (s: S) => T, s: S): Observable<T>
|
||||
export function fromSync$<T>(sync: () => T): Observable<T>
|
||||
export function fromSync$<S, T>(sync: (s: S) => T, s?: S): Observable<T> {
|
||||
return new Observable((subscriber: Observer<T>) => {
|
||||
try {
|
||||
subscriber.next(sync(s as S))
|
||||
subscriber.complete()
|
||||
} catch (e) {
|
||||
subscriber.error(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
4
web/projects/ui/src/app/util/web.util.ts
Normal file
4
web/projects/ui/src/app/util/web.util.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export function strip(html: string) {
|
||||
let doc = new DOMParser().parseFromString(html, 'text/html')
|
||||
return doc.body.textContent || ''
|
||||
}
|
||||
Reference in New Issue
Block a user