| 92 | } |
| 93 | |
| 94 | function accessResult<Input, Key, Value>( |
| 95 | resource: any, |
| 96 | fetch: Input => Thenable<Value>, |
| 97 | input: Input, |
| 98 | key: Key, |
| 99 | ): Thenable<Value> { |
| 100 | const entriesForResource = getEntriesForResource(resource); |
| 101 | const entry = entriesForResource.get(key); |
| 102 | if (entry === undefined) { |
| 103 | const thenable = fetch(input); |
| 104 | thenable.then( |
| 105 | value => { |
| 106 | const fulfilledThenable: FulfilledThenable<Value> = (thenable: any); |
| 107 | fulfilledThenable.status = 'fulfilled'; |
| 108 | fulfilledThenable.value = value; |
| 109 | }, |
| 110 | error => { |
| 111 | const rejectedThenable: RejectedThenable<Value> = (thenable: any); |
| 112 | rejectedThenable.status = 'rejected'; |
| 113 | rejectedThenable.reason = error; |
| 114 | }, |
| 115 | ); |
| 116 | entriesForResource.set(key, thenable); |
| 117 | return thenable; |
| 118 | } else { |
| 119 | return entry; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | export function createResource<Input, Key, Value>( |
| 124 | fetch: Input => Thenable<Value>, |