context 3.1.0-dev-c4ba9b β 3.1.0-next-20251205-bb85
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 +47 -180
- package/dist/context.cjs +63 -0
- package/dist/context.cjs.map +1 -0
- package/dist/context.mjs +62 -0
- package/dist/context.mjs.map +1 -0
- package/package.json +66 -32
- package/types/context.d.cts +27 -0
- package/types/context.d.cts.map +1 -0
- package/types/context.d.mts +27 -0
- package/types/context.d.mts.map +1 -0
- package/types/context.d.ts +21 -10
- package/vitest.config.ts +21 -0
- package/dist/cjs/context.development.js +0 -62
- package/dist/cjs/context.js +0 -7
- package/dist/cjs/context.production.js +0 -1
- package/dist/cjs/package.json +0 -1
- package/dist/es/context.development.js +0 -57
- package/dist/es/context.production.js +0 -1
- package/dist/es/package.json +0 -1
- package/dist/umd/context.development.js +0 -66
- package/dist/umd/context.production.js +0 -1
- package/tsconfig.json +0 -13
- package/types/context.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -1,212 +1,79 @@
|
|
|
1
|
-
# Context
|
|
1
|
+
# Context πͺ
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
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.
|
|
3
|
+
Lightweight context propagation for JavaScript and TypeScript. Create a scoped storage object, run code inside it, and read the active value anywhere down the call stackβwithout depending on React.
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
- `createContext(defaultValue?)` β simplest option with a single value per run.
|
|
6
|
+
- `createCascade(initializer?)` β merges parent and child context objects, ideal for layered data.
|
|
7
|
+
- `bind` β capture context for later execution (useful with async callbacks).
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
## Installation
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
## API Reference
|
|
13
|
-
|
|
14
|
-
### Top Level Exports
|
|
15
|
-
|
|
16
|
-
The context package exports these two functions:
|
|
17
|
-
|
|
18
|
-
- `createContext`: Creates a new context.
|
|
19
|
-
- `createCascade`: Creates a new cascading context.
|
|
20
|
-
|
|
21
|
-
## createContext()
|
|
22
|
-
|
|
23
|
-
Create context is the minimal implementation of context. It allows propagation of values down in your function call.
|
|
24
|
-
|
|
25
|
-
createContext takes a single argument - defaultContextValue. This value is used when not withing a running context.
|
|
26
|
-
|
|
27
|
-
### Arguments
|
|
28
|
-
|
|
29
|
-
| Argument | Type | Optional? | Description |
|
|
30
|
-
| ------------------- | ----- | --------- | ------------------------------------------------------------ |
|
|
31
|
-
| defaultContextValue | `any` | Yes | The default value to use when not withing a running context. |
|
|
32
|
-
|
|
33
|
-
### Returned object
|
|
34
|
-
|
|
35
|
-
`createContext` returns an object containing the following functions:
|
|
36
|
-
|
|
37
|
-
- `use`: Returns the current context value, or the default value when not withing a running context.
|
|
38
|
-
- `useX`: Returns the current context, throws an error if not within a running context or the context is undefined. `useX` will throw even if a default value is provided.
|
|
39
|
-
- `run`: Runs the context, passing the given value into the context.
|
|
40
|
-
|
|
41
|
-
**Note About Typescript Usage**
|
|
42
|
-
For convenience, `use` assumes we're alwyas inside a context. If you want to have runtime safety, you can use `useX` instead to make sure you're excplicitly using a defined context.
|
|
43
|
-
|
|
44
|
-
### Usage Example
|
|
45
|
-
|
|
46
|
-
```js
|
|
47
|
-
const context = createContext(0); // Create a context with a default value of 0.
|
|
48
|
-
|
|
49
|
-
function myFunc() {
|
|
50
|
-
context.run(100, someOtherFunc); // Run the context with a value of 100.
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function someOtherFunc() {
|
|
54
|
-
const number = context.use(); // Returns the value of the context.
|
|
55
|
-
}
|
|
11
|
+
```bash
|
|
12
|
+
npm i context
|
|
56
13
|
```
|
|
57
14
|
|
|
58
|
-
##
|
|
59
|
-
|
|
60
|
-
`createCascade` is a more advanced version of `createContext` that allows you to create cascading contexts. It assumes the value is always an object, and when nesting context layers, it merges their values together.
|
|
61
|
-
|
|
62
|
-
`createCascade` does not take a default value, but an initializer function instead. This initializer is called on each `run` call, and it allows you to modify or augment the context value being passed down. The init function is passed the context object run was called with, and the parent context if it was called within a previously created context. The init function needs to return the desired context object.
|
|
63
|
-
|
|
64
|
-
### Arguments
|
|
65
|
-
|
|
66
|
-
| Argument | Type | Optional? | Description |
|
|
67
|
-
| ----------- | ---------- | --------- | ------------------------------------------------------------ |
|
|
68
|
-
| initializer | `Function` | Yes | The initializer function to use when creating a new context. |
|
|
15
|
+
## createContext
|
|
69
16
|
|
|
70
|
-
|
|
71
|
-
The initializer receives the context object, and the parent context object, if present.
|
|
72
|
-
|
|
73
|
-
### Returned object
|
|
74
|
-
|
|
75
|
-
`createCascade` returns an object containing the following functions:
|
|
76
|
-
|
|
77
|
-
- `use`: Returns the current context value.
|
|
78
|
-
- `useX`: Returns the current context, throws an error if not within a running context.
|
|
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
|
-
- `bind`: Binds a given function to the context. Allows for delayd execution of a function as if it was called within the context.
|
|
81
|
-
|
|
82
|
-
### Usage Examples
|
|
83
|
-
|
|
84
|
-
#### Initializer Function
|
|
17
|
+
Create an isolated context with an optional default value used outside of a running scope.
|
|
85
18
|
|
|
86
19
|
```js
|
|
87
|
-
|
|
88
|
-
if (parentContext === null) {
|
|
89
|
-
// we're at the top level
|
|
90
|
-
// so let's add a default cart object
|
|
91
|
-
return Object.assign(ctx, {
|
|
92
|
-
cart: [],
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// we're in a sub context, so we already have those default values.
|
|
97
|
-
return ctx;
|
|
98
|
-
});
|
|
99
|
-
```
|
|
20
|
+
import { createContext } from 'context';
|
|
100
21
|
|
|
101
|
-
|
|
22
|
+
const requestContext = createContext({ requestId: undefined });
|
|
102
23
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
import sayUsername from './sayUsername';
|
|
109
|
-
|
|
110
|
-
function myFunction(username) {
|
|
111
|
-
return ctx.run({ username }, sayUsername);
|
|
24
|
+
function handleRequest(requestId) {
|
|
25
|
+
return requestContext.run({ requestId }, () => {
|
|
26
|
+
logRequest();
|
|
27
|
+
return doWork();
|
|
28
|
+
});
|
|
112
29
|
}
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
If `run` is called within a different `run` call, the context values will be merged when the callback is run. When we exit our callback, the context will be reset to the values it had before the call.
|
|
116
|
-
|
|
117
|
-
### use() and useX()
|
|
118
|
-
|
|
119
|
-
These are the main ways to access the context within your code. They return the current object that stored within the context, and differ in the way they handle calls outside of the context.
|
|
120
30
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
```js
|
|
126
|
-
// sayUsername.js
|
|
127
|
-
import ctx from './ctx';
|
|
128
|
-
|
|
129
|
-
function sayUsername() {
|
|
130
|
-
const context = ctx.use(); // { username: 'John Doe' }
|
|
131
|
-
|
|
132
|
-
if (!context) {
|
|
133
|
-
// we're not within a `run` call. This function was called outside of a running context.
|
|
134
|
-
return "Hey, I don't know you, and this is crazy!";
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return `Hello, ${context.username}!`;
|
|
31
|
+
function logRequest() {
|
|
32
|
+
// Inside run β current context value
|
|
33
|
+
console.log(requestContext.use().requestId);
|
|
138
34
|
}
|
|
139
35
|
```
|
|
140
36
|
|
|
141
|
-
|
|
142
|
-
// handleCart.js
|
|
143
|
-
import ctx from './ctx';
|
|
144
|
-
|
|
145
|
-
function handleCart() {
|
|
146
|
-
const context = ctx.useX(
|
|
147
|
-
'handleCart was called outside of a running context'
|
|
148
|
-
); // { cart: { items: [ 'foo', 'bar' ] } }
|
|
149
|
-
// This throws an error if we're not within a `run` call.
|
|
150
|
-
// You should catch this error and handle it somewhere above this function.
|
|
37
|
+
**API:**
|
|
151
38
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
39
|
+
- `run(value, callback)` β activates the context for the callback. Nested runs temporarily override the value and restore it afterwards.
|
|
40
|
+
- `use()` β returns the current value or the default when no context is active.
|
|
41
|
+
- `useX(message?)` β like `use`, but throws if called outside of `run`, ignoring any default value.
|
|
155
42
|
|
|
156
|
-
|
|
43
|
+
## createCascade
|
|
157
44
|
|
|
158
|
-
|
|
45
|
+
`createCascade` composes context objects instead of replacing them. Each `run` merges the current value with the parent context, so shared properties flow down while children can add or override keys.
|
|
159
46
|
|
|
160
47
|
```js
|
|
161
|
-
|
|
162
|
-
import ctx from './ctx';
|
|
48
|
+
import { createCascade } from 'context';
|
|
163
49
|
|
|
164
|
-
|
|
165
|
-
return ctx.bind({ productId }, handleProductData);
|
|
50
|
+
const userContext = createCascade();
|
|
166
51
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
52
|
+
userContext.run({ user: { id: 5, name: 'Ada' } }, () => {
|
|
53
|
+
// child run inherits id/name and adds role
|
|
54
|
+
userContext.run({ user: { role: 'admin' } }, () => {
|
|
55
|
+
const ctx = userContext.use();
|
|
56
|
+
// ctx.user => { id: 5, name: 'Ada', role: 'admin' }
|
|
170
57
|
});
|
|
171
|
-
}
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
## Typescript Support
|
|
175
|
-
|
|
176
|
-
both `createContext` and `createCascade` have full typescript support. To gain the full benefits of typescript within your context, it is best to annotate your context with its types:
|
|
177
|
-
|
|
178
|
-
```ts
|
|
179
|
-
const someContext = createContext<number>(0);
|
|
180
|
-
|
|
181
|
-
const someCascadeContext = createCascade<{
|
|
182
|
-
username: string;
|
|
183
|
-
firstName: string;
|
|
184
|
-
middleName?: string;
|
|
185
|
-
lastName: string;
|
|
186
|
-
age: number;
|
|
187
|
-
}>();
|
|
58
|
+
});
|
|
188
59
|
```
|
|
189
60
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
## Troubleshooting
|
|
193
|
-
|
|
194
|
-
### Working with async functions/promises
|
|
195
|
-
|
|
196
|
-
Working with context inside within async code may lead to unexpected results when we don't fully consider what's happening. Trying to call your context from the async part of your code will probably return `null` instead of your values.
|
|
61
|
+
**API additions:**
|
|
197
62
|
|
|
198
|
-
|
|
63
|
+
- `use()` / `useX(message?)` β return the merged object for the current layer.
|
|
64
|
+
- `run(value, callback)` β merges `value` into the parent context without passing arguments to `callback`.
|
|
65
|
+
- `bind(value, callback)` β returns a function that executes `callback` inside a context seeded with `value`, preserving arguments when invoked later.
|
|
199
66
|
|
|
200
|
-
|
|
67
|
+
## TypeScript
|
|
201
68
|
|
|
202
|
-
|
|
69
|
+
Both factories are fully typed. Annotate your contexts for stricter checks:
|
|
203
70
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
71
|
+
```ts
|
|
72
|
+
const settings = createContext<{ locale: string }>();
|
|
73
|
+
const session = createCascade<{ user?: { id: string } }>();
|
|
74
|
+
```
|
|
207
75
|
|
|
208
|
-
|
|
209
|
-
This is the most obvious and easiest to achieve, though not always what you need. The basic idea is that you take whatever you need from the context when it is still available to you.
|
|
76
|
+
## Notes
|
|
210
77
|
|
|
211
|
-
|
|
212
|
-
|
|
78
|
+
- Context is synchronous by design; reading it after an async boundary requires `bind` or wrapping the async call inside `run`.
|
|
79
|
+
- Each `run` call leaves the parent context untouched once the callback completes.
|
package/dist/context.cjs
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
let vest_utils = require("vest-utils");
|
|
2
|
+
|
|
3
|
+
//#region src/context.ts
|
|
4
|
+
const USEX_DEFAULT_ERROR_MESSAGE = "Not inside of a running context.";
|
|
5
|
+
const EMPTY_CONTEXT = Symbol();
|
|
6
|
+
/**
|
|
7
|
+
* Base context interface.
|
|
8
|
+
*/
|
|
9
|
+
function createContext(defaultContextValue) {
|
|
10
|
+
let contextValue = EMPTY_CONTEXT;
|
|
11
|
+
return {
|
|
12
|
+
run,
|
|
13
|
+
use,
|
|
14
|
+
useX
|
|
15
|
+
};
|
|
16
|
+
function use() {
|
|
17
|
+
return isInsideContext() ? contextValue : defaultContextValue;
|
|
18
|
+
}
|
|
19
|
+
function useX(errorMessage) {
|
|
20
|
+
(0, vest_utils.invariant)(isInsideContext(), (0, vest_utils.defaultTo)(errorMessage, USEX_DEFAULT_ERROR_MESSAGE));
|
|
21
|
+
return contextValue;
|
|
22
|
+
}
|
|
23
|
+
function run(value, cb) {
|
|
24
|
+
const parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
|
|
25
|
+
contextValue = value;
|
|
26
|
+
const res = cb();
|
|
27
|
+
contextValue = parentContext;
|
|
28
|
+
return res;
|
|
29
|
+
}
|
|
30
|
+
function isInsideContext() {
|
|
31
|
+
return contextValue !== EMPTY_CONTEXT;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Cascading context - another implementation of context, that assumes the context value is an object.
|
|
36
|
+
* When nesting context runs, the the values of the current layer merges with the layers above it.
|
|
37
|
+
*/
|
|
38
|
+
function createCascade(init) {
|
|
39
|
+
const ctx = createContext();
|
|
40
|
+
return {
|
|
41
|
+
bind,
|
|
42
|
+
run,
|
|
43
|
+
use: ctx.use,
|
|
44
|
+
useX: ctx.useX
|
|
45
|
+
};
|
|
46
|
+
function run(value, fn) {
|
|
47
|
+
const parentContext = ctx.use();
|
|
48
|
+
const out = (0, vest_utils.assign)({}, parentContext ? parentContext : {}, (0, vest_utils.dynamicValue)(init, value, parentContext) ?? 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
|
+
//#endregion
|
|
61
|
+
exports.createCascade = createCascade;
|
|
62
|
+
exports.createContext = createContext;
|
|
63
|
+
//# sourceMappingURL=context.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.cjs","names":["contextValue: T | symbol"],"sources":["../src/context.ts"],"sourcesContent":["import type { CB, Maybe } from 'vest-utils';\nimport {\n assign,\n defaultTo,\n invariant,\n dynamicValue,\n Nullable,\n} from 'vest-utils';\n\nconst USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';\nconst EMPTY_CONTEXT = Symbol();\n\n/**\n * Base context interface.\n */\nexport function createContext<T>(defaultContextValue?: T): CtxApi<T> {\n let contextValue: T | symbol = EMPTY_CONTEXT;\n\n return {\n run,\n use,\n useX,\n };\n\n function use(): T {\n return (isInsideContext() ? contextValue : defaultContextValue) as T;\n }\n\n function useX(errorMessage?: string): T {\n invariant(\n isInsideContext(),\n defaultTo(errorMessage, USEX_DEFAULT_ERROR_MESSAGE),\n );\n return contextValue as T;\n }\n\n function run<R>(value: T, cb: () => R): R {\n const parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;\n\n contextValue = value;\n\n const res = cb();\n\n contextValue = parentContext;\n return res;\n }\n\n function isInsideContext(): boolean {\n return contextValue !== EMPTY_CONTEXT;\n }\n}\n\n/**\n * Cascading context - another implementation of context, that assumes the context value is an object.\n * When nesting context runs, the the values of the current layer merges with the layers above it.\n */\nexport function createCascade<T extends Record<string, unknown>>(\n init?: (value: Partial<T>, parentContext: Maybe<T>) => Nullable<T>,\n): CtxCascadeApi<T> {\n const ctx = createContext<T>();\n\n return {\n bind,\n run,\n use: ctx.use,\n useX: ctx.useX,\n };\n\n function run<R>(value: Partial<T>, fn: () => R): R {\n const parentContext = ctx.use();\n\n const out = assign(\n {},\n parentContext ? parentContext : {},\n dynamicValue(init, value, parentContext) ?? value,\n ) as T;\n\n return ctx.run(Object.freeze(out), fn) as R;\n }\n\n function bind<Fn extends CB>(value: Partial<T>, fn: Fn) {\n return function (...runTimeArgs: Parameters<Fn>) {\n return run<ReturnType<Fn>>(value, function () {\n return fn(...runTimeArgs);\n });\n } as Fn;\n }\n}\n\ntype ContextConsumptionApi<T> = {\n use: () => T;\n useX: (errorMessage?: string) => T;\n};\n\nexport type CtxApi<T> = ContextConsumptionApi<T> & {\n run: <R>(value: T, cb: () => R) => R;\n};\n\nexport type CtxCascadeApi<T> = ContextConsumptionApi<T> & {\n run: <R>(value: Partial<T>, fn: () => R) => R;\n bind: <Fn extends CB>(value: Partial<T>, fn: Fn) => Fn;\n};\n"],"mappings":";;;AASA,MAAM,6BAA6B;AACnC,MAAM,gBAAgB,QAAQ;;;;AAK9B,SAAgB,cAAiB,qBAAoC;CACnE,IAAIA,eAA2B;AAE/B,QAAO;EACL;EACA;EACA;EACD;CAED,SAAS,MAAS;AAChB,SAAQ,iBAAiB,GAAG,eAAe;;CAG7C,SAAS,KAAK,cAA0B;AACtC,4BACE,iBAAiB,4BACP,cAAc,2BAA2B,CACpD;AACD,SAAO;;CAGT,SAAS,IAAO,OAAU,IAAgB;EACxC,MAAM,gBAAgB,iBAAiB,GAAG,KAAK,GAAG;AAElD,iBAAe;EAEf,MAAM,MAAM,IAAI;AAEhB,iBAAe;AACf,SAAO;;CAGT,SAAS,kBAA2B;AAClC,SAAO,iBAAiB;;;;;;;AAQ5B,SAAgB,cACd,MACkB;CAClB,MAAM,MAAM,eAAkB;AAE9B,QAAO;EACL;EACA;EACA,KAAK,IAAI;EACT,MAAM,IAAI;EACX;CAED,SAAS,IAAO,OAAmB,IAAgB;EACjD,MAAM,gBAAgB,IAAI,KAAK;EAE/B,MAAM,6BACJ,EAAE,EACF,gBAAgB,gBAAgB,EAAE,+BACrB,MAAM,OAAO,cAAc,IAAI,MAC7C;AAED,SAAO,IAAI,IAAI,OAAO,OAAO,IAAI,EAAE,GAAG;;CAGxC,SAAS,KAAoB,OAAmB,IAAQ;AACtD,SAAO,SAAU,GAAG,aAA6B;AAC/C,UAAO,IAAoB,OAAO,WAAY;AAC5C,WAAO,GAAG,GAAG,YAAY;KACzB"}
|
package/dist/context.mjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { assign, defaultTo, dynamicValue, invariant } from "vest-utils";
|
|
2
|
+
|
|
3
|
+
//#region src/context.ts
|
|
4
|
+
const USEX_DEFAULT_ERROR_MESSAGE = "Not inside of a running context.";
|
|
5
|
+
const EMPTY_CONTEXT = Symbol();
|
|
6
|
+
/**
|
|
7
|
+
* Base context interface.
|
|
8
|
+
*/
|
|
9
|
+
function createContext(defaultContextValue) {
|
|
10
|
+
let contextValue = EMPTY_CONTEXT;
|
|
11
|
+
return {
|
|
12
|
+
run,
|
|
13
|
+
use,
|
|
14
|
+
useX
|
|
15
|
+
};
|
|
16
|
+
function use() {
|
|
17
|
+
return isInsideContext() ? contextValue : defaultContextValue;
|
|
18
|
+
}
|
|
19
|
+
function useX(errorMessage) {
|
|
20
|
+
invariant(isInsideContext(), defaultTo(errorMessage, USEX_DEFAULT_ERROR_MESSAGE));
|
|
21
|
+
return contextValue;
|
|
22
|
+
}
|
|
23
|
+
function run(value, cb) {
|
|
24
|
+
const parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
|
|
25
|
+
contextValue = value;
|
|
26
|
+
const res = cb();
|
|
27
|
+
contextValue = parentContext;
|
|
28
|
+
return res;
|
|
29
|
+
}
|
|
30
|
+
function isInsideContext() {
|
|
31
|
+
return contextValue !== EMPTY_CONTEXT;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Cascading context - another implementation of context, that assumes the context value is an object.
|
|
36
|
+
* When nesting context runs, the the values of the current layer merges with the layers above it.
|
|
37
|
+
*/
|
|
38
|
+
function createCascade(init) {
|
|
39
|
+
const ctx = createContext();
|
|
40
|
+
return {
|
|
41
|
+
bind,
|
|
42
|
+
run,
|
|
43
|
+
use: ctx.use,
|
|
44
|
+
useX: ctx.useX
|
|
45
|
+
};
|
|
46
|
+
function run(value, fn) {
|
|
47
|
+
const parentContext = ctx.use();
|
|
48
|
+
const out = assign({}, parentContext ? parentContext : {}, dynamicValue(init, value, parentContext) ?? 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
|
+
//#endregion
|
|
61
|
+
export { createCascade, createContext };
|
|
62
|
+
//# sourceMappingURL=context.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.mjs","names":["contextValue: T | symbol"],"sources":["../src/context.ts"],"sourcesContent":["import type { CB, Maybe } from 'vest-utils';\nimport {\n assign,\n defaultTo,\n invariant,\n dynamicValue,\n Nullable,\n} from 'vest-utils';\n\nconst USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';\nconst EMPTY_CONTEXT = Symbol();\n\n/**\n * Base context interface.\n */\nexport function createContext<T>(defaultContextValue?: T): CtxApi<T> {\n let contextValue: T | symbol = EMPTY_CONTEXT;\n\n return {\n run,\n use,\n useX,\n };\n\n function use(): T {\n return (isInsideContext() ? contextValue : defaultContextValue) as T;\n }\n\n function useX(errorMessage?: string): T {\n invariant(\n isInsideContext(),\n defaultTo(errorMessage, USEX_DEFAULT_ERROR_MESSAGE),\n );\n return contextValue as T;\n }\n\n function run<R>(value: T, cb: () => R): R {\n const parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;\n\n contextValue = value;\n\n const res = cb();\n\n contextValue = parentContext;\n return res;\n }\n\n function isInsideContext(): boolean {\n return contextValue !== EMPTY_CONTEXT;\n }\n}\n\n/**\n * Cascading context - another implementation of context, that assumes the context value is an object.\n * When nesting context runs, the the values of the current layer merges with the layers above it.\n */\nexport function createCascade<T extends Record<string, unknown>>(\n init?: (value: Partial<T>, parentContext: Maybe<T>) => Nullable<T>,\n): CtxCascadeApi<T> {\n const ctx = createContext<T>();\n\n return {\n bind,\n run,\n use: ctx.use,\n useX: ctx.useX,\n };\n\n function run<R>(value: Partial<T>, fn: () => R): R {\n const parentContext = ctx.use();\n\n const out = assign(\n {},\n parentContext ? parentContext : {},\n dynamicValue(init, value, parentContext) ?? value,\n ) as T;\n\n return ctx.run(Object.freeze(out), fn) as R;\n }\n\n function bind<Fn extends CB>(value: Partial<T>, fn: Fn) {\n return function (...runTimeArgs: Parameters<Fn>) {\n return run<ReturnType<Fn>>(value, function () {\n return fn(...runTimeArgs);\n });\n } as Fn;\n }\n}\n\ntype ContextConsumptionApi<T> = {\n use: () => T;\n useX: (errorMessage?: string) => T;\n};\n\nexport type CtxApi<T> = ContextConsumptionApi<T> & {\n run: <R>(value: T, cb: () => R) => R;\n};\n\nexport type CtxCascadeApi<T> = ContextConsumptionApi<T> & {\n run: <R>(value: Partial<T>, fn: () => R) => R;\n bind: <Fn extends CB>(value: Partial<T>, fn: Fn) => Fn;\n};\n"],"mappings":";;;AASA,MAAM,6BAA6B;AACnC,MAAM,gBAAgB,QAAQ;;;;AAK9B,SAAgB,cAAiB,qBAAoC;CACnE,IAAIA,eAA2B;AAE/B,QAAO;EACL;EACA;EACA;EACD;CAED,SAAS,MAAS;AAChB,SAAQ,iBAAiB,GAAG,eAAe;;CAG7C,SAAS,KAAK,cAA0B;AACtC,YACE,iBAAiB,EACjB,UAAU,cAAc,2BAA2B,CACpD;AACD,SAAO;;CAGT,SAAS,IAAO,OAAU,IAAgB;EACxC,MAAM,gBAAgB,iBAAiB,GAAG,KAAK,GAAG;AAElD,iBAAe;EAEf,MAAM,MAAM,IAAI;AAEhB,iBAAe;AACf,SAAO;;CAGT,SAAS,kBAA2B;AAClC,SAAO,iBAAiB;;;;;;;AAQ5B,SAAgB,cACd,MACkB;CAClB,MAAM,MAAM,eAAkB;AAE9B,QAAO;EACL;EACA;EACA,KAAK,IAAI;EACT,MAAM,IAAI;EACX;CAED,SAAS,IAAO,OAAmB,IAAgB;EACjD,MAAM,gBAAgB,IAAI,KAAK;EAE/B,MAAM,MAAM,OACV,EAAE,EACF,gBAAgB,gBAAgB,EAAE,EAClC,aAAa,MAAM,OAAO,cAAc,IAAI,MAC7C;AAED,SAAO,IAAI,IAAI,OAAO,OAAO,IAAI,EAAE,GAAG;;CAGxC,SAAS,KAAoB,OAAmB,IAAQ;AACtD,SAAO,SAAU,GAAG,aAA6B;AAC/C,UAAO,IAAoB,OAAO,WAAY;AAC5C,WAAO,GAAG,GAAG,YAAY;KACzB"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "3.1.0-
|
|
2
|
+
"version": "3.1.0-next-20251205-bb85",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"main": "./dist/
|
|
5
|
-
"types": "./types/context.d.
|
|
4
|
+
"main": "./dist/context.cjs",
|
|
5
|
+
"types": "./types/context.d.cts",
|
|
6
6
|
"name": "context",
|
|
7
7
|
"author": "ealush",
|
|
8
8
|
"scripts": {
|
|
@@ -11,33 +11,9 @@
|
|
|
11
11
|
"release": "vx release"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"vest-utils": "
|
|
15
|
-
},
|
|
16
|
-
"module": "./dist/es/context.production.js",
|
|
17
|
-
"exports": {
|
|
18
|
-
".": {
|
|
19
|
-
"development": {
|
|
20
|
-
"types": "./types/context.d.ts",
|
|
21
|
-
"browser": "./dist/es/context.development.js",
|
|
22
|
-
"umd": "./dist/umd/context.development.js",
|
|
23
|
-
"import": "./dist/es/context.development.js",
|
|
24
|
-
"require": "./dist/cjs/context.development.js",
|
|
25
|
-
"node": "./dist/cjs/context.development.js",
|
|
26
|
-
"module": "./dist/es/context.development.js",
|
|
27
|
-
"default": "./dist/cjs/context.development.js"
|
|
28
|
-
},
|
|
29
|
-
"types": "./types/context.d.ts",
|
|
30
|
-
"browser": "./dist/es/context.production.js",
|
|
31
|
-
"umd": "./dist/umd/context.production.js",
|
|
32
|
-
"import": "./dist/es/context.production.js",
|
|
33
|
-
"require": "./dist/cjs/context.production.js",
|
|
34
|
-
"node": "./dist/cjs/context.production.js",
|
|
35
|
-
"module": "./dist/es/context.production.js",
|
|
36
|
-
"default": "./dist/cjs/context.production.js"
|
|
37
|
-
},
|
|
38
|
-
"./package.json": "./package.json",
|
|
39
|
-
"./": "./"
|
|
14
|
+
"vest-utils": "1.4.0-next-20251205-bb85"
|
|
40
15
|
},
|
|
16
|
+
"module": "./dist/context.mjs",
|
|
41
17
|
"repository": {
|
|
42
18
|
"type": "git",
|
|
43
19
|
"url": "https://github.com/ealush/vest.git",
|
|
@@ -46,6 +22,64 @@
|
|
|
46
22
|
"bugs": {
|
|
47
23
|
"url": "https://github.com/ealush/vest.git/issues"
|
|
48
24
|
},
|
|
49
|
-
"unpkg": "./dist/
|
|
50
|
-
"jsdelivr": "./dist/
|
|
51
|
-
|
|
25
|
+
"unpkg": "./dist/context.mjs",
|
|
26
|
+
"jsdelivr": "./dist/context.mjs",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./types/context.d.cts",
|
|
30
|
+
"import": "./dist/context.mjs",
|
|
31
|
+
"require": "./dist/context.cjs"
|
|
32
|
+
},
|
|
33
|
+
"./*": {
|
|
34
|
+
"types": "./types/context.d.cts",
|
|
35
|
+
"default": "./*"
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json",
|
|
38
|
+
"./context": {
|
|
39
|
+
"types": "./types/context.d.cts",
|
|
40
|
+
"default": "./types/context.d.cts"
|
|
41
|
+
},
|
|
42
|
+
"./context.d.ts": {
|
|
43
|
+
"types": "./types/context.d.cts",
|
|
44
|
+
"default": "./types/context.d.ts"
|
|
45
|
+
},
|
|
46
|
+
"./types/*": {
|
|
47
|
+
"types": "./types/context.d.cts",
|
|
48
|
+
"default": "./types/*"
|
|
49
|
+
},
|
|
50
|
+
"./dist/*": {
|
|
51
|
+
"types": "./types/context.d.cts",
|
|
52
|
+
"default": "./dist/*"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"exports": {
|
|
57
|
+
".": {
|
|
58
|
+
"types": "./types/context.d.cts",
|
|
59
|
+
"import": "./dist/context.mjs",
|
|
60
|
+
"require": "./dist/context.cjs"
|
|
61
|
+
},
|
|
62
|
+
"./*": {
|
|
63
|
+
"types": "./types/context.d.cts",
|
|
64
|
+
"default": "./*"
|
|
65
|
+
},
|
|
66
|
+
"./package.json": "./package.json",
|
|
67
|
+
"./context": {
|
|
68
|
+
"types": "./types/context.d.cts",
|
|
69
|
+
"default": "./types/context.d.cts"
|
|
70
|
+
},
|
|
71
|
+
"./context.d.ts": {
|
|
72
|
+
"types": "./types/context.d.cts",
|
|
73
|
+
"default": "./types/context.d.ts"
|
|
74
|
+
},
|
|
75
|
+
"./types/*": {
|
|
76
|
+
"types": "./types/context.d.cts",
|
|
77
|
+
"default": "./types/*"
|
|
78
|
+
},
|
|
79
|
+
"./dist/*": {
|
|
80
|
+
"types": "./types/context.d.cts",
|
|
81
|
+
"default": "./dist/*"
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { CB, Maybe, Nullable } from "vest-utils";
|
|
2
|
+
|
|
3
|
+
//#region src/context.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Base context interface.
|
|
7
|
+
*/
|
|
8
|
+
declare function createContext<T>(defaultContextValue?: T): CtxApi<T>;
|
|
9
|
+
/**
|
|
10
|
+
* Cascading context - another implementation of context, that assumes the context value is an object.
|
|
11
|
+
* When nesting context runs, the the values of the current layer merges with the layers above it.
|
|
12
|
+
*/
|
|
13
|
+
declare function createCascade<T extends Record<string, unknown>>(init?: (value: Partial<T>, parentContext: Maybe<T>) => Nullable<T>): CtxCascadeApi<T>;
|
|
14
|
+
type ContextConsumptionApi<T> = {
|
|
15
|
+
use: () => T;
|
|
16
|
+
useX: (errorMessage?: string) => T;
|
|
17
|
+
};
|
|
18
|
+
type CtxApi<T> = ContextConsumptionApi<T> & {
|
|
19
|
+
run: <R>(value: T, cb: () => R) => R;
|
|
20
|
+
};
|
|
21
|
+
type CtxCascadeApi<T> = ContextConsumptionApi<T> & {
|
|
22
|
+
run: <R>(value: Partial<T>, fn: () => R) => R;
|
|
23
|
+
bind: <Fn extends CB>(value: Partial<T>, fn: Fn) => Fn;
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
export { CtxApi, CtxCascadeApi, createCascade, createContext };
|
|
27
|
+
//# sourceMappingURL=context.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.cts","names":["CB","Maybe","Nullable","createContext","T","CtxApi","createCascade","Record","Partial","CtxCascadeApi","ContextConsumptionApi","R","Fn"],"sources":["../src/context.d.ts"],"sourcesContent":["import type { CB, Maybe } from 'vest-utils';\nimport { Nullable } from 'vest-utils';\n/**\n * Base context interface.\n */\nexport declare function createContext<T>(defaultContextValue?: T): CtxApi<T>;\n/**\n * Cascading context - another implementation of context, that assumes the context value is an object.\n * When nesting context runs, the the values of the current layer merges with the layers above it.\n */\nexport declare function createCascade<T extends Record<string, unknown>>(init?: (value: Partial<T>, parentContext: Maybe<T>) => Nullable<T>): CtxCascadeApi<T>;\ntype ContextConsumptionApi<T> = {\n use: () => T;\n useX: (errorMessage?: string) => T;\n};\nexport type CtxApi<T> = ContextConsumptionApi<T> & {\n run: <R>(value: T, cb: () => R) => R;\n};\nexport type CtxCascadeApi<T> = ContextConsumptionApi<T> & {\n run: <R>(value: Partial<T>, fn: () => R) => R;\n bind: <Fn extends CB>(value: Partial<T>, fn: Fn) => Fn;\n};\nexport {};\n"],"mappings":";;;;;AAKA;;AAA0EI,iBAAlDD,aAAkDC,CAAAA,CAAAA,CAAAA,CAAAA,mBAAAA,CAAAA,EAAXA,CAAWA,CAAAA,EAAPC,MAAOD,CAAAA,CAAAA,CAAAA;;;AAK1E;;AAAgGA,iBAAxEE,aAAwEF,CAAAA,UAAhDG,MAAgDH,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,CAAAA,CAAAA,IAAAA,CAAAA,EAAAA,CAAAA,KAAAA,EAARI,OAAQJ,CAAAA,CAAAA,CAAAA,EAAAA,aAAAA,EAAmBH,KAAnBG,CAAyBA,CAAzBA,CAAAA,EAAAA,GAAgCF,QAAhCE,CAAyCA,CAAzCA,CAAAA,CAAAA,EAA8CK,aAA9CL,CAA4DA,CAA5DA,CAAAA;KAC3FM,qBADmFF,CAAAA,CAAAA,CAAAA,GAAAA;EAAiCJ,GAAAA,EAAAA,GAAAA,GAE1GA,CAF0GA;EAANH,IAAAA,EAAAA,CAAAA,YAAAA,CAAAA,EAAAA,MAAAA,EAAAA,GAG9EG,CAH8EH;CAAsBG;AAATF,KAKpHG,MALoHH,CAAAA,CAAAA,CAAAA,GAKxGQ,qBALwGR,CAKlFE,CALkFF,CAAAA,GAAAA;EAA4BE,GAAAA,EAAAA,CAAAA,CAAAA,CAAAA,CAAAA,KAAAA,EAMxIA,CANwIA,EAAAA,EAAAA,EAAAA,GAAAA,GAM3HO,CAN2HP,EAAAA,GAMrHO,CANqHP;CAAdK;AAAa,KAQ/IA,aAR+I,CAAA,CAAA,CAAA,GAQ5HC,qBAR4H,CAQtGN,CARsG,CAAA,GAAA;EACtJM,GAAAA,EAAAA,CAAAA,CAAAA,CAAAA,CAAAA,KAAAA,EAQeF,OARfE,CAQuBN,CARF,CAAA,EAAA,EAAAA,EAAAA,GAAA,GAQgBO,CANLP,EAAC,GAMUO,CANV;EAE1BN,IAAAA,EAAAA,CAAAA,WAKUL,EALJI,CAAAA,CAAAA,KAAA,EAKeI,OALf,CAKuBJ,CALvB,CAAA,EAAA,EAAA,EAK+BQ,EAL/B,EAAA,GAKsCA,EALtC;CAA4BR"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { CB, Maybe, Nullable } from "vest-utils";
|
|
2
|
+
|
|
3
|
+
//#region src/context.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Base context interface.
|
|
7
|
+
*/
|
|
8
|
+
declare function createContext<T>(defaultContextValue?: T): CtxApi<T>;
|
|
9
|
+
/**
|
|
10
|
+
* Cascading context - another implementation of context, that assumes the context value is an object.
|
|
11
|
+
* When nesting context runs, the the values of the current layer merges with the layers above it.
|
|
12
|
+
*/
|
|
13
|
+
declare function createCascade<T extends Record<string, unknown>>(init?: (value: Partial<T>, parentContext: Maybe<T>) => Nullable<T>): CtxCascadeApi<T>;
|
|
14
|
+
type ContextConsumptionApi<T> = {
|
|
15
|
+
use: () => T;
|
|
16
|
+
useX: (errorMessage?: string) => T;
|
|
17
|
+
};
|
|
18
|
+
type CtxApi<T> = ContextConsumptionApi<T> & {
|
|
19
|
+
run: <R>(value: T, cb: () => R) => R;
|
|
20
|
+
};
|
|
21
|
+
type CtxCascadeApi<T> = ContextConsumptionApi<T> & {
|
|
22
|
+
run: <R>(value: Partial<T>, fn: () => R) => R;
|
|
23
|
+
bind: <Fn extends CB>(value: Partial<T>, fn: Fn) => Fn;
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
export { CtxApi, CtxCascadeApi, createCascade, createContext };
|
|
27
|
+
//# sourceMappingURL=context.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.mts","names":["CB","Maybe","Nullable","createContext","T","CtxApi","createCascade","Record","Partial","CtxCascadeApi","ContextConsumptionApi","R","Fn"],"sources":["../src/context.d.ts"],"sourcesContent":["import type { CB, Maybe } from 'vest-utils';\nimport { Nullable } from 'vest-utils';\n/**\n * Base context interface.\n */\nexport declare function createContext<T>(defaultContextValue?: T): CtxApi<T>;\n/**\n * Cascading context - another implementation of context, that assumes the context value is an object.\n * When nesting context runs, the the values of the current layer merges with the layers above it.\n */\nexport declare function createCascade<T extends Record<string, unknown>>(init?: (value: Partial<T>, parentContext: Maybe<T>) => Nullable<T>): CtxCascadeApi<T>;\ntype ContextConsumptionApi<T> = {\n use: () => T;\n useX: (errorMessage?: string) => T;\n};\nexport type CtxApi<T> = ContextConsumptionApi<T> & {\n run: <R>(value: T, cb: () => R) => R;\n};\nexport type CtxCascadeApi<T> = ContextConsumptionApi<T> & {\n run: <R>(value: Partial<T>, fn: () => R) => R;\n bind: <Fn extends CB>(value: Partial<T>, fn: Fn) => Fn;\n};\nexport {};\n"],"mappings":";;;;;AAKA;;AAA0EI,iBAAlDD,aAAkDC,CAAAA,CAAAA,CAAAA,CAAAA,mBAAAA,CAAAA,EAAXA,CAAWA,CAAAA,EAAPC,MAAOD,CAAAA,CAAAA,CAAAA;;;AAK1E;;AAAgGA,iBAAxEE,aAAwEF,CAAAA,UAAhDG,MAAgDH,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,CAAAA,CAAAA,IAAAA,CAAAA,EAAAA,CAAAA,KAAAA,EAARI,OAAQJ,CAAAA,CAAAA,CAAAA,EAAAA,aAAAA,EAAmBH,KAAnBG,CAAyBA,CAAzBA,CAAAA,EAAAA,GAAgCF,QAAhCE,CAAyCA,CAAzCA,CAAAA,CAAAA,EAA8CK,aAA9CL,CAA4DA,CAA5DA,CAAAA;KAC3FM,qBADmFF,CAAAA,CAAAA,CAAAA,GAAAA;EAAiCJ,GAAAA,EAAAA,GAAAA,GAE1GA,CAF0GA;EAANH,IAAAA,EAAAA,CAAAA,YAAAA,CAAAA,EAAAA,MAAAA,EAAAA,GAG9EG,CAH8EH;CAAsBG;AAATF,KAKpHG,MALoHH,CAAAA,CAAAA,CAAAA,GAKxGQ,qBALwGR,CAKlFE,CALkFF,CAAAA,GAAAA;EAA4BE,GAAAA,EAAAA,CAAAA,CAAAA,CAAAA,CAAAA,KAAAA,EAMxIA,CANwIA,EAAAA,EAAAA,EAAAA,GAAAA,GAM3HO,CAN2HP,EAAAA,GAMrHO,CANqHP;CAAdK;AAAa,KAQ/IA,aAR+I,CAAA,CAAA,CAAA,GAQ5HC,qBAR4H,CAQtGN,CARsG,CAAA,GAAA;EACtJM,GAAAA,EAAAA,CAAAA,CAAAA,CAAAA,CAAAA,KAAAA,EAQeF,OARfE,CAQuBN,CARF,CAAA,EAAA,EAAAA,EAAAA,GAAA,GAQgBO,CANLP,EAAC,GAMUO,CANV;EAE1BN,IAAAA,EAAAA,CAAAA,WAKUL,EALJI,CAAAA,CAAAA,KAAA,EAKeI,OALf,CAKuBJ,CALvB,CAAA,EAAA,EAAA,EAK+BQ,EAL/B,EAAA,GAKsCA,EALtC;CAA4BR"}
|
package/types/context.d.ts
CHANGED
|
@@ -1,16 +1,27 @@
|
|
|
1
|
-
import { CB } from
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { CB, Maybe, Nullable } from "vest-utils";
|
|
2
|
+
|
|
3
|
+
//#region src/context.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Base context interface.
|
|
7
|
+
*/
|
|
8
|
+
declare function createContext<T>(defaultContextValue?: T): CtxApi<T>;
|
|
9
|
+
/**
|
|
10
|
+
* Cascading context - another implementation of context, that assumes the context value is an object.
|
|
11
|
+
* When nesting context runs, the the values of the current layer merges with the layers above it.
|
|
12
|
+
*/
|
|
13
|
+
declare function createCascade<T extends Record<string, unknown>>(init?: (value: Partial<T>, parentContext: Maybe<T>) => Nullable<T>): CtxCascadeApi<T>;
|
|
4
14
|
type ContextConsumptionApi<T> = {
|
|
5
|
-
|
|
6
|
-
|
|
15
|
+
use: () => T;
|
|
16
|
+
useX: (errorMessage?: string) => T;
|
|
7
17
|
};
|
|
8
18
|
type CtxApi<T> = ContextConsumptionApi<T> & {
|
|
9
|
-
|
|
19
|
+
run: <R>(value: T, cb: () => R) => R;
|
|
10
20
|
};
|
|
11
21
|
type CtxCascadeApi<T> = ContextConsumptionApi<T> & {
|
|
12
|
-
|
|
13
|
-
|
|
22
|
+
run: <R>(value: Partial<T>, fn: () => R) => R;
|
|
23
|
+
bind: <Fn extends CB>(value: Partial<T>, fn: Fn) => Fn;
|
|
14
24
|
};
|
|
15
|
-
|
|
16
|
-
|
|
25
|
+
//#endregion
|
|
26
|
+
export { CtxApi, CtxCascadeApi, createCascade, createContext };
|
|
27
|
+
//# sourceMappingURL=context.d.cts.map
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import path, { resolve } from 'path';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
|
|
4
|
+
import { defineConfig } from 'vitest/config';
|
|
5
|
+
|
|
6
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
+
const __dirname = path.dirname(__filename);
|
|
8
|
+
|
|
9
|
+
export default defineConfig({
|
|
10
|
+
test: {
|
|
11
|
+
globals: true,
|
|
12
|
+
include: ['./**/__tests__/*.test.ts'],
|
|
13
|
+
setupFiles: [resolve(__dirname, '../../', 'vx/config/vitest')],
|
|
14
|
+
},
|
|
15
|
+
root: __dirname,
|
|
16
|
+
resolve: {
|
|
17
|
+
alias: {
|
|
18
|
+
context: resolve(__dirname, 'src/context.ts'),
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
});
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var vestUtils = require('vest-utils');
|
|
6
|
-
|
|
7
|
-
var USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';
|
|
8
|
-
var EMPTY_CONTEXT = Symbol();
|
|
9
|
-
function createContext(defaultContextValue) {
|
|
10
|
-
var contextValue = EMPTY_CONTEXT;
|
|
11
|
-
return {
|
|
12
|
-
run: run,
|
|
13
|
-
use: use,
|
|
14
|
-
useX: useX
|
|
15
|
-
};
|
|
16
|
-
function use() {
|
|
17
|
-
return (isInsideContext() ? contextValue : defaultContextValue);
|
|
18
|
-
}
|
|
19
|
-
function useX(errorMessage) {
|
|
20
|
-
vestUtils.invariant(isInsideContext(), vestUtils.defaultTo(errorMessage, USEX_DEFAULT_ERROR_MESSAGE));
|
|
21
|
-
return contextValue;
|
|
22
|
-
}
|
|
23
|
-
function run(value, cb) {
|
|
24
|
-
var parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
|
|
25
|
-
contextValue = value;
|
|
26
|
-
var res = cb();
|
|
27
|
-
contextValue = parentContext;
|
|
28
|
-
return res;
|
|
29
|
-
}
|
|
30
|
-
function isInsideContext() {
|
|
31
|
-
return contextValue !== EMPTY_CONTEXT;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
function createCascade(init) {
|
|
35
|
-
var ctx = createContext();
|
|
36
|
-
return {
|
|
37
|
-
bind: bind,
|
|
38
|
-
run: run,
|
|
39
|
-
use: ctx.use,
|
|
40
|
-
useX: ctx.useX
|
|
41
|
-
};
|
|
42
|
-
function run(value, fn) {
|
|
43
|
-
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);
|
|
46
|
-
return ctx.run(Object.freeze(out), fn);
|
|
47
|
-
}
|
|
48
|
-
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
|
-
}
|
|
54
|
-
return run(value, function () {
|
|
55
|
-
return fn.apply(void 0, runTimeArgs);
|
|
56
|
-
});
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
exports.createCascade = createCascade;
|
|
62
|
-
exports.createContext = createContext;
|
package/dist/cjs/context.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
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;
|
package/dist/cjs/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"commonjs"}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { invariant, defaultTo, assign, optionalFunctionValue } from 'vest-utils';
|
|
2
|
-
|
|
3
|
-
var USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';
|
|
4
|
-
var EMPTY_CONTEXT = Symbol();
|
|
5
|
-
function createContext(defaultContextValue) {
|
|
6
|
-
var contextValue = EMPTY_CONTEXT;
|
|
7
|
-
return {
|
|
8
|
-
run: run,
|
|
9
|
-
use: use,
|
|
10
|
-
useX: useX
|
|
11
|
-
};
|
|
12
|
-
function use() {
|
|
13
|
-
return (isInsideContext() ? contextValue : defaultContextValue);
|
|
14
|
-
}
|
|
15
|
-
function useX(errorMessage) {
|
|
16
|
-
invariant(isInsideContext(), defaultTo(errorMessage, USEX_DEFAULT_ERROR_MESSAGE));
|
|
17
|
-
return contextValue;
|
|
18
|
-
}
|
|
19
|
-
function run(value, cb) {
|
|
20
|
-
var parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
|
|
21
|
-
contextValue = value;
|
|
22
|
-
var res = cb();
|
|
23
|
-
contextValue = parentContext;
|
|
24
|
-
return res;
|
|
25
|
-
}
|
|
26
|
-
function isInsideContext() {
|
|
27
|
-
return contextValue !== EMPTY_CONTEXT;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
function createCascade(init) {
|
|
31
|
-
var ctx = createContext();
|
|
32
|
-
return {
|
|
33
|
-
bind: bind,
|
|
34
|
-
run: run,
|
|
35
|
-
use: ctx.use,
|
|
36
|
-
useX: ctx.useX
|
|
37
|
-
};
|
|
38
|
-
function run(value, fn) {
|
|
39
|
-
var _a;
|
|
40
|
-
var parentContext = ctx.use();
|
|
41
|
-
var out = assign({}, parentContext ? parentContext : {}, (_a = optionalFunctionValue(init, value, parentContext)) !== null && _a !== void 0 ? _a : value);
|
|
42
|
-
return ctx.run(Object.freeze(out), fn);
|
|
43
|
-
}
|
|
44
|
-
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
|
-
}
|
|
50
|
-
return run(value, function () {
|
|
51
|
-
return fn.apply(void 0, runTimeArgs);
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export { createCascade, createContext };
|
|
@@ -1 +0,0 @@
|
|
|
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};
|
package/dist/es/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|
|
@@ -1,66 +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
|
-
var USEX_DEFAULT_ERROR_MESSAGE = 'Not inside of a running context.';
|
|
8
|
-
var EMPTY_CONTEXT = Symbol();
|
|
9
|
-
function createContext(defaultContextValue) {
|
|
10
|
-
var contextValue = EMPTY_CONTEXT;
|
|
11
|
-
return {
|
|
12
|
-
run: run,
|
|
13
|
-
use: use,
|
|
14
|
-
useX: useX
|
|
15
|
-
};
|
|
16
|
-
function use() {
|
|
17
|
-
return (isInsideContext() ? contextValue : defaultContextValue);
|
|
18
|
-
}
|
|
19
|
-
function useX(errorMessage) {
|
|
20
|
-
vestUtils.invariant(isInsideContext(), vestUtils.defaultTo(errorMessage, USEX_DEFAULT_ERROR_MESSAGE));
|
|
21
|
-
return contextValue;
|
|
22
|
-
}
|
|
23
|
-
function run(value, cb) {
|
|
24
|
-
var parentContext = isInsideContext() ? use() : EMPTY_CONTEXT;
|
|
25
|
-
contextValue = value;
|
|
26
|
-
var res = cb();
|
|
27
|
-
contextValue = parentContext;
|
|
28
|
-
return res;
|
|
29
|
-
}
|
|
30
|
-
function isInsideContext() {
|
|
31
|
-
return contextValue !== EMPTY_CONTEXT;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
function createCascade(init) {
|
|
35
|
-
var ctx = createContext();
|
|
36
|
-
return {
|
|
37
|
-
bind: bind,
|
|
38
|
-
run: run,
|
|
39
|
-
use: ctx.use,
|
|
40
|
-
useX: ctx.useX
|
|
41
|
-
};
|
|
42
|
-
function run(value, fn) {
|
|
43
|
-
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);
|
|
46
|
-
return ctx.run(Object.freeze(out), fn);
|
|
47
|
-
}
|
|
48
|
-
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
|
-
}
|
|
54
|
-
return run(value, function () {
|
|
55
|
-
return fn.apply(void 0, runTimeArgs);
|
|
56
|
-
});
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
exports.createCascade = createCascade;
|
|
62
|
-
exports.createContext = createContext;
|
|
63
|
-
|
|
64
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
65
|
-
|
|
66
|
-
}));
|
|
@@ -1 +0,0 @@
|
|
|
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})}));
|
package/tsconfig.json
DELETED
package/types/context.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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;CACxD,CAAC"}
|