Skip to content

Commit

Permalink
[accel-wave] refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
koyopro committed Dec 23, 2024
1 parent 48207cb commit fdf9b92
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions packages/accel-wave/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const mount = (model: Model, attr: string, uploader: BaseUploader) => {

export class Item {
constructor(
public identifier: string | undefined,
public identifier: string,
public file: File
) {}
}
Expand All @@ -38,17 +38,17 @@ export class BaseUploader extends Config {
}

get filename() {
return this.item?.identifier;
return this.item?.identifier ?? "";
}
set filename(value: string | undefined) {
set filename(value: string) {
if (this.item) this.item.identifier = value;
}

get file() {
if (this.item?.file) return this.item.file;
if (this.model && this.attr) {
const identifier = (this.model as any)[this.attr];
const path = `${this.storeDir}/${identifier}`;
const path = this.pathFor(identifier);
this.item = new Item(identifier, this._storage.retrive(path));
return this.item.file;
}
Expand All @@ -61,28 +61,33 @@ export class BaseUploader extends Config {
}

url() {
if (!this.filename) return undefined;
if (!this.item) return undefined;

const path = `${this.storeDir}/${this.filename}`;
if (this.assetHost) {
return new URL(path, this.assetHost);
return new URL(this.path, this.assetHost);
} else {
return this._storage.url(path);
return this._storage.url(this.path);
}
}

store(file?: File | undefined | null) {
if (file) this.file = file;
if (file === null) this.file = undefined;
if (this.hasUpdate && this.item?.file && this.filename) {
const path = `${this.storeDir}/${this.filename}`;
this._storage.store(this.item.file, path);
this._storage.store(this.item.file, this.path);
this.hasUpdate = false;
}
for (const item of this.removedItems) {
const path = `${this.storeDir}/${item.identifier}`;
this._storage.delete(path);
this._storage.delete(this.pathFor(item.identifier));
}
this.removedItems = [];
}

protected get path() {
return this.pathFor(this.filename);
}

protected pathFor(identifier: string) {
return `${this.storeDir}/${identifier}`;
}
}

0 comments on commit fdf9b92

Please sign in to comment.