parent
7c514fdef3
commit
74880d09ff
File diff suppressed because it is too large
Load Diff
@ -1,3 +1,3 @@ |
|||||||
export class ContainerRequester { |
import { Requester } from "./Requester"; |
||||||
|
|
||||||
} |
export class ContainerRequester extends Requester {} |
||||||
|
@ -1,200 +1,3 @@ |
|||||||
import type { LeafUri } from "../util/uriTypes"; |
import { Requester } from "./Requester"; |
||||||
import { ANY_KEY, RequestBatcher } from "../util/RequestBatcher"; |
|
||||||
import type { SolidLdoDatasetContext } from "../SolidLdoDatasetContext"; |
|
||||||
import type { DatasetChanges } from "@ldo/rdf-utils"; |
|
||||||
import type { |
|
||||||
CreateResult, |
|
||||||
CreateResultWithoutOverwrite, |
|
||||||
} from "./requests/createDataResource"; |
|
||||||
import { createDataResource } from "./requests/createDataResource"; |
|
||||||
import type { ReadResult } from "./requests/readResource"; |
|
||||||
import { readResource } from "./requests/readResource"; |
|
||||||
import type { |
|
||||||
UploadResult, |
|
||||||
UploadResultWithoutOverwrite, |
|
||||||
} from "./requests/uploadResource"; |
|
||||||
import { uploadResource } from "./requests/uploadResource"; |
|
||||||
import type { DeleteResult } from "./requests/deleteResource"; |
|
||||||
import { deleteResource } from "./requests/deleteResource"; |
|
||||||
import type { UpdateResult } from "./requests/updateDataResource"; |
|
||||||
|
|
||||||
const READ_KEY = "read"; |
export class LeafRequester extends Requester {} |
||||||
const CREATE_KEY = "createDataResource"; |
|
||||||
const UPLOAD_KEY = "upload"; |
|
||||||
const UPDATE_KEY = "updateDataREsource"; |
|
||||||
const DELETE_KEY = "delete"; |
|
||||||
|
|
||||||
export class LeafRequester { |
|
||||||
private readonly requestBatcher = new RequestBatcher(); |
|
||||||
|
|
||||||
// All intance variables
|
|
||||||
readonly uri: LeafUri; |
|
||||||
private context: SolidLdoDatasetContext; |
|
||||||
|
|
||||||
constructor(uri: LeafUri, context: SolidLdoDatasetContext) { |
|
||||||
this.uri = uri; |
|
||||||
this.context = context; |
|
||||||
} |
|
||||||
|
|
||||||
isLoading(): boolean { |
|
||||||
return this.requestBatcher.isLoading(ANY_KEY); |
|
||||||
} |
|
||||||
isCreating(): boolean { |
|
||||||
return this.requestBatcher.isLoading(CREATE_KEY); |
|
||||||
} |
|
||||||
isUploading(): boolean { |
|
||||||
return this.requestBatcher.isLoading(UPLOAD_KEY); |
|
||||||
} |
|
||||||
isReading(): boolean { |
|
||||||
return this.requestBatcher.isLoading(READ_KEY); |
|
||||||
} |
|
||||||
isUpdating(): boolean { |
|
||||||
return this.requestBatcher.isLoading(UPDATE_KEY); |
|
||||||
} |
|
||||||
isDeletinng(): boolean { |
|
||||||
return this.requestBatcher.isLoading(DELETE_KEY); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Read this resource. |
|
||||||
*/ |
|
||||||
async read(): Promise<ReadResult> { |
|
||||||
const transaction = this.context.solidLdoDataset.startTransaction(); |
|
||||||
const result = await this.requestBatcher.queueProcess({ |
|
||||||
name: READ_KEY, |
|
||||||
args: [{ uri: this.uri, transaction, fetch: this.context.fetch }], |
|
||||||
perform: readResource, |
|
||||||
modifyQueue: (queue, isLoading) => { |
|
||||||
if (queue.length === 0) { |
|
||||||
return isLoading[READ_KEY]; |
|
||||||
} else { |
|
||||||
return queue[queue.length - 1].name === READ_KEY; |
|
||||||
} |
|
||||||
}, |
|
||||||
}); |
|
||||||
if (result.type !== "error") { |
|
||||||
transaction.commit(); |
|
||||||
} |
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Creates a Resource |
|
||||||
* @param overwrite: If true, this will orverwrite the resource if it already |
|
||||||
* exists |
|
||||||
*/ |
|
||||||
async createDataResource( |
|
||||||
overwrite?: false, |
|
||||||
): Promise<CreateResultWithoutOverwrite>; |
|
||||||
async createDataResource(overwrite: true): Promise<CreateResult>; |
|
||||||
async createDataResource( |
|
||||||
overwrite?: boolean, |
|
||||||
): Promise<CreateResultWithoutOverwrite | CreateResult>; |
|
||||||
async createDataResource( |
|
||||||
overwrite?: boolean, |
|
||||||
): Promise<CreateResultWithoutOverwrite> { |
|
||||||
const transaction = this.context.solidLdoDataset.startTransaction(); |
|
||||||
const result = await this.requestBatcher.queueProcess({ |
|
||||||
name: CREATE_KEY, |
|
||||||
args: [ |
|
||||||
{ uri: this.uri, transaction, fetch: this.context.fetch }, |
|
||||||
overwrite, |
|
||||||
], |
|
||||||
perform: createDataResource, |
|
||||||
modifyQueue: (queue, isLoading, args) => { |
|
||||||
const lastElementInQueue = queue[queue.length - 1]; |
|
||||||
return ( |
|
||||||
lastElementInQueue && |
|
||||||
lastElementInQueue.name === CREATE_KEY && |
|
||||||
!!lastElementInQueue.args[1] === !!args[1] |
|
||||||
); |
|
||||||
}, |
|
||||||
}); |
|
||||||
if (result.type !== "error") { |
|
||||||
transaction.commit(); |
|
||||||
} |
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Upload a binary |
|
||||||
* @param blob |
|
||||||
* @param mimeType |
|
||||||
* @param overwrite: If true, will overwrite an existing file |
|
||||||
*/ |
|
||||||
async upload( |
|
||||||
blob: Blob, |
|
||||||
mimeType: string, |
|
||||||
overwrite?: false, |
|
||||||
): Promise<UploadResultWithoutOverwrite>; |
|
||||||
async upload( |
|
||||||
blob: Blob, |
|
||||||
mimeType: string, |
|
||||||
overwrite: true, |
|
||||||
): Promise<UploadResult>; |
|
||||||
async upload( |
|
||||||
blob: Blob, |
|
||||||
mimeType: string, |
|
||||||
overwrite?: boolean, |
|
||||||
): Promise<UploadResultWithoutOverwrite | UploadResult>; |
|
||||||
async upload( |
|
||||||
blob: Blob, |
|
||||||
mimeType: string, |
|
||||||
overwrite?: boolean, |
|
||||||
): Promise<UploadResultWithoutOverwrite | UploadResult> { |
|
||||||
const transaction = this.context.solidLdoDataset.startTransaction(); |
|
||||||
const result = await this.requestBatcher.queueProcess({ |
|
||||||
name: UPLOAD_KEY, |
|
||||||
args: [ |
|
||||||
{ uri: this.uri, transaction, fetch: this.context.fetch }, |
|
||||||
blob, |
|
||||||
mimeType, |
|
||||||
overwrite, |
|
||||||
], |
|
||||||
perform: uploadResource, |
|
||||||
modifyQueue: (queue, isLoading, args) => { |
|
||||||
const lastElementInQueue = queue[queue.length - 1]; |
|
||||||
return ( |
|
||||||
lastElementInQueue && |
|
||||||
lastElementInQueue.name === UPLOAD_KEY && |
|
||||||
!!lastElementInQueue.args[3] === !!args[3] |
|
||||||
); |
|
||||||
}, |
|
||||||
}); |
|
||||||
if (result.type !== "error") { |
|
||||||
transaction.commit(); |
|
||||||
} |
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Update the data on this resource |
|
||||||
* @param changes |
|
||||||
*/ |
|
||||||
updateDataResource(_changes: DatasetChanges): Promise<UpdateResult> { |
|
||||||
throw new Error("Not Implemented"); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Delete this resource |
|
||||||
*/ |
|
||||||
async delete(): Promise<DeleteResult> { |
|
||||||
const transaction = this.context.solidLdoDataset.startTransaction(); |
|
||||||
const result = await this.requestBatcher.queueProcess({ |
|
||||||
name: DELETE_KEY, |
|
||||||
args: [{ uri: this.uri, transaction, fetch: this.context.fetch }], |
|
||||||
perform: deleteResource, |
|
||||||
modifyQueue: (queue, isLoading) => { |
|
||||||
if (queue.length === 0) { |
|
||||||
return isLoading[DELETE_KEY]; |
|
||||||
} else { |
|
||||||
return queue[queue.length - 1].name === DELETE_KEY; |
|
||||||
} |
|
||||||
}, |
|
||||||
}); |
|
||||||
if (result.type !== "error") { |
|
||||||
transaction.commit(); |
|
||||||
} |
|
||||||
return result; |
|
||||||
} |
|
||||||
} |
|
||||||
|
@ -0,0 +1,199 @@ |
|||||||
|
import { ANY_KEY, RequestBatcher } from "../util/RequestBatcher"; |
||||||
|
import type { SolidLdoDatasetContext } from "../SolidLdoDatasetContext"; |
||||||
|
import type { DatasetChanges } from "@ldo/rdf-utils"; |
||||||
|
import type { |
||||||
|
CreateResult, |
||||||
|
CreateResultWithoutOverwrite, |
||||||
|
} from "./requests/createDataResource"; |
||||||
|
import { createDataResource } from "./requests/createDataResource"; |
||||||
|
import type { ReadResult } from "./requests/readResource"; |
||||||
|
import { readResource } from "./requests/readResource"; |
||||||
|
import type { |
||||||
|
UploadResult, |
||||||
|
UploadResultWithoutOverwrite, |
||||||
|
} from "./requests/uploadResource"; |
||||||
|
import { uploadResource } from "./requests/uploadResource"; |
||||||
|
import type { DeleteResult } from "./requests/deleteResource"; |
||||||
|
import { deleteResource } from "./requests/deleteResource"; |
||||||
|
import type { UpdateResult } from "./requests/updateDataResource"; |
||||||
|
|
||||||
|
const READ_KEY = "read"; |
||||||
|
const CREATE_KEY = "createDataResource"; |
||||||
|
const UPLOAD_KEY = "upload"; |
||||||
|
const UPDATE_KEY = "updateDataREsource"; |
||||||
|
const DELETE_KEY = "delete"; |
||||||
|
|
||||||
|
export abstract class Requester { |
||||||
|
protected readonly requestBatcher = new RequestBatcher(); |
||||||
|
|
||||||
|
// All intance variables
|
||||||
|
readonly uri: string; |
||||||
|
protected context: SolidLdoDatasetContext; |
||||||
|
|
||||||
|
constructor(uri: string, context: SolidLdoDatasetContext) { |
||||||
|
this.uri = uri; |
||||||
|
this.context = context; |
||||||
|
} |
||||||
|
|
||||||
|
isLoading(): boolean { |
||||||
|
return this.requestBatcher.isLoading(ANY_KEY); |
||||||
|
} |
||||||
|
isCreating(): boolean { |
||||||
|
return this.requestBatcher.isLoading(CREATE_KEY); |
||||||
|
} |
||||||
|
isUploading(): boolean { |
||||||
|
return this.requestBatcher.isLoading(UPLOAD_KEY); |
||||||
|
} |
||||||
|
isReading(): boolean { |
||||||
|
return this.requestBatcher.isLoading(READ_KEY); |
||||||
|
} |
||||||
|
isUpdating(): boolean { |
||||||
|
return this.requestBatcher.isLoading(UPDATE_KEY); |
||||||
|
} |
||||||
|
isDeletinng(): boolean { |
||||||
|
return this.requestBatcher.isLoading(DELETE_KEY); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Read this resource. |
||||||
|
*/ |
||||||
|
async read(): Promise<ReadResult> { |
||||||
|
const transaction = this.context.solidLdoDataset.startTransaction(); |
||||||
|
const result = await this.requestBatcher.queueProcess({ |
||||||
|
name: READ_KEY, |
||||||
|
args: [{ uri: this.uri, transaction, fetch: this.context.fetch }], |
||||||
|
perform: readResource, |
||||||
|
modifyQueue: (queue, isLoading) => { |
||||||
|
if (queue.length === 0) { |
||||||
|
return isLoading[READ_KEY]; |
||||||
|
} else { |
||||||
|
return queue[queue.length - 1].name === READ_KEY; |
||||||
|
} |
||||||
|
}, |
||||||
|
}); |
||||||
|
if (result.type !== "error") { |
||||||
|
transaction.commit(); |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a Resource |
||||||
|
* @param overwrite: If true, this will orverwrite the resource if it already |
||||||
|
* exists |
||||||
|
*/ |
||||||
|
async createDataResource( |
||||||
|
overwrite?: false, |
||||||
|
): Promise<CreateResultWithoutOverwrite>; |
||||||
|
async createDataResource(overwrite: true): Promise<CreateResult>; |
||||||
|
async createDataResource( |
||||||
|
overwrite?: boolean, |
||||||
|
): Promise<CreateResultWithoutOverwrite | CreateResult>; |
||||||
|
async createDataResource( |
||||||
|
overwrite?: boolean, |
||||||
|
): Promise<CreateResultWithoutOverwrite> { |
||||||
|
const transaction = this.context.solidLdoDataset.startTransaction(); |
||||||
|
const result = await this.requestBatcher.queueProcess({ |
||||||
|
name: CREATE_KEY, |
||||||
|
args: [ |
||||||
|
{ uri: this.uri, transaction, fetch: this.context.fetch }, |
||||||
|
overwrite, |
||||||
|
], |
||||||
|
perform: createDataResource, |
||||||
|
modifyQueue: (queue, isLoading, args) => { |
||||||
|
const lastElementInQueue = queue[queue.length - 1]; |
||||||
|
return ( |
||||||
|
lastElementInQueue && |
||||||
|
lastElementInQueue.name === CREATE_KEY && |
||||||
|
!!lastElementInQueue.args[1] === !!args[1] |
||||||
|
); |
||||||
|
}, |
||||||
|
}); |
||||||
|
if (result.type !== "error") { |
||||||
|
transaction.commit(); |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Upload a binary |
||||||
|
* @param blob |
||||||
|
* @param mimeType |
||||||
|
* @param overwrite: If true, will overwrite an existing file |
||||||
|
*/ |
||||||
|
async upload( |
||||||
|
blob: Blob, |
||||||
|
mimeType: string, |
||||||
|
overwrite?: false, |
||||||
|
): Promise<UploadResultWithoutOverwrite>; |
||||||
|
async upload( |
||||||
|
blob: Blob, |
||||||
|
mimeType: string, |
||||||
|
overwrite: true, |
||||||
|
): Promise<UploadResult>; |
||||||
|
async upload( |
||||||
|
blob: Blob, |
||||||
|
mimeType: string, |
||||||
|
overwrite?: boolean, |
||||||
|
): Promise<UploadResultWithoutOverwrite | UploadResult>; |
||||||
|
async upload( |
||||||
|
blob: Blob, |
||||||
|
mimeType: string, |
||||||
|
overwrite?: boolean, |
||||||
|
): Promise<UploadResultWithoutOverwrite | UploadResult> { |
||||||
|
const transaction = this.context.solidLdoDataset.startTransaction(); |
||||||
|
const result = await this.requestBatcher.queueProcess({ |
||||||
|
name: UPLOAD_KEY, |
||||||
|
args: [ |
||||||
|
{ uri: this.uri, transaction, fetch: this.context.fetch }, |
||||||
|
blob, |
||||||
|
mimeType, |
||||||
|
overwrite, |
||||||
|
], |
||||||
|
perform: uploadResource, |
||||||
|
modifyQueue: (queue, isLoading, args) => { |
||||||
|
const lastElementInQueue = queue[queue.length - 1]; |
||||||
|
return ( |
||||||
|
lastElementInQueue && |
||||||
|
lastElementInQueue.name === UPLOAD_KEY && |
||||||
|
!!lastElementInQueue.args[3] === !!args[3] |
||||||
|
); |
||||||
|
}, |
||||||
|
}); |
||||||
|
if (result.type !== "error") { |
||||||
|
transaction.commit(); |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Update the data on this resource |
||||||
|
* @param changes |
||||||
|
*/ |
||||||
|
updateDataResource(_changes: DatasetChanges): Promise<UpdateResult> { |
||||||
|
throw new Error("Not Implemented"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Delete this resource |
||||||
|
*/ |
||||||
|
async delete(): Promise<DeleteResult> { |
||||||
|
const transaction = this.context.solidLdoDataset.startTransaction(); |
||||||
|
const result = await this.requestBatcher.queueProcess({ |
||||||
|
name: DELETE_KEY, |
||||||
|
args: [{ uri: this.uri, transaction, fetch: this.context.fetch }], |
||||||
|
perform: deleteResource, |
||||||
|
modifyQueue: (queue, isLoading) => { |
||||||
|
if (queue.length === 0) { |
||||||
|
return isLoading[DELETE_KEY]; |
||||||
|
} else { |
||||||
|
return queue[queue.length - 1].name === DELETE_KEY; |
||||||
|
} |
||||||
|
}, |
||||||
|
}); |
||||||
|
if (result.type !== "error") { |
||||||
|
transaction.commit(); |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
} |
@ -1,9 +1,22 @@ |
|||||||
|
import type { Requester } from "../requester/Requester"; |
||||||
import type { ContainerUri } from "../util/uriTypes"; |
import type { ContainerUri } from "../util/uriTypes"; |
||||||
import { Resource } from "./Resource"; |
import { Resource } from "./Resource"; |
||||||
|
|
||||||
export class Container { |
export class Container extends Resource { |
||||||
|
protected requester: Requester; |
||||||
|
getParentContainer(): Promise<Container | undefined> { |
||||||
|
throw new Error("Method not implemented."); |
||||||
|
} |
||||||
|
getRootContainer(): Promise<Container> { |
||||||
|
throw new Error("Method not implemented."); |
||||||
|
} |
||||||
|
getMimeType(): string { |
||||||
|
throw new Error("Method not implemented."); |
||||||
|
} |
||||||
readonly uri: ContainerUri; |
readonly uri: ContainerUri; |
||||||
private
|
private rootContainer: boolean | undefined; |
||||||
|
|
||||||
isFetched():
|
isRootContainer(): boolean | undefined { |
||||||
|
return this.rootContainer; |
||||||
|
} |
||||||
} |
} |
||||||
|
@ -0,0 +1,56 @@ |
|||||||
|
import type { DatasetChanges } from "@ldo/rdf-utils"; |
||||||
|
import type { Requester } from "../requester/Requester"; |
||||||
|
import type { UpdateResultError } from "../requester/requests/updateDataResource"; |
||||||
|
import type { |
||||||
|
UploadResultError, |
||||||
|
UploadResultWithoutOverwriteError, |
||||||
|
} from "../requester/requests/uploadResource"; |
||||||
|
import type { Container } from "./Container"; |
||||||
|
import { Resource } from "./Resource"; |
||||||
|
|
||||||
|
export class Leaf extends Resource { |
||||||
|
protected requester: Requester; |
||||||
|
getParentContainer(): Promise<Container | undefined> { |
||||||
|
throw new Error("Method not implemented."); |
||||||
|
} |
||||||
|
getRootContainer(): Promise<Container> { |
||||||
|
throw new Error("Method not implemented."); |
||||||
|
} |
||||||
|
getMimeType(): string { |
||||||
|
throw new Error("Method not implemented."); |
||||||
|
} |
||||||
|
isBinary(): boolean | undefined { |
||||||
|
if (!this.didInitialFetch) { |
||||||
|
return undefined; |
||||||
|
} |
||||||
|
return !!this.binaryData; |
||||||
|
} |
||||||
|
isDataResource(): boolean | undefined { |
||||||
|
if (!this.didInitialFetch) { |
||||||
|
return undefined; |
||||||
|
} |
||||||
|
return !this.binaryData; |
||||||
|
} |
||||||
|
|
||||||
|
async uploadAndOverwrite( |
||||||
|
blob: Blob, |
||||||
|
mimeType: string, |
||||||
|
): Promise<this | UploadResultError> { |
||||||
|
return this.parseResult(await this.requester.upload(blob, mimeType)) as |
||||||
|
| this |
||||||
|
| UploadResultError; |
||||||
|
} |
||||||
|
|
||||||
|
async uploadIfAbsent( |
||||||
|
blob: Blob, |
||||||
|
mimeType: string, |
||||||
|
): Promise<this | UploadResultWithoutOverwriteError> { |
||||||
|
return this.parseResult( |
||||||
|
await this.requester.upload(blob, mimeType, true), |
||||||
|
) as this | UploadResultWithoutOverwriteError; |
||||||
|
} |
||||||
|
|
||||||
|
update(_changes: DatasetChanges): Promise<this | UpdateResultError> { |
||||||
|
throw new Error("Method not implemented"); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue