Complete Playground v1

This commit is contained in:
Jerome Wu
2022-10-07 17:54:57 +08:00
parent e02437421b
commit f4a27e3491
12 changed files with 500 additions and 270 deletions

View File

@@ -4,7 +4,7 @@ import {
CallbackData,
Callbacks,
DownloadProgressEvent,
FFFSPaths,
FSNode,
FFMessageEventCallback,
FFMessageLoadConfig,
OK,
@@ -94,6 +94,8 @@ export class FFmpeg extends EventEmitter {
#resolves: Callbacks = {};
#rejects: Callbacks = {};
public loaded = false;
constructor() {
super();
}
@@ -108,6 +110,9 @@ export class FFmpeg extends EventEmitter {
}: FFMessageEventCallback) => {
switch (type) {
case FFMessageType.LOAD:
this.loaded = true;
this.#resolves[id](data);
break;
case FFMessageType.EXEC:
case FFMessageType.WRITE_FILE:
case FFMessageType.READ_FILE:
@@ -227,6 +232,7 @@ export class FFmpeg extends EventEmitter {
if (this.#worker) {
this.#worker.terminate();
this.#worker = null;
this.loaded = false;
}
};
@@ -323,11 +329,11 @@ export class FFmpeg extends EventEmitter {
*
* @category File System
*/
public listDir = (path: string): Promise<FFFSPaths> =>
public listDir = (path: string): Promise<FSNode[]> =>
this.#send({
type: FFMessageType.LIST_DIR,
data: { path },
}) as Promise<FFFSPaths>;
}) as Promise<FSNode[]>;
/**
* Delete an empty directory.

View File

@@ -1,5 +1,4 @@
export type FFFSPath = string;
export type FFFSPaths = FFFSPath[];
/**
* ffmpeg-core loading configuration.
@@ -136,6 +135,11 @@ export type FileData = Uint8Array | string;
export type IsFirst = boolean;
export type OK = boolean;
export interface FSNode {
name: string;
isDir: boolean;
}
export type CallbackData =
| FileData
| ExitCode
@@ -146,7 +150,7 @@ export type CallbackData =
| IsFirst
| OK
| Error
| FFFSPaths
| FSNode[]
| undefined;
export interface Callbacks {

View File

@@ -18,7 +18,7 @@ import type {
IsFirst,
OK,
ExitCode,
FFFSPaths,
FSNode,
FileData,
} from "./types";
import { toBlobURL } from "./utils";
@@ -118,8 +118,15 @@ const createDir = ({ path }: FFMessageCreateDirData): OK => {
return true;
};
const listDir = ({ path }: FFMessageListDirData): FFFSPaths => {
return ffmpeg.FS.readdir(path);
const listDir = ({ path }: FFMessageListDirData): FSNode[] => {
const names = ffmpeg.FS.readdir(path);
const nodes: FSNode[] = [];
for (const name of names) {
const stat = ffmpeg.FS.stat(`${path}/${name}`);
const isDir = ffmpeg.FS.isDir(stat.mode);
nodes.push({ name, isDir });
}
return nodes;
};
// TODO: check if deletion works.