| 1 | export default class PromiseState { |
| 2 | |
| 3 | // creates a new PromiseState that is pending |
| 4 | static create(meta) { |
| 5 | return new PromiseState({ |
| 6 | pending: true, |
| 7 | meta: meta |
| 8 | }) |
| 9 | } |
| 10 | |
| 11 | // creates as PromiseState that is refreshing |
| 12 | // can be called without a previous PromiseState and will be both pending and refreshing |
| 13 | static refresh(previous, meta) { |
| 14 | const p = previous || PromiseState.create(meta) |
| 15 | |
| 16 | return new PromiseState({ |
| 17 | pending: p.pending, |
| 18 | refreshing: true, |
| 19 | fulfilled: p.fulfilled, |
| 20 | rejected: p.rejected, |
| 21 | value: p.value, |
| 22 | reason: p.reason, |
| 23 | meta: p.meta |
| 24 | }) |
| 25 | } |
| 26 | |
| 27 | // creates a PromiseState that is resolved with the given value. |
| 28 | // if the given value is already a PromiseState, |
| 29 | // it will be returned as is and ignore the provided meta. |
| 30 | static resolve(value, meta) { |
| 31 | if (value instanceof PromiseState) { |
| 32 | return value |
| 33 | } |
| 34 | |
| 35 | return new PromiseState({ |
| 36 | fulfilled: true, |
| 37 | value: value, |
| 38 | meta: meta |
| 39 | }) |
| 40 | } |
| 41 | |
| 42 | // creates a PromiseState that is rejected with the given reason |
| 43 | static reject(reason, meta) { |
| 44 | return new PromiseState({ |
| 45 | rejected: true, |
| 46 | reason: reason, |
| 47 | meta: meta |
| 48 | }) |
| 49 | } |
| 50 | |
| 51 | // The PromiseState.all(iterable) method returns a PromiseState |
| 52 | // that resolves when all of the PromiseStates in the iterable |
| 53 | // argument have resolved, or rejects with the reason of the |
| 54 | // first passed PromiseState that rejects. |
| 55 | static all(iterable) { |
| 56 | if (!Array.isArray(iterable)) { |
| 57 | iterable = Array.from(iterable) |
| 58 | } |
| 59 | |
| 60 | return new PromiseState({ |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…