Skip to content

Commit

Permalink
Merge pull request #547 from libarchive
Browse files Browse the repository at this point in the history
  • Loading branch information
release-train[bot] committed Aug 31, 2019
2 parents c0c9e4c + d848874 commit a78c2b9
Show file tree
Hide file tree
Showing 23 changed files with 547 additions and 412 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
},
"homepage": "https://github.com/spacetme/bemuse",
"devDependencies": {
"@types/bluebird": "3",
"@types/bluebird-global": "3",
"@types/chai": "^4.2.0",
"@types/eslint": "^4.16.4",
"@types/minimatch": "^3.0.3",
Expand Down Expand Up @@ -169,6 +171,7 @@
"keycode": "^2.2.0",
"keytime": "^0.1.0",
"lazy-property": "^1.0.0",
"libarchive.js": "^1.3.0",
"lodash": "^4.17.11",
"markdown-it": "^6.0.0",
"mean": "^1.0.1",
Expand Down
15 changes: 15 additions & 0 deletions public/vendor/libarchive.js-1.3.0/dist/wasm-gen/libarchive.js

Large diffs are not rendered by default.

Binary file not shown.
1 change: 1 addition & 0 deletions public/vendor/libarchive.js-1.3.0/dist/worker-bundle.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/app/song-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export function loadSongFromResources(resources, options = {}) {
var onMessage = options.onMessage || (() => {})
return resources.fileList
.then(fileList => {
console.log(fileList)
return fileList.filter(filename =>
/\.(bms|bme|bml|bmson)$/i.test(filename)
)
Expand Down
69 changes: 69 additions & 0 deletions src/progress/Progress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Observable from 'bemuse/utils/observable'

interface IProgressFormatter {
(progress: Progress): string
}

/**
* The Progress class represents the progress of an asynchronous operation.
* It is inspired by C#'s IProgress interface.
*
* See: http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx
*/
export class Progress {
/** The current progress (out of `Progress#total`). */
public current: number | undefined = undefined
/** Total progress value */
public total: number | undefined = undefined
/** Some arbitrary information associated with this Progress. */
public extra: any
/**
* The formatter of this progress. This formatter will be used to compute
* the text representation of this progress (`Progress#toString`).
*/
public formatter?: IProgressFormatter
private _observable = new Observable<void>()
/**
* Updates the progress.
*/
report(current: number, total: number, extra?: any) {
this.current = current
this.total = total
this.extra = extra
this._observable.notify()
}
/**
* Attaches a progress listener function to this progress.
* The function `f` will be called immediately and synchronously upon watching,
* and will be called when the progress value is updated.
* @param f
* @returns a function that, when called, unsubscribes this listener.
*/
watch(f: (progress: Progress) => void) {
f(this)
return this._observable.watch(() => f(this))
}
/**
* The current progress as a fraction (out of 1).
*/
get progress() {
if (this.total && this.current !== undefined && this.current !== null) {
return this.current / this.total
} else {
return null
}
}
/**
* Returns a string representation of this progress instance.
* This method is used for displaying the progress as a text.
*/
toString() {
if (this.formatter !== undefined) {
return this.formatter(this)
} else if (this.progress !== null) {
return this.current + ' / ' + this.total
} else {
return ''
}
}
}
2 changes: 1 addition & 1 deletion src/progress/formatters.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Formatters from './formatters'
import Progress from './'
import { Progress } from './Progress'

describe('ProgressFormatters', function() {
describe('BYTES_FORMATTER', function() {
Expand Down
2 changes: 1 addition & 1 deletion src/progress/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Progress from './'
import { Progress } from './Progress'

describe('Progress', function() {
describe('#watch', function() {
Expand Down
75 changes: 1 addition & 74 deletions src/progress/index.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,4 @@
// !! avoid external dependencies since this is used in boot script!

import Observable from 'bemuse/utils/observable'

interface IProgressFormatter {
(progress: Progress): string
}

/**
* The Progress class represents the progress of an asynchronous operation.
* It is inspired by C#'s IProgress interface.
*
* See: http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx
*/
export class Progress {
/** The current progress (out of `Progress#total`). */
public current: number | undefined = undefined
/** Total progress value */
public total: number | undefined = undefined
/** Some arbitrary information associated with this Progress. */
public extra: any
/**
* The formatter of this progress. This formatter will be used to compute
* the text representation of this progress (`Progress#toString`).
*/
public formatter?: IProgressFormatter
private _observable = new Observable<void>()

/**
* Updates the progress.
*/
report(current: number, total: number, extra: any) {
this.current = current
this.total = total
this.extra = extra
this._observable.notify()
}

/**
* Attaches a progress listener function to this progress.
* The function `f` will be called immediately and synchronously upon watching,
* and will be called when the progress value is updated.
* @param f
* @returns a function that, when called, unsubscribes this listener.
*/
watch(f: (progress: Progress) => void) {
f(this)
return this._observable.watch(() => f(this))
}

/**
* The current progress as a fraction (out of 1).
*/
get progress() {
if (this.total && this.current !== undefined && this.current !== null) {
return this.current / this.total
} else {
return null
}
}

/**
* Returns a string representation of this progress instance.
* This method is used for displaying the progress as a text.
*/
toString() {
if (this.formatter !== undefined) {
return this.formatter(this)
} else if (this.progress !== null) {
return this.current + ' / ' + this.total
} else {
return ''
}
}
}

import { Progress } from './Progress'
export default Progress
69 changes: 0 additions & 69 deletions src/progress/utils.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/progress/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Progress from './'
import { Progress } from './Progress'
import * as ProgressUtils from './utils'

describe('ProgressUtils', function() {
Expand Down
93 changes: 93 additions & 0 deletions src/progress/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { BYTES_FORMATTER } from 'bemuse/progress/formatters'
import { Progress } from './Progress'

/**
* Returns a callback that, when called,
* update the progress with increasing `current` value,
* out of a fixed `total`.
*/
export function fixed(total: number, progress: Progress) {
if (!progress) return () => {}
let loaded = 0
progress.report(0, total)
return (extra: any) => progress.report(++loaded, total, extra)
}

/**
* Updates the progress with the result of an atomic operation.
*/
export function atomic<T>(
progress: Progress,
promise: PromiseLike<T>
): PromiseLike<T> {
if (!progress) return promise
return Promise.resolve(promise).tap(data => {
if (hasByteLength(data)) {
progress.formatter = BYTES_FORMATTER
progress.report(data.byteLength, data.byteLength)
} else {
progress.report(1, 1)
}
})
}
function hasByteLength(data: any): data is { byteLength: number } {
return data && data.byteLength
}

/**
* Wraps an async function that may be called multiple times.
* Each call adds to the total, and each async resolution adds to current.
*/
export function wrapPromise<A extends any[], R>(
progress: Progress,
f: (...args: A) => PromiseLike<R>
): (...args: A) => PromiseLike<R> {
let current = 0
let total = 0
return function(this: any, ...args: A) {
progress.report(current, ++total)
return Promise.resolve(f.apply(this, args)).tap(() =>
progress.report(++current, total)
)
}
}

export function bind(from: Progress, to: Progress) {
return from.watch(() => to.report(from.current!, from.total!, from.extra))
}

/**
* Updates the `target` with progress data from multiple sources.
*/
export function simultaneous(target: Progress) {
let queue: Progress[] = []
let current: Progress | undefined
let unsubscribe: (() => void) | null = null
function update() {
if (current) {
target.report(current.current!, current.total!, current.extra)
}
if (queue.length > 0 && (!current || current.progress! >= 1)) {
bind(queue.shift()!)
}
}
function bind(progress: Progress) {
if (current === progress) {
return
}
if (unsubscribe) {
unsubscribe()
unsubscribe = null
}
current = progress
if (current) {
unsubscribe = current.watch(update)
}
}
return {
add(progress: Progress) {
queue.push(progress)
update()
},
}
}

0 comments on commit a78c2b9

Please sign in to comment.