context 3.0.0-dev-040354 → 3.0.0-dev-781e21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -75,7 +75,7 @@ The initializer receives the context object, and the parent context object, if p
75
75
  `createCascade` returns an object containing the following functions:
76
76
 
77
77
  - `use`: Returns the current context value.
78
- - `useX`: Returns the current context, throws an error if not within a running context or the context is undefined.
78
+ - `useX`: Returns the current context, throws an error if not within a running context.
79
79
  - `run`: Runs the context, passing the given value into the context. Merges the given value with the parent context if it exists, while not overriding the parent context.
80
80
  - `bind`: Binds a given function to the context. Allows for delayd execution of a function as if it was called within the context.
81
81
 
@@ -4,28 +4,32 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var vestUtils = require('vest-utils');
6
6
 
7
+ var USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';
8
+ var EMPTY_CONTEXT = Symbol();
7
9
  function createContext(defaultContextValue) {
8
- var contextValue = undefined;
10
+ var contextValue = EMPTY_CONTEXT;
9
11
  return {
10
12
  run: run,
11
13
  use: use,
12
14
  useX: useX
13
15
  };
14
16
  function use() {
15
- return vestUtils.defaultTo(contextValue, defaultContextValue);
17
+ return (isInsideContext() ? contextValue : defaultContextValue);
16
18
  }
17
19
  function useX(errorMessage) {
18
- var contextValue = use();
19
- vestUtils.invariant(contextValue, vestUtils.defaultTo(errorMessage, 'Context was used after it was closed'));
20
+ vestUtils.invariant(isInsideContext(), vestUtils.defaultTo(errorMessage, USEX_DEFAULT_ERROR_MESSAGE));
20
21
  return contextValue;
21
22
  }
22
23
  function run(value, cb) {
23
- var parentContext = use();
24
+ var parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
24
25
  contextValue = value;
25
26
  var res = cb();
26
27
  contextValue = parentContext;
27
28
  return res;
28
29
  }
30
+ function isInsideContext() {
31
+ return contextValue !== EMPTY_CONTEXT;
32
+ }
29
33
  }
30
34
  function createCascade(init) {
31
35
  var ctx = createContext();
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("vest-utils");function n(n){function r(){return e.defaultTo(t,n)}var t=void 0;return{run:function(e,n){var u=r();return t=e,e=n(),t=u,e},use:r,useX:function(n){var t=r();return e.invariant(t,e.defaultTo(n,"Context was used after it was closed")),t}}}exports.createCascade=function(r){function t(n,t){var o,a=u.use();return n=e.assign({},a||{},null!==(o=e.optionalFunctionValue(r,n,a))&&void 0!==o?o:n),u.run(Object.freeze(n),t)}var u=n();return{bind:function(e,n){return function(){for(var r=[],u=0;u<arguments.length;u++)r[u]=arguments[u];return t(e,(function(){return n.apply(void 0,r)}))}},run:t,use:u.use,useX:u.useX}},exports.createContext=n;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var n=require("vest-utils"),e=Symbol();function r(r){function t(){return u!==e?u:r}var u=e;return{run:function(n,r){var o=u!==e?t():e;return u=n,n=r(),u=o,n},use:t,useX:function(r){return n.invariant(u!==e,n.defaultTo(r,"Not inside of a running context.")),u}}}exports.createCascade=function(e){function t(r,t){var o,i=u.use();return r=n.assign({},i||{},null!==(o=n.optionalFunctionValue(e,r,i))&&void 0!==o?o:r),u.run(Object.freeze(r),t)}var u=r();return{bind:function(n,e){return function(){for(var r=[],u=0;u<arguments.length;u++)r[u]=arguments[u];return t(n,(function(){return e.apply(void 0,r)}))}},run:t,use:u.use,useX:u.useX}},exports.createContext=r;
@@ -1,27 +1,31 @@
1
- import { defaultTo, invariant, assign, optionalFunctionValue } from 'vest-utils';
1
+ import { invariant, defaultTo, assign, optionalFunctionValue } from 'vest-utils';
2
2
 
3
+ var USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';
4
+ var EMPTY_CONTEXT = Symbol();
3
5
  function createContext(defaultContextValue) {
4
- var contextValue = undefined;
6
+ var contextValue = EMPTY_CONTEXT;
5
7
  return {
6
8
  run: run,
7
9
  use: use,
8
10
  useX: useX
9
11
  };
10
12
  function use() {
11
- return defaultTo(contextValue, defaultContextValue);
13
+ return (isInsideContext() ? contextValue : defaultContextValue);
12
14
  }
13
15
  function useX(errorMessage) {
14
- var contextValue = use();
15
- invariant(contextValue, defaultTo(errorMessage, 'Context was used after it was closed'));
16
+ invariant(isInsideContext(), defaultTo(errorMessage, USEX_DEFAULT_ERROR_MESSAGE));
16
17
  return contextValue;
17
18
  }
18
19
  function run(value, cb) {
19
- var parentContext = use();
20
+ var parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
20
21
  contextValue = value;
21
22
  var res = cb();
22
23
  contextValue = parentContext;
23
24
  return res;
24
25
  }
26
+ function isInsideContext() {
27
+ return contextValue !== EMPTY_CONTEXT;
28
+ }
25
29
  }
26
30
  function createCascade(init) {
27
31
  var ctx = createContext();
@@ -1 +1 @@
1
- import{defaultTo as n,invariant as r,assign as t,optionalFunctionValue as u}from"vest-utils";function e(t){function u(){return n(e,t)}var e=void 0;return{run:function(n,r){var t=u();return e=n,n=r(),e=t,n},use:u,useX:function(t){var e=u();return r(e,n(t,"Context was used after it was closed")),e}}}function o(n){function r(r,e){var i,s=o.use();return r=t({},s||{},null!==(i=u(n,r,s))&&void 0!==i?i:r),o.run(Object.freeze(r),e)}var o=e();return{bind:function(n,t){return function(){for(var u=[],e=0;e<arguments.length;e++)u[e]=arguments[e];return r(n,(function(){return t.apply(void 0,u)}))}},run:r,use:o.use,useX:o.useX}}export{o as createCascade,e as createContext};
1
+ import{invariant as n,defaultTo as r,assign as u,optionalFunctionValue as t}from"vest-utils";var e=Symbol();function o(u){function t(){return o!==e?o:u}var o=e;return{run:function(n,r){var u=o!==e?t():e;return o=n,n=r(),o=u,n},use:t,useX:function(u){return n(o!==e,r(u,"Not inside of a running context.")),o}}}function i(n){function r(r,o){var i,f=e.use();return r=u({},f||{},null!==(i=t(n,r,f))&&void 0!==i?i:r),e.run(Object.freeze(r),o)}var e=o();return{bind:function(n,u){return function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return r(n,(function(){return u.apply(void 0,t)}))}},run:r,use:e.use,useX:e.useX}}export{i as createCascade,o as createContext};
@@ -4,28 +4,32 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.context = {}, global['vest-utils']));
5
5
  }(this, (function (exports, vestUtils) { 'use strict';
6
6
 
7
+ var USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';
8
+ var EMPTY_CONTEXT = Symbol();
7
9
  function createContext(defaultContextValue) {
8
- var contextValue = undefined;
10
+ var contextValue = EMPTY_CONTEXT;
9
11
  return {
10
12
  run: run,
11
13
  use: use,
12
14
  useX: useX
13
15
  };
14
16
  function use() {
15
- return vestUtils.defaultTo(contextValue, defaultContextValue);
17
+ return (isInsideContext() ? contextValue : defaultContextValue);
16
18
  }
17
19
  function useX(errorMessage) {
18
- var contextValue = use();
19
- vestUtils.invariant(contextValue, vestUtils.defaultTo(errorMessage, 'Context was used after it was closed'));
20
+ vestUtils.invariant(isInsideContext(), vestUtils.defaultTo(errorMessage, USEX_DEFAULT_ERROR_MESSAGE));
20
21
  return contextValue;
21
22
  }
22
23
  function run(value, cb) {
23
- var parentContext = use();
24
+ var parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
24
25
  contextValue = value;
25
26
  var res = cb();
26
27
  contextValue = parentContext;
27
28
  return res;
28
29
  }
30
+ function isInsideContext() {
31
+ return contextValue !== EMPTY_CONTEXT;
32
+ }
29
33
  }
30
34
  function createCascade(init) {
31
35
  var ctx = createContext();
@@ -1 +1 @@
1
- "use strict";!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("vest-utils")):"function"==typeof define&&define.amd?define(["exports","vest-utils"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).context={},e["vest-utils"])}(this,(function(e,t){function n(e){function n(){return t.defaultTo(u,e)}var u=void 0;return{run:function(e,t){var r=n();return u=e,e=t(),u=r,e},use:n,useX:function(e){var u=n();return t.invariant(u,t.defaultTo(e,"Context was used after it was closed")),u}}}e.createCascade=function(e){function u(n,u){var o,i=r.use();return n=t.assign({},i||{},null!==(o=t.optionalFunctionValue(e,n,i))&&void 0!==o?o:n),r.run(Object.freeze(n),u)}var r=n();return{bind:function(e,t){return function(){for(var n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];return u(e,(function(){return t.apply(void 0,n)}))}},run:u,use:r.use,useX:r.useX}},e.createContext=n,Object.defineProperty(e,"__esModule",{value:!0})}));
1
+ "use strict";!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("vest-utils")):"function"==typeof define&&define.amd?define(["exports","vest-utils"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).context={},e["vest-utils"])}(this,(function(e,n){function t(e){function t(){return r!==u?r:e}var r=u;return{run:function(e,n){var o=r!==u?t():u;return r=e,e=n(),r=o,e},use:t,useX:function(e){return n.invariant(r!==u,n.defaultTo(e,"Not inside of a running context.")),r}}}var u=Symbol();e.createCascade=function(e){function u(t,u){var o,i=r.use();return t=n.assign({},i||{},null!==(o=n.optionalFunctionValue(e,t,i))&&void 0!==o?o:t),r.run(Object.freeze(t),u)}var r=t();return{bind:function(e,n){return function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return u(e,(function(){return n.apply(void 0,t)}))}},run:u,use:r.use,useX:r.useX}},e.createContext=t,Object.defineProperty(e,"__esModule",{value:!0})}));
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "3.0.0-dev-040354",
2
+ "version": "3.0.0-dev-781e21",
3
3
  "license": "MIT",
4
4
  "main": "./dist/cjs/context.js",
5
5
  "types": "./types/context.d.ts",