Last active
May 23, 2019 09:54
-
-
Save damukles/db5ea26b4ad24720f342be70c35d6b44 to your computer and use it in GitHub Desktop.
remotedata.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class RemoteDataState { | |
public readonly isSuccess: boolean = false; | |
public readonly isFailure: boolean = false; | |
public readonly isLoading: boolean = false; | |
public readonly isNotAsked: boolean = false; | |
public readonly isUpdating: boolean = false; | |
public readonly isUpdateFailure: boolean = false; | |
public readonly isNotCreated: boolean = false; | |
} | |
class NotAsked extends RemoteDataState { | |
public readonly isNotAsked: boolean = true; | |
} | |
class Loading extends RemoteDataState { | |
public readonly isLoading: boolean = true; | |
} | |
class Failure<TError> extends RemoteDataState { | |
public readonly isFailure: boolean = true; | |
public readonly error!: TError; | |
constructor(error: TError) { | |
super(); | |
this.error = error; | |
} | |
} | |
class Success<TData> extends RemoteDataState { | |
public readonly isSuccess: boolean = true; | |
public readonly data!: TData; | |
constructor(data: TData) { | |
super(); | |
this.data = data; | |
} | |
} | |
class Updating<TData> extends Success<TData> { | |
public readonly isUpdating: boolean = true; | |
constructor(data: TData) { | |
super(data); | |
} | |
} | |
class UpdateFailure<TData, TError> extends Success<TData> { | |
public readonly isUpdateFailure = true; | |
public readonly error!: TError; | |
constructor(data: TData, error: TError) { | |
super(data); | |
this.error = error; | |
} | |
} | |
class NotCreated<TData> extends RemoteDataState { | |
public readonly isNotCreated: boolean = true; | |
public readonly data!: TData; | |
constructor(data: TData) { | |
super(); | |
this.data = data; | |
} | |
} | |
export class RemoteData<TData, TError> { | |
private state: RemoteDataState = new NotAsked(); | |
public setNotCreated(data: TData): void { | |
this.state = new NotCreated<TData>(data); | |
} | |
public setUpdateFailure(data: TData, error: TError): void { | |
this.state = new UpdateFailure<TData, TError>(data, error); | |
} | |
public setUpdating(data: TData): void { | |
this.state = new Updating<TData>(data); | |
} | |
public setSuccess(data: TData): void { | |
this.state = new Success<TData>(data); | |
} | |
public setFailure(error: TError): void { | |
this.state = new Failure<TError>(error); | |
} | |
public setLoading(): void { | |
this.state = new Loading(); | |
} | |
public setNotAsked(): void { | |
this.state = new NotAsked(); | |
} | |
public isNotCreated(): boolean { | |
return this.state.isNotCreated; | |
} | |
public isUpdateFailure(): boolean { | |
return this.state.isUpdateFailure; | |
} | |
public isUpdating(): boolean { | |
return this.state.isUpdating; | |
} | |
public isSuccess(): boolean { | |
return this.state.isSuccess; | |
} | |
public isFailure(): boolean { | |
return this.state.isFailure; | |
} | |
public isLoading(): boolean { | |
return this.state.isLoading; | |
} | |
public isNotAsked(): boolean { | |
return this.state.isNotAsked; | |
} | |
public getData(): TData { | |
if (this.state.isSuccess) { | |
return (this.state as Success<TData>).data; | |
} | |
if (this.state.isNotCreated) { | |
return (this.state as NotCreated<TData>).data; | |
} | |
throw new Error('RemoteData is not in a data carrying state'); | |
} | |
public getError(): TError { | |
if (this.state.isFailure) { | |
return (this.state as Failure<TError>).error; | |
} | |
if (this.state.isUpdateFailure) { | |
return (this.state as UpdateFailure<TData, TError>).error; | |
} | |
throw new Error('RemoteData is not in a failure state'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment