Skip to content

Commit

Permalink
Merge branch 'master' into fix/issue-306
Browse files Browse the repository at this point in the history
  • Loading branch information
NaridaL committed Jul 25, 2023
2 parents b084704 + 4b1478c commit 1c5276b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ the status of the promise and returns it. The returned object has the following
And the following methods:

- `case({fulfilled, rejected, pending})`: maps over the result using the provided handlers, or returns `undefined` if a handler isn't available for the current promise state.
- `then((value: TValue) => TResult1 | PromiseLike<TResult1>, [(rejectReason: any) => any])`: chains additional handlers to the provided promise.

The returned object implements `PromiseLike<TValue>`, so you can chain additional `Promise` handlers using `then`. You may also use it with `await` in `async` functions.

Expand All @@ -109,9 +108,8 @@ This is useful to replace one promise based observable with another, without goi

### Parameters

- `origPromise`
- `oldPromise` **IThenable&lt;T>** ? The previously observed promise
- `promise` **IThenable&lt;T>** The promise which will be observed
- `origPromise` The promise which will be observed
- `oldPromise` The previously observed promise

### Examples

Expand Down Expand Up @@ -192,7 +190,7 @@ fetchResult.then(
)
```

Returns **IPromiseBasedObservable&lt;T>**
Returns **any** origPromise with added properties and methods described above.

## isPromiseBasedObservable

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobx-utils",
"version": "6.0.5",
"version": "6.0.7",
"description": "Utility functions and common patterns for MobX",
"main": "mobx-utils.umd.js",
"module": "mobx-utils.module.js",
Expand Down Expand Up @@ -91,4 +91,4 @@
"pre-commit": "lint-staged"
}
}
}
}
3 changes: 2 additions & 1 deletion src/computedFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
onBecomeUnobserved,
_isComputingDerivation,
isAction,
_getGlobalState,
} from "mobx"

export type IComputedFnOptions<F extends (...args: any[]) => any> = {
Expand Down Expand Up @@ -66,7 +67,7 @@ export function computedFn<T extends (...args: any[]) => any>(
if (entry.exists()) return entry.get().get()
// if function is invoked, and its a cache miss without reactive, there is no point in caching...
if (!opts.keepAlive && !_isComputingDerivation()) {
if (!memoWarned) {
if (!memoWarned && _getGlobalState().computedRequiresReaction) {
console.warn(
"invoking a computedFn from outside an reactive context won't be memoized, unless keepAlive is set"
)
Expand Down
22 changes: 11 additions & 11 deletions src/from-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const FULFILLED = "fulfilled"
export const REJECTED = "rejected"

type CaseHandlers<U, T> = {
pending?: (t?: unknown) => U
pending?: (t?: T) => U
fulfilled?: (t: T) => U
rejected?: (e: any) => U
}
Expand All @@ -18,9 +18,9 @@ export interface IBasePromiseBasedObservable<T> extends PromiseLike<T> {
case<U>(handlers: CaseHandlers<U, T>, defaultFulfilled?: boolean): U
}

export type IPendingPromise = {
export type IPendingPromise<T> = {
readonly state: "pending"
readonly value: unknown // can be error, T or nothing at this point
readonly value: T | undefined
}

export type IFulfilledPromise<T> = {
Expand All @@ -34,7 +34,7 @@ export type IRejectedPromise = {
}

export type IPromiseBasedObservable<T> = IBasePromiseBasedObservable<T> &
(IPendingPromise | IFulfilledPromise<T> | IRejectedPromise)
(IPendingPromise<T> | IFulfilledPromise<T> | IRejectedPromise)

function caseImpl<U, T>(
this: IPromiseBasedObservable<T>,
Expand All @@ -58,7 +58,6 @@ function caseImpl<U, T>(
*
* And the following methods:
* - `case({fulfilled, rejected, pending})`: maps over the result using the provided handlers, or returns `undefined` if a handler isn't available for the current promise state.
* - `then((value: TValue) => TResult1 | PromiseLike<TResult1>, [(rejectReason: any) => any])`: chains additional handlers to the provided promise.
*
* The returned object implements `PromiseLike<TValue>`, so you can chain additional `Promise` handlers using `then`. You may also use it with `await` in `async` functions.
*
Expand Down Expand Up @@ -144,13 +143,13 @@ function caseImpl<U, T>(
* (transformedResult) => console.log('transformed fetchResult: ' + transformedResult)
* )
*
* @param {IThenable<T>} promise The promise which will be observed
* @param {IThenable<T>} oldPromise? The previously observed promise
* @returns {IPromiseBasedObservable<T>}
* @param origPromise The promise which will be observed
* @param oldPromise The previously observed promise
* @returns origPromise with added properties and methods described above.
*/
export function fromPromise<T>(
origPromise: PromiseLike<T>,
oldPromise?: PromiseLike<T>
oldPromise?: IPromiseBasedObservable<T>
): IPromiseBasedObservable<T> {
invariant(arguments.length <= 2, "fromPromise expects up to two arguments")
invariant(
Expand Down Expand Up @@ -181,9 +180,10 @@ export function fromPromise<T>(

promise.isPromiseBasedObservable = true
promise.case = caseImpl
const oldState = oldPromise ? (oldPromise as any).state : undefined
const oldData =
oldState === FULFILLED || oldState === PENDING ? (oldPromise as any).value : undefined
oldPromise && (oldPromise.state === FULFILLED || oldPromise.state === PENDING)
? oldPromise.value
: undefined
extendObservable(
promise,
{
Expand Down

0 comments on commit 1c5276b

Please sign in to comment.