@webiny/api 5.34.8-beta.1 → 5.35.0-beta.0

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/Benchmark.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { Benchmark as BenchmarkInterface, BenchmarkEnableOnCallable, BenchmarkMeasurement, BenchmarkMeasureOptions, BenchmarkOutputCallable, BenchmarkRuns } from "./types";
2
+ export declare class Benchmark implements BenchmarkInterface {
3
+ readonly measurements: BenchmarkMeasurement[];
4
+ private outputDone;
5
+ private isAlreadyRunning;
6
+ private totalElapsed;
7
+ readonly runs: BenchmarkRuns;
8
+ private readonly enableOnCallables;
9
+ private readonly onOutputCallables;
10
+ private state;
11
+ get elapsed(): number;
12
+ constructor();
13
+ enableOn(cb: BenchmarkEnableOnCallable): void;
14
+ onOutput(cb: BenchmarkOutputCallable): void;
15
+ enable(): void;
16
+ disable(): void;
17
+ /**
18
+ * When running the output, we need to reverse the callables array, so that the last one added is the first one executed.
19
+ *
20
+ * The first one is our built-in console.log output, which we want to be the last one executed - and we need to stop output if user wants to end it.
21
+ */
22
+ output(): Promise<void>;
23
+ measure<T = any>(options: BenchmarkMeasureOptions | string, cb: () => Promise<T>): Promise<T>;
24
+ private getIsAlreadyRunning;
25
+ private startRunning;
26
+ private endRunning;
27
+ private getIsEnabled;
28
+ private addElapsed;
29
+ private addRun;
30
+ private setState;
31
+ private startMeasurement;
32
+ private stopMeasurement;
33
+ }
package/Benchmark.js ADDED
@@ -0,0 +1,161 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.Benchmark = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var BenchmarkState;
10
+ (function (BenchmarkState) {
11
+ BenchmarkState["DISABLED"] = "disabled";
12
+ BenchmarkState["ENABLED"] = "enabled";
13
+ BenchmarkState["UNDETERMINED"] = "undetermined";
14
+ })(BenchmarkState || (BenchmarkState = {}));
15
+ class Benchmark {
16
+ get elapsed() {
17
+ return this.totalElapsed;
18
+ }
19
+ constructor() {
20
+ (0, _defineProperty2.default)(this, "measurements", []);
21
+ (0, _defineProperty2.default)(this, "outputDone", false);
22
+ (0, _defineProperty2.default)(this, "isAlreadyRunning", false);
23
+ (0, _defineProperty2.default)(this, "totalElapsed", 0);
24
+ (0, _defineProperty2.default)(this, "runs", {});
25
+ (0, _defineProperty2.default)(this, "enableOnCallables", []);
26
+ (0, _defineProperty2.default)(this, "onOutputCallables", []);
27
+ (0, _defineProperty2.default)(this, "state", BenchmarkState.UNDETERMINED);
28
+ /**
29
+ * The default output is to the console.
30
+ * This one is executed after all other user defined outputs.
31
+ */
32
+ this.onOutputCallables.push(async () => {
33
+ console.log(`Benchmark total time elapsed: ${this.elapsed}ms`);
34
+ console.log("Benchmark measurements:");
35
+ console.log(this.measurements);
36
+ });
37
+ }
38
+ enableOn(cb) {
39
+ this.enableOnCallables.push(cb);
40
+ }
41
+ onOutput(cb) {
42
+ this.onOutputCallables.push(cb);
43
+ }
44
+ enable() {
45
+ this.setState(BenchmarkState.ENABLED);
46
+ }
47
+ disable() {
48
+ this.setState(BenchmarkState.DISABLED);
49
+ }
50
+
51
+ /**
52
+ * When running the output, we need to reverse the callables array, so that the last one added is the first one executed.
53
+ *
54
+ * The first one is our built-in console.log output, which we want to be the last one executed - and we need to stop output if user wants to end it.
55
+ */
56
+ async output() {
57
+ /**
58
+ * No point in outputting more than once or if no measurements were made.
59
+ */
60
+ if (this.outputDone || this.measurements.length === 0) {
61
+ return;
62
+ }
63
+ const callables = this.onOutputCallables.reverse();
64
+ for (const cb of callables) {
65
+ const result = await cb({
66
+ benchmark: this,
67
+ stop: () => "stop"
68
+ });
69
+ if (result === "stop") {
70
+ return;
71
+ }
72
+ }
73
+ this.outputDone = true;
74
+ }
75
+ async measure(options, cb) {
76
+ const enabled = await this.getIsEnabled();
77
+ if (!enabled) {
78
+ return cb();
79
+ }
80
+ const measurement = this.startMeasurement(options);
81
+ const isAlreadyRunning = this.getIsAlreadyRunning();
82
+ this.startRunning();
83
+ try {
84
+ return await cb();
85
+ } finally {
86
+ const measurementEnded = this.stopMeasurement(measurement);
87
+ this.measurements.push(measurementEnded);
88
+ this.addRun(measurementEnded);
89
+ /**
90
+ * Only add to total time if this run is not a child of another run.
91
+ * And then end running.
92
+ */
93
+ if (!isAlreadyRunning) {
94
+ this.addElapsed(measurementEnded);
95
+ this.endRunning();
96
+ }
97
+ }
98
+ }
99
+ getIsAlreadyRunning() {
100
+ return this.isAlreadyRunning;
101
+ }
102
+ startRunning() {
103
+ this.isAlreadyRunning = true;
104
+ }
105
+ endRunning() {
106
+ this.isAlreadyRunning = false;
107
+ }
108
+ async getIsEnabled() {
109
+ if (this.state === BenchmarkState.ENABLED) {
110
+ return true;
111
+ } else if (this.state === BenchmarkState.DISABLED) {
112
+ return false;
113
+ }
114
+ for (const cb of this.enableOnCallables) {
115
+ const result = await cb();
116
+ if (result) {
117
+ this.enable();
118
+ return true;
119
+ }
120
+ }
121
+ this.disable();
122
+ return false;
123
+ }
124
+ addElapsed(measurement) {
125
+ this.totalElapsed = this.totalElapsed + measurement.elapsed;
126
+ }
127
+ addRun(measurement) {
128
+ const name = `${measurement.category}#${measurement.name}`;
129
+ if (!this.runs[name]) {
130
+ this.runs[name] = 0;
131
+ }
132
+ this.runs[name]++;
133
+ }
134
+ setState(state) {
135
+ this.state = state;
136
+ }
137
+ startMeasurement(options) {
138
+ const name = typeof options === "string" ? options : options.name;
139
+ const category = typeof options === "string" ? "webiny" : options.category;
140
+ return {
141
+ name,
142
+ category,
143
+ start: new Date(),
144
+ memoryStart: process.memoryUsage().heapUsed
145
+ };
146
+ }
147
+ stopMeasurement(measurement) {
148
+ const end = new Date();
149
+ const memoryEnd = process.memoryUsage().heapUsed;
150
+ const elapsed = end.getTime() - measurement.start.getTime();
151
+ return {
152
+ name: measurement.name,
153
+ category: measurement.category,
154
+ start: measurement.start,
155
+ end,
156
+ elapsed,
157
+ memory: memoryEnd - measurement.memoryStart
158
+ };
159
+ }
160
+ }
161
+ exports.Benchmark = Benchmark;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["BenchmarkState","Benchmark","elapsed","totalElapsed","constructor","UNDETERMINED","onOutputCallables","push","console","log","measurements","enableOn","cb","enableOnCallables","onOutput","enable","setState","ENABLED","disable","DISABLED","output","outputDone","length","callables","reverse","result","benchmark","stop","measure","options","enabled","getIsEnabled","measurement","startMeasurement","isAlreadyRunning","getIsAlreadyRunning","startRunning","measurementEnded","stopMeasurement","addRun","addElapsed","endRunning","state","name","category","runs","start","Date","memoryStart","process","memoryUsage","heapUsed","end","memoryEnd","getTime","memory"],"sources":["Benchmark.ts"],"sourcesContent":["import {\n Benchmark as BenchmarkInterface,\n BenchmarkEnableOnCallable,\n BenchmarkMeasurement,\n BenchmarkMeasureOptions,\n BenchmarkOutputCallable,\n BenchmarkRuns\n} from \"~/types\";\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\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 constructor() {\n /**\n * The default output is to the console.\n * This one is executed after all other user defined outputs.\n */\n this.onOutputCallables.push(async () => {\n console.log(`Benchmark total time elapsed: ${this.elapsed}ms`);\n console.log(\"Benchmark measurements:\");\n console.log(this.measurements);\n });\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 first one is our built-in console.log output, which we want to be the last one executed - and we need to stop output if user wants to end it.\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 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;AAAA,WAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;AAAA,GAAdA,cAAc,KAAdA,cAAc;AAWZ,MAAMC,SAAS,CAA+B;EAWjD,IAAWC,OAAO,GAAW;IACzB,OAAO,IAAI,CAACC,YAAY;EAC5B;EAEOC,WAAW,GAAG;IAAA,oDAdkC,EAAE;IAAA,kDAEpC,KAAK;IAAA,wDACC,KAAK;IAAA,oDACT,CAAC;IAAA,4CACc,CAAC,CAAC;IAAA,yDAC0B,EAAE;IAAA,yDACJ,EAAE;IAAA,6CAClCJ,cAAc,CAACK,YAAY;IAOvD;AACR;AACA;AACA;IACQ,IAAI,CAACC,iBAAiB,CAACC,IAAI,CAAC,YAAY;MACpCC,OAAO,CAACC,GAAG,CAAE,iCAAgC,IAAI,CAACP,OAAQ,IAAG,CAAC;MAC9DM,OAAO,CAACC,GAAG,CAAC,yBAAyB,C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AAS,IAAGZ,WAAW,CAACW,IAAK,EAAC;IAC1D,IAAI,CAAC,IAAI,CAACE,IAAI,CAACF,IAAI,CAAC,EAAE;MAClB,IAAI,CAACE,IAAI,CAACF,IAAI,CAAC,GAAG,CAAC;IACvB;IACA,IAAI,CAACE,IAAI,CAACF,IAAI,CAAC,EAAE;EACrB;EAEQ3B,QAAQ,CAAC0B,KAAqB,EAAQ;IAC1C,IAAI,CAACA,KAAK,GAAGA,KAAK;EACtB;EAEQT,gBAAgB,CAACJ,OAAyC,EAA6B;IAC3F,MAAMc,IAAI,GAAG,OAAOd,OAAO,KAAK,QAAQ,GAAGA,OAAO,GAAGA,OAAO,CAACc,IAAI;IACjE,MAAMC,QAAQ,GAAG,OAAOf,OAAO,KAAK,QAAQ,GAAG,QAAQ,GAAGA,OAAO,CAACe,QAAQ;IAC1E,OAAO;MACHD,IAAI;MACJC,QAAQ;MACRE,KAAK,EAAE,IAAIC,IAAI,EAAE;MACjBC,WAAW,EAAEC,OAAO,CAACC,WAAW,EAAE,CAACC;IACvC,CAAC;EACL;EAEQb,eAAe,CAACN,WAAsC,EAAwB;IAClF,MAAMoB,GAAG,GAAG,IAAIL,IAAI,EAAE;IACtB,MAAMM,SAAS,GAAGJ,OAAO,CAACC,WAAW,EAAE,CAACC,QAAQ;IAChD,MAAMjD,OAAO,GAAGkD,GAAG,CAACE,OAAO,EAAE,GAAGtB,WAAW,CAACc,KAAK,CAACQ,OAAO,EAAE;IAC3D,OAAO;MACHX,IAAI,EAAEX,WAAW,CAACW,IAAI;MACtBC,QAAQ,EAAEZ,WAAW,CAACY,QAAQ;MAC9BE,KAAK,EAAEd,WAAW,CAACc,KAAK;MACxBM,GAAG;MACHlD,OAAO;MACPqD,MAAM,EAAEF,SAAS,GAAGrB,WAAW,CAACgB;IACpC,CAAC;EACL;AACJ;AAAC"}
package/Context.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Context as ContextInterface } from "./types";
2
2
  import { PluginsContainer } from "@webiny/plugins";
3
3
  import { PluginCollection } from "@webiny/plugins/types";
4
+ import { Benchmark } from "./Benchmark";
4
5
  export interface ContextParams {
5
6
  plugins?: PluginCollection;
6
7
  WEBINY_VERSION: string;
@@ -10,6 +11,7 @@ export declare class Context implements ContextInterface {
10
11
  args: any;
11
12
  readonly plugins: PluginsContainer;
12
13
  readonly WEBINY_VERSION: string;
14
+ readonly benchmark: Benchmark;
13
15
  private readonly waiters;
14
16
  constructor(params: ContextParams);
15
17
  getResult(): any;
package/Context.js CHANGED
@@ -1,22 +1,21 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.Context = void 0;
9
-
10
8
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
9
  var _plugins = require("@webiny/plugins");
13
-
10
+ var _Benchmark = require("./Benchmark");
11
+ var _BenchmarkPlugin = require("./plugins/BenchmarkPlugin");
14
12
  class Context {
15
13
  constructor(params) {
16
14
  (0, _defineProperty2.default)(this, "_result", void 0);
17
15
  (0, _defineProperty2.default)(this, "args", void 0);
18
16
  (0, _defineProperty2.default)(this, "plugins", void 0);
19
17
  (0, _defineProperty2.default)(this, "WEBINY_VERSION", void 0);
18
+ (0, _defineProperty2.default)(this, "benchmark", void 0);
20
19
  (0, _defineProperty2.default)(this, "waiters", []);
21
20
  const {
22
21
  plugins,
@@ -24,34 +23,34 @@ class Context {
24
23
  } = params;
25
24
  this.plugins = new _plugins.PluginsContainer(plugins || []);
26
25
  this.WEBINY_VERSION = WEBINY_VERSION;
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.Benchmark();
31
+ this.plugins.register(new _BenchmarkPlugin.BenchmarkPlugin(this.benchmark));
27
32
  }
28
-
29
33
  getResult() {
30
34
  return this._result;
31
35
  }
32
-
33
36
  hasResult() {
34
37
  return !!this._result;
35
38
  }
36
-
37
39
  setResult(value) {
38
40
  this._result = value;
39
41
  }
40
-
41
42
  waitFor(obj, cb) {
42
43
  const initialTargets = Array.isArray(obj) ? obj : [obj];
43
44
  const targets = [];
44
45
  /**
45
46
  * We go only through the first level properties
46
47
  */
47
-
48
48
  for (const key in initialTargets) {
49
49
  const target = initialTargets[key];
50
50
  /**
51
51
  * If property already exists, there is no need to wait for it, so we just continue the loop.
52
52
  * Also, if target is not a string, skip this property as it will fail to convert properly during the runtime.
53
53
  */
54
-
55
54
  if (this[target]) {
56
55
  continue;
57
56
  } else if (typeof target !== "string") {
@@ -61,8 +60,6 @@ class Context {
61
60
  * Since there is no property, we must define it with its setter and getter.
62
61
  * We could not know when it got defined otherwise.
63
62
  */
64
-
65
-
66
63
  Object.defineProperty(this, target, {
67
64
  /**
68
65
  * Setter sets the given value to this object.
@@ -75,7 +72,6 @@ class Context {
75
72
  /**
76
73
  * WWhen the property is set, we will go through all the waiters and, if any of them include currently set property, act on it.
77
74
  */
78
-
79
75
  for (const waiter of this.waiters) {
80
76
  if (waiter.targets.includes(target) === false) {
81
77
  continue;
@@ -83,13 +79,10 @@ class Context {
83
79
  /**
84
80
  * Remove currently set property so we know if there are any more to be waited for.
85
81
  */
86
-
87
-
88
82
  waiter.targets = waiter.targets.filter(t => t !== target);
89
83
  /**
90
84
  * If there are more to be waited, eg. user added [cms, pageBuilder] as waited properties, we just continue the loop.
91
85
  */
92
-
93
86
  if (waiter.targets.length > 0) {
94
87
  continue;
95
88
  }
@@ -97,12 +90,9 @@ class Context {
97
90
  * And if there is nothing more to be waited for, we execute the callable.
98
91
  * Note that this callable is not async.
99
92
  */
100
-
101
-
102
93
  waiter.cb(this);
103
94
  }
104
95
  },
105
-
106
96
  /**
107
97
  * As we have set property with __ around it, we must get it as well.
108
98
  */
@@ -115,14 +105,11 @@ class Context {
115
105
  /**
116
106
  * We add the target to be awaited.
117
107
  */
118
-
119
108
  targets.push(target);
120
109
  }
121
110
  /**
122
111
  * If there are no targets to be awaited, just fire the callable.
123
112
  */
124
-
125
-
126
113
  if (targets.length === 0) {
127
114
  cb(this);
128
115
  return;
@@ -130,11 +117,8 @@ class Context {
130
117
  /**
131
118
  * Otherwise add the waiter for the target properties.
132
119
  */
133
-
134
-
135
120
  this.waiters.push({
136
121
  targets,
137
-
138
122
  /**
139
123
  * TODO @ts-refactor
140
124
  * Problem with possible subtype initialization
@@ -143,7 +127,5 @@ class Context {
143
127
  cb
144
128
  });
145
129
  }
146
-
147
130
  }
148
-
149
131
  exports.Context = Context;
package/Context.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["Context","constructor","params","plugins","WEBINY_VERSION","PluginsContainer","getResult","_result","hasResult","setResult","value","waitFor","obj","cb","initialTargets","Array","isArray","targets","key","target","Object","defineProperty","set","newTargetKey","waiter","waiters","includes","filter","t","length","get","configurable","push"],"sources":["Context.ts"],"sourcesContent":["import { Context as ContextInterface } from \"~/types\";\nimport { PluginsContainer } from \"@webiny/plugins\";\nimport { PluginCollection } from \"@webiny/plugins/types\";\n\ninterface Waiter {\n targets: string[];\n cb: (context: ContextInterface) => void;\n}\n\nexport interface ContextParams {\n plugins?: PluginCollection;\n WEBINY_VERSION: string;\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\n private readonly waiters: Waiter[] = [];\n\n public constructor(params: ContextParams) {\n const { plugins, WEBINY_VERSION } = params;\n this.plugins = new PluginsContainer(plugins || []);\n this.WEBINY_VERSION = WEBINY_VERSION;\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 any);\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-ignore\n cb\n });\n }\n}\n"],"mappings":";;;;;;;;;;;AACA;;AAYO,MAAMA,OAAN,CAA0C;EAQtCC,WAAW,CAACC,MAAD,EAAwB;IAAA;IAAA;IAAA;IAAA;IAAA,+CAFL,EAEK;IACtC,MAAM;MAAEC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,CAAsBC,CAAC,IAAIA,CAAC,KAAKT,MAAjC,CAAjB;YACA;AACxB;AACA;;YACwB,IAAIK,MAAM,CAACP,OAAP,CAAeY,MAAf,GAAwB,CAA5B,EAA+B;cAC3B;YACH;YACD;AACxB;AACA;AACA;;;YACwBL,MAAM,CAACX,EAAP,CAAU,IAAV;UACH;QACJ,CAhC+B;;QAiChC;AAChB;AACA;QACgBiB,GAAG,EAAE,MAAW;UACZ,MAAMP,YAAY,GAAI,KAAIJ,MAAO,IAAjC;UACA,OAAO,KAAKI,YAAL,CAAP;QACH,CAvC+B;QAwChCQ,YAAY,EAAE;MAxCkB,CAApC;MA0CA;AACZ;AACA;;MACYd,OAAO,CAACe,IAAR,CAAab,MAAb;IACH;IACD;AACR;AACA;;;IACQ,IAAIF,OAAO,CAACY,MAAR,KAAmB,CAAvB,EAA0B;MACtBhB,EAAE,CAAC,IAAD,CAAF;MACA;IACH;IACD;AACR;AACA;;;IACQ,KAAKY,OAAL,CAAaO,IAAb,CAAkB;MACdf,OADc;;MAEd;AACZ;AACA;AACA;MACY;MACAJ;IAPc,CAAlB;EASH;;AApH4C"}
1
+ {"version":3,"names":["Context","constructor","params","plugins","WEBINY_VERSION","PluginsContainer","benchmark","Benchmark","register","BenchmarkPlugin","getResult","_result","hasResult","setResult","value","waitFor","obj","cb","initialTargets","Array","isArray","targets","key","target","Object","defineProperty","set","newTargetKey","waiter","waiters","includes","filter","t","length","get","configurable","push"],"sources":["Context.ts"],"sourcesContent":["import { Context as ContextInterface } from \"~/types\";\nimport { PluginsContainer } from \"@webiny/plugins\";\nimport { PluginCollection } from \"@webiny/plugins/types\";\nimport { Benchmark } from \"~/Benchmark\";\nimport { BenchmarkPlugin } from \"~/plugins/BenchmarkPlugin\";\n\ninterface Waiter {\n targets: string[];\n cb: (context: ContextInterface) => void;\n}\n\nexport interface ContextParams {\n plugins?: PluginCollection;\n WEBINY_VERSION: string;\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\n private readonly waiters: Waiter[] = [];\n\n public constructor(params: ContextParams) {\n const { plugins, WEBINY_VERSION } = params;\n this.plugins = new PluginsContainer(plugins || []);\n this.WEBINY_VERSION = WEBINY_VERSION;\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 any);\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-ignore\n cb\n });\n }\n}\n"],"mappings":";;;;;;;;AACA;AAEA;AACA;AAYO,MAAMA,OAAO,CAA6B;EAStCC,WAAW,CAACC,MAAqB,EAAE;IAAA;IAAA;IAAA;IAAA;IAAA;IAAA,+CAFL,EAAE;IAGnC,MAAM;MAAEC,OAAO;MAAEC;IAAe,CAAC,GAAGF,MAAM;IAC1C,IAAI,CAACC,OAAO,GAAG,IAAIE,yBAAgB,CAACF,OAAO,IAAI,EAAE,CAAC;IAClD,IAAI,CAACC,cAAc,GAAGA,cAAc;IACpC;AACR;AACA;AACA;IACQ,IAAI,CAACE,SAAS,GAAG,IAAIC,oBAAS,EAAE;IAChC,IAAI,CAACJ,OAAO,CAACK,QAAQ,CAAC,IAAIC,gCAAe,CAAC,IAAI,CAACH,SAAS,CAAC,CAAC;EAC9D;EAEOI,SAAS,GAAQ;IACpB,OAAO,IAAI,CAACC,OAAO;EACvB;EAEOC,SAAS,GAAY;IACxB,OAAO,CAAC,CAAC,IAAI,CAACD,OAAO;EACzB;EAEOE,SAAS,CAACC,KAAU,EAAQ;IAC/B,IAAI,CAACH,OAAO,GAAGG,KAAK;EACxB;EAEOC,OAAO,CACVC,GAAsB,EACtBC,EAAwB,EACpB;IACJ,MAAMC,cAAc,GAAGC,KAAK,CAACC,OAAO,CAACJ,GAAG,CAAC,GAAGA,GAAG,GAAG,CAACA,GAAG,CAAC;IACvD,MAAMK,OAAiB,GAAG,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AAK,CAAC,EAAE;MACtBhB,EAAE,CAAC,IAAI,CAAQ;MACf;IACJ;IACA;AACR;AACA;IACQ,IAAI,CAACY,OAAO,CAACO,IAAI,CAAC;MACdf,OAAO;MACP;AACZ;AACA;AACA;MACY;MACAJ;IACJ,CAAC,CAAC;EACN;AACJ;AAAC"}
package/index.js CHANGED
@@ -3,9 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
-
7
6
  var _Context = require("./Context");
8
-
9
7
  Object.keys(_Context).forEach(function (key) {
10
8
  if (key === "default" || key === "__esModule") return;
11
9
  if (key in exports && exports[key] === _Context[key]) return;
@@ -16,9 +14,7 @@ Object.keys(_Context).forEach(function (key) {
16
14
  }
17
15
  });
18
16
  });
19
-
20
17
  var _ContextPlugin = require("./plugins/ContextPlugin");
21
-
22
18
  Object.keys(_ContextPlugin).forEach(function (key) {
23
19
  if (key === "default" || key === "__esModule") return;
24
20
  if (key in exports && exports[key] === _ContextPlugin[key]) return;
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"~/Context\";\nexport * from \"~/plugins/ContextPlugin\";\n"],"mappings":";;;;;;AAAA;;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA"}
1
+ {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"~/Context\";\nexport * from \"~/plugins/ContextPlugin\";\n"],"mappings":";;;;;AAAA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/api",
3
- "version": "5.34.8-beta.1",
3
+ "version": "5.35.0-beta.0",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,16 +12,16 @@
12
12
  ],
13
13
  "license": "MIT",
14
14
  "dependencies": {
15
- "@babel/runtime": "7.19.0",
16
- "@webiny/plugins": "5.34.8-beta.1"
15
+ "@babel/runtime": "7.20.13",
16
+ "@webiny/plugins": "5.35.0-beta.0"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@babel/cli": "^7.19.3",
20
20
  "@babel/core": "^7.19.3",
21
21
  "@babel/preset-env": "^7.19.4",
22
22
  "@babel/preset-typescript": "^7.18.6",
23
- "@webiny/cli": "^5.34.8-beta.1",
24
- "@webiny/project-utils": "^5.34.8-beta.1",
23
+ "@webiny/cli": "^5.35.0-beta.0",
24
+ "@webiny/project-utils": "^5.35.0-beta.0",
25
25
  "rimraf": "^3.0.2",
26
26
  "ttypescript": "^1.5.13",
27
27
  "typescript": "4.7.4"
@@ -34,5 +34,5 @@
34
34
  "build": "yarn webiny run build",
35
35
  "watch": "yarn webiny run watch"
36
36
  },
37
- "gitHead": "6e77eebaac687279fe82ea04f667b7e84214b96a"
37
+ "gitHead": "8acc9e8892842cabb3980ce0b6432fde55968d5b"
38
38
  }
@@ -0,0 +1,14 @@
1
+ import { Plugin } from "@webiny/plugins";
2
+ import { Benchmark } from "../types";
3
+ /**
4
+ * This plugin should be initialized only once per context, hence the name of the plugin.
5
+ */
6
+ export declare class BenchmarkPlugin extends Plugin {
7
+ static readonly type: string;
8
+ name: string;
9
+ readonly benchmark: Benchmark;
10
+ constructor(benchmark: Benchmark);
11
+ enable(): void;
12
+ disable(): void;
13
+ measure<T = any>(name: string, cb: () => Promise<T>): Promise<T>;
14
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.BenchmarkPlugin = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _plugins = require("@webiny/plugins");
10
+ /**
11
+ * This plugin should be initialized only once per context, hence the name of the plugin.
12
+ */
13
+ class BenchmarkPlugin extends _plugins.Plugin {
14
+ constructor(benchmark) {
15
+ super();
16
+ (0, _defineProperty2.default)(this, "name", "context.benchmark");
17
+ (0, _defineProperty2.default)(this, "benchmark", void 0);
18
+ this.benchmark = benchmark;
19
+ }
20
+ enable() {
21
+ this.benchmark.enable();
22
+ }
23
+ disable() {
24
+ this.benchmark.disable();
25
+ }
26
+ async measure(name, cb) {
27
+ return this.benchmark.measure(name, cb);
28
+ }
29
+ }
30
+ exports.BenchmarkPlugin = BenchmarkPlugin;
31
+ (0, _defineProperty2.default)(BenchmarkPlugin, "type", "context.benchmark");
@@ -0,0 +1 @@
1
+ {"version":3,"names":["BenchmarkPlugin","Plugin","constructor","benchmark","enable","disable","measure","name","cb"],"sources":["BenchmarkPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins\";\nimport { Benchmark } from \"~/types\";\n\n/**\n * This plugin should be initialized only once per context, hence the name of the plugin.\n */\nexport class BenchmarkPlugin extends Plugin {\n public static override readonly type: string = \"context.benchmark\";\n public override name = \"context.benchmark\";\n public readonly benchmark: Benchmark;\n\n public constructor(benchmark: Benchmark) {\n super();\n this.benchmark = benchmark;\n }\n\n public enable(): void {\n this.benchmark.enable();\n }\n\n public disable(): void {\n this.benchmark.disable();\n }\n\n public async measure<T = any>(name: string, cb: () => Promise<T>): Promise<T> {\n return this.benchmark.measure<T>(name, cb);\n }\n}\n"],"mappings":";;;;;;;;AAAA;AAGA;AACA;AACA;AACO,MAAMA,eAAe,SAASC,eAAM,CAAC;EAKjCC,WAAW,CAACC,SAAoB,EAAE;IACrC,KAAK,EAAE;IAAC,4CAJW,mBAAmB;IAAA;IAKtC,IAAI,CAACA,SAAS,GAAGA,SAAS;EAC9B;EAEOC,MAAM,GAAS;IAClB,IAAI,CAACD,SAAS,CAACC,MAAM,EAAE;EAC3B;EAEOC,OAAO,GAAS;IACnB,IAAI,CAACF,SAAS,CAACE,OAAO,EAAE;EAC5B;EAEA,MAAaC,OAAO,CAAUC,IAAY,EAAEC,EAAoB,EAAc;IAC1E,OAAO,IAAI,CAACL,SAAS,CAACG,OAAO,CAAIC,IAAI,EAAEC,EAAE,CAAC;EAC9C;AACJ;AAAC;AAAA,8BArBYR,eAAe,UACuB,mBAAmB"}
@@ -1,38 +1,28 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.createContextPlugin = exports.ContextPlugin = void 0;
9
-
10
8
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
9
  var _plugins = require("@webiny/plugins");
13
-
14
10
  class ContextPlugin extends _plugins.Plugin {
15
11
  constructor(callable) {
16
12
  super();
17
13
  (0, _defineProperty2.default)(this, "_callable", void 0);
18
14
  this._callable = callable;
19
15
  }
20
-
21
16
  async apply(context) {
22
17
  if (typeof this._callable !== "function") {
23
18
  throw Error(`Missing callable in ContextPlugin! Either pass a callable to plugin constructor or extend the plugin and override the "apply" method.`);
24
19
  }
25
-
26
20
  return this._callable(context);
27
21
  }
28
-
29
22
  }
30
-
31
23
  exports.ContextPlugin = ContextPlugin;
32
24
  (0, _defineProperty2.default)(ContextPlugin, "type", "context");
33
-
34
25
  const createContextPlugin = callable => {
35
26
  return new ContextPlugin(callable);
36
27
  };
37
-
38
28
  exports.createContextPlugin = createContextPlugin;
@@ -1 +1 @@
1
- {"version":3,"names":["ContextPlugin","Plugin","constructor","callable","_callable","apply","context","Error","createContextPlugin"],"sources":["ContextPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins\";\nimport { Context } from \"~/types\";\n\nexport interface ContextPluginCallable<T extends Context = Context> {\n (context: T): void | Promise<void>;\n}\n\nexport class ContextPlugin<T extends Context = Context> extends Plugin {\n public static override readonly type: string = \"context\";\n private readonly _callable: ContextPluginCallable<T>;\n\n constructor(callable: ContextPluginCallable<T>) {\n super();\n this._callable = callable;\n }\n\n public async apply(context: T): Promise<void> {\n if (typeof this._callable !== \"function\") {\n throw Error(\n `Missing callable in ContextPlugin! Either pass a callable to plugin constructor or extend the plugin and override the \"apply\" method.`\n );\n }\n\n return this._callable(context);\n }\n}\n\nexport const createContextPlugin = <T extends Context = Context>(\n callable: ContextPluginCallable<T>\n): ContextPlugin<T> => {\n return new ContextPlugin<T>(callable);\n};\n"],"mappings":";;;;;;;;;;;AAAA;;AAOO,MAAMA,aAAN,SAAyDC,eAAzD,CAAgE;EAInEC,WAAW,CAACC,QAAD,EAAqC;IAC5C;IAD4C;IAE5C,KAAKC,SAAL,GAAiBD,QAAjB;EACH;;EAEiB,MAALE,KAAK,CAACC,OAAD,EAA4B;IAC1C,IAAI,OAAO,KAAKF,SAAZ,KAA0B,UAA9B,EAA0C;MACtC,MAAMG,KAAK,CACN,uIADM,CAAX;IAGH;;IAED,OAAO,KAAKH,SAAL,CAAeE,OAAf,CAAP;EACH;;AAjBkE;;;8BAA1DN,a,UACsC,S;;AAmB5C,MAAMQ,mBAAmB,GAC5BL,QAD+B,IAEZ;EACnB,OAAO,IAAIH,aAAJ,CAAqBG,QAArB,CAAP;AACH,CAJM"}
1
+ {"version":3,"names":["ContextPlugin","Plugin","constructor","callable","_callable","apply","context","Error","createContextPlugin"],"sources":["ContextPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins\";\nimport { Context } from \"~/types\";\n\nexport interface ContextPluginCallable<T extends Context = Context> {\n (context: T): void | Promise<void>;\n}\n\nexport class ContextPlugin<T extends Context = Context> extends Plugin {\n public static override readonly type: string = \"context\";\n private readonly _callable: ContextPluginCallable<T>;\n\n constructor(callable: ContextPluginCallable<T>) {\n super();\n this._callable = callable;\n }\n\n public async apply(context: T): Promise<void> {\n if (typeof this._callable !== \"function\") {\n throw Error(\n `Missing callable in ContextPlugin! Either pass a callable to plugin constructor or extend the plugin and override the \"apply\" method.`\n );\n }\n\n return this._callable(context);\n }\n}\n\nexport const createContextPlugin = <T extends Context = Context>(\n callable: ContextPluginCallable<T>\n): ContextPlugin<T> => {\n return new ContextPlugin<T>(callable);\n};\n"],"mappings":";;;;;;;;AAAA;AAOO,MAAMA,aAAa,SAAsCC,eAAM,CAAC;EAInEC,WAAW,CAACC,QAAkC,EAAE;IAC5C,KAAK,EAAE;IAAC;IACR,IAAI,CAACC,SAAS,GAAGD,QAAQ;EAC7B;EAEA,MAAaE,KAAK,CAACC,OAAU,EAAiB;IAC1C,IAAI,OAAO,IAAI,CAACF,SAAS,KAAK,UAAU,EAAE;MACtC,MAAMG,KAAK,CACN,uIAAsI,CAC1I;IACL;IAEA,OAAO,IAAI,CAACH,SAAS,CAACE,OAAO,CAAC;EAClC;AACJ;AAAC;AAAA,8BAlBYN,aAAa,UACyB,SAAS;AAmBrD,MAAMQ,mBAAmB,GAC5BL,QAAkC,IACf;EACnB,OAAO,IAAIH,aAAa,CAAIG,QAAQ,CAAC;AACzC,CAAC;AAAC"}
package/types.d.ts CHANGED
@@ -1,4 +1,40 @@
1
1
  import { PluginsContainer } from "@webiny/plugins";
2
+ export interface BenchmarkRuns {
3
+ [key: string]: number;
4
+ }
5
+ export interface BenchmarkMeasurement {
6
+ name: string;
7
+ category: string;
8
+ start: Date;
9
+ end: Date;
10
+ elapsed: number;
11
+ memory: number;
12
+ }
13
+ export interface BenchmarkEnableOnCallable {
14
+ (): Promise<boolean>;
15
+ }
16
+ export interface BenchmarkOutputCallableParams {
17
+ benchmark: Benchmark;
18
+ stop: () => "stop";
19
+ }
20
+ export interface BenchmarkOutputCallable {
21
+ (params: BenchmarkOutputCallableParams): Promise<"stop" | undefined | null | void>;
22
+ }
23
+ export interface BenchmarkMeasureOptions {
24
+ name: string;
25
+ category: string;
26
+ }
27
+ export interface Benchmark {
28
+ elapsed: number;
29
+ runs: BenchmarkRuns;
30
+ measurements: BenchmarkMeasurement[];
31
+ output: () => Promise<void>;
32
+ onOutput: (cb: BenchmarkOutputCallable) => void;
33
+ enableOn: (cb: BenchmarkEnableOnCallable) => void;
34
+ measure: <T = any>(options: BenchmarkMeasureOptions | string, cb: () => Promise<T>) => Promise<T>;
35
+ enable: () => void;
36
+ disable: () => void;
37
+ }
2
38
  /**
3
39
  * The main context which is constructed on every request.
4
40
  * All other contexts should extend or augment this one.
@@ -34,4 +70,5 @@ export interface Context {
34
70
  * In case of multiple objects defined, wait for all of them.
35
71
  */
36
72
  waitFor: <T extends Context = Context>(obj: string[] | string, cb: (context: T) => void) => void;
73
+ benchmark: Benchmark;
37
74
  }
package/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import { PluginsContainer } from \"@webiny/plugins\";\n\n/**\n * The main context which is constructed on every request.\n * All other contexts should extend or augment this one.\n */\nexport interface Context {\n plugins: PluginsContainer;\n args: any;\n readonly WEBINY_VERSION: string;\n /**\n * Not to be used outside of Webiny internal code.\n * @internal\n */\n hasResult: () => boolean;\n /**\n * Not to be used outside of Webiny internal code.\n * @internal\n *\n * @private\n */\n _result?: any;\n /**\n * Not to be used outside of Webiny internal code.\n * @internal\n */\n setResult: (value: any) => void;\n /**\n * Not to be used outside of Webiny internal code.\n * @internal\n */\n getResult: () => void;\n /**\n * Wait for property to be defined on the object and then execute the callable.\n * In case of multiple objects defined, wait for all of them.\n */\n waitFor: <T extends Context = Context>(\n obj: string[] | string,\n cb: (context: T) => void\n ) => void;\n}\n"],"mappings":""}
1
+ {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import { PluginsContainer } from \"@webiny/plugins\";\n\nexport interface BenchmarkRuns {\n [key: string]: number;\n}\n\nexport interface BenchmarkMeasurement {\n name: string;\n category: string;\n start: Date;\n end: Date;\n elapsed: number;\n memory: number;\n}\n\nexport interface BenchmarkEnableOnCallable {\n (): Promise<boolean>;\n}\n\nexport interface BenchmarkOutputCallableParams {\n benchmark: Benchmark;\n stop: () => \"stop\";\n}\nexport interface BenchmarkOutputCallable {\n (params: BenchmarkOutputCallableParams): Promise<\"stop\" | undefined | null | void>;\n}\nexport interface BenchmarkMeasureOptions {\n name: string;\n category: string;\n}\nexport interface Benchmark {\n elapsed: number;\n runs: BenchmarkRuns;\n measurements: BenchmarkMeasurement[];\n output: () => Promise<void>;\n onOutput: (cb: BenchmarkOutputCallable) => void;\n enableOn: (cb: BenchmarkEnableOnCallable) => void;\n measure: <T = any>(\n options: BenchmarkMeasureOptions | string,\n cb: () => Promise<T>\n ) => Promise<T>;\n enable: () => void;\n disable: () => void;\n}\n\n/**\n * The main context which is constructed on every request.\n * All other contexts should extend or augment this one.\n */\nexport interface Context {\n plugins: PluginsContainer;\n args: any;\n readonly WEBINY_VERSION: string;\n /**\n * Not to be used outside of Webiny internal code.\n * @internal\n */\n hasResult: () => boolean;\n /**\n * Not to be used outside of Webiny internal code.\n * @internal\n *\n * @private\n */\n _result?: any;\n /**\n * Not to be used outside of Webiny internal code.\n * @internal\n */\n setResult: (value: any) => void;\n /**\n * Not to be used outside of Webiny internal code.\n * @internal\n */\n getResult: () => void;\n /**\n * Wait for property to be defined on the object and then execute the callable.\n * In case of multiple objects defined, wait for all of them.\n */\n waitFor: <T extends Context = Context>(\n obj: string[] | string,\n cb: (context: T) => void\n ) => void;\n\n benchmark: Benchmark;\n}\n"],"mappings":""}