mobx-utils
Advanced tools
Comparing version 6.0.8 to 6.1.0
@@ -0,0 +0,0 @@ import { IObservableArray } from "mobx"; |
@@ -0,0 +0,0 @@ var __spreadArrays = (this && this.__spreadArrays) || function () { |
@@ -0,0 +0,0 @@ import { IDisposer } from "./utils"; |
@@ -0,0 +0,0 @@ import { isAction, autorun, action, isObservableArray, runInAction } from "mobx"; |
@@ -0,0 +0,0 @@ import { IComputedValueOptions } from "mobx"; |
@@ -78,3 +78,3 @@ var __assign = (this && this.__assign) || function () { | ||
if (!memoWarned && _getGlobalState().computedRequiresReaction) { | ||
console.warn("invoking a computedFn from outside an reactive context won't be memoized, unless keepAlive is set"); | ||
console.warn("Invoking a computedFn from outside a reactive context won't be memoized unless keepAlive is set."); | ||
memoWarned = true; | ||
@@ -81,0 +81,0 @@ } |
import { IComputedValueOptions } from "mobx"; | ||
export declare type ITransformer<A, B> = (object: A) => B; | ||
export declare type ITransformerCleanup<A, B> = (resultObject: B | undefined, sourceObject?: A) => void; | ||
export declare type ITransformerParams<A, B> = { | ||
onCleanup?: (resultObject: B | undefined, sourceObject?: A) => void; | ||
onCleanup?: ITransformerCleanup<A, B>; | ||
debugNameGenerator?: (sourceObject?: A) => string; | ||
keepAlive?: boolean; | ||
} & Omit<IComputedValueOptions<B>, "name">; | ||
export declare function createTransformer<A, B>(transformer: ITransformer<A, B>, onCleanup?: (resultObject: B | undefined, sourceObject?: A) => void): ITransformer<A, B>; | ||
export declare function createTransformer<A, B>(transformer: ITransformer<A, B>, onCleanup?: ITransformerCleanup<A, B>): ITransformer<A, B>; | ||
export declare function createTransformer<A, B>(transformer: ITransformer<A, B>, arg2?: ITransformerParams<A, B>): ITransformer<A, B>; |
@@ -13,4 +13,3 @@ var __assign = (this && this.__assign) || function () { | ||
import { computed, onBecomeUnobserved, _isComputingDerivation, } from "mobx"; | ||
import { invariant, addHiddenProp } from "./utils"; | ||
var memoizationId = 0; | ||
import { invariant } from "./utils"; | ||
/** | ||
@@ -22,9 +21,11 @@ * Creates a function that maps an object to a view. | ||
* | ||
* @param transformer | ||
* @param onCleanup | ||
* @param transformer A function which transforms instances of A into instances of B | ||
* @param arg2 An optional cleanup function which is called when the transformation is no longer | ||
* observed from a reactive context, or config options | ||
* @returns The memoized transformer function | ||
*/ | ||
export function createTransformer(transformer, arg2) { | ||
invariant(typeof transformer === "function" && transformer.length < 2, "createTransformer expects a function that accepts one argument"); | ||
// Memoizes: object id -> reactive view that applies transformer to the object | ||
var views = {}; | ||
// Memoizes: object -> reactive view that applies transformer to the object | ||
var views = new Map(); | ||
var onCleanup = undefined; | ||
@@ -41,3 +42,3 @@ var keepAlive = false; | ||
} | ||
function createView(sourceIdentifier, sourceObject) { | ||
function createView(sourceObject) { | ||
var latestValue; | ||
@@ -57,5 +58,6 @@ var computedValueOptions = {}; | ||
} | ||
var sourceType = typeof sourceObject; | ||
var prettifiedName = debugNameGenerator | ||
? debugNameGenerator(sourceObject) | ||
: "Transformer-" + transformer.name + "-" + sourceIdentifier; | ||
: "Transformer-" + transformer.name + "-" + (sourceType === "string" || sourceType === "number" ? sourceObject : "object"); | ||
var expr = computed(function () { | ||
@@ -66,3 +68,3 @@ return (latestValue = transformer(sourceObject)); | ||
var disposer_1 = onBecomeUnobserved(expr, function () { | ||
delete views[sourceIdentifier]; | ||
views.delete(sourceObject); | ||
disposer_1(); | ||
@@ -77,4 +79,4 @@ if (onCleanup) | ||
return function (object) { | ||
var identifier = getMemoizationId(object); | ||
var reactiveView = views[identifier]; | ||
checkTransformableObject(object); | ||
var reactiveView = views.get(object); | ||
if (reactiveView) | ||
@@ -94,20 +96,15 @@ return reactiveView.get(); | ||
// Not in cache; create a reactive view | ||
reactiveView = views[identifier] = createView(identifier, object); | ||
reactiveView = createView(object); | ||
views.set(object, reactiveView); | ||
return reactiveView.get(); | ||
}; | ||
} | ||
function getMemoizationId(object) { | ||
function checkTransformableObject(object) { | ||
var objectType = typeof object; | ||
if (objectType === "string") | ||
return "string:" + object; | ||
if (objectType === "number") | ||
return "number:" + object; | ||
if (object === null || (objectType !== "object" && objectType !== "function")) | ||
if (object === null || | ||
(objectType !== "object" && | ||
objectType !== "function" && | ||
objectType !== "string" && | ||
objectType !== "number")) | ||
throw new Error("[mobx-utils] transform expected an object, function, string or number, got: " + String(object)); | ||
var tid = object.$transformId; | ||
if (tid === undefined) { | ||
tid = "memoizationId:" + ++memoizationId; | ||
addHiddenProp(object, "$transformId", tid); | ||
} | ||
return tid; | ||
} |
@@ -0,0 +0,0 @@ import { ObservableMap, IComputedValue } from "mobx"; |
@@ -0,0 +0,0 @@ var __assign = (this && this.__assign) || function () { |
@@ -0,0 +0,0 @@ declare type BabelDescriptor = PropertyDescriptor & { |
@@ -0,0 +0,0 @@ import { addHiddenProp } from "./utils"; |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ import { IObjectDidChange, IArrayDidChange, IMapDidChange } from "mobx"; |
@@ -0,0 +0,0 @@ import { observe, isObservableMap, isObservableObject, isObservableArray, values, entries, } from "mobx"; |
@@ -16,5 +16,2 @@ /** | ||
* @example | ||
* const Todo = observer((props) => { | ||
* const todo = props.todo | ||
* const isSelected = mobxUtils.expr(() => props.viewState.selection === todo) | ||
* const TodoView = observer(({ todo, editorState }) => { | ||
@@ -21,0 +18,0 @@ * const isSelected = mobxUtils.expr(() => editorState.selection === todo) |
@@ -17,5 +17,2 @@ import { computed, _isComputingDerivation } from "mobx"; | ||
* @example | ||
* const Todo = observer((props) => { | ||
* const todo = props.todo | ||
* const isSelected = mobxUtils.expr(() => props.viewState.selection === todo) | ||
* const TodoView = observer(({ todo, editorState }) => { | ||
@@ -22,0 +19,0 @@ * const isSelected = mobxUtils.expr(() => editorState.selection === todo) |
@@ -0,0 +0,0 @@ export declare type PromiseState = "pending" | "fulfilled" | "rejected"; |
@@ -0,0 +0,0 @@ import { action, extendObservable } from "mobx"; |
@@ -0,0 +0,0 @@ import { IDisposer } from "./utils"; |
@@ -0,0 +0,0 @@ import { createAtom, _allowStateChanges } from "mobx"; |
@@ -0,0 +0,0 @@ import { IComputedValue } from "mobx"; |
@@ -0,0 +0,0 @@ import { getAtom, observe } from "mobx"; |
@@ -0,0 +0,0 @@ export interface ILazyObservable<T> { |
@@ -0,0 +0,0 @@ import { observable, action, _allowStateChanges } from "mobx"; |
@@ -0,0 +0,0 @@ export * from "./from-promise"; |
@@ -0,0 +0,0 @@ export * from "./from-promise"; |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ import { _isComputingDerivation } from "mobx"; |
@@ -0,0 +0,0 @@ export interface IStreamObserver<T> { |
@@ -0,0 +0,0 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { |
@@ -0,0 +0,0 @@ import { IObservableArray, ObservableMap } from "mobx"; |
@@ -0,0 +0,0 @@ var __extends = (this && this.__extends) || (function () { |
@@ -0,0 +0,0 @@ import { IDisposer } from "./utils"; |
@@ -0,0 +0,0 @@ import { isAction, autorun, action, isObservableArray, runInAction } from "mobx"; |
@@ -0,0 +0,0 @@ export declare type IDisposer = () => void; |
@@ -0,0 +0,0 @@ export var NOOP = function () { }; |
{ | ||
"name": "mobx-utils", | ||
"version": "6.0.8", | ||
"version": "6.1.0", | ||
"description": "Utility functions and common patterns for MobX", | ||
@@ -94,2 +94,2 @@ "main": "mobx-utils.umd.js", | ||
} | ||
} | ||
} |
@@ -573,5 +573,2 @@ # MobX-utils | ||
```javascript | ||
const Todo = observer((props) => { | ||
const todo = props.todo | ||
const isSelected = mobxUtils.expr(() => props.viewState.selection === todo) | ||
const TodoView = observer(({ todo, editorState }) => { | ||
@@ -592,6 +589,8 @@ const isSelected = mobxUtils.expr(() => editorState.selection === todo) | ||
- `transformer` | ||
- `arg2` | ||
- `onCleanup` | ||
- `transformer` A function which transforms instances of A into instances of B | ||
- `arg2` An optional cleanup function which is called when the transformation is no longer | ||
observed from a reactive context, or config options | ||
Returns **any** The memoized transformer function | ||
## deepObserve | ||
@@ -741,4 +740,4 @@ | ||
`createTransformer` turns a function (that should transform value `A` into another value `B`) into a reactive and memoizing function. | ||
In other words, if the `transformation` function computes B given a specific A, the same B will be returned for all other future invocations of the transformation with the same A. | ||
However, if A changes, the transformation will be re-applied so that B is updated accordingly. | ||
In other words, if the `transformation` function computes `B` given a specific `A`, the same `B` will be returned for all other future invocations of the transformation with the same `A`. | ||
However, if `A` changes, or any derivation accessed in the transformer function body gets invalidated, the transformation will be re-applied so that `B` is updated accordingly. | ||
And last but not least, if nobody is using the transformation of a specific A anymore, its entry will be removed from the memoization table. | ||
@@ -745,0 +744,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
292172
44
5937
920