context 3.0.3 → 3.0.5-dev-405df5

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
@@ -1,4 +1,6 @@
1
- # Context
1
+ # Context 🪆
2
+
3
+ [![Join Discord](https://badgen.net/discord/online-members/WmADZpJnSe?icon=discord&label=Discord)](https://discord.gg/WmADZpJnSe) [![Version](https://badgen.net/npm/v/vest?&icon=npm)](https://www.npmjs.com/package/context) [![Downloads](https://badgen.net/npm/dt/context?label=Downloads)](https://www.npmjs.com/package/context) [![bundlephobia](https://badgen.net/bundlephobia/minzip/context)](https://bundlephobia.com/package/context) [![Status](https://badgen.net/github/status/ealush/vest)](https://github.com/ealush/vest/actions)
2
4
 
3
5
  Simple utility for context propagation within Javascript applications and libraries. Loosely based on the ideas behind React's context, allows you to achieve the same goals (and more) without actually using react.
4
6
  It allows you to keep reference for shared variables, and access them down in your function call even if not declared in the same scope.
@@ -4,14 +4,17 @@ 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
+ const USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';
8
+ const EMPTY_CONTEXT = Symbol();
9
+ /**
10
+ * Base context interface.
11
+ */
9
12
  function createContext(defaultContextValue) {
10
- var contextValue = EMPTY_CONTEXT;
13
+ let contextValue = EMPTY_CONTEXT;
11
14
  return {
12
- run: run,
13
- use: use,
14
- useX: useX
15
+ run,
16
+ use,
17
+ useX,
15
18
  };
16
19
  function use() {
17
20
  return (isInsideContext() ? contextValue : defaultContextValue);
@@ -21,9 +24,9 @@ function createContext(defaultContextValue) {
21
24
  return contextValue;
22
25
  }
23
26
  function run(value, cb) {
24
- var parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
27
+ const parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
25
28
  contextValue = value;
26
- var res = cb();
29
+ const res = cb();
27
30
  contextValue = parentContext;
28
31
  return res;
29
32
  }
@@ -31,28 +34,28 @@ function createContext(defaultContextValue) {
31
34
  return contextValue !== EMPTY_CONTEXT;
32
35
  }
33
36
  }
37
+ /**
38
+ * Cascading context - another implementation of context, that assumes the context value is an object.
39
+ * When nesting context runs, the the values of the current layer merges with the layers above it.
40
+ */
34
41
  function createCascade(init) {
35
- var ctx = createContext();
42
+ const ctx = createContext();
36
43
  return {
37
- bind: bind,
38
- run: run,
44
+ bind,
45
+ run,
39
46
  use: ctx.use,
40
- useX: ctx.useX
47
+ useX: ctx.useX,
41
48
  };
42
49
  function run(value, fn) {
43
50
  var _a;
44
- var parentContext = ctx.use();
45
- var out = vestUtils.assign({}, parentContext ? parentContext : {}, (_a = vestUtils.optionalFunctionValue(init, value, parentContext)) !== null && _a !== void 0 ? _a : value);
51
+ const parentContext = ctx.use();
52
+ const out = vestUtils.assign({}, parentContext ? parentContext : {}, (_a = vestUtils.optionalFunctionValue(init, value, parentContext)) !== null && _a !== void 0 ? _a : value);
46
53
  return ctx.run(Object.freeze(out), fn);
47
54
  }
48
55
  function bind(value, fn) {
49
- return function () {
50
- var runTimeArgs = [];
51
- for (var _i = 0; _i < arguments.length; _i++) {
52
- runTimeArgs[_i] = arguments[_i];
53
- }
56
+ return function (...runTimeArgs) {
54
57
  return run(value, function () {
55
- return fn.apply(void 0, runTimeArgs);
58
+ return fn(...runTimeArgs);
56
59
  });
57
60
  };
58
61
  }
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var n=require("vest-utils"),e=Symbol();function r(r){var t=e;return{run:function(n,r){var i=o()?u():e;t=n;var a=r();return t=i,a},use:u,useX:function(e){return n.invariant(o(),n.defaultTo(e,"Not inside of a running context.")),t}};function u(){return o()?t:r}function o(){return t!==e}}exports.createCascade=function(e){var t=r();return{bind:function(n,e){return function(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];return u(n,(function(){return e.apply(void 0,r)}))}},run:u,use:t.use,useX:t.useX};function u(r,u){var o,i=t.use(),a=n.assign({},i||{},null!==(o=n.optionalFunctionValue(e,r,i))&&void 0!==o?o:r);return t.run(Object.freeze(a),u)}},exports.createContext=r;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var n=require("vest-utils");const t=Symbol();function e(e){let u=t;return{run:function(n,e){const i=o()?r():t;u=n;const s=e();return u=i,s},use:r,useX:function(t){return n.invariant(o(),n.defaultTo(t,"Not inside of a running context.")),u}};function r(){return o()?u:e}function o(){return u!==t}}exports.createCascade=function(t){const u=e();return{bind:function(n,t){return function(...e){return r(n,(function(){return t(...e)}))}},run:r,use:u.use,useX:u.useX};function r(e,r){var o;const i=u.use(),s=n.assign({},i||{},null!==(o=n.optionalFunctionValue(t,e,i))&&void 0!==o?o:e);return u.run(Object.freeze(s),r)}},exports.createContext=e;
@@ -1,13 +1,16 @@
1
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
+ const USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';
4
+ const EMPTY_CONTEXT = Symbol();
5
+ /**
6
+ * Base context interface.
7
+ */
5
8
  function createContext(defaultContextValue) {
6
- var contextValue = EMPTY_CONTEXT;
9
+ let contextValue = EMPTY_CONTEXT;
7
10
  return {
8
- run: run,
9
- use: use,
10
- useX: useX
11
+ run,
12
+ use,
13
+ useX,
11
14
  };
12
15
  function use() {
13
16
  return (isInsideContext() ? contextValue : defaultContextValue);
@@ -17,9 +20,9 @@ function createContext(defaultContextValue) {
17
20
  return contextValue;
18
21
  }
19
22
  function run(value, cb) {
20
- var parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
23
+ const parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
21
24
  contextValue = value;
22
- var res = cb();
25
+ const res = cb();
23
26
  contextValue = parentContext;
24
27
  return res;
25
28
  }
@@ -27,28 +30,28 @@ function createContext(defaultContextValue) {
27
30
  return contextValue !== EMPTY_CONTEXT;
28
31
  }
29
32
  }
33
+ /**
34
+ * Cascading context - another implementation of context, that assumes the context value is an object.
35
+ * When nesting context runs, the the values of the current layer merges with the layers above it.
36
+ */
30
37
  function createCascade(init) {
31
- var ctx = createContext();
38
+ const ctx = createContext();
32
39
  return {
33
- bind: bind,
34
- run: run,
40
+ bind,
41
+ run,
35
42
  use: ctx.use,
36
- useX: ctx.useX
43
+ useX: ctx.useX,
37
44
  };
38
45
  function run(value, fn) {
39
46
  var _a;
40
- var parentContext = ctx.use();
41
- var out = assign({}, parentContext ? parentContext : {}, (_a = optionalFunctionValue(init, value, parentContext)) !== null && _a !== void 0 ? _a : value);
47
+ const parentContext = ctx.use();
48
+ const out = assign({}, parentContext ? parentContext : {}, (_a = optionalFunctionValue(init, value, parentContext)) !== null && _a !== void 0 ? _a : value);
42
49
  return ctx.run(Object.freeze(out), fn);
43
50
  }
44
51
  function bind(value, fn) {
45
- return function () {
46
- var runTimeArgs = [];
47
- for (var _i = 0; _i < arguments.length; _i++) {
48
- runTimeArgs[_i] = arguments[_i];
49
- }
52
+ return function (...runTimeArgs) {
50
53
  return run(value, function () {
51
- return fn.apply(void 0, runTimeArgs);
54
+ return fn(...runTimeArgs);
52
55
  });
53
56
  };
54
57
  }
@@ -1 +1 @@
1
- import{invariant as n,defaultTo as r,assign as u,optionalFunctionValue as t}from"vest-utils";var e=Symbol();function o(u){var t=e;return{run:function(n,r){var u=i()?o():e;t=n;var f=r();return t=u,f},use:o,useX:function(u){return n(i(),r(u,"Not inside of a running context.")),t}};function o(){return i()?t:u}function i(){return t!==e}}function i(n){var r=o();return{bind:function(n,r){return function(){for(var u=[],t=0;t<arguments.length;t++)u[t]=arguments[t];return e(n,(function(){return r.apply(void 0,u)}))}},run:e,use:r.use,useX:r.useX};function e(e,o){var i,f=r.use(),c=u({},f||{},null!==(i=t(n,e,f))&&void 0!==i?i:e);return r.run(Object.freeze(c),o)}}export{i as createCascade,o as createContext};
1
+ import{invariant as n,defaultTo as t,assign as u,optionalFunctionValue as r}from"vest-utils";const e=Symbol();function o(u){let r=e;return{run:function(n,t){const u=c()?o():e;r=n;const i=t();return r=u,i},use:o,useX:function(u){return n(c(),t(u,"Not inside of a running context.")),r}};function o(){return c()?r:u}function c(){return r!==e}}function c(n){const t=o();return{bind:function(n,t){return function(...u){return e(n,(function(){return t(...u)}))}},run:e,use:t.use,useX:t.useX};function e(e,o){var c;const i=t.use(),s=u({},i||{},null!==(c=r(n,e,i))&&void 0!==c?c:e);return t.run(Object.freeze(s),o)}}export{c as createCascade,o as createContext};
@@ -4,14 +4,17 @@
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
+ const USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';
8
+ const EMPTY_CONTEXT = Symbol();
9
+ /**
10
+ * Base context interface.
11
+ */
9
12
  function createContext(defaultContextValue) {
10
- var contextValue = EMPTY_CONTEXT;
13
+ let contextValue = EMPTY_CONTEXT;
11
14
  return {
12
- run: run,
13
- use: use,
14
- useX: useX
15
+ run,
16
+ use,
17
+ useX,
15
18
  };
16
19
  function use() {
17
20
  return (isInsideContext() ? contextValue : defaultContextValue);
@@ -21,9 +24,9 @@
21
24
  return contextValue;
22
25
  }
23
26
  function run(value, cb) {
24
- var parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
27
+ const parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
25
28
  contextValue = value;
26
- var res = cb();
29
+ const res = cb();
27
30
  contextValue = parentContext;
28
31
  return res;
29
32
  }
@@ -31,28 +34,28 @@
31
34
  return contextValue !== EMPTY_CONTEXT;
32
35
  }
33
36
  }
37
+ /**
38
+ * Cascading context - another implementation of context, that assumes the context value is an object.
39
+ * When nesting context runs, the the values of the current layer merges with the layers above it.
40
+ */
34
41
  function createCascade(init) {
35
- var ctx = createContext();
42
+ const ctx = createContext();
36
43
  return {
37
- bind: bind,
38
- run: run,
44
+ bind,
45
+ run,
39
46
  use: ctx.use,
40
- useX: ctx.useX
47
+ useX: ctx.useX,
41
48
  };
42
49
  function run(value, fn) {
43
50
  var _a;
44
- var parentContext = ctx.use();
45
- var out = vestUtils.assign({}, parentContext ? parentContext : {}, (_a = vestUtils.optionalFunctionValue(init, value, parentContext)) !== null && _a !== void 0 ? _a : value);
51
+ const parentContext = ctx.use();
52
+ const out = vestUtils.assign({}, parentContext ? parentContext : {}, (_a = vestUtils.optionalFunctionValue(init, value, parentContext)) !== null && _a !== void 0 ? _a : value);
46
53
  return ctx.run(Object.freeze(out), fn);
47
54
  }
48
55
  function bind(value, fn) {
49
- return function () {
50
- var runTimeArgs = [];
51
- for (var _i = 0; _i < arguments.length; _i++) {
52
- runTimeArgs[_i] = arguments[_i];
53
- }
56
+ return function (...runTimeArgs) {
54
57
  return run(value, function () {
55
- return fn.apply(void 0, runTimeArgs);
58
+ return fn(...runTimeArgs);
56
59
  });
57
60
  };
58
61
  }
@@ -1 +1 @@
1
- !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){"use strict";var t=Symbol();function u(e){var u=t;return{run:function(e,n){var i=o()?r():t;u=e;var f=n();return u=i,f},use:r,useX:function(e){return n.invariant(o(),n.defaultTo(e,"Not inside of a running context.")),u}};function r(){return o()?u:e}function o(){return u!==t}}e.createCascade=function(e){var t=u();return{bind:function(e,n){return function(){for(var t=[],u=0;u<arguments.length;u++)t[u]=arguments[u];return r(e,(function(){return n.apply(void 0,t)}))}},run:r,use:t.use,useX:t.useX};function r(u,r){var o,i=t.use(),f=n.assign({},i||{},null!==(o=n.optionalFunctionValue(e,u,i))&&void 0!==o?o:u);return t.run(Object.freeze(f),r)}},e.createContext=u,Object.defineProperty(e,"__esModule",{value:!0})}));
1
+ !function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("vest-utils")):"function"==typeof define&&define.amd?define(["exports","vest-utils"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).context={},n["vest-utils"])}(this,(function(n,e){"use strict";const t=Symbol();function u(n){let u=t;return{run:function(n,e){const r=i()?o():t;u=n;const s=e();return u=r,s},use:o,useX:function(n){return e.invariant(i(),e.defaultTo(n,"Not inside of a running context.")),u}};function o(){return i()?u:n}function i(){return u!==t}}n.createCascade=function(n){const t=u();return{bind:function(n,e){return function(...t){return o(n,(function(){return e(...t)}))}},run:o,use:t.use,useX:t.useX};function o(u,o){var i;const r=t.use(),s=e.assign({},r||{},null!==(i=e.optionalFunctionValue(n,u,r))&&void 0!==i?i:u);return t.run(Object.freeze(s),o)}},n.createContext=u,Object.defineProperty(n,"__esModule",{value:!0})}));
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "3.0.3",
2
+ "version": "3.0.5-dev-405df5",
3
3
  "license": "MIT",
4
4
  "main": "./dist/cjs/context.js",
5
5
  "types": "./types/context.d.ts",
@@ -11,7 +11,7 @@
11
11
  "release": "vx release"
12
12
  },
13
13
  "dependencies": {
14
- "vest-utils": "^0.0.3"
14
+ "vest-utils": "^0.1.0-dev-405df5"
15
15
  },
16
16
  "module": "./dist/es/context.production.js",
17
17
  "exports": {
@@ -36,7 +36,7 @@
36
36
  "default": "./dist/cjs/context.production.js"
37
37
  },
38
38
  "./package.json": "./package.json",
39
- "./": "./"
39
+ "./*": "./*"
40
40
  },
41
41
  "repository": {
42
42
  "type": "git",
@@ -1,5 +1,12 @@
1
1
  import { CB } from 'vest-utils';
2
+ /**
3
+ * Base context interface.
4
+ */
2
5
  declare function createContext<T extends unknown>(defaultContextValue?: T): CtxApi<T>;
6
+ /**
7
+ * Cascading context - another implementation of context, that assumes the context value is an object.
8
+ * When nesting context runs, the the values of the current layer merges with the layers above it.
9
+ */
3
10
  declare function createCascade<T extends Record<string, unknown>>(init?: (value: Partial<T>, parentContext: T | void) => T | null): CtxCascadeApi<T>;
4
11
  type ContextConsumptionApi<T> = {
5
12
  use: () => T;
@@ -1 +1 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,MAAW,MAAM,YAAY,CAAC;AAWrC,iBAAgB,aAAa,CAAC,CAAC,SAAS,OAAO,EAC7C,mBAAmB,CAAC,EAAE,CAAC,GACtB,MAAM,CAAC,CAAC,CAAC,CAmCX;AAED,iBAAgB,aAAa,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7D,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,CAAC,GAAG,IAAI,KAAK,CAAC,GAAG,IAAI,GAC9D,aAAa,CAAC,CAAC,CAAC,CA6BlB;AAED,KAAK,qBAAqB,CAAC,CAAC,IAAI;IAC9B,GAAG,EAAE,MAAM,CAAC,CAAC;IACb,IAAI,EAAE,CAAC,YAAY,CAAC,EAAE,MAAM,KAAK,CAAC,CAAC;CACpC,CAAC;AAEF,KAAY,MAAM,CAAC,CAAC,IAAI,qBAAqB,CAAC,CAAC,CAAC,GAAG;IACjD,GAAG,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;CACtC,CAAC;AAEF,KAAY,aAAa,CAAC,CAAC,IAAI,qBAAqB,CAAC,CAAC,CAAC,GAAG;IACxD,GAAG,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,MAA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