context 1.1.1 → 2.0.0-dev-e266d9
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/CHANGELOG.md +62 -0
- package/README.md +78 -372
- package/dist/cjs/context.development.js +81 -0
- package/dist/cjs/context.js +7 -0
- package/dist/cjs/context.production.js +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/es/context.development.js +77 -0
- package/dist/es/context.production.js +1 -0
- package/dist/es/package.json +1 -0
- package/dist/umd/context.development.js +87 -0
- package/dist/umd/context.production.js +1 -0
- package/package.json +37 -36
- package/tsconfig.json +8 -0
- package/types/context.d.ts +7 -0
- package/dist/context.cjs.development.js +0 -180
- package/dist/context.cjs.development.js.map +0 -1
- package/dist/context.cjs.production.min.js +0 -2
- package/dist/context.cjs.production.min.js.map +0 -1
- package/dist/context.esm.js +0 -176
- package/dist/context.esm.js.map +0 -1
- package/dist/index.d.ts +0 -39
- package/dist/index.js +0 -8
- package/src/index.ts +0 -173
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
var assign = Object.assign;
|
|
2
|
+
|
|
3
|
+
function isFunction(value) {
|
|
4
|
+
return typeof value === 'function';
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function optionalFunctionValue(value) {
|
|
8
|
+
var args = [];
|
|
9
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
10
|
+
args[_i - 1] = arguments[_i];
|
|
11
|
+
}
|
|
12
|
+
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function defaultTo(callback, defaultValue) {
|
|
16
|
+
var _a;
|
|
17
|
+
return (_a = optionalFunctionValue(callback)) !== null && _a !== void 0 ? _a : defaultValue;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Throws a timed out error.
|
|
22
|
+
*/
|
|
23
|
+
function throwError(devMessage, productionMessage) {
|
|
24
|
+
throw new Error(devMessage );
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// eslint-disable-next-line max-lines-per-function
|
|
28
|
+
function createContext(init) {
|
|
29
|
+
var storage = { ancestry: [] };
|
|
30
|
+
return {
|
|
31
|
+
bind: bind,
|
|
32
|
+
run: run,
|
|
33
|
+
use: use,
|
|
34
|
+
useX: useX
|
|
35
|
+
};
|
|
36
|
+
function useX(errorMessage) {
|
|
37
|
+
var _a;
|
|
38
|
+
return ((_a = storage.ctx) !== null && _a !== void 0 ? _a : throwError(defaultTo(errorMessage, 'Context was used after it was closed')));
|
|
39
|
+
}
|
|
40
|
+
function run(ctxRef, fn) {
|
|
41
|
+
var _a;
|
|
42
|
+
var parentContext = use();
|
|
43
|
+
var out = assign({}, parentContext ? parentContext : {}, (_a = optionalFunctionValue(init, ctxRef, parentContext)) !== null && _a !== void 0 ? _a : ctxRef);
|
|
44
|
+
var ctx = set(Object.freeze(out));
|
|
45
|
+
storage.ancestry.unshift(ctx);
|
|
46
|
+
var res = fn(ctx);
|
|
47
|
+
clear();
|
|
48
|
+
return res;
|
|
49
|
+
}
|
|
50
|
+
function bind(ctxRef, fn) {
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
52
|
+
// @ts-ignore - this one's pretty hard to get right
|
|
53
|
+
var returnedFn = function () {
|
|
54
|
+
var runTimeArgs = [];
|
|
55
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
56
|
+
runTimeArgs[_i] = arguments[_i];
|
|
57
|
+
}
|
|
58
|
+
return run(ctxRef, function () {
|
|
59
|
+
return fn.apply(void 0, runTimeArgs);
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
return returnedFn;
|
|
63
|
+
}
|
|
64
|
+
function use() {
|
|
65
|
+
return storage.ctx;
|
|
66
|
+
}
|
|
67
|
+
function set(value) {
|
|
68
|
+
return (storage.ctx = value);
|
|
69
|
+
}
|
|
70
|
+
function clear() {
|
|
71
|
+
var _a;
|
|
72
|
+
storage.ancestry.shift();
|
|
73
|
+
set((_a = storage.ancestry[0]) !== null && _a !== void 0 ? _a : null);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { createContext };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var n=Object.assign;function t(n){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return"function"==typeof n?n.apply(void 0,t):n}function r(n,r){var e;return null!==(e=t(n))&&void 0!==e?e:r}export function createContext(e){function u(r,u){var c,a,f=o();return r=n({},f||{},null!==(c=t(e,r,f))&&void 0!==c?c:r),c=i.ctx=Object.freeze(r),i.ancestry.unshift(c),u=u(c),i.ancestry.shift(),i.ctx=null!==(a=i.ancestry[0])&&void 0!==a?a:null,u}function o(){return i.ctx}var i={ancestry:[]};return{bind:function(n,t){return function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];return u(n,(function(){return t.apply(void 0,r)}))}},run:u,use:o,useX:function(n){var t;if(null===(t=i.ctx)||void 0===t)throw n=r(n,"Context was used after it was closed"),Error(r(void 0,n));return t}}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.context = {}));
|
|
5
|
+
}(this, (function (exports) { 'use strict';
|
|
6
|
+
|
|
7
|
+
var assign = Object.assign;
|
|
8
|
+
|
|
9
|
+
function isFunction(value) {
|
|
10
|
+
return typeof value === 'function';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function optionalFunctionValue(value) {
|
|
14
|
+
var args = [];
|
|
15
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
16
|
+
args[_i - 1] = arguments[_i];
|
|
17
|
+
}
|
|
18
|
+
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function defaultTo(callback, defaultValue) {
|
|
22
|
+
var _a;
|
|
23
|
+
return (_a = optionalFunctionValue(callback)) !== null && _a !== void 0 ? _a : defaultValue;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Throws a timed out error.
|
|
28
|
+
*/
|
|
29
|
+
function throwError(devMessage, productionMessage) {
|
|
30
|
+
throw new Error(devMessage );
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// eslint-disable-next-line max-lines-per-function
|
|
34
|
+
function createContext(init) {
|
|
35
|
+
var storage = { ancestry: [] };
|
|
36
|
+
return {
|
|
37
|
+
bind: bind,
|
|
38
|
+
run: run,
|
|
39
|
+
use: use,
|
|
40
|
+
useX: useX
|
|
41
|
+
};
|
|
42
|
+
function useX(errorMessage) {
|
|
43
|
+
var _a;
|
|
44
|
+
return ((_a = storage.ctx) !== null && _a !== void 0 ? _a : throwError(defaultTo(errorMessage, 'Context was used after it was closed')));
|
|
45
|
+
}
|
|
46
|
+
function run(ctxRef, fn) {
|
|
47
|
+
var _a;
|
|
48
|
+
var parentContext = use();
|
|
49
|
+
var out = assign({}, parentContext ? parentContext : {}, (_a = optionalFunctionValue(init, ctxRef, parentContext)) !== null && _a !== void 0 ? _a : ctxRef);
|
|
50
|
+
var ctx = set(Object.freeze(out));
|
|
51
|
+
storage.ancestry.unshift(ctx);
|
|
52
|
+
var res = fn(ctx);
|
|
53
|
+
clear();
|
|
54
|
+
return res;
|
|
55
|
+
}
|
|
56
|
+
function bind(ctxRef, fn) {
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
58
|
+
// @ts-ignore - this one's pretty hard to get right
|
|
59
|
+
var returnedFn = function () {
|
|
60
|
+
var runTimeArgs = [];
|
|
61
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
62
|
+
runTimeArgs[_i] = arguments[_i];
|
|
63
|
+
}
|
|
64
|
+
return run(ctxRef, function () {
|
|
65
|
+
return fn.apply(void 0, runTimeArgs);
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
return returnedFn;
|
|
69
|
+
}
|
|
70
|
+
function use() {
|
|
71
|
+
return storage.ctx;
|
|
72
|
+
}
|
|
73
|
+
function set(value) {
|
|
74
|
+
return (storage.ctx = value);
|
|
75
|
+
}
|
|
76
|
+
function clear() {
|
|
77
|
+
var _a;
|
|
78
|
+
storage.ancestry.shift();
|
|
79
|
+
set((_a = storage.ancestry[0]) !== null && _a !== void 0 ? _a : null);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
exports.createContext = createContext;
|
|
84
|
+
|
|
85
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
86
|
+
|
|
87
|
+
})));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).context={})}(this,(function(n){function t(n){for(var t=[],e=1;e<arguments.length;e++)t[e-1]=arguments[e];return"function"==typeof n?n.apply(void 0,t):n}function e(n,e){var r;return null!==(r=t(n))&&void 0!==r?r:e}var r=Object.assign;n.createContext=function(n){function o(e,o){var f,c,s=u();return e=r({},s||{},null!==(f=t(n,e,s))&&void 0!==f?f:e),f=i.ctx=Object.freeze(e),i.ancestry.unshift(f),o=o(f),i.ancestry.shift(),i.ctx=null!==(c=i.ancestry[0])&&void 0!==c?c:null,o}function u(){return i.ctx}var i={ancestry:[]};return{bind:function(n,t){return function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return o(n,(function(){return t.apply(void 0,e)}))}},run:o,use:u,useX:function(n){var t;if(null===(t=i.ctx)||void 0===t)throw n=e(n,"Context was used after it was closed"),Error(e(void 0,n));return t}}},Object.defineProperty(n,"__esModule",{value:!0})}));
|
package/package.json
CHANGED
|
@@ -1,45 +1,46 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "
|
|
2
|
+
"version": "2.0.0-dev-e266d9",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"main": "dist/
|
|
5
|
-
"
|
|
6
|
-
"files": [
|
|
7
|
-
"dist",
|
|
8
|
-
"src"
|
|
9
|
-
],
|
|
10
|
-
"engines": {
|
|
11
|
-
"node": ">=10"
|
|
12
|
-
},
|
|
13
|
-
"scripts": {
|
|
14
|
-
"start": "tsdx watch",
|
|
15
|
-
"build": "tsdx build",
|
|
16
|
-
"test": "tsdx test",
|
|
17
|
-
"lint": "tsdx lint",
|
|
18
|
-
"prepare": "tsdx build"
|
|
19
|
-
},
|
|
20
|
-
"peerDependencies": {},
|
|
21
|
-
"husky": {
|
|
22
|
-
"hooks": {
|
|
23
|
-
"pre-commit": "tsdx lint"
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
"prettier": {
|
|
27
|
-
"printWidth": 80,
|
|
28
|
-
"semi": true,
|
|
29
|
-
"singleQuote": true,
|
|
30
|
-
"trailingComma": "es5"
|
|
31
|
-
},
|
|
4
|
+
"main": "./dist/cjs/context.js",
|
|
5
|
+
"types": "./types/context.d.ts",
|
|
32
6
|
"name": "context",
|
|
33
7
|
"author": "ealush",
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "vx test",
|
|
10
|
+
"build": "vx build",
|
|
11
|
+
"release": "vx release"
|
|
12
|
+
},
|
|
13
|
+
"module": "./dist/es/context.production.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"development": {
|
|
17
|
+
"types": "./types/context.d.ts",
|
|
18
|
+
"browser": "./dist/es/context.development.js",
|
|
19
|
+
"umd": "./dist/umd/context.development.js",
|
|
20
|
+
"import": "./dist/es/context.development.js",
|
|
21
|
+
"require": "./dist/cjs/context.development.js",
|
|
22
|
+
"node": "./dist/cjs/context.development.js",
|
|
23
|
+
"module": "./dist/es/context.development.js",
|
|
24
|
+
"default": "./dist/cjs/context.development.js"
|
|
25
|
+
},
|
|
26
|
+
"types": "./types/context.d.ts",
|
|
27
|
+
"browser": "./dist/es/context.production.js",
|
|
28
|
+
"umd": "./dist/umd/context.production.js",
|
|
29
|
+
"import": "./dist/es/context.production.js",
|
|
30
|
+
"require": "./dist/cjs/context.production.js",
|
|
31
|
+
"node": "./dist/cjs/context.production.js",
|
|
32
|
+
"module": "./dist/es/context.production.js",
|
|
33
|
+
"default": "./dist/cjs/context.production.js"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json",
|
|
36
|
+
"./": "./"
|
|
40
37
|
},
|
|
41
38
|
"repository": {
|
|
42
39
|
"type": "git",
|
|
43
|
-
"url": "
|
|
40
|
+
"url": "https://github.com/ealush/vest.git",
|
|
41
|
+
"directory": "packages/context"
|
|
42
|
+
},
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/ealush/vest.git/issues"
|
|
44
45
|
}
|
|
45
46
|
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
declare function createContext<T extends Record<string, unknown>>(init?: (ctxRef: Partial<T>, parentContext: T | void) => T | null): {
|
|
2
|
+
run: <R>(ctxRef: Partial<T>, fn: (context: T) => R) => R;
|
|
3
|
+
bind: <Fn extends (...args: any[]) => any>(ctxRef: Partial<T>, fn: Fn) => Fn;
|
|
4
|
+
use: () => T | undefined;
|
|
5
|
+
useX: (errorMessage?: string) => T;
|
|
6
|
+
};
|
|
7
|
+
export { createContext };
|
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
function _defineProperties(target, props) {
|
|
6
|
-
for (var i = 0; i < props.length; i++) {
|
|
7
|
-
var descriptor = props[i];
|
|
8
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
9
|
-
descriptor.configurable = true;
|
|
10
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
11
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function _createClass(Constructor, protoProps, staticProps) {
|
|
16
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
17
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
18
|
-
return Constructor;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
var getInnerName = function getInnerName(name) {
|
|
22
|
-
return "__" + name;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
var Context = /*#__PURE__*/function () {
|
|
26
|
-
function Context(_ref, ctxRef) {
|
|
27
|
-
var _init;
|
|
28
|
-
|
|
29
|
-
var use = _ref.use,
|
|
30
|
-
set = _ref.set,
|
|
31
|
-
addQueryableProperties = _ref.addQueryableProperties,
|
|
32
|
-
init = _ref.init;
|
|
33
|
-
this._parentContext = null;
|
|
34
|
-
var ctx = use();
|
|
35
|
-
var usedRef = typeof init === 'function' ? (_init = init(ctxRef, ctx)) !== null && _init !== void 0 ? _init : ctxRef : ctxRef;
|
|
36
|
-
var queryableProperties = addQueryableProperties(usedRef);
|
|
37
|
-
|
|
38
|
-
if (usedRef && typeof usedRef === 'object') {
|
|
39
|
-
for (var key in queryableProperties) {
|
|
40
|
-
if (Object.prototype.hasOwnProperty.call(usedRef, key)) {
|
|
41
|
-
this[getInnerName(key)] = usedRef[key];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
this.addLookupProperty(key);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (ctx) {
|
|
49
|
-
this.setParentContext(ctx);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
set(this);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
Context.is = function is(value) {
|
|
56
|
-
return value instanceof Context;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
var _proto = Context.prototype;
|
|
60
|
-
|
|
61
|
-
_proto.addLookupProperty = function addLookupProperty(key) {
|
|
62
|
-
var innerName = getInnerName(key);
|
|
63
|
-
Object.defineProperty(this, key, {
|
|
64
|
-
get: function get() {
|
|
65
|
-
return this.lookup(innerName);
|
|
66
|
-
},
|
|
67
|
-
set: function set(value) {
|
|
68
|
-
throw new Error("Context: Unable to set \"" + key + "\" to `" + JSON.stringify(value) + "`. Context properties cannot be set directly. Use context.run() instead.");
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
} // @ts-ignore - we actually do use lookup
|
|
72
|
-
;
|
|
73
|
-
|
|
74
|
-
_proto.lookup = function lookup(key) {
|
|
75
|
-
var ctx = this;
|
|
76
|
-
|
|
77
|
-
do {
|
|
78
|
-
if (ctx.hasOwnProperty(key)) {
|
|
79
|
-
return ctx[key];
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (Context.is(ctx.parentContext)) {
|
|
83
|
-
ctx = ctx.parentContext;
|
|
84
|
-
} else {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
} while (ctx);
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
_proto.setParentContext = function setParentContext(parentContext) {
|
|
91
|
-
if (Context.is(this)) {
|
|
92
|
-
this._parentContext = parentContext;
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
_createClass(Context, [{
|
|
97
|
-
key: "parentContext",
|
|
98
|
-
get: function get() {
|
|
99
|
-
return this._parentContext;
|
|
100
|
-
}
|
|
101
|
-
}]);
|
|
102
|
-
|
|
103
|
-
return Context;
|
|
104
|
-
}();
|
|
105
|
-
|
|
106
|
-
function createContext(init) {
|
|
107
|
-
var storage = {
|
|
108
|
-
ctx: undefined
|
|
109
|
-
};
|
|
110
|
-
var queryableProperties = {};
|
|
111
|
-
|
|
112
|
-
function addQueryableProperties(ctxRef) {
|
|
113
|
-
if (!ctxRef || typeof ctxRef !== 'object') {
|
|
114
|
-
return {};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
for (var key in ctxRef) {
|
|
118
|
-
if (Object.prototype.hasOwnProperty.call(ctxRef, key)) {
|
|
119
|
-
queryableProperties[key] = true;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return queryableProperties;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function use() {
|
|
127
|
-
return storage.ctx;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function set(value) {
|
|
131
|
-
return storage.ctx = value;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function clear() {
|
|
135
|
-
var ctx = use();
|
|
136
|
-
|
|
137
|
-
if (!Context.is(ctx)) {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
set(ctx.parentContext);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function run(ctxRef, fn) {
|
|
145
|
-
var ctx = new Context({
|
|
146
|
-
set: set,
|
|
147
|
-
use: use,
|
|
148
|
-
addQueryableProperties: addQueryableProperties,
|
|
149
|
-
init: init
|
|
150
|
-
}, ctxRef);
|
|
151
|
-
var res = fn(ctx);
|
|
152
|
-
clear();
|
|
153
|
-
return res;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function bind(ctxRef, fn) {
|
|
157
|
-
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
158
|
-
args[_key - 2] = arguments[_key];
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return function () {
|
|
162
|
-
for (var _len2 = arguments.length, runTimeArgs = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
163
|
-
runTimeArgs[_key2] = arguments[_key2];
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return run(ctxRef, function () {
|
|
167
|
-
return fn.apply(void 0, args.concat(runTimeArgs));
|
|
168
|
-
});
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return {
|
|
173
|
-
use: use,
|
|
174
|
-
run: run,
|
|
175
|
-
bind: bind
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
exports.default = createContext;
|
|
180
|
-
//# sourceMappingURL=context.cjs.development.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"context.cjs.development.js","sources":["../src/index.ts"],"sourcesContent":["type TypeCTXRef = { [key: string]: any };\n\nexport interface ICTXFN {\n (context: Context): any;\n}\ninterface Init {\n (ctxRef?: TypeCTXRef, parentContext?: Context | void): TypeCTXRef | null;\n}\n\ntype ContextOptions = {\n use: () => Context | void;\n set: (value: any) => any;\n addQueryableProperties: (ctxRef: TypeCTXRef) => TQueryableProperties;\n init?: Init;\n};\n\ntype TQueryableProperties = { [key: string]: true };\n\nexport type TCTX = {\n use: () => Context | void;\n run: (ctxRef: TypeCTXRef, fn: ICTXFN) => any;\n bind: (\n ctxRef: TypeCTXRef,\n fn: (...args: any[]) => any,\n ...args: any[]\n ) => (...runTimeArgs: any[]) => any;\n};\n\nconst getInnerName = (name: string): string => `__${name}`;\n\nclass Context {\n private _parentContext: Context | null = null;\n [key: string]: any;\n\n static is(value: any): value is Context {\n return value instanceof Context;\n }\n\n constructor(\n { use, set, addQueryableProperties, init }: ContextOptions,\n ctxRef: TypeCTXRef\n ) {\n const ctx = use();\n\n const usedRef =\n typeof init === 'function' ? init(ctxRef, ctx) ?? ctxRef : ctxRef;\n\n const queryableProperties = addQueryableProperties(usedRef);\n\n if (usedRef && typeof usedRef === 'object') {\n for (const key in queryableProperties) {\n if (Object.prototype.hasOwnProperty.call(usedRef, key)) {\n this[getInnerName(key)] = usedRef[key];\n }\n this.addLookupProperty(key);\n }\n }\n\n if (ctx) {\n this.setParentContext(ctx);\n }\n\n set(this);\n }\n\n addLookupProperty(key: string) {\n const innerName = getInnerName(key);\n\n Object.defineProperty(this, key, {\n get() {\n return this.lookup(innerName);\n },\n set(value) {\n throw new Error(\n `Context: Unable to set \"${key}\" to \\`${JSON.stringify(\n value\n )}\\`. Context properties cannot be set directly. Use context.run() instead.`\n );\n },\n });\n }\n\n // @ts-ignore - we actually do use lookup\n private lookup(key: string) {\n let ctx: Context = this;\n do {\n if (ctx.hasOwnProperty(key)) {\n return ctx[key];\n }\n if (Context.is(ctx.parentContext)) {\n ctx = ctx.parentContext;\n } else {\n return;\n }\n } while (ctx);\n }\n\n private setParentContext(parentContext: Context) {\n if (Context.is(this)) {\n this._parentContext = parentContext;\n }\n }\n\n get parentContext(): Context | null {\n return this._parentContext;\n }\n}\n\nfunction createContext(init?: Init) {\n const storage = {\n ctx: undefined,\n };\n\n const queryableProperties: TQueryableProperties = {};\n\n function addQueryableProperties(ctxRef: TypeCTXRef): TQueryableProperties {\n if (!ctxRef || typeof ctxRef !== 'object') {\n return {};\n }\n\n for (const key in ctxRef) {\n if (Object.prototype.hasOwnProperty.call(ctxRef, key)) {\n queryableProperties[key] = true;\n }\n }\n\n return queryableProperties;\n }\n\n function use(): Context | void {\n return storage.ctx;\n }\n function set(value: any) {\n return (storage.ctx = value);\n }\n function clear() {\n const ctx = use();\n\n if (!Context.is(ctx)) {\n return;\n }\n\n set(ctx.parentContext);\n }\n function run(ctxRef: TypeCTXRef, fn: ICTXFN) {\n const ctx = new Context({ set, use, addQueryableProperties, init }, ctxRef);\n\n const res = fn(ctx);\n\n clear();\n return res;\n }\n\n function bind(\n ctxRef: TypeCTXRef,\n fn: (...args: any[]) => any,\n ...args: any[]\n ) {\n return function(...runTimeArgs: any[]) {\n return run(ctxRef, function() {\n return fn(...args, ...runTimeArgs);\n });\n };\n }\n\n return {\n use,\n run,\n bind,\n };\n}\n\nexport default createContext;\n"],"names":["getInnerName","name","Context","ctxRef","use","set","addQueryableProperties","init","ctx","usedRef","queryableProperties","key","Object","prototype","hasOwnProperty","call","addLookupProperty","setParentContext","is","value","innerName","defineProperty","get","lookup","Error","JSON","stringify","parentContext","_parentContext","createContext","storage","undefined","clear","run","fn","res","bind","args","runTimeArgs"],"mappings":"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"}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(t){return"__"+t},e=function(){function e(e,n){var r,o=e.use,i=e.set,u=e.addQueryableProperties,a=e.init;this._parentContext=null;var c=o(),s="function"==typeof a&&null!==(r=a(n,c))&&void 0!==r?r:n,f=u(s);if(s&&"object"==typeof s)for(var p in f)Object.prototype.hasOwnProperty.call(s,p)&&(this[t(p)]=s[p]),this.addLookupProperty(p);c&&this.setParentContext(c),i(this)}e.is=function(t){return t instanceof e};var n,r=e.prototype;return r.addLookupProperty=function(e){var n=t(e);Object.defineProperty(this,e,{get:function(){return this.lookup(n)},set:function(t){throw new Error('Context: Unable to set "'+e+'" to `'+JSON.stringify(t)+"`. Context properties cannot be set directly. Use context.run() instead.")}})},r.lookup=function(t){var n=this;do{if(n.hasOwnProperty(t))return n[t];if(!e.is(n.parentContext))return;n=n.parentContext}while(n)},r.setParentContext=function(t){e.is(this)&&(this._parentContext=t)},(n=[{key:"parentContext",get:function(){return this._parentContext}}])&&function(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}(e.prototype,n),e}();exports.default=function(t){var n={ctx:void 0},r={};function o(t){if(!t||"object"!=typeof t)return{};for(var e in t)Object.prototype.hasOwnProperty.call(t,e)&&(r[e]=!0);return r}function i(){return n.ctx}function u(t){return n.ctx=t}function a(n,r){var a,c=r(new e({set:u,use:i,addQueryableProperties:o,init:t},n));return a=i(),e.is(a)&&u(a.parentContext),c}return{use:i,run:a,bind:function(t,e){for(var n=arguments.length,r=new Array(n>2?n-2:0),o=2;o<n;o++)r[o-2]=arguments[o];return function(){for(var n=arguments.length,o=new Array(n),i=0;i<n;i++)o[i]=arguments[i];return a(t,(function(){return e.apply(void 0,r.concat(o))}))}}}};
|
|
2
|
-
//# sourceMappingURL=context.cjs.production.min.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"context.cjs.production.min.js","sources":["../src/index.ts"],"sourcesContent":["type TypeCTXRef = { [key: string]: any };\n\nexport interface ICTXFN {\n (context: Context): any;\n}\ninterface Init {\n (ctxRef?: TypeCTXRef, parentContext?: Context | void): TypeCTXRef | null;\n}\n\ntype ContextOptions = {\n use: () => Context | void;\n set: (value: any) => any;\n addQueryableProperties: (ctxRef: TypeCTXRef) => TQueryableProperties;\n init?: Init;\n};\n\ntype TQueryableProperties = { [key: string]: true };\n\nexport type TCTX = {\n use: () => Context | void;\n run: (ctxRef: TypeCTXRef, fn: ICTXFN) => any;\n bind: (\n ctxRef: TypeCTXRef,\n fn: (...args: any[]) => any,\n ...args: any[]\n ) => (...runTimeArgs: any[]) => any;\n};\n\nconst getInnerName = (name: string): string => `__${name}`;\n\nclass Context {\n private _parentContext: Context | null = null;\n [key: string]: any;\n\n static is(value: any): value is Context {\n return value instanceof Context;\n }\n\n constructor(\n { use, set, addQueryableProperties, init }: ContextOptions,\n ctxRef: TypeCTXRef\n ) {\n const ctx = use();\n\n const usedRef =\n typeof init === 'function' ? init(ctxRef, ctx) ?? ctxRef : ctxRef;\n\n const queryableProperties = addQueryableProperties(usedRef);\n\n if (usedRef && typeof usedRef === 'object') {\n for (const key in queryableProperties) {\n if (Object.prototype.hasOwnProperty.call(usedRef, key)) {\n this[getInnerName(key)] = usedRef[key];\n }\n this.addLookupProperty(key);\n }\n }\n\n if (ctx) {\n this.setParentContext(ctx);\n }\n\n set(this);\n }\n\n addLookupProperty(key: string) {\n const innerName = getInnerName(key);\n\n Object.defineProperty(this, key, {\n get() {\n return this.lookup(innerName);\n },\n set(value) {\n throw new Error(\n `Context: Unable to set \"${key}\" to \\`${JSON.stringify(\n value\n )}\\`. Context properties cannot be set directly. Use context.run() instead.`\n );\n },\n });\n }\n\n // @ts-ignore - we actually do use lookup\n private lookup(key: string) {\n let ctx: Context = this;\n do {\n if (ctx.hasOwnProperty(key)) {\n return ctx[key];\n }\n if (Context.is(ctx.parentContext)) {\n ctx = ctx.parentContext;\n } else {\n return;\n }\n } while (ctx);\n }\n\n private setParentContext(parentContext: Context) {\n if (Context.is(this)) {\n this._parentContext = parentContext;\n }\n }\n\n get parentContext(): Context | null {\n return this._parentContext;\n }\n}\n\nfunction createContext(init?: Init) {\n const storage = {\n ctx: undefined,\n };\n\n const queryableProperties: TQueryableProperties = {};\n\n function addQueryableProperties(ctxRef: TypeCTXRef): TQueryableProperties {\n if (!ctxRef || typeof ctxRef !== 'object') {\n return {};\n }\n\n for (const key in ctxRef) {\n if (Object.prototype.hasOwnProperty.call(ctxRef, key)) {\n queryableProperties[key] = true;\n }\n }\n\n return queryableProperties;\n }\n\n function use(): Context | void {\n return storage.ctx;\n }\n function set(value: any) {\n return (storage.ctx = value);\n }\n function clear() {\n const ctx = use();\n\n if (!Context.is(ctx)) {\n return;\n }\n\n set(ctx.parentContext);\n }\n function run(ctxRef: TypeCTXRef, fn: ICTXFN) {\n const ctx = new Context({ set, use, addQueryableProperties, init }, ctxRef);\n\n const res = fn(ctx);\n\n clear();\n return res;\n }\n\n function bind(\n ctxRef: TypeCTXRef,\n fn: (...args: any[]) => any,\n ...args: any[]\n ) {\n return function(...runTimeArgs: any[]) {\n return run(ctxRef, function() {\n return fn(...args, ...runTimeArgs);\n });\n };\n }\n\n return {\n use,\n run,\n bind,\n };\n}\n\nexport default createContext;\n"],"names":["getInnerName","name","Context","ctxRef","use","set","addQueryableProperties","init","ctx","usedRef","queryableProperties","key","Object","prototype","hasOwnProperty","call","addLookupProperty","setParentContext","this","is","value","innerName","defineProperty","get","lookup","Error","JSON","stringify","parentContext","_parentContext","storage","undefined","run","fn","res","bind","args","runTimeArgs"],"mappings":"oEA4BA,IAAMA,EAAe,SAACC,cAA8BA,GAE9CC,0BAUFC,SADEC,IAAAA,IAAKC,IAAAA,IAAKC,IAAAA,uBAAwBC,IAAAA,yBARG,SAWjCC,EAAMJ,IAENK,EACY,mBAATF,aAAsBA,EAAKJ,EAAQK,kBAAiBL,EAEvDO,EAAsBJ,EAAuBG,MAE/CA,GAA8B,iBAAZA,MACf,IAAME,KAAOD,EACZE,OAAOC,UAAUC,eAAeC,KAAKN,EAASE,UAC3CX,EAAaW,IAAQF,EAAQE,SAE/BK,kBAAkBL,GAIvBH,QACGS,iBAAiBT,GAGxBH,EAAIa,QA5BCC,GAAP,SAAUC,UACDA,aAAiBlB,gCA8B1Bc,kBAAA,SAAkBL,OACVU,EAAYrB,EAAaW,GAE/BC,OAAOU,eAAeJ,KAAMP,EAAK,CAC/BY,sBACSL,KAAKM,OAAOH,IAErBhB,aAAIe,SACI,IAAIK,iCACmBd,WAAae,KAAKC,UAC3CP,qFAQFI,OAAA,SAAOb,OACTH,EAAeU,OAChB,IACGV,EAAIM,eAAeH,UACdH,EAAIG,OAETT,EAAQiB,GAAGX,EAAIoB,sBACjBpB,EAAMA,EAAIoB,oBAILpB,MAGHS,iBAAA,SAAiBW,GACnB1B,EAAQiB,GAAGD,aACRW,eAAiBD,mDAKjBV,KAAKW,6NAIhB,SAAuBtB,OACfuB,EAAU,CACdtB,SAAKuB,GAGDrB,EAA4C,YAEzCJ,EAAuBH,OACzBA,GAA4B,iBAAXA,QACb,OAGJ,IAAMQ,KAAOR,EACZS,OAAOC,UAAUC,eAAeC,KAAKZ,EAAQQ,KAC/CD,EAAoBC,IAAO,UAIxBD,WAGAN,WACA0B,EAAQtB,aAERH,EAAIe,UACHU,EAAQtB,IAAMY,WAWfY,EAAI7B,EAAoB8B,OARzBzB,EAWA0B,EAAMD,EAFA,IAAI/B,EAAQ,CAAEG,IAAAA,EAAKD,IAAAA,EAAKE,uBAAAA,EAAwBC,KAAAA,GAAQJ,WAT9DK,EAAMJ,IAEPF,EAAQiB,GAAGX,IAIhBH,EAAIG,EAAIoB,eAQDM,QAeF,CACL9B,IAAAA,EACA4B,IAAAA,EACAG,cAdAhC,EACA8B,8BACGG,mCAAAA,2BAEI,sCAAYC,2BAAAA,yBACVL,EAAI7B,GAAQ,kBACV8B,eAAMG,SAASC"}
|
package/dist/context.esm.js
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
function _defineProperties(target, props) {
|
|
2
|
-
for (var i = 0; i < props.length; i++) {
|
|
3
|
-
var descriptor = props[i];
|
|
4
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
5
|
-
descriptor.configurable = true;
|
|
6
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
7
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function _createClass(Constructor, protoProps, staticProps) {
|
|
12
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
13
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
14
|
-
return Constructor;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
var getInnerName = function getInnerName(name) {
|
|
18
|
-
return "__" + name;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
var Context = /*#__PURE__*/function () {
|
|
22
|
-
function Context(_ref, ctxRef) {
|
|
23
|
-
var _init;
|
|
24
|
-
|
|
25
|
-
var use = _ref.use,
|
|
26
|
-
set = _ref.set,
|
|
27
|
-
addQueryableProperties = _ref.addQueryableProperties,
|
|
28
|
-
init = _ref.init;
|
|
29
|
-
this._parentContext = null;
|
|
30
|
-
var ctx = use();
|
|
31
|
-
var usedRef = typeof init === 'function' ? (_init = init(ctxRef, ctx)) !== null && _init !== void 0 ? _init : ctxRef : ctxRef;
|
|
32
|
-
var queryableProperties = addQueryableProperties(usedRef);
|
|
33
|
-
|
|
34
|
-
if (usedRef && typeof usedRef === 'object') {
|
|
35
|
-
for (var key in queryableProperties) {
|
|
36
|
-
if (Object.prototype.hasOwnProperty.call(usedRef, key)) {
|
|
37
|
-
this[getInnerName(key)] = usedRef[key];
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
this.addLookupProperty(key);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (ctx) {
|
|
45
|
-
this.setParentContext(ctx);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
set(this);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
Context.is = function is(value) {
|
|
52
|
-
return value instanceof Context;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
var _proto = Context.prototype;
|
|
56
|
-
|
|
57
|
-
_proto.addLookupProperty = function addLookupProperty(key) {
|
|
58
|
-
var innerName = getInnerName(key);
|
|
59
|
-
Object.defineProperty(this, key, {
|
|
60
|
-
get: function get() {
|
|
61
|
-
return this.lookup(innerName);
|
|
62
|
-
},
|
|
63
|
-
set: function set(value) {
|
|
64
|
-
throw new Error("Context: Unable to set \"" + key + "\" to `" + JSON.stringify(value) + "`. Context properties cannot be set directly. Use context.run() instead.");
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
} // @ts-ignore - we actually do use lookup
|
|
68
|
-
;
|
|
69
|
-
|
|
70
|
-
_proto.lookup = function lookup(key) {
|
|
71
|
-
var ctx = this;
|
|
72
|
-
|
|
73
|
-
do {
|
|
74
|
-
if (ctx.hasOwnProperty(key)) {
|
|
75
|
-
return ctx[key];
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (Context.is(ctx.parentContext)) {
|
|
79
|
-
ctx = ctx.parentContext;
|
|
80
|
-
} else {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
} while (ctx);
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
_proto.setParentContext = function setParentContext(parentContext) {
|
|
87
|
-
if (Context.is(this)) {
|
|
88
|
-
this._parentContext = parentContext;
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
_createClass(Context, [{
|
|
93
|
-
key: "parentContext",
|
|
94
|
-
get: function get() {
|
|
95
|
-
return this._parentContext;
|
|
96
|
-
}
|
|
97
|
-
}]);
|
|
98
|
-
|
|
99
|
-
return Context;
|
|
100
|
-
}();
|
|
101
|
-
|
|
102
|
-
function createContext(init) {
|
|
103
|
-
var storage = {
|
|
104
|
-
ctx: undefined
|
|
105
|
-
};
|
|
106
|
-
var queryableProperties = {};
|
|
107
|
-
|
|
108
|
-
function addQueryableProperties(ctxRef) {
|
|
109
|
-
if (!ctxRef || typeof ctxRef !== 'object') {
|
|
110
|
-
return {};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
for (var key in ctxRef) {
|
|
114
|
-
if (Object.prototype.hasOwnProperty.call(ctxRef, key)) {
|
|
115
|
-
queryableProperties[key] = true;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return queryableProperties;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function use() {
|
|
123
|
-
return storage.ctx;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function set(value) {
|
|
127
|
-
return storage.ctx = value;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function clear() {
|
|
131
|
-
var ctx = use();
|
|
132
|
-
|
|
133
|
-
if (!Context.is(ctx)) {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
set(ctx.parentContext);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function run(ctxRef, fn) {
|
|
141
|
-
var ctx = new Context({
|
|
142
|
-
set: set,
|
|
143
|
-
use: use,
|
|
144
|
-
addQueryableProperties: addQueryableProperties,
|
|
145
|
-
init: init
|
|
146
|
-
}, ctxRef);
|
|
147
|
-
var res = fn(ctx);
|
|
148
|
-
clear();
|
|
149
|
-
return res;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function bind(ctxRef, fn) {
|
|
153
|
-
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
154
|
-
args[_key - 2] = arguments[_key];
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return function () {
|
|
158
|
-
for (var _len2 = arguments.length, runTimeArgs = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
159
|
-
runTimeArgs[_key2] = arguments[_key2];
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return run(ctxRef, function () {
|
|
163
|
-
return fn.apply(void 0, args.concat(runTimeArgs));
|
|
164
|
-
});
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
return {
|
|
169
|
-
use: use,
|
|
170
|
-
run: run,
|
|
171
|
-
bind: bind
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
export default createContext;
|
|
176
|
-
//# sourceMappingURL=context.esm.js.map
|