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.
@@ -1 +0,0 @@
1
- {"version":3,"file":"context.esm.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":";;;;;;;;;;;;;;;;AA4BA,IAAMA,YAAY,GAAG,SAAfA,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W,OAAO,CAACtB,GAAR,GAAcW,KAAtB;AACD;;AACD,WAASa,KAAT;AACE,QAAMxB,GAAG,GAAGJ,GAAG,EAAf;;AAEA,QAAI,CAACF,OAAO,CAACgB,EAAR,CAAWV,GAAX,CAAL,EAAsB;AACpB;AACD;;AAEDH,IAAAA,GAAG,CAACG,GAAG,CAACmB,aAAL,CAAH;AACD;;AACD,WAASM,GAAT,CAAa9B,MAAb,EAAiC+B,EAAjC;AACE,QAAM1B,GAAG,GAAG,IAAIN,OAAJ,CAAY;AAAEG,MAAAA,GAAG,EAAHA,GAAF;AAAOD,MAAAA,GAAG,EAAHA,GAAP;AAAYE,MAAAA,sBAAsB,EAAtBA,sBAAZ;AAAoCC,MAAAA,IAAI,EAAJA;AAApC,KAAZ,EAAwDJ,MAAxD,CAAZ;AAEA,QAAMgC,GAAG,GAAGD,EAAE,CAAC1B,GAAD,CAAd;AAEAwB,IAAAA,KAAK;AACL,WAAOG,GAAP;AACD;;AAED,WAASC,IAAT,CACEjC,MADF,EAEE+B,EAFF;sCAGKG;AAAAA,MAAAA;;;AAEH,WAAO;yCAAYC;AAAAA,QAAAA;;;AACjB,aAAOL,GAAG,CAAC9B,MAAD,EAAS;AACjB,eAAO+B,EAAE,MAAF,SAAMG,IAAN,QAAeC,WAAf,EAAP;AACD,OAFS,CAAV;AAGD,KAJD;AAKD;;AAED,SAAO;AACLlC,IAAAA,GAAG,EAAHA,GADK;AAEL6B,IAAAA,GAAG,EAAHA,GAFK;AAGLG,IAAAA,IAAI,EAAJA;AAHK,GAAP;AAKD;;;;"}
package/dist/index.d.ts DELETED
@@ -1,39 +0,0 @@
1
- declare type TypeCTXRef = {
2
- [key: string]: any;
3
- };
4
- export interface ICTXFN {
5
- (context: Context): any;
6
- }
7
- interface Init {
8
- (ctxRef?: TypeCTXRef, parentContext?: Context | void): TypeCTXRef | null;
9
- }
10
- declare type ContextOptions = {
11
- use: () => Context | void;
12
- set: (value: any) => any;
13
- addQueryableProperties: (ctxRef: TypeCTXRef) => TQueryableProperties;
14
- init?: Init;
15
- };
16
- declare type TQueryableProperties = {
17
- [key: string]: true;
18
- };
19
- export declare type TCTX = {
20
- use: () => Context | void;
21
- run: (ctxRef: TypeCTXRef, fn: ICTXFN) => any;
22
- bind: (ctxRef: TypeCTXRef, fn: (...args: any[]) => any, ...args: any[]) => (...runTimeArgs: any[]) => any;
23
- };
24
- declare class Context {
25
- private _parentContext;
26
- [key: string]: any;
27
- static is(value: any): value is Context;
28
- constructor({ use, set, addQueryableProperties, init }: ContextOptions, ctxRef: TypeCTXRef);
29
- addLookupProperty(key: string): void;
30
- private lookup;
31
- private setParentContext;
32
- get parentContext(): Context | null;
33
- }
34
- declare function createContext(init?: Init): {
35
- use: () => Context | void;
36
- run: (ctxRef: TypeCTXRef, fn: ICTXFN) => any;
37
- bind: (ctxRef: TypeCTXRef, fn: (...args: any[]) => any, ...args: any[]) => (...runTimeArgs: any[]) => any;
38
- };
39
- export default createContext;
package/dist/index.js DELETED
@@ -1,8 +0,0 @@
1
-
2
- 'use strict'
3
-
4
- if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./context.cjs.production.min.js')
6
- } else {
7
- module.exports = require('./context.cjs.development.js')
8
- }
package/src/index.ts DELETED
@@ -1,173 +0,0 @@
1
- type TypeCTXRef = { [key: string]: any };
2
-
3
- export interface ICTXFN {
4
- (context: Context): any;
5
- }
6
- interface Init {
7
- (ctxRef?: TypeCTXRef, parentContext?: Context | void): TypeCTXRef | null;
8
- }
9
-
10
- type ContextOptions = {
11
- use: () => Context | void;
12
- set: (value: any) => any;
13
- addQueryableProperties: (ctxRef: TypeCTXRef) => TQueryableProperties;
14
- init?: Init;
15
- };
16
-
17
- type TQueryableProperties = { [key: string]: true };
18
-
19
- export type TCTX = {
20
- use: () => Context | void;
21
- run: (ctxRef: TypeCTXRef, fn: ICTXFN) => any;
22
- bind: (
23
- ctxRef: TypeCTXRef,
24
- fn: (...args: any[]) => any,
25
- ...args: any[]
26
- ) => (...runTimeArgs: any[]) => any;
27
- };
28
-
29
- const getInnerName = (name: string): string => `__${name}`;
30
-
31
- class Context {
32
- private _parentContext: Context | null = null;
33
- [key: string]: any;
34
-
35
- static is(value: any): value is Context {
36
- return value instanceof Context;
37
- }
38
-
39
- constructor(
40
- { use, set, addQueryableProperties, init }: ContextOptions,
41
- ctxRef: TypeCTXRef
42
- ) {
43
- const ctx = use();
44
-
45
- const usedRef =
46
- typeof init === 'function' ? init(ctxRef, ctx) ?? ctxRef : ctxRef;
47
-
48
- const queryableProperties = addQueryableProperties(usedRef);
49
-
50
- if (usedRef && typeof usedRef === 'object') {
51
- for (const key in queryableProperties) {
52
- if (Object.prototype.hasOwnProperty.call(usedRef, key)) {
53
- this[getInnerName(key)] = usedRef[key];
54
- }
55
- this.addLookupProperty(key);
56
- }
57
- }
58
-
59
- if (ctx) {
60
- this.setParentContext(ctx);
61
- }
62
-
63
- set(this);
64
- }
65
-
66
- addLookupProperty(key: string) {
67
- const innerName = getInnerName(key);
68
-
69
- Object.defineProperty(this, key, {
70
- get() {
71
- return this.lookup(innerName);
72
- },
73
- set(value) {
74
- throw new Error(
75
- `Context: Unable to set "${key}" to \`${JSON.stringify(
76
- value
77
- )}\`. Context properties cannot be set directly. Use context.run() instead.`
78
- );
79
- },
80
- });
81
- }
82
-
83
- // @ts-ignore - we actually do use lookup
84
- private lookup(key: string) {
85
- let ctx: Context = this;
86
- do {
87
- if (ctx.hasOwnProperty(key)) {
88
- return ctx[key];
89
- }
90
- if (Context.is(ctx.parentContext)) {
91
- ctx = ctx.parentContext;
92
- } else {
93
- return;
94
- }
95
- } while (ctx);
96
- }
97
-
98
- private setParentContext(parentContext: Context) {
99
- if (Context.is(this)) {
100
- this._parentContext = parentContext;
101
- }
102
- }
103
-
104
- get parentContext(): Context | null {
105
- return this._parentContext;
106
- }
107
- }
108
-
109
- function createContext(init?: Init) {
110
- const storage = {
111
- ctx: undefined,
112
- };
113
-
114
- const queryableProperties: TQueryableProperties = {};
115
-
116
- function addQueryableProperties(ctxRef: TypeCTXRef): TQueryableProperties {
117
- if (!ctxRef || typeof ctxRef !== 'object') {
118
- return {};
119
- }
120
-
121
- for (const key in ctxRef) {
122
- if (Object.prototype.hasOwnProperty.call(ctxRef, key)) {
123
- queryableProperties[key] = true;
124
- }
125
- }
126
-
127
- return queryableProperties;
128
- }
129
-
130
- function use(): Context | void {
131
- return storage.ctx;
132
- }
133
- function set(value: any) {
134
- return (storage.ctx = value);
135
- }
136
- function clear() {
137
- const ctx = use();
138
-
139
- if (!Context.is(ctx)) {
140
- return;
141
- }
142
-
143
- set(ctx.parentContext);
144
- }
145
- function run(ctxRef: TypeCTXRef, fn: ICTXFN) {
146
- const ctx = new Context({ set, use, addQueryableProperties, init }, ctxRef);
147
-
148
- const res = fn(ctx);
149
-
150
- clear();
151
- return res;
152
- }
153
-
154
- function bind(
155
- ctxRef: TypeCTXRef,
156
- fn: (...args: any[]) => any,
157
- ...args: any[]
158
- ) {
159
- return function(...runTimeArgs: any[]) {
160
- return run(ctxRef, function() {
161
- return fn(...args, ...runTimeArgs);
162
- });
163
- };
164
- }
165
-
166
- return {
167
- use,
168
- run,
169
- bind,
170
- };
171
- }
172
-
173
- export default createContext;