Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mobx-utils

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-utils - npm Package Compare versions

Comparing version 0.1.2 to 0.1.3

319

mobx-utils.umd.js

@@ -1,318 +0,1 @@

(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mobxUtils = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";
var mobx_1 = require("mobx");
var utils_1 = require("./utils");
var PromiseBasedObservable = (function () {
function PromiseBasedObservable(promise, initialValue, modifier) {
var _this = this;
if (initialValue === void 0) { initialValue = undefined; }
if (modifier === void 0) { modifier = utils_1.IDENTITY; }
this.promise = promise;
this.modifier = modifier;
this._state = mobx_1.observable("pending");
this._reason = undefined;
this._observable = mobx_1.observable(modifier(initialValue));
promise.then(mobx_1.action("observableFromPromise-resolve", function (value) {
_this._observable.set(_this.modifier(value));
_this._state.set("fulfilled");
})).catch(mobx_1.action("observableFromPromise-reject", function (reason) {
_this._reason.set(reason);
_this._state.set("rejected");
}));
}
Object.defineProperty(PromiseBasedObservable.prototype, "value", {
get: function () {
return this._observable.get();
},
enumerable: true,
configurable: true
});
Object.defineProperty(PromiseBasedObservable.prototype, "state", {
get: function () {
return this._state.get();
},
enumerable: true,
configurable: true
});
Object.defineProperty(PromiseBasedObservable.prototype, "reason", {
get: function () {
return this._reason.get();
},
enumerable: true,
configurable: true
});
return PromiseBasedObservable;
}());
/**
*
*
* @param {IThenable<T>} promise
* @param {T} [initialValue=undefined]
* @param {any} [modifier=IDENTITY]
* @returns {IPromiseBasedObservable<T>}
*/
function fromPromise(promise, initialValue, modifier) {
if (initialValue === void 0) { initialValue = undefined; }
if (modifier === void 0) { modifier = utils_1.IDENTITY; }
return new PromiseBasedObservable(promise, initialValue, modifier);
}
exports.fromPromise = fromPromise;
},{"./utils":8,"mobx":"mobx"}],2:[function(require,module,exports){
"use strict";
var mobx_1 = require("mobx");
var utils_1 = require("./utils");
/**
* `fromResource` creates an observable which current state can be inspected using `.get()`,
* and which can be kept in sync with some external datasource that can be subscribed to.
*
* The created observable will only subscribe to the datasource if it is in use somewhere,
* (un)subscribing when needed. To enable `fromResource` to do that two callbacks need to be provided,
* one to subscribe, and one to unsubscribe. The subscribe callback itself will receive a `sink` callback, which can be used
* to update the current state of the observable, allowing observes to react.
*
* Whatever is passed to `sink` will be returned by `get()`. It is the `get()` call itself which is being tracked,
* so make sure that you don't dereference to early.
*
* The following example code creates an observable that connects to a `dbUserRecord`,
* which comes from an imaginary database and notifies when it has changed.
*
* @example
* function createObservableUser(dbUserRecord) {
* let currentSubscription;
* return fromResource(
* (sink) => {
* // sink the current state
* sink(dbUserRecord.fields)
* // subscribe to the record, invoke the sink callback whenever new data arrives
* currentSubscription = dbUserRecord.onUpdated(() => {
* sink(dbUserRecord.fields)
* })
* },
* () => {
* // the user observable is not in use at the moment, unsubscribe (for now)
* dbUserRecord.unsubscribe(currentSubscription)
* },
* dbUserRecord.fields // optionally, provide initial data
* )
* }
*
* // usage:
* const myUserObservable = createObservableUser(myDatabaseConnector.query("name = 'Michel'"))
* autorun(() => {
* // printed everytime the database updates its records
* console.log(myUserObservable.get().displayName)
* })
*
* const userComponent = observer(({ user }) =>
* <div>{user.get().displayName}</div>
* )
*
* @export
* @template T
* @param {(sink: (newValue: T) => void) => void} subscriber
* @param {IDisposer} [unsubscriber=NOOP]
* @param {T} [initialValue=undefined] the data that will be returned by `get()` until the `sink` has emitted its first data
* @returns {{
* get(): T;
* dispose(): void;
* }}
*/
function fromResource(subscriber, unsubscriber, initialValue) {
if (unsubscriber === void 0) { unsubscriber = utils_1.NOOP; }
if (initialValue === void 0) { initialValue = undefined; }
var isActive = false;
var isDisposed = false;
var value = initialValue;
var atom = new mobx_1.Atom("ResourceBasedObservable", function () {
utils_1.invariant(!isActive && !isDisposed);
isActive = true;
subscriber(mobx_1.action("ResourceBasedObservable-sink", function (newValue) {
value = newValue;
atom.reportChanged();
}));
}, function () {
utils_1.invariant(isActive);
unsubscriber();
isActive = false;
});
return {
get: function () {
utils_1.invariant(!isDisposed, "subscribingObservable has already been disposed");
var isBeingTracked = atom.reportObserved();
if (!isBeingTracked && !isActive)
console.warn("Called `get` of an subscribingObservable outside a reaction. Current value will be returned but no new subscription has started");
return value;
},
dispose: function () {
isDisposed = true;
if (isActive)
unsubscriber();
}
};
}
exports.fromResource = fromResource;
},{"./utils":8,"mobx":"mobx"}],3:[function(require,module,exports){
"use strict";
var mobx_1 = require("mobx");
/**
* Like normal `when`, except that this `when` will automatically dispose if the condition isn't met within a certain amount of time.
*
* @example
* test("expect store to load", t => {
* const store = {
* items: [],
* loaded: false
* }
* fetchDataForStore((data) => {
* store.items = data;
* store.loaded = true;
* })
* whenWithTimeout(
* () => store.loaded
* () => t.end()
* 2000,
* () => t.fail("expected store to load")
* )
* })
*
*
* @export
* @param {() => boolean} expr see when, the expression to await
* @param {() => void} action see when, the action to execut when expr returns truthy
* @param {number} [timeout=10000] maximum amount when spends waiting before giving up
* @param {any} [onTimeout=() => {}] the ontimeout handler will be called if the condition wasn't met withing the given time
* @returns {IDisposer} disposer function that can be used to cancel the when prematurely. Neither action or onTimeout will be fired if disposed
*/
function whenWithTimeout(expr, action, timeout, onTimeout) {
if (timeout === void 0) { timeout = 10000; }
if (onTimeout === void 0) { onTimeout = function () { }; }
var done = false;
var handle = setTimeout(function () {
if (!done) {
disposer();
onTimeout();
}
}, timeout);
var disposer = mobx_1.when(expr, function () {
done = true;
clearTimeout(handle);
action();
});
return function () {
clearTimeout(handle);
disposer();
};
}
exports.whenWithTimeout = whenWithTimeout;
},{"mobx":"mobx"}],4:[function(require,module,exports){
"use strict";
var mobx_1 = require("mobx");
/**
* 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.
*
* @example
* 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
*
* @param {IComputedValue<any>} computedValue created using the `computed` function
* @returns {IDisposer} stops this keep alive so that the computed value goes back to normal behavior
*/
/**
* 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.
*
* @example
* const obj = observable({
* number: 3,
* doubler: function() { return this.number * 2 }
* })
* const stop = keepAlive(obj, "doubler")
*
* @param {Object} target an object that has a computed property, created by `@computed` or `extendObservable`
* @param {string} property the name of the property to keep alive
* @returns {IDisposer} stops this keep alive so that the computed value goes back to normal behavior
*/
function keepAlive(_1, _2) {
var computed = mobx_1.extras.getAtom(_1, _2);
if (!computed)
throw new Error("No computed provided, please provide an object created with `computed(() => expr)` or an object + property name");
return computed.observe(function () { });
}
exports.keepAlive = keepAlive;
},{"mobx":"mobx"}],5:[function(require,module,exports){
"use strict";
var utils_1 = require("./utils");
var mobx_1 = require("mobx");
function lazyObservable(fetch, initialValue, modifier) {
if (initialValue === void 0) { initialValue = undefined; }
if (modifier === void 0) { modifier = utils_1.IDENTITY; }
var started = false;
var value = mobx_1.observable(modifier(initialValue));
return {
get: function () {
if (!started) {
started = true;
fetch(mobx_1.action("lazyObservable-fetched", function (newValue) {
value.set(newValue);
}));
}
return value.get();
}
};
}
exports.lazyObservable = lazyObservable;
},{"./utils":8,"mobx":"mobx"}],6:[function(require,module,exports){
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(require("./from-promise"));
__export(require("./guarded-when"));
__export(require("./keep-alive"));
__export(require("./queue-processor"));
__export(require("./lazy-observable"));
__export(require("./from-resource"));
__export(require("./utils"));
},{"./from-promise":1,"./from-resource":2,"./guarded-when":3,"./keep-alive":4,"./lazy-observable":5,"./queue-processor":7,"./utils":8}],7:[function(require,module,exports){
"use strict";
var mobx_1 = require("mobx");
function queueProcessor(observableArray, processor, debounce) {
if (debounce === void 0) { debounce = 0; }
if (!mobx_1.isObservableArray(observableArray))
throw new Error("Expected observable array as first argument");
if (!mobx_1.isAction(processor))
processor = mobx_1.action("queueProcessor", processor);
var runner = function () { return observableArray.splice(0).forEach(processor); };
if (debounce > 0)
return mobx_1.autorunAsync(runner, debounce);
else
return mobx_1.autorun(runner);
}
exports.queueProcessor = queueProcessor;
},{"mobx":"mobx"}],8:[function(require,module,exports){
"use strict";
exports.NOOP = function () { };
exports.IDENTITY = function (_) { return _; };
function invariant(cond, message) {
if (message === void 0) { message = "Illegal state"; }
if (!cond)
throw new Error("[mobx-utils] " + message);
}
exports.invariant = invariant;
},{}]},{},[6])(6)
});
!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(3)),o(r(5)),o(r(6)),o(r(8)),o(r(7)),o(r(4)),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)}t.NOOP=function(){},t.IDENTITY=function(e){return e},t.invariant=r},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),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=void 0,this._observable=n.observable(r(t)),e.then(n.action("observableFromPromise-resolve",function(e){o._observable.set(o.modifier(e)),o._state.set("fulfilled")}))["catch"](n.action("observableFromPromise-reject",function(e){o._reason.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 this._reason.get()},enumerable:!0,configurable:!0}),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,u=r,a=new n.Atom("ResourceBasedObservable",function(){i.invariant(!o&&!s),o=!0,e(n.action("ResourceBasedObservable-sink",function(e){u=e,a.reportChanged()}))},function(){i.invariant(o),t(),o=!1});return{get:function(){i.invariant(!s,"subscribingObservable has already been disposed");var e=a.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"),u},dispose:function(){s=!0,o&&t()}}}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||(u(),o())},r),u=n.when(e,function(){i=!0,clearTimeout(s),t()});return function(){clearTimeout(s),u()}}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));return{get:function(){return o||(o=!0,e(i.action("lazyObservable-fetched",function(e){s.set(e)}))),s.get()}}}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(){return e.splice(0).forEach(t)};return r>0?n.autorunAsync(o,r):n.autorun(o)}var n=r(1);t.queueProcessor=o}])});

10

package.json
{
"name": "mobx-utils",
"version": "0.1.2",
"version": "0.1.3",
"description": "Utility functions for MobX",

@@ -8,4 +8,4 @@ "main": "lib/mobx-utils.js",

"scripts": {
"build": "tsc -p src && npm run browserify",
"browserify": "browserify --entry lib/mobx-utils.js --external mobx --standalone mobxUtils --outfile mobx-utils.umd.js",
"build": "tsc -p src && npm run webpack",
"webpack": "webpack -p",
"test": "npm run build && tape test/*.js | faucet",

@@ -31,3 +31,2 @@ "prepublish": "npm run build && npm run build-docs",

"devDependencies": {
"browserify": "^13.1.0",
"coveralls": "^2.11.4",

@@ -39,3 +38,4 @@ "documentation": "^4.0.0-beta9",

"tape": "^4.2.2",
"typescript": "^1.8.10"
"typescript": "^1.8.10",
"webpack": "^1.13.1"
},

@@ -42,0 +42,0 @@ "dependencies": {},

@@ -19,3 +19,3 @@ # MobX-utils

[lib/from-promise.js:53-57](https://github.com/mobxjs/mobx-utils/blob/6c0ce1200fbdb4514cd36905a070e4a60c98b868/lib/from-promise.js#L53-L57 "Source code on GitHub")
[lib/from-promise.js:53-57](https://github.com/mobxjs/mobx-utils/blob/7cf95e0302a17be8b373378094750c8492a6331f/lib/from-promise.js#L53-L57 "Source code on GitHub")

@@ -32,3 +32,3 @@ **Parameters**

[lib/guarded-when.js:32-51](https://github.com/mobxjs/mobx-utils/blob/6c0ce1200fbdb4514cd36905a070e4a60c98b868/lib/guarded-when.js#L32-L51 "Source code on GitHub")
[lib/guarded-when.js:32-51](https://github.com/mobxjs/mobx-utils/blob/7cf95e0302a17be8b373378094750c8492a6331f/lib/guarded-when.js#L32-L51 "Source code on GitHub")

@@ -69,3 +69,3 @@ 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:35-40](https://github.com/mobxjs/mobx-utils/blob/6c0ce1200fbdb4514cd36905a070e4a60c98b868/lib/keep-alive.js#L35-L40 "Source code on GitHub")
[lib/keep-alive.js:35-40](https://github.com/mobxjs/mobx-utils/blob/7cf95e0302a17be8b373378094750c8492a6331f/lib/keep-alive.js#L35-L40 "Source code on GitHub")

@@ -97,3 +97,3 @@ MobX normally suspends any computed value that is not in use by any reaction,

[lib/keep-alive.js:35-40](https://github.com/mobxjs/mobx-utils/blob/6c0ce1200fbdb4514cd36905a070e4a60c98b868/lib/keep-alive.js#L35-L40 "Source code on GitHub")
[lib/keep-alive.js:35-40](https://github.com/mobxjs/mobx-utils/blob/7cf95e0302a17be8b373378094750c8492a6331f/lib/keep-alive.js#L35-L40 "Source code on GitHub")

@@ -125,3 +125,3 @@ MobX normally suspends any computed value that is not in use by any reaction,

[lib/from-resource.js:60-92](https://github.com/mobxjs/mobx-utils/blob/6c0ce1200fbdb4514cd36905a070e4a60c98b868/lib/from-resource.js#L60-L92 "Source code on GitHub")
[lib/from-resource.js:60-92](https://github.com/mobxjs/mobx-utils/blob/7cf95e0302a17be8b373378094750c8492a6331f/lib/from-resource.js#L60-L92 "Source code on GitHub")

@@ -128,0 +128,0 @@ `fromResource` creates an observable which current state can be inspected using `.get()`,

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc