context 3.0.9-next-25c20e → 3.0.9-next-a084e9
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/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "3.0.9-next-
|
|
2
|
+
"version": "3.0.9-next-a084e9",
|
|
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": "1.0.0-next-
|
|
14
|
+
"vest-utils": "1.0.0-next-a084e9"
|
|
15
15
|
},
|
|
16
16
|
"module": "./dist/es/context.production.js",
|
|
17
17
|
"exports": {
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var vestUtils = require('vest-utils');
|
|
4
|
-
|
|
5
|
-
const USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';
|
|
6
|
-
const EMPTY_CONTEXT = Symbol();
|
|
7
|
-
/**
|
|
8
|
-
* Base context interface.
|
|
9
|
-
*/
|
|
10
|
-
function createContext(defaultContextValue) {
|
|
11
|
-
let contextValue = EMPTY_CONTEXT;
|
|
12
|
-
return {
|
|
13
|
-
run,
|
|
14
|
-
use,
|
|
15
|
-
useX,
|
|
16
|
-
};
|
|
17
|
-
function use() {
|
|
18
|
-
return (isInsideContext() ? contextValue : defaultContextValue);
|
|
19
|
-
}
|
|
20
|
-
function useX(errorMessage) {
|
|
21
|
-
vestUtils.invariant(isInsideContext(), vestUtils.defaultTo(errorMessage, USEX_DEFAULT_ERROR_MESSAGE));
|
|
22
|
-
return contextValue;
|
|
23
|
-
}
|
|
24
|
-
function run(value, cb) {
|
|
25
|
-
const parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
|
|
26
|
-
contextValue = value;
|
|
27
|
-
const res = cb();
|
|
28
|
-
contextValue = parentContext;
|
|
29
|
-
return res;
|
|
30
|
-
}
|
|
31
|
-
function isInsideContext() {
|
|
32
|
-
return contextValue !== EMPTY_CONTEXT;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Cascading context - another implementation of context, that assumes the context value is an object.
|
|
37
|
-
* When nesting context runs, the the values of the current layer merges with the layers above it.
|
|
38
|
-
*/
|
|
39
|
-
function createCascade(init) {
|
|
40
|
-
const ctx = createContext();
|
|
41
|
-
return {
|
|
42
|
-
bind,
|
|
43
|
-
run,
|
|
44
|
-
use: ctx.use,
|
|
45
|
-
useX: ctx.useX,
|
|
46
|
-
};
|
|
47
|
-
function run(value, fn) {
|
|
48
|
-
var _a;
|
|
49
|
-
const parentContext = ctx.use();
|
|
50
|
-
const out = vestUtils.assign({}, parentContext ? parentContext : {}, (_a = vestUtils.optionalFunctionValue(init, value, parentContext)) !== null && _a !== void 0 ? _a : value);
|
|
51
|
-
return ctx.run(Object.freeze(out), fn);
|
|
52
|
-
}
|
|
53
|
-
function bind(value, fn) {
|
|
54
|
-
return function (...runTimeArgs) {
|
|
55
|
-
return run(value, function () {
|
|
56
|
-
return fn(...runTimeArgs);
|
|
57
|
-
});
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
exports.createCascade = createCascade;
|
|
63
|
-
exports.createContext = createContext;
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { invariant, defaultTo, assign, optionalFunctionValue } from 'vest-utils';
|
|
2
|
-
|
|
3
|
-
const USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';
|
|
4
|
-
const EMPTY_CONTEXT = Symbol();
|
|
5
|
-
/**
|
|
6
|
-
* Base context interface.
|
|
7
|
-
*/
|
|
8
|
-
function createContext(defaultContextValue) {
|
|
9
|
-
let contextValue = EMPTY_CONTEXT;
|
|
10
|
-
return {
|
|
11
|
-
run,
|
|
12
|
-
use,
|
|
13
|
-
useX,
|
|
14
|
-
};
|
|
15
|
-
function use() {
|
|
16
|
-
return (isInsideContext() ? contextValue : defaultContextValue);
|
|
17
|
-
}
|
|
18
|
-
function useX(errorMessage) {
|
|
19
|
-
invariant(isInsideContext(), defaultTo(errorMessage, USEX_DEFAULT_ERROR_MESSAGE));
|
|
20
|
-
return contextValue;
|
|
21
|
-
}
|
|
22
|
-
function run(value, cb) {
|
|
23
|
-
const parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
|
|
24
|
-
contextValue = value;
|
|
25
|
-
const res = cb();
|
|
26
|
-
contextValue = parentContext;
|
|
27
|
-
return res;
|
|
28
|
-
}
|
|
29
|
-
function isInsideContext() {
|
|
30
|
-
return contextValue !== EMPTY_CONTEXT;
|
|
31
|
-
}
|
|
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
|
-
*/
|
|
37
|
-
function createCascade(init) {
|
|
38
|
-
const ctx = createContext();
|
|
39
|
-
return {
|
|
40
|
-
bind,
|
|
41
|
-
run,
|
|
42
|
-
use: ctx.use,
|
|
43
|
-
useX: ctx.useX,
|
|
44
|
-
};
|
|
45
|
-
function run(value, fn) {
|
|
46
|
-
var _a;
|
|
47
|
-
const parentContext = ctx.use();
|
|
48
|
-
const out = assign({}, parentContext ? parentContext : {}, (_a = optionalFunctionValue(init, value, parentContext)) !== null && _a !== void 0 ? _a : value);
|
|
49
|
-
return ctx.run(Object.freeze(out), fn);
|
|
50
|
-
}
|
|
51
|
-
function bind(value, fn) {
|
|
52
|
-
return function (...runTimeArgs) {
|
|
53
|
-
return run(value, function () {
|
|
54
|
-
return fn(...runTimeArgs);
|
|
55
|
-
});
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export { createCascade, createContext };
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vest-utils')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', 'vest-utils'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.context = {}, global["vest-utils"]));
|
|
5
|
-
})(this, (function (exports, vestUtils) { 'use strict';
|
|
6
|
-
|
|
7
|
-
const USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';
|
|
8
|
-
const EMPTY_CONTEXT = Symbol();
|
|
9
|
-
/**
|
|
10
|
-
* Base context interface.
|
|
11
|
-
*/
|
|
12
|
-
function createContext(defaultContextValue) {
|
|
13
|
-
let contextValue = EMPTY_CONTEXT;
|
|
14
|
-
return {
|
|
15
|
-
run,
|
|
16
|
-
use,
|
|
17
|
-
useX,
|
|
18
|
-
};
|
|
19
|
-
function use() {
|
|
20
|
-
return (isInsideContext() ? contextValue : defaultContextValue);
|
|
21
|
-
}
|
|
22
|
-
function useX(errorMessage) {
|
|
23
|
-
vestUtils.invariant(isInsideContext(), vestUtils.defaultTo(errorMessage, USEX_DEFAULT_ERROR_MESSAGE));
|
|
24
|
-
return contextValue;
|
|
25
|
-
}
|
|
26
|
-
function run(value, cb) {
|
|
27
|
-
const parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
|
|
28
|
-
contextValue = value;
|
|
29
|
-
const res = cb();
|
|
30
|
-
contextValue = parentContext;
|
|
31
|
-
return res;
|
|
32
|
-
}
|
|
33
|
-
function isInsideContext() {
|
|
34
|
-
return contextValue !== EMPTY_CONTEXT;
|
|
35
|
-
}
|
|
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
|
-
*/
|
|
41
|
-
function createCascade(init) {
|
|
42
|
-
const ctx = createContext();
|
|
43
|
-
return {
|
|
44
|
-
bind,
|
|
45
|
-
run,
|
|
46
|
-
use: ctx.use,
|
|
47
|
-
useX: ctx.useX,
|
|
48
|
-
};
|
|
49
|
-
function run(value, fn) {
|
|
50
|
-
var _a;
|
|
51
|
-
const parentContext = ctx.use();
|
|
52
|
-
const out = vestUtils.assign({}, parentContext ? parentContext : {}, (_a = vestUtils.optionalFunctionValue(init, value, parentContext)) !== null && _a !== void 0 ? _a : value);
|
|
53
|
-
return ctx.run(Object.freeze(out), fn);
|
|
54
|
-
}
|
|
55
|
-
function bind(value, fn) {
|
|
56
|
-
return function (...runTimeArgs) {
|
|
57
|
-
return run(value, function () {
|
|
58
|
-
return fn(...runTimeArgs);
|
|
59
|
-
});
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
exports.createCascade = createCascade;
|
|
65
|
-
exports.createContext = createContext;
|
|
66
|
-
|
|
67
|
-
}));
|