Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
feat: Instance interface from Baidu
Browse files Browse the repository at this point in the history
  • Loading branch information
dotennin committed Jul 18, 2020
1 parent ecf4ef7 commit 38fed41
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 115 deletions.
186 changes: 91 additions & 95 deletions dist/bundle.user.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/containers/DownloadList/Item.tsx
@@ -1,5 +1,4 @@
import React from 'react'
import { formatByte } from '../../utils'
import { ItemProxy } from '../../services/ItemProxy'
import Operation from './Operation'
import ProgressStatus from './ProgressStatus'
Expand All @@ -26,7 +25,7 @@ function Item({ fsId }: IProps) {
<ProgressStatus fsId={fsId} />
</div>
</td>
<td data-label="url">{formatByte(size)}</td>
<td data-label="url">{InstanceForSystem.friendlyFileSize(size)}</td>
<td data-label="speed">
<SpeedStatus fsId={fsId} />
</td>
Expand Down
4 changes: 2 additions & 2 deletions src/containers/DownloadList/SpeedStatus.tsx
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { connect } from 'react-redux'
import { IStoreState } from '../../store'
import { ItemProxy } from '../../services/ItemProxy'
import { formatByte } from '../../utils'
import { InstanceForSystem } from '../../services/InstaceForSystem'

interface IProps {
fsId: ItemProxy['fsId']
Expand All @@ -11,7 +11,7 @@ const mapStoreToProps = (store: IStoreState, props: IProps) => ({
speedOverlay: store.download.downloadItems[props.fsId]?.speedOverlay,
})
function SpeedStatus({ speedOverlay }: ReturnType<typeof mapStoreToProps & IProps>) {
return <>{formatByte(speedOverlay)} /s</>
return <>{InstanceForSystem.friendlyFileSize(speedOverlay)} /s</>
}

export default connect(mapStoreToProps)(SpeedStatus)
20 changes: 15 additions & 5 deletions src/services/InstaceForSystem.ts
@@ -1,4 +1,4 @@
import { IDialog, IItem, IProgress, StatusTypes, ValueTypes } from './types'
import { IInstance, IItem, IProgress, StatusTypes, ValueTypes } from './types'
import { GM } from './gmInterface/gmInterface'
import { ItemProxy } from './ItemProxy'
import { store } from '../store'
Expand All @@ -8,10 +8,20 @@ import interfaceModule from '../modules/interfaceModule'

type ItemObject = Record<ItemProxy['fsId'], ItemProxy>
const InstanceForSystem = {
list: eval(`require('system-core:context/context.js')`).instanceForSystem.list,
dialog: eval(`require("system-core:system/uiService/dialog/dialog.js")`) as IDialog,
list: eval(`require('system-core:context/context.js')`).instanceForSystem.list as IInstance['list'],
dialog: eval(`require("system-core:system/uiService/dialog/dialog.js")`) as IInstance['dialog'],
hash: eval(`require('base:widget/hash/hash.js')`) as IInstance['hash'],
friendlyFileSize: (size: number): string =>
eval(`require('base:widget/tools/service/tools.format.js').toFriendlyFileSize(${size})`),
maxDownloadCount: 2,
allDownloads: {} as ItemObject,
fileManagerApi: eval(
`require("disk-system:widget/system/fileService/fileManagerApi/fileManagerApi.js")`
) as IInstance['fileManagerApi'],
listInit: eval(`require("disk-system:widget/pageModule/list/listInit.js")`) as IInstance['listInit'],
listInstance: eval(
`require("system-core:context/context.js").instanceForSystem.listInstance`
) as IInstance['listInstance'],

initState: function() {
return new Promise((resolve) => {
Expand Down Expand Up @@ -47,7 +57,7 @@ const InstanceForSystem = {
},

get selectedList() {
const selected: IItem[] = this.list.getSelected()
const selected = this.list.getSelected()

return selected
.filter((arr) => {
Expand All @@ -57,7 +67,7 @@ const InstanceForSystem = {
},

get currentList() {
return this.list.getCurrentList() as IItem[]
return this.list.getCurrentList()
},

stopAll: function() {
Expand Down
33 changes: 30 additions & 3 deletions src/services/types.ts
Expand Up @@ -59,7 +59,34 @@ interface IDialogConfirmConfig extends IDialogAlertConfig {
onCancel?: Function
extra?: Function
}
export interface IDialog {
alert: (configOrBody: string | IDialogAlertConfig) => void
confirm: <T extends string | IDialogConfirmConfig>(title: T, message: T extends string ? string : never) => void

export interface IInstance {
dialog: {
alert: (configOrBody: string | IDialogAlertConfig) => void
confirm: <T extends string | IDialogConfirmConfig>(title: T, message: T extends string ? string : never) => void
}
hash: {
set: (key: string, value: string) => void
get: (key: string) => string
listen: (key: string, callback: Function) => void
del: (key: string) => void
}
fileManagerApi: {
createNewDir: (path: string, callback: (e: number) => void, resolveFileName: string) => void // 0 successful ?
deleteFiles: (path: string[], callback: (e: number) => void) => void // 0 successful ?
reName: (source: string, callback: (e: number) => void, targetName: string) => void
}
list: {
getHistoryPath: () => string
goHistory: (path: string) => void
getSelected: () => IItem[]
getCurrentList: () => IItem[]
}
listInit: {
getCheckedIndexs: () => number[]
getCheckedItems: () => IItem[]
}
listInstance: {
cancelFilesSelect: () => void
}
}
8 changes: 0 additions & 8 deletions src/utils/index.ts
@@ -1,11 +1,3 @@
export const formatByte = (byte: number) => {
if (byte <= 1000) {
return `${byte} B`
}
const KiByte = Math.round(byte / 1024)
return KiByte <= 1000 ? `${KiByte} KB` : `${Math.round(KiByte / 10.24) / 100} MB`
}

/**
* Wrapped console.log function.
*
Expand Down

0 comments on commit 38fed41

Please sign in to comment.