| 23 | } |
| 24 | |
| 25 | class SnapshotResource<T> implements Resource<T> { |
| 26 | constructor(readonly snapshot: Signal<ResourceSnapshot<T>>) {} |
| 27 | |
| 28 | private get state(): ResourceSnapshot<T> { |
| 29 | return this.snapshot(); |
| 30 | } |
| 31 | |
| 32 | readonly value = computed(() => { |
| 33 | if (this.state.status === 'error') { |
| 34 | throw new ResourceValueError(this.state.error); |
| 35 | } |
| 36 | return this.state.value; |
| 37 | }); |
| 38 | readonly status = computed(() => this.state.status); |
| 39 | readonly error = computed(() => (this.state.status === 'error' ? this.state.error : undefined)); |
| 40 | readonly isLoading = computed( |
| 41 | () => this.state.status === 'loading' || this.state.status === 'reloading', |
| 42 | ); |
| 43 | |
| 44 | private isValueDefined = computed( |
| 45 | () => this.state.status !== 'error' && this.state.value !== undefined, |
| 46 | ); |
| 47 | |
| 48 | hasValue(this: T extends undefined ? this : never): this is Resource<Exclude<T, undefined>>; |
| 49 | hasValue(): boolean; |
| 50 | hasValue(): boolean { |
| 51 | return this.isValueDefined(); |
| 52 | } |
| 53 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…