@webiny/handler 5.22.0-beta.1 → 5.22.0-beta.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/handler",
3
- "version": "5.22.0-beta.1",
3
+ "version": "5.22.0-beta.2",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -15,14 +15,14 @@
15
15
  ],
16
16
  "dependencies": {
17
17
  "@babel/runtime": "7.16.7",
18
- "@webiny/plugins": "5.22.0-beta.1"
18
+ "@webiny/plugins": "5.22.0-beta.2"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@babel/cli": "^7.16.0",
22
22
  "@babel/core": "^7.16.0",
23
23
  "@babel/preset-env": "^7.16.4",
24
- "@webiny/cli": "^5.22.0-beta.1",
25
- "@webiny/project-utils": "^5.22.0-beta.1",
24
+ "@webiny/cli": "^5.22.0-beta.2",
25
+ "@webiny/project-utils": "^5.22.0-beta.2",
26
26
  "rimraf": "^3.0.2",
27
27
  "ttypescript": "^1.5.13",
28
28
  "typescript": "^4.1.3"
@@ -35,5 +35,5 @@
35
35
  "build": "yarn webiny run build",
36
36
  "watch": "yarn webiny run watch"
37
37
  },
38
- "gitHead": "b651010b23f28b3ce1c27130713322ce65b32dcd"
38
+ "gitHead": "c472d20682885fb538354c206a148deaae14bfe8"
39
39
  }
@@ -1,20 +1,19 @@
1
1
  import { Context as ContextInterface, HandlerArgs } from "../types";
2
2
  import { PluginsContainer } from "@webiny/plugins";
3
3
  export interface Params {
4
- args: HandlerArgs;
4
+ args?: HandlerArgs;
5
5
  plugins?: Plugin | Plugin[] | Plugin[][] | PluginsContainer;
6
6
  WEBINY_VERSION: string;
7
7
  }
8
8
  export declare class Context implements ContextInterface {
9
9
  _result: any;
10
- private readonly _plugins;
11
- private readonly _args;
12
- private readonly _version;
13
- get plugins(): PluginsContainer;
14
- get args(): HandlerArgs;
15
- get WEBINY_VERSION(): string;
10
+ readonly plugins: PluginsContainer;
11
+ readonly args: HandlerArgs;
12
+ readonly WEBINY_VERSION: string;
13
+ private readonly waiters;
16
14
  constructor(params: Params);
17
15
  getResult(): any;
18
16
  hasResult(): boolean;
19
17
  setResult(value: any): void;
18
+ waitFor<T extends ContextInterface = ContextInterface>(obj: string | string[], cb: (context: T) => void): void;
20
19
  }
@@ -12,31 +12,20 @@ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/de
12
12
  var _plugins = require("@webiny/plugins");
13
13
 
14
14
  class Context {
15
- get plugins() {
16
- return this._plugins;
17
- }
18
-
19
- get args() {
20
- return this._args;
21
- }
22
-
23
- get WEBINY_VERSION() {
24
- return this._version;
25
- }
26
-
27
15
  constructor(params) {
28
16
  (0, _defineProperty2.default)(this, "_result", void 0);
29
- (0, _defineProperty2.default)(this, "_plugins", void 0);
30
- (0, _defineProperty2.default)(this, "_args", void 0);
31
- (0, _defineProperty2.default)(this, "_version", void 0);
17
+ (0, _defineProperty2.default)(this, "plugins", void 0);
18
+ (0, _defineProperty2.default)(this, "args", void 0);
19
+ (0, _defineProperty2.default)(this, "WEBINY_VERSION", void 0);
20
+ (0, _defineProperty2.default)(this, "waiters", []);
32
21
  const {
33
22
  plugins,
34
23
  args,
35
24
  WEBINY_VERSION
36
25
  } = params;
37
- this._plugins = new _plugins.PluginsContainer(plugins || []);
38
- this._args = args;
39
- this._version = WEBINY_VERSION;
26
+ this.plugins = new _plugins.PluginsContainer(plugins || []);
27
+ this.args = args || [];
28
+ this.WEBINY_VERSION = WEBINY_VERSION;
40
29
  }
41
30
 
42
31
  getResult() {
@@ -51,6 +40,99 @@ class Context {
51
40
  this._result = value;
52
41
  }
53
42
 
43
+ waitFor(obj, cb) {
44
+ const initialTargets = Array.isArray(obj) ? obj : [obj];
45
+ const targets = [];
46
+ /**
47
+ * We go only through the first level properties
48
+ */
49
+
50
+ for (const target of initialTargets) {
51
+ /**
52
+ * If property already exists, there is no need to wait for it, so we just continue the loop.
53
+ */
54
+ if (this[target]) {
55
+ continue;
56
+ }
57
+ /**
58
+ * Since there is no property, we must define it with its setter and getter.
59
+ * We could not know when it got defined otherwise.
60
+ */
61
+
62
+
63
+ Object.defineProperty(this, target, {
64
+ /**
65
+ * Setter sets the given value to this object.
66
+ * We cannot set it on exact property name it is defined because it would go into loop of setting itself.
67
+ * And that is why we add __ around the property name.
68
+ */
69
+ set: value => {
70
+ this[`__${target}__`] = value;
71
+ /**
72
+ * WWhen the property is set, we will go through all the waiters and, if any of them include currently set property, act on it.
73
+ */
74
+
75
+ for (const waiter of this.waiters) {
76
+ if (waiter.targets.includes(target) === false) {
77
+ continue;
78
+ }
79
+ /**
80
+ * Remove currently set property so we know if there are any more to be waited for.
81
+ */
82
+
83
+
84
+ waiter.targets = waiter.targets.filter(t => t !== target);
85
+ /**
86
+ * If there are more to be waited, eg. user added [cms, pageBuilder] as waited properties, we just continue the loop.
87
+ */
88
+
89
+ if (waiter.targets.length > 0) {
90
+ continue;
91
+ }
92
+ /**
93
+ * And if there is nothing more to be waited for, we execute the callable.
94
+ * Note that this callable is not async.
95
+ */
96
+
97
+
98
+ waiter.cb(this);
99
+ }
100
+ },
101
+
102
+ /**
103
+ * As we have set property with __ around it, we must get it as well.
104
+ */
105
+ get: () => {
106
+ return this[`__${target}__`];
107
+ },
108
+ configurable: false
109
+ });
110
+ /**
111
+ * We add the target to be awaited.
112
+ */
113
+
114
+ targets.push(target);
115
+ }
116
+ /**
117
+ * If there are no targets to be awaited, just fire the callable.
118
+ */
119
+
120
+
121
+ if (targets.length === 0) {
122
+ cb(this);
123
+ return;
124
+ }
125
+ /**
126
+ * Otherwise add the waiter for the target properties.
127
+ */
128
+
129
+
130
+ this.waiters.push({
131
+ targets,
132
+ cb
133
+ });
134
+ }
135
+
54
136
  }
55
137
 
56
138
  exports.Context = Context;
package/types.d.ts CHANGED
@@ -35,6 +35,11 @@ export interface Context {
35
35
  * @internal
36
36
  */
37
37
  getResult: () => void;
38
+ /**
39
+ * Wait for property to be defined on the object and then execute the callable.
40
+ * In case of multiple objects defined, wait for all of them.
41
+ */
42
+ waitFor: <T extends Context = Context>(obj: string[] | string, cb: (context: T) => void) => void;
38
43
  }
39
44
  /**
40
45
  * Left for backwards-compatibility.