@webiny/api 0.0.0-unstable.e622468070 → 0.0.0-unstable.e9333b3f0b

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.
Files changed (56) hide show
  1. package/Benchmark.js +120 -149
  2. package/Benchmark.js.map +1 -1
  3. package/Context.d.ts +1 -3
  4. package/Context.js +59 -130
  5. package/Context.js.map +1 -1
  6. package/abstractions.d.ts +5 -0
  7. package/abstractions.js +5 -0
  8. package/abstractions.js.map +1 -0
  9. package/createConditionalPluginFactory.js +5 -8
  10. package/createConditionalPluginFactory.js.map +1 -1
  11. package/decorateContext.js +10 -12
  12. package/decorateContext.js.map +1 -1
  13. package/helpers/InterfaceGenerator/date.js +0 -3
  14. package/helpers/InterfaceGenerator/id.js +0 -3
  15. package/helpers/InterfaceGenerator/identity.js +0 -3
  16. package/helpers/InterfaceGenerator/index.d.ts +1 -0
  17. package/helpers/InterfaceGenerator/index.js +0 -3
  18. package/helpers/InterfaceGenerator/numeric.js +0 -3
  19. package/helpers/InterfaceGenerator/text.d.ts +11 -0
  20. package/helpers/InterfaceGenerator/text.js +0 -0
  21. package/helpers/InterfaceGenerator/truthful.js +0 -3
  22. package/index.d.ts +1 -3
  23. package/index.js +1 -5
  24. package/package.json +17 -14
  25. package/plugins/BenchmarkPlugin.js +18 -19
  26. package/plugins/BenchmarkPlugin.js.map +1 -1
  27. package/testing/index.d.ts +3 -0
  28. package/testing/index.js +3 -0
  29. package/testing/lifecycleTracker.d.ts +14 -0
  30. package/testing/lifecycleTracker.js +37 -0
  31. package/testing/lifecycleTracker.js.map +1 -0
  32. package/testing/sleep.d.ts +1 -0
  33. package/testing/sleep.js +6 -0
  34. package/testing/sleep.js.map +1 -0
  35. package/testing/until.d.ts +7 -0
  36. package/testing/until.js +22 -0
  37. package/testing/until.js.map +1 -0
  38. package/types.d.ts +1 -6
  39. package/types.js +0 -3
  40. package/ServiceDiscovery.d.ts +0 -12
  41. package/ServiceDiscovery.js +0 -70
  42. package/ServiceDiscovery.js.map +0 -1
  43. package/helpers/InterfaceGenerator/date.js.map +0 -1
  44. package/helpers/InterfaceGenerator/id.js.map +0 -1
  45. package/helpers/InterfaceGenerator/identity.js.map +0 -1
  46. package/helpers/InterfaceGenerator/index.js.map +0 -1
  47. package/helpers/InterfaceGenerator/numeric.js.map +0 -1
  48. package/helpers/InterfaceGenerator/truthful.js.map +0 -1
  49. package/index.js.map +0 -1
  50. package/plugins/CompressorPlugin.d.ts +0 -14
  51. package/plugins/CompressorPlugin.js +0 -14
  52. package/plugins/CompressorPlugin.js.map +0 -1
  53. package/plugins/ContextPlugin.d.ts +0 -16
  54. package/plugins/ContextPlugin.js +0 -23
  55. package/plugins/ContextPlugin.js.map +0 -1
  56. package/types.js.map +0 -1
package/Benchmark.js CHANGED
@@ -1,153 +1,124 @@
1
- var BenchmarkState = /*#__PURE__*/function (BenchmarkState) {
2
- BenchmarkState["DISABLED"] = "disabled";
3
- BenchmarkState["ENABLED"] = "enabled";
4
- BenchmarkState["UNDETERMINED"] = "undetermined";
5
- return BenchmarkState;
6
- }(BenchmarkState || {});
7
- const createDefaultOutputCallable = () => {
8
- return async ({
9
- benchmark
10
- }) => {
11
- console.log(`Benchmark total time elapsed: ${benchmark.elapsed}ms`);
12
- console.log("Benchmark measurements:");
13
- console.log(benchmark.measurements);
14
- };
15
- };
16
- export class Benchmark {
17
- measurements = [];
18
- outputDone = false;
19
- isAlreadyRunning = false;
20
- totalElapsed = 0;
21
- runs = {};
22
- enableOnCallables = [];
23
- onOutputCallables = [];
24
- state = BenchmarkState.UNDETERMINED;
25
- get elapsed() {
26
- return this.totalElapsed;
27
- }
28
- enableOn(cb) {
29
- this.enableOnCallables.push(cb);
30
- }
31
- onOutput(cb) {
32
- this.onOutputCallables.push(cb);
33
- }
34
- enable() {
35
- this.setState(BenchmarkState.ENABLED);
36
- }
37
- disable() {
38
- this.setState(BenchmarkState.DISABLED);
39
- }
40
-
41
- /**
42
- * When running the output, we need to reverse the callables array, so that the last one added is the first one executed.
43
- *
44
- * The last one is our built-in console.log output.
45
- */
46
- async output() {
47
- /**
48
- * No point in outputting more than once or if no measurements were made.
49
- */
50
- if (this.outputDone || this.measurements.length === 0) {
51
- return;
52
- }
53
- const callables = [...this.onOutputCallables].reverse();
54
- callables.push(createDefaultOutputCallable());
55
- for (const cb of callables) {
56
- const result = await cb({
57
- benchmark: this,
58
- stop: () => "stop"
59
- });
60
- if (result === "stop") {
61
- return;
62
- }
63
- }
64
- this.outputDone = true;
65
- }
66
- async measure(options, cb) {
67
- const enabled = await this.getIsEnabled();
68
- if (!enabled) {
69
- return cb();
70
- }
71
- const measurement = this.startMeasurement(options);
72
- const isAlreadyRunning = this.getIsAlreadyRunning();
73
- this.startRunning();
74
- try {
75
- return await cb();
76
- } finally {
77
- const measurementEnded = this.stopMeasurement(measurement);
78
- this.measurements.push(measurementEnded);
79
- this.addRun(measurementEnded);
80
- /**
81
- * Only add to total time if this run is not a child of another run.
82
- * And then end running.
83
- */
84
- if (!isAlreadyRunning) {
85
- this.addElapsed(measurementEnded);
86
- this.endRunning();
87
- }
88
- }
89
- }
90
- getIsAlreadyRunning() {
91
- return this.isAlreadyRunning;
92
- }
93
- startRunning() {
94
- this.isAlreadyRunning = true;
95
- }
96
- endRunning() {
97
- this.isAlreadyRunning = false;
98
- }
99
- async getIsEnabled() {
100
- if (this.state === BenchmarkState.ENABLED) {
101
- return true;
102
- } else if (this.state === BenchmarkState.DISABLED) {
103
- return false;
104
- }
105
- for (const cb of this.enableOnCallables) {
106
- const result = await cb();
107
- if (result) {
108
- this.enable();
109
- return true;
110
- }
111
- }
112
- this.disable();
113
- return false;
114
- }
115
- addElapsed(measurement) {
116
- this.totalElapsed = this.totalElapsed + measurement.elapsed;
117
- }
118
- addRun(measurement) {
119
- const name = `${measurement.category}#${measurement.name}`;
120
- if (!this.runs[name]) {
121
- this.runs[name] = 0;
122
- }
123
- this.runs[name]++;
124
- }
125
- setState(state) {
126
- this.state = state;
127
- }
128
- startMeasurement(options) {
129
- const name = typeof options === "string" ? options : options.name;
130
- const category = typeof options === "string" ? "webiny" : options.category;
131
- return {
132
- name,
133
- category,
134
- start: new Date(),
135
- memoryStart: process.memoryUsage().heapUsed
136
- };
137
- }
138
- stopMeasurement(measurement) {
139
- const end = new Date();
140
- const memoryEnd = process.memoryUsage().heapUsed;
141
- const elapsed = end.getTime() - measurement.start.getTime();
142
- return {
143
- name: measurement.name,
144
- category: measurement.category,
145
- start: measurement.start,
146
- end,
147
- elapsed,
148
- memory: memoryEnd - measurement.memoryStart
1
+ const createDefaultOutputCallable = ()=>async ({ benchmark })=>{
2
+ console.log(`Benchmark total time elapsed: ${benchmark.elapsed}ms`);
3
+ console.log("Benchmark measurements:");
4
+ console.log(benchmark.measurements);
149
5
  };
150
- }
6
+ class Benchmark {
7
+ get elapsed() {
8
+ return this.totalElapsed;
9
+ }
10
+ enableOn(cb) {
11
+ this.enableOnCallables.push(cb);
12
+ }
13
+ onOutput(cb) {
14
+ this.onOutputCallables.push(cb);
15
+ }
16
+ enable() {
17
+ this.setState("enabled");
18
+ }
19
+ disable() {
20
+ this.setState("disabled");
21
+ }
22
+ async output() {
23
+ if (this.outputDone || 0 === this.measurements.length) return;
24
+ const callables = [
25
+ ...this.onOutputCallables
26
+ ].reverse();
27
+ callables.push(createDefaultOutputCallable());
28
+ for (const cb of callables){
29
+ const result = await cb({
30
+ benchmark: this,
31
+ stop: ()=>"stop"
32
+ });
33
+ if ("stop" === result) return;
34
+ }
35
+ this.outputDone = true;
36
+ }
37
+ async measure(options, cb) {
38
+ const enabled = await this.getIsEnabled();
39
+ if (!enabled) return cb();
40
+ const measurement = this.startMeasurement(options);
41
+ const isAlreadyRunning = this.getIsAlreadyRunning();
42
+ this.startRunning();
43
+ try {
44
+ return await cb();
45
+ } finally{
46
+ const measurementEnded = this.stopMeasurement(measurement);
47
+ this.measurements.push(measurementEnded);
48
+ this.addRun(measurementEnded);
49
+ if (!isAlreadyRunning) {
50
+ this.addElapsed(measurementEnded);
51
+ this.endRunning();
52
+ }
53
+ }
54
+ }
55
+ getIsAlreadyRunning() {
56
+ return this.isAlreadyRunning;
57
+ }
58
+ startRunning() {
59
+ this.isAlreadyRunning = true;
60
+ }
61
+ endRunning() {
62
+ this.isAlreadyRunning = false;
63
+ }
64
+ async getIsEnabled() {
65
+ if ("enabled" === this.state) return true;
66
+ if ("disabled" === this.state) return false;
67
+ for (const cb of this.enableOnCallables){
68
+ const result = await cb();
69
+ if (result) {
70
+ this.enable();
71
+ return true;
72
+ }
73
+ }
74
+ this.disable();
75
+ return false;
76
+ }
77
+ addElapsed(measurement) {
78
+ this.totalElapsed = this.totalElapsed + measurement.elapsed;
79
+ }
80
+ addRun(measurement) {
81
+ const name = `${measurement.category}#${measurement.name}`;
82
+ if (!this.runs[name]) this.runs[name] = 0;
83
+ this.runs[name]++;
84
+ }
85
+ setState(state) {
86
+ this.state = state;
87
+ }
88
+ startMeasurement(options) {
89
+ const name = "string" == typeof options ? options : options.name;
90
+ const category = "string" == typeof options ? "webiny" : options.category;
91
+ return {
92
+ name,
93
+ category,
94
+ start: new Date(),
95
+ memoryStart: process.memoryUsage().heapUsed
96
+ };
97
+ }
98
+ stopMeasurement(measurement) {
99
+ const end = new Date();
100
+ const memoryEnd = process.memoryUsage().heapUsed;
101
+ const elapsed = end.getTime() - measurement.start.getTime();
102
+ return {
103
+ name: measurement.name,
104
+ category: measurement.category,
105
+ start: measurement.start,
106
+ end,
107
+ elapsed,
108
+ memory: memoryEnd - measurement.memoryStart
109
+ };
110
+ }
111
+ constructor(){
112
+ this.measurements = [];
113
+ this.outputDone = false;
114
+ this.isAlreadyRunning = false;
115
+ this.totalElapsed = 0;
116
+ this.runs = {};
117
+ this.enableOnCallables = [];
118
+ this.onOutputCallables = [];
119
+ this.state = "undetermined";
120
+ }
151
121
  }
122
+ export { Benchmark };
152
123
 
153
124
  //# sourceMappingURL=Benchmark.js.map
package/Benchmark.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["BenchmarkState","createDefaultOutputCallable","benchmark","console","log","elapsed","measurements","Benchmark","outputDone","isAlreadyRunning","totalElapsed","runs","enableOnCallables","onOutputCallables","state","UNDETERMINED","enableOn","cb","push","onOutput","enable","setState","ENABLED","disable","DISABLED","output","length","callables","reverse","result","stop","measure","options","enabled","getIsEnabled","measurement","startMeasurement","getIsAlreadyRunning","startRunning","measurementEnded","stopMeasurement","addRun","addElapsed","endRunning","name","category","start","Date","memoryStart","process","memoryUsage","heapUsed","end","memoryEnd","getTime","memory"],"sources":["Benchmark.ts"],"sourcesContent":["import type {\n Benchmark as BenchmarkInterface,\n BenchmarkEnableOnCallable,\n BenchmarkMeasurement,\n BenchmarkMeasureOptions,\n BenchmarkOutputCallable,\n BenchmarkRuns\n} from \"~/types.js\";\n\nenum BenchmarkState {\n DISABLED = \"disabled\",\n ENABLED = \"enabled\",\n UNDETERMINED = \"undetermined\"\n}\n\ninterface BenchmarkMeasurementStart\n extends Pick<BenchmarkMeasurement, \"name\" | \"category\" | \"start\"> {\n memoryStart: number;\n}\n\nconst createDefaultOutputCallable = (): BenchmarkOutputCallable => {\n return async ({ benchmark }) => {\n console.log(`Benchmark total time elapsed: ${benchmark.elapsed}ms`);\n console.log(\"Benchmark measurements:\");\n console.log(benchmark.measurements);\n };\n};\n\nexport class Benchmark implements BenchmarkInterface {\n public readonly measurements: BenchmarkMeasurement[] = [];\n\n private outputDone = false;\n private isAlreadyRunning = false;\n private totalElapsed = 0;\n public readonly runs: BenchmarkRuns = {};\n private readonly enableOnCallables: BenchmarkEnableOnCallable[] = [];\n private readonly onOutputCallables: BenchmarkOutputCallable[] = [];\n private state: BenchmarkState = BenchmarkState.UNDETERMINED;\n\n public get elapsed(): number {\n return this.totalElapsed;\n }\n\n public enableOn(cb: BenchmarkEnableOnCallable): void {\n this.enableOnCallables.push(cb);\n }\n\n public onOutput(cb: BenchmarkOutputCallable): void {\n this.onOutputCallables.push(cb);\n }\n\n public enable(): void {\n this.setState(BenchmarkState.ENABLED);\n }\n\n public disable(): void {\n this.setState(BenchmarkState.DISABLED);\n }\n\n /**\n * When running the output, we need to reverse the callables array, so that the last one added is the first one executed.\n *\n * The last one is our built-in console.log output.\n */\n public async output(): Promise<void> {\n /**\n * No point in outputting more than once or if no measurements were made.\n */\n if (this.outputDone || this.measurements.length === 0) {\n return;\n }\n const callables = [...this.onOutputCallables].reverse();\n callables.push(createDefaultOutputCallable());\n for (const cb of callables) {\n const result = await cb({\n benchmark: this,\n stop: () => \"stop\"\n });\n if (result === \"stop\") {\n return;\n }\n }\n this.outputDone = true;\n }\n\n public async measure<T = any>(\n options: BenchmarkMeasureOptions | string,\n cb: () => Promise<T>\n ): Promise<T> {\n const enabled = await this.getIsEnabled();\n if (!enabled) {\n return cb();\n }\n const measurement = this.startMeasurement(options);\n const isAlreadyRunning = this.getIsAlreadyRunning();\n this.startRunning();\n try {\n return await cb();\n } finally {\n const measurementEnded = this.stopMeasurement(measurement);\n this.measurements.push(measurementEnded);\n this.addRun(measurementEnded);\n /**\n * Only add to total time if this run is not a child of another run.\n * And then end running.\n */\n if (!isAlreadyRunning) {\n this.addElapsed(measurementEnded);\n this.endRunning();\n }\n }\n }\n\n private getIsAlreadyRunning(): boolean {\n return this.isAlreadyRunning;\n }\n private startRunning(): void {\n this.isAlreadyRunning = true;\n }\n private endRunning(): void {\n this.isAlreadyRunning = false;\n }\n\n private async getIsEnabled(): Promise<boolean> {\n if (this.state === BenchmarkState.ENABLED) {\n return true;\n } else if (this.state === BenchmarkState.DISABLED) {\n return false;\n }\n\n for (const cb of this.enableOnCallables) {\n const result = await cb();\n if (result) {\n this.enable();\n return true;\n }\n }\n this.disable();\n return false;\n }\n\n private addElapsed(measurement: Pick<BenchmarkMeasurement, \"elapsed\">): void {\n this.totalElapsed = this.totalElapsed + measurement.elapsed;\n }\n\n private addRun(measurement: Pick<BenchmarkMeasurement, \"name\" | \"category\">): void {\n const name = `${measurement.category}#${measurement.name}`;\n if (!this.runs[name]) {\n this.runs[name] = 0;\n }\n this.runs[name]++;\n }\n\n private setState(state: BenchmarkState): void {\n this.state = state;\n }\n\n private startMeasurement(options: BenchmarkMeasureOptions | string): BenchmarkMeasurementStart {\n const name = typeof options === \"string\" ? options : options.name;\n const category = typeof options === \"string\" ? \"webiny\" : options.category;\n return {\n name,\n category,\n start: new Date(),\n memoryStart: process.memoryUsage().heapUsed\n };\n }\n\n private stopMeasurement(measurement: BenchmarkMeasurementStart): BenchmarkMeasurement {\n const end = new Date();\n const memoryEnd = process.memoryUsage().heapUsed;\n const elapsed = end.getTime() - measurement.start.getTime();\n return {\n name: measurement.name,\n category: measurement.category,\n start: measurement.start,\n end,\n elapsed,\n memory: memoryEnd - measurement.memoryStart\n };\n }\n}\n"],"mappings":"IASKA,cAAc,0BAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAA,OAAdA,cAAc;AAAA,EAAdA,cAAc;AAWnB,MAAMC,2BAA2B,GAAGA,CAAA,KAA+B;EAC/D,OAAO,OAAO;IAAEC;EAAU,CAAC,KAAK;IAC5BC,OAAO,CAACC,GAAG,CAAC,iCAAiCF,SAAS,CAACG,OAAO,IAAI,CAAC;IACnEF,OAAO,CAACC,GAAG,CAAC,yBAAyB,CAAC;IACtCD,OAAO,CAACC,GAAG,CAACF,SAAS,CAACI,YAAY,CAAC;EACvC,CAAC;AACL,CAAC;AAED,OAAO,MAAMC,SAAS,CAA+B;EACjCD,YAAY,GAA2B,EAAE;EAEjDE,UAAU,GAAG,KAAK;EAClBC,gBAAgB,GAAG,KAAK;EACxBC,YAAY,GAAG,CAAC;EACRC,IAAI,GAAkB,CAAC,CAAC;EACvBC,iBAAiB,GAAgC,EAAE;EACnDC,iBAAiB,GAA8B,EAAE;EAC1DC,KAAK,GAAmBd,cAAc,CAACe,YAAY;EAE3D,IAAWV,OAAOA,CAAA,EAAW;IACzB,OAAO,IAAI,CAACK,YAAY;EAC5B;EAEOM,QAAQA,CAACC,EAA6B,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SAAS,GAAGlB,WAAW,CAACa;IACpC,CAAC;EACL;AACJ","ignoreList":[]}
1
+ {"version":3,"file":"Benchmark.js","sources":["../src/Benchmark.ts"],"sourcesContent":["import type {\n Benchmark as BenchmarkInterface,\n BenchmarkEnableOnCallable,\n BenchmarkMeasurement,\n BenchmarkMeasureOptions,\n BenchmarkOutputCallable,\n BenchmarkRuns\n} from \"~/types.js\";\n\nenum BenchmarkState {\n DISABLED = \"disabled\",\n ENABLED = \"enabled\",\n UNDETERMINED = \"undetermined\"\n}\n\ninterface BenchmarkMeasurementStart extends Pick<\n BenchmarkMeasurement,\n \"name\" | \"category\" | \"start\"\n> {\n memoryStart: number;\n}\n\nconst createDefaultOutputCallable = (): BenchmarkOutputCallable => {\n return async ({ benchmark }) => {\n console.log(`Benchmark total time elapsed: ${benchmark.elapsed}ms`);\n console.log(\"Benchmark measurements:\");\n console.log(benchmark.measurements);\n };\n};\n\nexport class Benchmark implements BenchmarkInterface {\n public readonly measurements: BenchmarkMeasurement[] = [];\n\n private outputDone = false;\n private isAlreadyRunning = false;\n private totalElapsed = 0;\n public readonly runs: BenchmarkRuns = {};\n private readonly enableOnCallables: BenchmarkEnableOnCallable[] = [];\n private readonly onOutputCallables: BenchmarkOutputCallable[] = [];\n private state: BenchmarkState = BenchmarkState.UNDETERMINED;\n\n public get elapsed(): number {\n return this.totalElapsed;\n }\n\n public enableOn(cb: BenchmarkEnableOnCallable): void {\n this.enableOnCallables.push(cb);\n }\n\n public onOutput(cb: BenchmarkOutputCallable): void {\n this.onOutputCallables.push(cb);\n }\n\n public enable(): void {\n this.setState(BenchmarkState.ENABLED);\n }\n\n public disable(): void {\n this.setState(BenchmarkState.DISABLED);\n }\n\n /**\n * When running the output, we need to reverse the callables array, so that the last one added is the first one executed.\n *\n * The last one is our built-in console.log output.\n */\n public async output(): Promise<void> {\n /**\n * No point in outputting more than once or if no measurements were made.\n */\n if (this.outputDone || this.measurements.length === 0) {\n return;\n }\n const callables = [...this.onOutputCallables].reverse();\n callables.push(createDefaultOutputCallable());\n for (const cb of callables) {\n const result = await cb({\n benchmark: this,\n stop: () => \"stop\"\n });\n if (result === \"stop\") {\n return;\n }\n }\n this.outputDone = true;\n }\n\n public async measure<T = any>(\n options: BenchmarkMeasureOptions | string,\n cb: () => Promise<T>\n ): Promise<T> {\n const enabled = await this.getIsEnabled();\n if (!enabled) {\n return cb();\n }\n const measurement = this.startMeasurement(options);\n const isAlreadyRunning = this.getIsAlreadyRunning();\n this.startRunning();\n try {\n return await cb();\n } finally {\n const measurementEnded = this.stopMeasurement(measurement);\n this.measurements.push(measurementEnded);\n this.addRun(measurementEnded);\n /**\n * Only add to total time if this run is not a child of another run.\n * And then end running.\n */\n if (!isAlreadyRunning) {\n this.addElapsed(measurementEnded);\n this.endRunning();\n }\n }\n }\n\n private getIsAlreadyRunning(): boolean {\n return this.isAlreadyRunning;\n }\n private startRunning(): void {\n this.isAlreadyRunning = true;\n }\n private endRunning(): void {\n this.isAlreadyRunning = false;\n }\n\n private async getIsEnabled(): Promise<boolean> {\n if (this.state === BenchmarkState.ENABLED) {\n return true;\n } else if (this.state === BenchmarkState.DISABLED) {\n return false;\n }\n\n for (const cb of this.enableOnCallables) {\n const result = await cb();\n if (result) {\n this.enable();\n return true;\n }\n }\n this.disable();\n return false;\n }\n\n private addElapsed(measurement: Pick<BenchmarkMeasurement, \"elapsed\">): void {\n this.totalElapsed = this.totalElapsed + measurement.elapsed;\n }\n\n private addRun(measurement: Pick<BenchmarkMeasurement, \"name\" | \"category\">): void {\n const name = `${measurement.category}#${measurement.name}`;\n if (!this.runs[name]) {\n this.runs[name] = 0;\n }\n this.runs[name]++;\n }\n\n private setState(state: BenchmarkState): void {\n this.state = state;\n }\n\n private startMeasurement(options: BenchmarkMeasureOptions | string): BenchmarkMeasurementStart {\n const name = typeof options === \"string\" ? options : options.name;\n const category = typeof options === \"string\" ? \"webiny\" : options.category;\n return {\n name,\n category,\n start: new Date(),\n memoryStart: process.memoryUsage().heapUsed\n };\n }\n\n private stopMeasurement(measurement: BenchmarkMeasurementStart): BenchmarkMeasurement {\n const end = new Date();\n const memoryEnd = process.memoryUsage().heapUsed;\n const elapsed = end.getTime() - measurement.start.getTime();\n return {\n name: measurement.name,\n category: measurement.category,\n start: measurement.start,\n end,\n elapsed,\n memory: memoryEnd - measurement.memoryStart\n };\n }\n}\n"],"names":["createDefaultOutputCallable","benchmark","console","Benchmark","cb","callables","result","options","enabled","measurement","isAlreadyRunning","measurementEnded","name","state","category","Date","process","end","memoryEnd","elapsed"],"mappings":"AAsBA,MAAMA,8BAA8B,IACzB,OAAO,EAAEC,SAAS,EAAE;QACvBC,QAAQ,GAAG,CAAC,CAAC,8BAA8B,EAAED,UAAU,OAAO,CAAC,EAAE,CAAC;QAClEC,QAAQ,GAAG,CAAC;QACZA,QAAQ,GAAG,CAACD,UAAU,YAAY;IACtC;AAGG,MAAME;IAWT,IAAW,UAAkB;QACzB,OAAO,IAAI,CAAC,YAAY;IAC5B;IAEO,SAASC,EAA6B,EAAQ;QACjD,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAACA;IAChC;IAEO,SAASA,EAA2B,EAAQ;QAC/C,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAACA;IAChC;IAEO,SAAe;QAClB,IAAI,CAAC,QAAQ,CAAC;IAClB;IAEO,UAAgB;QACnB,IAAI,CAAC,QAAQ,CAAC;IAClB;IAOA,MAAa,SAAwB;QAIjC,IAAI,IAAI,CAAC,UAAU,IAAI,AAA6B,MAA7B,IAAI,CAAC,YAAY,CAAC,MAAM,EAC3C;QAEJ,MAAMC,YAAY;eAAI,IAAI,CAAC,iBAAiB;SAAC,CAAC,OAAO;QACrDA,UAAU,IAAI,CAACL;QACf,KAAK,MAAMI,MAAMC,UAAW;YACxB,MAAMC,SAAS,MAAMF,GAAG;gBACpB,WAAW,IAAI;gBACf,MAAM,IAAM;YAChB;YACA,IAAIE,AAAW,WAAXA,QACA;QAER;QACA,IAAI,CAAC,UAAU,GAAG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EAChB,IAAI,CAAC,IAAI,CAACA,KAAK,GAAG;QAEtB,IAAI,CAAC,IAAI,CAACA,KAAK;IACnB;IAEQ,SAASC,KAAqB,EAAQ;QAC1C,IAAI,CAAC,KAAK,GAAGA;IACjB;IAEQ,iBAAiBN,OAAyC,EAA6B;QAC3F,MAAMK,OAAO,AAAmB,YAAnB,OAAOL,UAAuBA,UAAUA,QAAQ,IAAI;QACjE,MAAMO,WAAW,AAAmB,YAAnB,OAAOP,UAAuB,WAAWA,QAAQ,QAAQ;QAC1E,OAAO;YACHK;YACAE;YACA,OAAO,IAAIC;YACX,aAAaC,QAAQ,WAAW,GAAG,QAAQ;QAC/C;IACJ;IAEQ,gBAAgBP,WAAsC,EAAwB;QAClF,MAAMQ,MAAM,IAAIF;QAChB,MAAMG,YAAYF,QAAQ,WAAW,GAAG,QAAQ;QAChD,MAAMG,UAAUF,IAAI,OAAO,KAAKR,YAAY,KAAK,CAAC,OAAO;QACzD,OAAO;YACH,MAAMA,YAAY,IAAI;YACtB,UAAUA,YAAY,QAAQ;YAC9B,OAAOA,YAAY,KAAK;YACxBQ;YACAE;YACA,QAAQD,YAAYT,YAAY,WAAW;QAC/C;IACJ;;aAvJgB,YAAY,GAA2B,EAAE;aAEjD,UAAU,GAAG;aACb,gBAAgB,GAAG;aACnB,YAAY,GAAG;aACP,IAAI,GAAkB,CAAC;aACtB,iBAAiB,GAAgC,EAAE;aACnD,iBAAiB,GAA8B,EAAE;aAC1D,KAAK,GAAL;;AAgJZ"}
package/Context.d.ts CHANGED
@@ -2,8 +2,7 @@ import type { Context as ContextInterface } from "./types.js";
2
2
  import { PluginsContainer } from "@webiny/plugins";
3
3
  import type { PluginCollection } from "@webiny/plugins/types.js";
4
4
  import { Benchmark } from "./Benchmark.js";
5
- import type { ICompressor } from "@webiny/utils/compression/Compressor.js";
6
- import { Container } from "@webiny/di";
5
+ import { Container } from "@webiny/feature/api";
7
6
  export interface ContextParams {
8
7
  plugins?: PluginCollection | PluginsContainer;
9
8
  WEBINY_VERSION: string;
@@ -14,7 +13,6 @@ export declare class Context implements ContextInterface {
14
13
  readonly plugins: PluginsContainer;
15
14
  readonly WEBINY_VERSION: string;
16
15
  readonly benchmark: Benchmark;
17
- readonly compressor: ICompressor;
18
16
  readonly container: Container;
19
17
  private readonly waiters;
20
18
  constructor(params: ContextParams);
package/Context.js CHANGED
@@ -1,139 +1,68 @@
1
1
  import { PluginsContainer } from "@webiny/plugins";
2
2
  import { Benchmark } from "./Benchmark.js";
3
3
  import { BenchmarkPlugin } from "./plugins/BenchmarkPlugin.js";
4
- import { createDefaultCompressor } from "@webiny/utils/compression/index.js";
5
- import { CompressorPlugin } from "./plugins/CompressorPlugin.js";
6
- import { Container } from "@webiny/di";
7
- const getPluginsContainer = plugins => {
8
- if (!plugins) {
9
- return new PluginsContainer();
10
- }
11
- if (plugins instanceof PluginsContainer) {
12
- return plugins;
13
- }
14
- return new PluginsContainer(plugins);
4
+ import { Container } from "@webiny/feature/api";
5
+ import { CompressionFeature } from "@webiny/utils/features/compression/feature.js";
6
+ const getPluginsContainer = (plugins)=>{
7
+ if (!plugins) return new PluginsContainer();
8
+ if (plugins instanceof PluginsContainer) return plugins;
9
+ return new PluginsContainer(plugins);
15
10
  };
16
- export class Context {
17
- waiters = [];
18
- constructor(params) {
19
- const {
20
- plugins,
21
- WEBINY_VERSION
22
- } = params;
23
- this.plugins = getPluginsContainer(plugins);
24
- this.WEBINY_VERSION = WEBINY_VERSION;
25
- this.container = new Container();
26
- /**
27
- * At the moment, let's have benchmark as part of the context.
28
- * Also, register the plugin to have benchmark accessible via plugins container.
29
- */
30
- this.benchmark = new Benchmark();
31
- this.compressor = createDefaultCompressor({
32
- plugins: this.plugins
33
- });
34
- this.plugins.register(new BenchmarkPlugin(this.benchmark), new CompressorPlugin({
35
- getCompressor: () => {
36
- return this.compressor;
37
- }
38
- }));
39
- }
40
- getResult() {
41
- return this._result;
42
- }
43
- hasResult() {
44
- return !!this._result;
45
- }
46
- setResult(value) {
47
- this._result = value;
48
- }
49
- waitFor(obj, cb) {
50
- const initialTargets = Array.isArray(obj) ? obj : [obj];
51
- const targets = [];
52
- /**
53
- * We go only through the first level properties
54
- */
55
- for (const key in initialTargets) {
56
- const target = initialTargets[key];
57
- /**
58
- * If property already exists, there is no need to wait for it, so we just continue the loop.
59
- * Also, if target is not a string, skip this property as it will fail to convert properly during the runtime.
60
- */
61
- if (this[target]) {
62
- continue;
63
- } else if (typeof target !== "string") {
64
- continue;
65
- }
66
- /**
67
- * Since there is no property, we must define it with its setter and getter.
68
- * We could not know when it got defined otherwise.
69
- */
70
- Object.defineProperty(this, target, {
71
- /**
72
- * Setter sets the given value to this object.
73
- * We cannot set it on exact property name it is defined because it would go into loop of setting itself.
74
- * And that is why we add __ around the property name.
75
- */
76
- set: value => {
77
- const newTargetKey = `__${target}__`;
78
- this[newTargetKey] = value;
79
- /**
80
- * WWhen the property is set, we will go through all the waiters and, if any of them include currently set property, act on it.
81
- */
82
- for (const waiter of this.waiters) {
83
- if (waiter.targets.includes(target) === false) {
84
- continue;
85
- }
86
- /**
87
- * Remove currently set property so we know if there are any more to be waited for.
88
- */
89
- waiter.targets = waiter.targets.filter(t => t !== target);
90
- /**
91
- * If there are more to be waited, eg. user added [cms, pageBuilder] as waited properties, we just continue the loop.
92
- */
93
- if (waiter.targets.length > 0) {
94
- continue;
95
- }
96
- /**
97
- * And if there is nothing more to be waited for, we execute the callable.
98
- * Note that this callable is not async.
99
- */
100
- waiter.cb(this);
101
- }
102
- },
103
- /**
104
- * As we have set property with __ around it, we must get it as well.
105
- */
106
- get: () => {
107
- const newTargetKey = `__${target}__`;
108
- return this[newTargetKey];
109
- },
110
- configurable: false
111
- });
112
- /**
113
- * We add the target to be awaited.
114
- */
115
- targets.push(target);
11
+ class Context {
12
+ constructor(params){
13
+ this.waiters = [];
14
+ const { plugins, WEBINY_VERSION } = params;
15
+ this.plugins = getPluginsContainer(plugins);
16
+ this.WEBINY_VERSION = WEBINY_VERSION;
17
+ this.container = new Container();
18
+ CompressionFeature.register(this.container);
19
+ this.benchmark = new Benchmark();
20
+ this.plugins.register(new BenchmarkPlugin(this.benchmark));
21
+ }
22
+ getResult() {
23
+ return this._result;
116
24
  }
117
- /**
118
- * If there are no targets to be awaited, just fire the callable.
119
- */
120
- if (targets.length === 0) {
121
- cb(this);
122
- return;
25
+ hasResult() {
26
+ return !!this._result;
27
+ }
28
+ setResult(value) {
29
+ this._result = value;
30
+ }
31
+ waitFor(obj, cb) {
32
+ const initialTargets = Array.isArray(obj) ? obj : [
33
+ obj
34
+ ];
35
+ const targets = [];
36
+ for(const key in initialTargets){
37
+ const target = initialTargets[key];
38
+ if (!this[target]) {
39
+ if ("string" == typeof target) {
40
+ Object.defineProperty(this, target, {
41
+ set: (value)=>{
42
+ const newTargetKey = `__${target}__`;
43
+ this[newTargetKey] = value;
44
+ for (const waiter of this.waiters)if (false !== waiter.targets.includes(target)) {
45
+ waiter.targets = waiter.targets.filter((t)=>t !== target);
46
+ if (!(waiter.targets.length > 0)) waiter.cb(this);
47
+ }
48
+ },
49
+ get: ()=>{
50
+ const newTargetKey = `__${target}__`;
51
+ return this[newTargetKey];
52
+ },
53
+ configurable: false
54
+ });
55
+ targets.push(target);
56
+ }
57
+ }
58
+ }
59
+ if (0 === targets.length) return void cb(this);
60
+ this.waiters.push({
61
+ targets,
62
+ cb
63
+ });
123
64
  }
124
- /**
125
- * Otherwise add the waiter for the target properties.
126
- */
127
- this.waiters.push({
128
- targets,
129
- /**
130
- * TODO @ts-refactor
131
- * Problem with possible subtype initialization
132
- */
133
- // @ts-expect-error
134
- cb
135
- });
136
- }
137
65
  }
66
+ export { Context };
138
67
 
139
68
  //# sourceMappingURL=Context.js.map
package/Context.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["PluginsContainer","Benchmark","BenchmarkPlugin","createDefaultCompressor","CompressorPlugin","Container","getPluginsContainer","plugins","Context","waiters","constructor","params","WEBINY_VERSION","container","benchmark","compressor","register","getCompressor","getResult","_result","hasResult","setResult","value","waitFor","obj","cb","initialTargets","Array","isArray","targets","key","target","Object","defineProperty","set","newTargetKey","waiter","includes","filter","t","length","get","configurable","push"],"sources":["Context.ts"],"sourcesContent":["import type { Context as ContextInterface } from \"~/types.js\";\nimport { PluginsContainer } from \"@webiny/plugins\";\nimport type { PluginCollection } from \"@webiny/plugins/types.js\";\nimport { Benchmark } from \"~/Benchmark.js\";\nimport { BenchmarkPlugin } from \"~/plugins/BenchmarkPlugin.js\";\nimport type { ICompressor } from \"@webiny/utils/compression/Compressor.js\";\nimport { createDefaultCompressor } from \"@webiny/utils/compression/index.js\";\nimport { CompressorPlugin } from \"~/plugins/CompressorPlugin.js\";\nimport { Container } from \"@webiny/di\";\n\ninterface Waiter {\n targets: string[];\n cb: (context: ContextInterface) => void;\n}\n\nexport interface ContextParams {\n plugins?: PluginCollection | PluginsContainer;\n WEBINY_VERSION: string;\n}\n\nconst getPluginsContainer = (plugins?: PluginCollection | PluginsContainer): PluginsContainer => {\n if (!plugins) {\n return new PluginsContainer();\n }\n if (plugins instanceof PluginsContainer) {\n return plugins;\n }\n return new PluginsContainer(plugins);\n};\n\nexport class Context implements ContextInterface {\n public _result: any;\n public args: any;\n public readonly plugins: PluginsContainer;\n public readonly WEBINY_VERSION: string;\n public readonly benchmark: Benchmark;\n public readonly compressor: ICompressor;\n public readonly container: Container;\n\n private readonly waiters: Waiter[] = [];\n\n public constructor(params: ContextParams) {\n const { plugins, WEBINY_VERSION } = params;\n this.plugins = getPluginsContainer(plugins);\n this.WEBINY_VERSION = WEBINY_VERSION;\n this.container = new Container();\n /**\n * At the moment, let's have benchmark as part of the context.\n * Also, register the plugin to have benchmark accessible via plugins container.\n */\n this.benchmark = new Benchmark();\n this.compressor = createDefaultCompressor({\n plugins: this.plugins\n });\n this.plugins.register(\n new BenchmarkPlugin(this.benchmark),\n new CompressorPlugin({\n getCompressor: () => {\n return this.compressor;\n }\n })\n );\n }\n\n public getResult(): any {\n return this._result;\n }\n\n public hasResult(): boolean {\n return !!this._result;\n }\n\n public setResult(value: any): void {\n this._result = value;\n }\n\n public waitFor<T extends ContextInterface = ContextInterface>(\n obj: string | string[],\n cb: (context: T) => void\n ): void {\n const initialTargets = Array.isArray(obj) ? obj : [obj];\n const targets: string[] = [];\n /**\n * We go only through the first level properties\n */\n for (const key in initialTargets) {\n const target = initialTargets[key] as keyof this;\n /**\n * If property already exists, there is no need to wait for it, so we just continue the loop.\n * Also, if target is not a string, skip this property as it will fail to convert properly during the runtime.\n */\n if (this[target]) {\n continue;\n } else if (typeof target !== \"string\") {\n continue;\n }\n /**\n * Since there is no property, we must define it with its setter and getter.\n * We could not know when it got defined otherwise.\n */\n Object.defineProperty(this, target, {\n /**\n * Setter sets the given value to this object.\n * We cannot set it on exact property name it is defined because it would go into loop of setting itself.\n * And that is why we add __ around the property name.\n */\n set: (value: any) => {\n const newTargetKey = `__${target}__` as keyof this;\n this[newTargetKey] = value;\n /**\n * WWhen the property is set, we will go through all the waiters and, if any of them include currently set property, act on it.\n */\n for (const waiter of this.waiters) {\n if (waiter.targets.includes(target) === false) {\n continue;\n }\n /**\n * Remove currently set property so we know if there are any more to be waited for.\n */\n waiter.targets = waiter.targets.filter(t => t !== target);\n /**\n * If there are more to be waited, eg. user added [cms, pageBuilder] as waited properties, we just continue the loop.\n */\n if (waiter.targets.length > 0) {\n continue;\n }\n /**\n * And if there is nothing more to be waited for, we execute the callable.\n * Note that this callable is not async.\n */\n waiter.cb(this);\n }\n },\n /**\n * As we have set property with __ around it, we must get it as well.\n */\n get: (): any => {\n const newTargetKey = `__${target}__` as keyof this;\n return this[newTargetKey];\n },\n configurable: false\n });\n /**\n * We add the target to be awaited.\n */\n targets.push(target as string);\n }\n /**\n * If there are no targets to be awaited, just fire the callable.\n */\n if (targets.length === 0) {\n cb(this as unknown as T);\n return;\n }\n /**\n * Otherwise add the waiter for the target properties.\n */\n this.waiters.push({\n targets,\n /**\n * TODO @ts-refactor\n * Problem with possible subtype initialization\n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,CAAC;QACD;AAChB;AACA;QACgBgB,GAAG,EAAEA,CAAA,KAAW;UACZ,MAAMN,YAAY,GAAG,KAAKJ,MAAM,IAAkB;UAClD,OAAO,IAAI,CAACI,YAAY,CAAC;QAC7B,CAAC;QACDO,YAAY,EAAE;MAClB,CAAC,CAAC;MACF;AACZ;AACA;MACYb,OAAO,CAACc,IAAI,CAACZ,MAAgB,CAAC;IAClC;IACA;AACR;AACA;IACQ,IAAIF,OAAO,CAACW,MAAM,KAAK,CAAC,EAAE;MACtBf,EAAE,CAAC,IAAoB,CAAC;MACxB;IACJ;IACA;AACR;AACA;IACQ,IAAI,CAAChB,OAAO,CAACkC,IAAI,CAAC;MACdd,OAAO;MACP;AACZ;AACA;AACA;MACY;MACAJ;IACJ,CAAC,CAAC;EACN;AACJ","ignoreList":[]}
1
+ {"version":3,"file":"Context.js","sources":["../src/Context.ts"],"sourcesContent":["import type { Context as ContextInterface } from \"~/types.js\";\nimport { PluginsContainer } from \"@webiny/plugins\";\nimport type { PluginCollection } from \"@webiny/plugins/types.js\";\nimport { Benchmark } from \"~/Benchmark.js\";\nimport { BenchmarkPlugin } from \"~/plugins/BenchmarkPlugin.js\";\nimport { Container } from \"@webiny/feature/api\";\nimport { CompressionFeature } from \"@webiny/utils/features/compression/feature.js\";\n\ninterface Waiter {\n targets: string[];\n cb: (context: ContextInterface) => void;\n}\n\nexport interface ContextParams {\n plugins?: PluginCollection | PluginsContainer;\n WEBINY_VERSION: string;\n}\n\nconst getPluginsContainer = (plugins?: PluginCollection | PluginsContainer): PluginsContainer => {\n if (!plugins) {\n return new PluginsContainer();\n }\n if (plugins instanceof PluginsContainer) {\n return plugins;\n }\n return new PluginsContainer(plugins);\n};\n\nexport class Context implements ContextInterface {\n public _result: any;\n public args: any;\n public readonly plugins: PluginsContainer;\n public readonly WEBINY_VERSION: string;\n public readonly benchmark: Benchmark;\n public readonly container: Container;\n\n private readonly waiters: Waiter[] = [];\n\n public constructor(params: ContextParams) {\n const { plugins, WEBINY_VERSION } = params;\n this.plugins = getPluginsContainer(plugins);\n this.WEBINY_VERSION = WEBINY_VERSION;\n this.container = new Container();\n /**\n * Register base features.\n */\n CompressionFeature.register(this.container);\n /**\n * At the moment, let's have benchmark as part of the context.\n * Also, register the plugin to have benchmark accessible via plugins container.\n */\n this.benchmark = new Benchmark();\n this.plugins.register(new BenchmarkPlugin(this.benchmark));\n }\n\n public getResult(): any {\n return this._result;\n }\n\n public hasResult(): boolean {\n return !!this._result;\n }\n\n public setResult(value: any): void {\n this._result = value;\n }\n\n public waitFor<T extends ContextInterface = ContextInterface>(\n obj: string | string[],\n cb: (context: T) => void\n ): void {\n const initialTargets = Array.isArray(obj) ? obj : [obj];\n const targets: string[] = [];\n /**\n * We go only through the first level properties\n */\n for (const key in initialTargets) {\n const target = initialTargets[key] as keyof this;\n /**\n * If property already exists, there is no need to wait for it, so we just continue the loop.\n * Also, if target is not a string, skip this property as it will fail to convert properly during the runtime.\n */\n if (this[target]) {\n continue;\n } else if (typeof target !== \"string\") {\n continue;\n }\n /**\n * Since there is no property, we must define it with its setter and getter.\n * We could not know when it got defined otherwise.\n */\n Object.defineProperty(this, target, {\n /**\n * Setter sets the given value to this object.\n * We cannot set it on exact property name it is defined because it would go into loop of setting itself.\n * And that is why we add __ around the property name.\n */\n set: (value: any) => {\n const newTargetKey = `__${target}__` as keyof this;\n this[newTargetKey] = value;\n /**\n * WWhen the property is set, we will go through all the waiters and, if any of them include currently set property, act on it.\n */\n for (const waiter of this.waiters) {\n if (waiter.targets.includes(target) === false) {\n continue;\n }\n /**\n * Remove currently set property so we know if there are any more to be waited for.\n */\n waiter.targets = waiter.targets.filter(t => t !== target);\n /**\n * If there are more to be waited, eg. user added [cms, pageBuilder] as waited properties, we just continue the loop.\n */\n if (waiter.targets.length > 0) {\n continue;\n }\n /**\n * And if there is nothing more to be waited for, we execute the callable.\n * Note that this callable is not async.\n */\n waiter.cb(this);\n }\n },\n /**\n * As we have set property with __ around it, we must get it as well.\n */\n get: (): any => {\n const newTargetKey = `__${target}__` as keyof this;\n return this[newTargetKey];\n },\n configurable: false\n });\n /**\n * We add the target to be awaited.\n */\n targets.push(target as string);\n }\n /**\n * If there are no targets to be awaited, just fire the callable.\n */\n if (targets.length === 0) {\n cb(this as unknown as T);\n return;\n }\n /**\n * Otherwise add the waiter for the target properties.\n */\n this.waiters.push({\n targets,\n /**\n * TODO @ts-refactor\n * Problem with possible subtype initialization\n */\n // @ts-expect-error\n cb\n });\n }\n}\n"],"names":["getPluginsContainer","plugins","PluginsContainer","Context","params","WEBINY_VERSION","Container","CompressionFeature","Benchmark","BenchmarkPlugin","value","obj","cb","initialTargets","Array","targets","key","target","Object","newTargetKey","waiter","t"],"mappings":";;;;;AAkBA,MAAMA,sBAAsB,CAACC;IACzB,IAAI,CAACA,SACD,OAAO,IAAIC;IAEf,IAAID,mBAAmBC,kBACnB,OAAOD;IAEX,OAAO,IAAIC,iBAAiBD;AAChC;AAEO,MAAME;IAUT,YAAmBC,MAAqB,CAAE;aAFzB,OAAO,GAAa,EAAE;QAGnC,MAAM,EAAEH,OAAO,EAAEI,cAAc,EAAE,GAAGD;QACpC,IAAI,CAAC,OAAO,GAAGJ,oBAAoBC;QACnC,IAAI,CAAC,cAAc,GAAGI;QACtB,IAAI,CAAC,SAAS,GAAG,IAAIC;QAIrBC,mBAAmB,QAAQ,CAAC,IAAI,CAAC,SAAS;QAK1C,IAAI,CAAC,SAAS,GAAG,IAAIC;QACrB,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAIC,gBAAgB,IAAI,CAAC,SAAS;IAC5D;IAEO,YAAiB;QACpB,OAAO,IAAI,CAAC,OAAO;IACvB;IAEO,YAAqB;QACxB,OAAO,CAAC,CAAC,IAAI,CAAC,OAAO;IACzB;IAEO,UAAUC,KAAU,EAAQ;QAC/B,IAAI,CAAC,OAAO,GAAGA;IACnB;IAEO,QACHC,GAAsB,EACtBC,EAAwB,EACpB;QACJ,MAAMC,iBAAiBC,MAAM,OAAO,CAACH,OAAOA,MAAM;YAACA;SAAI;QACvD,MAAMI,UAAoB,EAAE;QAI5B,IAAK,MAAMC,OAAOH,eAAgB;YAC9B,MAAMI,SAASJ,cAAc,CAACG,IAAI;YAKlC,KAAI,IAAI,CAACC,OAAO,EAET;gBAAA,IAAI,AAAkB,YAAlB,OAAOA;oBAOlBC,OAAO,cAAc,CAAC,IAAI,EAAED,QAAQ;wBAMhC,KAAK,CAACP;4BACF,MAAMS,eAAe,CAAC,EAAE,EAAEF,OAAO,EAAE,CAAC;4BACpC,IAAI,CAACE,aAAa,GAAGT;4BAIrB,KAAK,MAAMU,UAAU,IAAI,CAAC,OAAO,CAC7B,IAAIA,AAAoC,UAApCA,OAAO,OAAO,CAAC,QAAQ,CAACH;gCAM5BG,OAAO,OAAO,GAAGA,OAAO,OAAO,CAAC,MAAM,CAACC,CAAAA,IAAKA,MAAMJ;gCAIlD,KAAIG,CAAAA,OAAO,OAAO,CAAC,MAAM,GAAG,IAO5BA,OAAO,EAAE,CAAC,IAAI;;wBAEtB;wBAIA,KAAK;4BACD,MAAMD,eAAe,CAAC,EAAE,EAAEF,OAAO,EAAE,CAAC;4BACpC,OAAO,IAAI,CAACE,aAAa;wBAC7B;wBACA,cAAc;oBAClB;oBAIAJ,QAAQ,IAAI,CAACE;;YAlDb;QAmDJ;QAIA,IAAIF,AAAmB,MAAnBA,QAAQ,MAAM,EAAQ,YACtBH,GAAG,IAAI;QAMX,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;YACdG;YAMAH;QACJ;IACJ;AACJ"}
@@ -0,0 +1,5 @@
1
+ import type { Benchmark as BenchmarkInterface } from "./types.js";
2
+ export declare const BenchmarkAbstraction: import("@webiny/di").Abstraction<BenchmarkInterface>;
3
+ export declare namespace BenchmarkAbstraction {
4
+ type Interface = BenchmarkInterface;
5
+ }
@@ -0,0 +1,5 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ const BenchmarkAbstraction = createAbstraction("Benchmark");
3
+ export { BenchmarkAbstraction };
4
+
5
+ //# sourceMappingURL=abstractions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abstractions.js","sources":["../src/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Benchmark as BenchmarkInterface } from \"./types.js\";\n\nexport const BenchmarkAbstraction = createAbstraction<BenchmarkInterface>(\"Benchmark\");\nexport namespace BenchmarkAbstraction {\n export type Interface = BenchmarkInterface;\n}\n"],"names":["BenchmarkAbstraction","createAbstraction"],"mappings":";AAGO,MAAMA,uBAAuBC,kBAAsC"}
@@ -1,10 +1,7 @@
1
- export const createConditionalPluginFactory = (condition, cb) => {
2
- return () => {
3
- if (condition()) {
4
- return cb();
5
- }
6
- return Promise.resolve([]);
7
- };
8
- };
1
+ const createConditionalPluginFactory = (condition, cb)=>()=>{
2
+ if (condition()) return cb();
3
+ return Promise.resolve([]);
4
+ };
5
+ export { createConditionalPluginFactory };
9
6
 
10
7
  //# sourceMappingURL=createConditionalPluginFactory.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["createConditionalPluginFactory","condition","cb","Promise","resolve"],"sources":["createConditionalPluginFactory.ts"],"sourcesContent":["import type { PluginFactory } from \"@webiny/plugins/types.js\";\n\nexport type Condition = () => boolean;\n\nexport const createConditionalPluginFactory = (\n condition: Condition,\n cb: PluginFactory\n): PluginFactory => {\n return () => {\n if (condition()) {\n return cb();\n }\n\n return Promise.resolve([]);\n };\n};\n"],"mappings":"AAIA,OAAO,MAAMA,8BAA8B,GAAGA,CAC1CC,SAAoB,EACpBC,EAAiB,KACD;EAChB,OAAO,MAAM;IACT,IAAID,SAAS,CAAC,CAAC,EAAE;MACb,OAAOC,EAAE,CAAC,CAAC;IACf;IAEA,OAAOC,OAAO,CAACC,OAAO,CAAC,EAAE,CAAC;EAC9B,CAAC;AACL,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"createConditionalPluginFactory.js","sources":["../src/createConditionalPluginFactory.ts"],"sourcesContent":["import type { PluginFactory } from \"@webiny/plugins/types.js\";\n\nexport type Condition = () => boolean;\n\nexport const createConditionalPluginFactory = (\n condition: Condition,\n cb: PluginFactory\n): PluginFactory => {\n return () => {\n if (condition()) {\n return cb();\n }\n\n return Promise.resolve([]);\n };\n};\n"],"names":["createConditionalPluginFactory","condition","cb","Promise"],"mappings":"AAIO,MAAMA,iCAAiC,CAC1CC,WACAC,KAEO;QACH,IAAID,aACA,OAAOC;QAGX,OAAOC,QAAQ,OAAO,CAAC,EAAE;IAC7B"}
@@ -1,14 +1,12 @@
1
- export const decorateContext = (decoratee, decorators) => {
2
- return Object.keys(decorators).reduce((acc, key) => {
3
- const decoratedMethod = acc[key];
4
- return {
5
- ...acc,
6
- [key]: function (...args) {
7
- // @ts-expect-error
8
- return decorators[key](decoratedMethod.bind(decoratee))(...args);
9
- }
10
- };
11
- }, decoratee);
12
- };
1
+ const decorateContext = (decoratee, decorators)=>Object.keys(decorators).reduce((acc, key)=>{
2
+ const decoratedMethod = acc[key];
3
+ return {
4
+ ...acc,
5
+ [key]: function(...args) {
6
+ return decorators[key](decoratedMethod.bind(decoratee))(...args);
7
+ }
8
+ };
9
+ }, decoratee);
10
+ export { decorateContext };
13
11
 
14
12
  //# sourceMappingURL=decorateContext.js.map