mobx-utils
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -0,1 +1,5 @@ | ||
# 1.1.3 | ||
* Introduced `fromStream` and `toStream` for interoperability with TC 39 / RxJS observable streams, see [Mobx #677](https://github.com/mobxjs/mobx/issues/677) | ||
# 1.1.2 | ||
@@ -2,0 +6,0 @@ |
@@ -10,3 +10,3 @@ /** | ||
* so make sure that you don't dereference to early. | ||
* | ||
* @example | ||
@@ -13,0 +13,0 @@ * const userProfile = lazyObservable( |
@@ -13,3 +13,3 @@ "use strict"; | ||
* so make sure that you don't dereference to early. | ||
* | ||
* @example | ||
@@ -16,0 +16,0 @@ * const userProfile = lazyObservable( |
export * from "./from-promise"; | ||
export * from "./lazy-observable"; | ||
export * from "./from-resource"; | ||
export * from "./observable-stream"; | ||
export * from "./create-view-model"; | ||
@@ -5,0 +6,0 @@ export * from "./guarded-when"; |
@@ -8,2 +8,3 @@ "use strict"; | ||
__export(require("./from-resource")); | ||
__export(require("./observable-stream")); | ||
__export(require("./create-view-model")); | ||
@@ -10,0 +11,0 @@ __export(require("./guarded-when")); |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("mobx")):"function"==typeof define&&define.amd?define(["mobx"],t):"object"==typeof exports?exports.mobxUtils=t(require("mobx")):e.mobxUtils=t(e.mobx)}(this,function(e){return function(e){function t(o){if(r[o])return r[o].exports;var n=r[o]={exports:{},id:o,loaded:!1};return e[o].call(n.exports,n,n.exports,t),n.loaded=!0,n.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){"use strict";function o(e){for(var r in e)t.hasOwnProperty(r)||(t[r]=e[r])}o(r(5)),o(r(9)),o(r(6)),o(r(4)),o(r(7)),o(r(8)),o(r(10)),o(r(3)),o(r(2))},function(t,r){t.exports=e},function(e,t){"use strict";function r(e,t){if(void 0===t&&(t="Illegal state"),!e)throw new Error("[mobx-utils] "+t)}function o(e){n.indexOf(e)===-1&&(n.push(e),console.error("[mobx-utils] Deprecated: "+e))}t.NOOP=function(){},t.IDENTITY=function(e){return e},t.invariant=r;var n=[];t.deprecated=o},function(e,t,r){"use strict";function o(e,t,r,o){if(void 0===r&&(r=0),void 0===o&&(o=0),!n.isObservableArray(e))throw new Error("Expected observable array as first argument");n.isAction(t)||(t=n.action("chunkProcessor",t));var i=function(){for(var r=function(){var r=0===o?e.length:Math.min(e.length,o),i=e.slice(0,r);n.runInAction(function(){return e.splice(0,r)}),t(i)};e.length>0;)r()};return r>0?n.autorunAsync(i,r):n.autorun(i)}var n=r(1);t.chunkProcessor=o},function(e,t,r){"use strict";function o(e){return new u(e)}var n=this&&this.__decorate||function(e,t,r,o){var n,i=arguments.length,s=i<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,r):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,r,o);else for(var a=e.length-1;a>=0;a--)(n=e[a])&&(s=(i<3?n(s):i>3?n(t,r,s):n(t,r))||s);return i>3&&s&&Object.defineProperty(t,r,s),s},i=r(1),s=r(2),a=["model","reset","submit","isDirty","isPropertyDirty"],u=function(){function e(e){var t=this;this.model=e,this.localValues=i.asMap({}),s.invariant(i.isObservableObject(e),"createViewModel expects an observable object"),Object.keys(e).forEach(function(e){s.invariant(a.indexOf(e)===-1,"The propertyname "+e+" is reserved and cannot be used with viewModels"),Object.defineProperty(t,e,{enumerable:!0,configurable:!0,get:function(){return t.isPropertyDirty(e)?t.localValues.get(e):t.model[e]},set:i.action(function(r){(t.isPropertyDirty(e)||r!==t.model[e])&&t.localValues.set(e,r)})})})}return Object.defineProperty(e.prototype,"isDirty",{get:function(){return this.localValues.size>0},enumerable:!0,configurable:!0}),e.prototype.isPropertyDirty=function(e){return this.localValues.has(e)},e.prototype.submit=function(){var e=this;this.localValues.keys().forEach(function(t){var r=e.localValues.get(t),o=e.model[t];i.isObservableArray(o)?o.replace(r):i.isObservableMap(o)?(o.clear(),o.merge(r)):e.model[t]=r}),this.localValues.clear()},e.prototype.reset=function(){this.localValues.clear()},e.prototype.resetProperty=function(e){this.localValues["delete"](e)},n([i.computed],e.prototype,"isDirty",null),n([i.action],e.prototype,"submit",null),n([i.action],e.prototype,"reset",null),n([i.action],e.prototype,"resetProperty",null),e}();t.createViewModel=o},function(e,t,r){"use strict";function o(e,t,r){return void 0===t&&(t=void 0),void 0===r&&(r=i.IDENTITY),new s(e,t,r)}var n=r(1),i=r(2);t.PENDING="pending",t.FULFILLED="fulfilled",t.REJECTED="rejected";var s=function(){function e(e,t,r){var o=this;void 0===t&&(t=void 0),void 0===r&&(r=i.IDENTITY),this.promise=e,this.modifier=r,this._state=n.observable("pending"),this._reason=n.observable(void 0),this._observable=n.observable(r(t)),e.then(n.action("observableFromPromise-resolve",function(e){o._observable.set(e),o._state.set("fulfilled")}),n.action("observableFromPromise-reject",function(e){o._reason.set(e),o._observable.set(e),o._state.set("rejected")}))}return Object.defineProperty(e.prototype,"value",{get:function(){return this._observable.get()},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"state",{get:function(){return this._state.get()},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"reason",{get:function(){return i.deprecated("In `fromPromise`: `.reason` is deprecated, use `.value` instead"),this._reason.get()},enumerable:!0,configurable:!0}),e.prototype["case"]=function(e){switch(this.state){case"pending":return e.pending&&e.pending();case"rejected":return e.rejected&&e.rejected(this.value);case"fulfilled":return e.fulfilled&&e.fulfilled(this.value)}},e}();t.fromPromise=o},function(e,t,r){"use strict";function o(e,t,r){void 0===t&&(t=i.NOOP),void 0===r&&(r=void 0);var o=!1,s=!1,a=r,u=function(){o&&(o=!1,t())},c=new n.Atom("ResourceBasedObservable",function(){i.invariant(!o&&!s),o=!0,e(n.action("ResourceBasedObservable-sink",function(e){a=e,c.reportChanged()}))},u);return{current:function(){i.invariant(!s,"subscribingObservable has already been disposed");var e=c.reportObserved();return e||o||console.warn("Called `get` of an subscribingObservable outside a reaction. Current value will be returned but no new subscription has started"),a},dispose:function(){s=!0,u()},isAlive:function(){return o}}}var n=r(1),i=r(2);t.fromResource=o},function(e,t,r){"use strict";function o(e,t,r,o){void 0===r&&(r=1e4),void 0===o&&(o=function(){});var i=!1,s=setTimeout(function(){i||(a(),o())},r),a=n.when(e,function(){i=!0,clearTimeout(s),t()});return function(){clearTimeout(s),a()}}var n=r(1);t.whenWithTimeout=o},function(e,t,r){"use strict";function o(e,t){var r=n.extras.getAtom(e,t);if(!r)throw new Error("No computed provided, please provide an object created with `computed(() => expr)` or an object + property name");return r.observe(function(){})}var n=r(1);t.keepAlive=o},function(e,t,r){"use strict";function o(e,t,r){void 0===t&&(t=void 0),void 0===r&&(r=n.IDENTITY);var o=!1,s=i.observable(r(t)),a=function(){return o||(o=!0,e(i.action("lazyObservable-fetch",function(e){s.set(e)}))),s.get()};return{current:a,refresh:function(){return o=!1,a()}}}var n=r(2),i=r(1);t.lazyObservable=o},function(e,t,r){"use strict";function o(e,t,r){if(void 0===r&&(r=0),!n.isObservableArray(e))throw new Error("Expected observable array as first argument");n.isAction(t)||(t=n.action("queueProcessor",t));var o=function(){var r=e.slice(0);n.runInAction(function(){return e.splice(0)}),r.forEach(t)};return r>0?n.autorunAsync(o,r):n.autorun(o)}var n=r(1);t.queueProcessor=o}])}); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("mobx")):"function"==typeof define&&define.amd?define(["mobx"],t):"object"==typeof exports?exports.mobxUtils=t(require("mobx")):e.mobxUtils=t(e.mobx)}(this,function(e){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){"use strict";function n(e){for(var r in e)t.hasOwnProperty(r)||(t[r]=e[r])}n(r(5)),n(r(9)),n(r(6)),n(r(10)),n(r(4)),n(r(7)),n(r(8)),n(r(11)),n(r(3)),n(r(2))},function(t,r){t.exports=e},function(e,t){"use strict";function r(e,t){if(void 0===t&&(t="Illegal state"),!e)throw new Error("[mobx-utils] "+t)}function n(e){o.indexOf(e)===-1&&(o.push(e),console.error("[mobx-utils] Deprecated: "+e))}t.NOOP=function(){},t.IDENTITY=function(e){return e},t.invariant=r;var o=[];t.deprecated=n},function(e,t,r){"use strict";function n(e,t,r,n){if(void 0===r&&(r=0),void 0===n&&(n=0),!o.isObservableArray(e))throw new Error("Expected observable array as first argument");o.isAction(t)||(t=o.action("chunkProcessor",t));var i=function(){for(var r=function(){var r=0===n?e.length:Math.min(e.length,n),i=e.slice(0,r);o.runInAction(function(){return e.splice(0,r)}),t(i)};e.length>0;)r()};return r>0?o.autorunAsync(i,r):o.autorun(i)}var o=r(1);t.chunkProcessor=n},function(e,t,r){"use strict";function n(e){return new c(e)}var o=this&&this.__decorate||function(e,t,r,n){var o,i=arguments.length,s=i<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,r,n);else for(var u=e.length-1;u>=0;u--)(o=e[u])&&(s=(i<3?o(s):i>3?o(t,r,s):o(t,r))||s);return i>3&&s&&Object.defineProperty(t,r,s),s},i=r(1),s=r(2),u=["model","reset","submit","isDirty","isPropertyDirty"],c=function(){function e(e){var t=this;this.model=e,this.localValues=i.asMap({}),s.invariant(i.isObservableObject(e),"createViewModel expects an observable object"),Object.keys(e).forEach(function(e){s.invariant(u.indexOf(e)===-1,"The propertyname "+e+" is reserved and cannot be used with viewModels"),Object.defineProperty(t,e,{enumerable:!0,configurable:!0,get:function(){return t.isPropertyDirty(e)?t.localValues.get(e):t.model[e]},set:i.action(function(r){(t.isPropertyDirty(e)||r!==t.model[e])&&t.localValues.set(e,r)})})})}return Object.defineProperty(e.prototype,"isDirty",{get:function(){return this.localValues.size>0},enumerable:!0,configurable:!0}),e.prototype.isPropertyDirty=function(e){return this.localValues.has(e)},e.prototype.submit=function(){var e=this;this.localValues.keys().forEach(function(t){var r=e.localValues.get(t),n=e.model[t];i.isObservableArray(n)?n.replace(r):i.isObservableMap(n)?(n.clear(),n.merge(r)):e.model[t]=r}),this.localValues.clear()},e.prototype.reset=function(){this.localValues.clear()},e.prototype.resetProperty=function(e){this.localValues["delete"](e)},o([i.computed],e.prototype,"isDirty",null),o([i.action],e.prototype,"submit",null),o([i.action],e.prototype,"reset",null),o([i.action],e.prototype,"resetProperty",null),e}();t.createViewModel=n},function(e,t,r){"use strict";function n(e,t,r){return void 0===t&&(t=void 0),void 0===r&&(r=i.IDENTITY),new s(e,t,r)}var o=r(1),i=r(2);t.PENDING="pending",t.FULFILLED="fulfilled",t.REJECTED="rejected";var s=function(){function e(e,t,r){var n=this;void 0===t&&(t=void 0),void 0===r&&(r=i.IDENTITY),this.promise=e,this.modifier=r,this._state=o.observable("pending"),this._reason=o.observable(void 0),this._observable=o.observable(r(t)),e.then(o.action("observableFromPromise-resolve",function(e){n._observable.set(e),n._state.set("fulfilled")}),o.action("observableFromPromise-reject",function(e){n._reason.set(e),n._observable.set(e),n._state.set("rejected")}))}return Object.defineProperty(e.prototype,"value",{get:function(){return this._observable.get()},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"state",{get:function(){return this._state.get()},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"reason",{get:function(){return i.deprecated("In `fromPromise`: `.reason` is deprecated, use `.value` instead"),this._reason.get()},enumerable:!0,configurable:!0}),e.prototype["case"]=function(e){switch(this.state){case"pending":return e.pending&&e.pending();case"rejected":return e.rejected&&e.rejected(this.value);case"fulfilled":return e.fulfilled&&e.fulfilled(this.value)}},e}();t.fromPromise=n},function(e,t,r){"use strict";function n(e,t,r){void 0===t&&(t=i.NOOP),void 0===r&&(r=void 0);var n=!1,s=!1,u=r,c=function(){n&&(n=!1,t())},a=new o.Atom("ResourceBasedObservable",function(){i.invariant(!n&&!s),n=!0,e(o.action("ResourceBasedObservable-sink",function(e){u=e,a.reportChanged()}))},c);return{current:function(){i.invariant(!s,"subscribingObservable has already been disposed");var e=a.reportObserved();return e||n||console.warn("Called `get` of an subscribingObservable outside a reaction. Current value will be returned but no new subscription has started"),u},dispose:function(){s=!0,c()},isAlive:function(){return n}}}var o=r(1),i=r(2);t.fromResource=n},function(e,t,r){"use strict";function n(e,t,r,n){void 0===r&&(r=1e4),void 0===n&&(n=function(){});var i=!1,s=setTimeout(function(){i||(u(),n())},r),u=o.when(e,function(){i=!0,clearTimeout(s),t()});return function(){clearTimeout(s),u()}}var o=r(1);t.whenWithTimeout=n},function(e,t,r){"use strict";function n(e,t){var r=o.extras.getAtom(e,t);if(!r)throw new Error("No computed provided, please provide an object created with `computed(() => expr)` or an object + property name");return r.observe(function(){})}var o=r(1);t.keepAlive=n},function(e,t,r){"use strict";function n(e,t,r){void 0===t&&(t=void 0),void 0===r&&(r=o.IDENTITY);var n=!1,s=i.observable(r(t)),u=function(){return n||(n=!0,e(i.action("lazyObservable-fetch",function(e){s.set(e)}))),s.get()};return{current:u,refresh:function(){return n=!1,u()}}}var o=r(2),i=r(1);t.lazyObservable=n},function(e,t,r){"use strict";function n(){return"function"==typeof Symbol&&Symbol.observable||"@@observable"}function o(){return this}function i(e){var t=c.computed(e);return r={subscribe:function(e){return{unsubscribe:t.observe("function"==typeof e?function(t){return e(t)}:function(t){return e.next(t)})}}},r[n()]=o,r;var r}function s(e,t){return void 0===t&&(t=void 0),new a(e,t)}var u=this&&this.__decorate||function(e,t,r,n){var o,i=arguments.length,s=i<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,r,n);else for(var u=e.length-1;u>=0;u--)(o=e[u])&&(s=(i<3?o(s):i>3?o(t,r,s):o(t,r))||s);return i>3&&s&&Object.defineProperty(t,r,s),s},c=r(1);t.toStream=i;var a=function(){function e(e,t){this.current=c.asReference(void 0),this.current=t,this.subscription=e.subscribe(this)}return e.prototype.dispose=function(){this.subscription.unsubscribe()},e.prototype.next=function(e){this.current=e},e.prototype.complete=function(){this.subscription.unsubscribe(),this.dispose()},e.prototype.error=function(e){this.current=e,this.dispose()},u([c.observable],e.prototype,"current",void 0),e}();t.fromStream=s},function(e,t,r){"use strict";function n(e,t,r){if(void 0===r&&(r=0),!o.isObservableArray(e))throw new Error("Expected observable array as first argument");o.isAction(t)||(t=o.action("queueProcessor",t));var n=function(){var r=e.slice(0);o.runInAction(function(){return e.splice(0)}),r.forEach(t)};return r>0?o.autorunAsync(n,r):o.autorun(n)}var o=r(1);t.queueProcessor=n}])}); |
{ | ||
"name": "mobx-utils", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Utility functions and common patterns for MobX", | ||
@@ -11,4 +11,7 @@ "main": "lib/mobx-utils.js", | ||
"test": "npm run build && tape test/*.js | faucet", | ||
"lint:js": "eslint ./test", | ||
"lint:ts": "tslint ./src/*.ts", | ||
"lint": "npm run lint:ts && npm run lint:js", | ||
"prepublish": "npm run build && npm run build-docs", | ||
"test-travis": "npm run build && istanbul cover tape test/*.js", | ||
"test-travis": "npm run build && npm run lint && istanbul cover tape test/*.js", | ||
"coverage": "npm run build && istanbul cover tape test/*.js && cat ./coverage/lcov.info|coveralls", | ||
@@ -31,9 +34,14 @@ "build-docs": "npm run build && documentation readme lib/mobx-utils.js --github --section API" | ||
"devDependencies": { | ||
"babel-eslint": "^7.1.0", | ||
"coveralls": "^2.11.4", | ||
"documentation": "^4.0.0-beta9", | ||
"eslint": "^3.10.1", | ||
"faucet": "0.0.1", | ||
"istanbul": "^0.3.21", | ||
"mobx": "^2.4.2", | ||
"rxjs": "^5.0.2", | ||
"tape": "^4.2.2", | ||
"typescript": "^1.8.10", | ||
"tslint": "^3.15.1", | ||
"tslint-eslint-rules": "^2.1.0", | ||
"typescript": "^2.0.9", | ||
"webpack": "^1.13.1" | ||
@@ -40,0 +48,0 @@ }, |
113
README.md
@@ -13,3 +13,3 @@ # MobX-utils | ||
# Installation | ||
# Installation & Usage | ||
@@ -20,2 +20,4 @@ NPM: `npm install mobx-utils --save` | ||
`import {function_name} from 'mobx-utils'` | ||
# API | ||
@@ -25,3 +27,3 @@ | ||
[lib/from-promise.js:103-107](https://github.com/mobxjs/mobx-utils/blob/14c00824294e58ad9d471f0a10e8d0aade13ed26/lib/from-promise.js#L103-L107 "Source code on GitHub") | ||
[lib/from-promise.js:103-107](https://github.com/mobxjs/mobx-utils/blob/197f8f8666eb54d6a1b2497f2bc6f81815316949/lib/from-promise.js#L103-L107 "Source code on GitHub") | ||
@@ -82,3 +84,3 @@ `fromPromise` takes a Promise and returns an object with 3 observable properties that track | ||
[lib/lazy-observable.js:37-58](https://github.com/mobxjs/mobx-utils/blob/14c00824294e58ad9d471f0a10e8d0aade13ed26/lib/lazy-observable.js#L37-L58 "Source code on GitHub") | ||
[lib/lazy-observable.js:37-58](https://github.com/mobxjs/mobx-utils/blob/197f8f8666eb54d6a1b2497f2bc6f81815316949/lib/lazy-observable.js#L37-L58 "Source code on GitHub") | ||
@@ -120,3 +122,3 @@ `lazyObservable` creates an observable around a `fetch` method that will not be invoked | ||
[lib/from-resource.js:67-101](https://github.com/mobxjs/mobx-utils/blob/14c00824294e58ad9d471f0a10e8d0aade13ed26/lib/from-resource.js#L67-L101 "Source code on GitHub") | ||
[lib/from-resource.js:67-101](https://github.com/mobxjs/mobx-utils/blob/197f8f8666eb54d6a1b2497f2bc6f81815316949/lib/from-resource.js#L67-L101 "Source code on GitHub") | ||
@@ -183,5 +185,60 @@ `fromResource` creates an observable which current state can be inspected using `.current()`, | ||
## toStream | ||
[lib/observable-stream.js:37-52](https://github.com/mobxjs/mobx-utils/blob/197f8f8666eb54d6a1b2497f2bc6f81815316949/lib/observable-stream.js#L37-L52 "Source code on GitHub") | ||
Converts an expression to an observable stream (a.k.a. TC 39 Observable / RxJS observable). | ||
The provided expression is tracked by mobx as long as there are subscribers, automatically | ||
emitting when new values become available. The expressions respect transactions. | ||
**Parameters** | ||
- `expression` | ||
**Examples** | ||
```javascript | ||
const user = observable({ | ||
firstName: "C.S", | ||
lastName: "Lewis" | ||
}) | ||
Rx.Observable | ||
.from(mobxUtils.toStream(() => user.firstname + user.lastName)) | ||
.scan(nameChanges => nameChanges + 1, 0) | ||
.subscribe(nameChanges => console.log("Changed name ", nameChanges, "times")) | ||
``` | ||
Returns **IObservableStream<T>** | ||
## fromStream | ||
[lib/observable-stream.js:104-107](https://github.com/mobxjs/mobx-utils/blob/197f8f8666eb54d6a1b2497f2bc6f81815316949/lib/observable-stream.js#L104-L107 "Source code on GitHub") | ||
Converts an subscribable, observable stream (TC 39 observable / RxJS stream) | ||
into an object which stores the current value (as `current`). The subscription can be cancelled through the `dispose` method. | ||
Takes an initial value as second optional argument | ||
**Parameters** | ||
- `observable` **IObservableStream<T>** | ||
- `initialValue` | ||
**Examples** | ||
```javascript | ||
const debouncedClickDelta = MobxUtils.fromStream(Rx.Observable.fromEvent(button, 'click') | ||
.throttleTime(1000) | ||
.map(event => event.clientX) | ||
.scan((count, clientX) => count + clientX, 0) | ||
) | ||
autorun(() => { | ||
console.log("distance moved", debouncedClickDelta.current) | ||
}) | ||
``` | ||
## createViewModel | ||
[lib/create-view-model.js:127-129](https://github.com/mobxjs/mobx-utils/blob/14c00824294e58ad9d471f0a10e8d0aade13ed26/lib/create-view-model.js#L127-L129 "Source code on GitHub") | ||
[lib/create-view-model.js:127-129](https://github.com/mobxjs/mobx-utils/blob/197f8f8666eb54d6a1b2497f2bc6f81815316949/lib/create-view-model.js#L127-L129 "Source code on GitHub") | ||
@@ -237,3 +294,3 @@ `createViewModel` takes an object with observable properties (model) | ||
[lib/guarded-when.js:32-51](https://github.com/mobxjs/mobx-utils/blob/14c00824294e58ad9d471f0a10e8d0aade13ed26/lib/guarded-when.js#L32-L51 "Source code on GitHub") | ||
[lib/guarded-when.js:32-51](https://github.com/mobxjs/mobx-utils/blob/197f8f8666eb54d6a1b2497f2bc6f81815316949/lib/guarded-when.js#L32-L51 "Source code on GitHub") | ||
@@ -274,12 +331,7 @@ Like normal `when`, except that this `when` will automatically dispose if the condition isn't met within a certain amount of time. | ||
[lib/keep-alive.js:31-36](https://github.com/mobxjs/mobx-utils/blob/14c00824294e58ad9d471f0a10e8d0aade13ed26/lib/keep-alive.js#L31-L36 "Source code on GitHub") | ||
[lib/keep-alive.js:31-36](https://github.com/mobxjs/mobx-utils/blob/197f8f8666eb54d6a1b2497f2bc6f81815316949/lib/keep-alive.js#L31-L36 "Source code on GitHub") | ||
MobX normally suspends any computed value that is not in use by any reaction, | ||
and lazily re-evaluates the expression if needed outside a reaction while not in use. | ||
`keepAlive` marks a computed value as always in use, meaning that it will always fresh, but never disposed automatically. | ||
**Parameters** | ||
- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** an object that has a computed property, created by `@computed` or `extendObservable` | ||
- `property` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the name of the property to keep alive | ||
- `computedValue` **IComputedValue<any>** created using the `computed` function | ||
- `_1` | ||
@@ -291,7 +343,8 @@ - `_2` | ||
```javascript | ||
const obj = observable({ | ||
number: 3, | ||
doubler: function() { return this.number * 2 } | ||
}) | ||
const stop = keepAlive(obj, "doubler") | ||
const number = observable(3) | ||
const doubler = computed(() => number.get() * 2) | ||
const stop = keepAlive(doubler) | ||
// doubler will now stay in sync reactively even when there are no further observers | ||
stop() | ||
// normal behavior, doubler results will be recomputed if not observed but needed, but lazily | ||
``` | ||
@@ -303,7 +356,12 @@ | ||
[lib/keep-alive.js:31-36](https://github.com/mobxjs/mobx-utils/blob/14c00824294e58ad9d471f0a10e8d0aade13ed26/lib/keep-alive.js#L31-L36 "Source code on GitHub") | ||
[lib/keep-alive.js:31-36](https://github.com/mobxjs/mobx-utils/blob/197f8f8666eb54d6a1b2497f2bc6f81815316949/lib/keep-alive.js#L31-L36 "Source code on GitHub") | ||
MobX normally suspends any computed value that is not in use by any reaction, | ||
and lazily re-evaluates the expression if needed outside a reaction while not in use. | ||
`keepAlive` marks a computed value as always in use, meaning that it will always fresh, but never disposed automatically. | ||
**Parameters** | ||
- `computedValue` **IComputedValue<any>** created using the `computed` function | ||
- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** an object that has a computed property, created by `@computed` or `extendObservable` | ||
- `property` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the name of the property to keep alive | ||
- `_1` | ||
@@ -315,8 +373,7 @@ - `_2` | ||
```javascript | ||
const number = observable(3) | ||
const doubler = computed(() => number.get() * 2) | ||
const stop = keepAlive(doubler) | ||
// doubler will now stay in sync reactively even when there are no further observers | ||
stop() | ||
// normal behavior, doubler results will be recomputed if not observed but needed, but lazily | ||
const obj = observable({ | ||
number: 3, | ||
doubler: function() { return this.number * 2 } | ||
}) | ||
const stop = keepAlive(obj, "doubler") | ||
``` | ||
@@ -328,3 +385,3 @@ | ||
[lib/queue-processor.js:22-40](https://github.com/mobxjs/mobx-utils/blob/14c00824294e58ad9d471f0a10e8d0aade13ed26/lib/queue-processor.js#L22-L40 "Source code on GitHub") | ||
[lib/queue-processor.js:22-40](https://github.com/mobxjs/mobx-utils/blob/197f8f8666eb54d6a1b2497f2bc6f81815316949/lib/queue-processor.js#L22-L40 "Source code on GitHub") | ||
@@ -357,3 +414,3 @@ `queueProcessor` takes an observable array, observes it and calls `processor` | ||
[lib/chunk-processor.js:27-52](https://github.com/mobxjs/mobx-utils/blob/14c00824294e58ad9d471f0a10e8d0aade13ed26/lib/chunk-processor.js#L27-L52 "Source code on GitHub") | ||
[lib/chunk-processor.js:27-52](https://github.com/mobxjs/mobx-utils/blob/197f8f8666eb54d6a1b2497f2bc6f81815316949/lib/chunk-processor.js#L27-L52 "Source code on GitHub") | ||
@@ -360,0 +417,0 @@ `chunkProcessor` takes an observable array, observes it and calls `processor` |
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
75856
29
1144
432
13