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 +3 -1
- package/dist/cjs/context.development.js +23 -20
- package/dist/cjs/context.production.js +1 -1
- package/dist/es/context.development.js +23 -20
- package/dist/es/context.production.js +1 -1
- package/dist/umd/context.development.js +23 -20
- package/dist/umd/context.production.js +1 -1
- package/package.json +3 -3
- package/types/context.d.ts +7 -0
- package/types/context.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
# Context
|
|
1
|
+
# Context 🪆
|
|
2
|
+
|
|
3
|
+
[](https://discord.gg/WmADZpJnSe) [](https://www.npmjs.com/package/context) [](https://www.npmjs.com/package/context) [](https://bundlephobia.com/package/context) [](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
|
-
|
|
8
|
-
|
|
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
|
-
|
|
13
|
+
let contextValue = EMPTY_CONTEXT;
|
|
11
14
|
return {
|
|
12
|
-
run
|
|
13
|
-
use
|
|
14
|
-
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
|
-
|
|
27
|
+
const parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
|
|
25
28
|
contextValue = value;
|
|
26
|
-
|
|
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
|
-
|
|
42
|
+
const ctx = createContext();
|
|
36
43
|
return {
|
|
37
|
-
bind
|
|
38
|
-
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
|
-
|
|
45
|
-
|
|
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
|
|
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")
|
|
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
|
-
|
|
4
|
-
|
|
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
|
-
|
|
9
|
+
let contextValue = EMPTY_CONTEXT;
|
|
7
10
|
return {
|
|
8
|
-
run
|
|
9
|
-
use
|
|
10
|
-
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
|
-
|
|
23
|
+
const parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
|
|
21
24
|
contextValue = value;
|
|
22
|
-
|
|
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
|
-
|
|
38
|
+
const ctx = createContext();
|
|
32
39
|
return {
|
|
33
|
-
bind
|
|
34
|
-
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
|
-
|
|
41
|
-
|
|
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
|
|
54
|
+
return fn(...runTimeArgs);
|
|
52
55
|
});
|
|
53
56
|
};
|
|
54
57
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{invariant as n,defaultTo as
|
|
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
|
-
|
|
8
|
-
|
|
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
|
-
|
|
13
|
+
let contextValue = EMPTY_CONTEXT;
|
|
11
14
|
return {
|
|
12
|
-
run
|
|
13
|
-
use
|
|
14
|
-
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
|
-
|
|
27
|
+
const parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
|
|
25
28
|
contextValue = value;
|
|
26
|
-
|
|
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
|
-
|
|
42
|
+
const ctx = createContext();
|
|
36
43
|
return {
|
|
37
|
-
bind
|
|
38
|
-
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
|
-
|
|
45
|
-
|
|
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
|
|
58
|
+
return fn(...runTimeArgs);
|
|
56
59
|
});
|
|
57
60
|
};
|
|
58
61
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e
|
|
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.
|
|
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
|
|
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",
|
package/types/context.d.ts
CHANGED
|
@@ -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;
|
package/types/context.d.ts.map
CHANGED
|
@@ -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,MAAM,CAAC,KAAK,CAAC,CAAC;IAC9C,IAAI,EAAE,CAAC,EAAE,SAAS,EAAE,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC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