@lowdefy/engine 3.23.2 → 4.0.0-alpha.6

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/dist/Events.js CHANGED
@@ -1,166 +1,121 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _helpers = require("@lowdefy/helpers");
9
-
10
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
11
-
12
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
13
-
14
- class Events {
15
- constructor(_ref) {
16
- var {
17
- arrayIndices,
18
- block,
19
- context
20
- } = _ref;
21
- this.defaultDebounceMs = 300;
22
- this.events = {};
23
- this.timeouts = {};
24
- this.arrayIndices = arrayIndices;
25
- this.block = block;
26
- this.context = context;
27
- this.init = this.init.bind(this);
28
- this.triggerEvent = this.triggerEvent.bind(this);
29
- this.registerEvent = this.registerEvent.bind(this);
30
- this.initEvent = this.initEvent.bind(this);
31
- this.init();
32
- }
33
-
34
- initEvent(actions) {
35
- return {
36
- actions: (_helpers.type.isObject(actions) ? actions.try : actions) || [],
37
- catchActions: (_helpers.type.isObject(actions) ? actions.catch : []) || [],
38
- debounce: _helpers.type.isObject(actions) ? actions.debounce : null,
39
- history: [],
40
- loading: false
41
- };
42
- }
43
-
44
- init() {
45
- Object.keys(this.block.events).forEach(eventName => {
46
- this.events[eventName] = this.initEvent(this.block.events[eventName]);
47
- });
48
- }
49
-
50
- registerEvent(_ref2) {
51
- var {
52
- name,
53
- actions
54
- } = _ref2;
55
- this.events[name] = this.initEvent(actions);
56
- }
57
-
58
- triggerEvent(_ref3) {
59
- var _this = this;
60
-
61
- return _asyncToGenerator(function* () {
62
- var {
63
- name,
64
- event
65
- } = _ref3;
66
- var eventDescription = _this.events[name];
67
- var result = {
68
- blockId: _this.block.blockId,
69
- event,
70
- eventName: name,
71
- responses: {},
72
- endTimestamp: new Date(),
73
- startTimestamp: new Date(),
74
- success: true,
75
- bounced: false
76
- }; // no event
77
-
78
- if (_helpers.type.isUndefined(eventDescription)) {
79
- return result;
80
- }
81
-
82
- eventDescription.loading = true;
83
- _this.block.update = true;
84
-
85
- _this.context.update();
86
-
87
- var actionHandle = /*#__PURE__*/function () {
88
- var _ref4 = _asyncToGenerator(function* () {
89
- var res = yield _this.context.Actions.callActions({
90
- actions: eventDescription.actions,
91
- arrayIndices: _this.arrayIndices,
92
- block: _this.block,
93
- catchActions: eventDescription.catchActions,
94
- event,
95
- eventName: name
96
- });
97
- eventDescription.history.unshift(res);
98
-
99
- _this.context.eventLog.unshift(res);
100
-
101
- eventDescription.loading = false;
102
- _this.block.update = true;
103
-
104
- _this.context.update();
105
-
106
- return res;
1
+ /*
2
+ Copyright 2020-2021 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { type } from '@lowdefy/helpers';
16
+ let Events = class Events {
17
+ initEvent(actions) {
18
+ return {
19
+ actions: (type.isObject(actions) ? actions.try : actions) || [],
20
+ catchActions: (type.isObject(actions) ? actions.catch : []) || [],
21
+ debounce: type.isObject(actions) ? actions.debounce : null,
22
+ history: [],
23
+ loading: false
24
+ };
25
+ }
26
+ init() {
27
+ Object.keys(this.block.events).forEach((eventName)=>{
28
+ this.events[eventName] = this.initEvent(this.block.events[eventName]);
107
29
  });
108
-
109
- return function actionHandle() {
110
- return _ref4.apply(this, arguments);
30
+ }
31
+ registerEvent({ name , actions }) {
32
+ this.events[name] = this.initEvent(actions);
33
+ }
34
+ async triggerEvent({ name , event }) {
35
+ const eventDescription = this.events[name];
36
+ let result = {
37
+ blockId: this.block.blockId,
38
+ event,
39
+ eventName: name,
40
+ responses: {},
41
+ endTimestamp: new Date(),
42
+ startTimestamp: new Date(),
43
+ success: true,
44
+ bounced: false
111
45
  };
112
- }(); // no debounce
113
-
114
-
115
- if (_helpers.type.isNone(eventDescription.debounce)) {
116
- return actionHandle();
117
- }
118
-
119
- var delay = !_helpers.type.isNone(eventDescription.debounce.ms) ? eventDescription.debounce.ms : _this.defaultDebounceMs; // leading edge: bounce
120
-
121
- if (_this.timeouts[name] && eventDescription.debounce.immediate === true) {
122
- result.bounced = true;
123
- eventDescription.history.unshift(result);
124
-
125
- _this.context.eventLog.unshift(result);
126
-
127
- return result;
128
- } // leading edge: trigger
129
-
130
-
131
- if (eventDescription.debounce.immediate === true) {
132
- _this.timeouts[name] = setTimeout(() => {
133
- _this.timeouts[name] = null;
134
- }, delay);
135
- return actionHandle();
136
- } // trailing edge
137
-
138
-
139
- if (eventDescription.bouncer) {
140
- eventDescription.bouncer();
141
- }
142
-
143
- return new Promise(resolve => {
144
- var timeout = setTimeout( /*#__PURE__*/_asyncToGenerator(function* () {
145
- eventDescription.bouncer = null;
146
- var res = yield actionHandle();
147
- resolve(res);
148
- }), delay);
149
-
150
- eventDescription.bouncer = () => {
151
- clearTimeout(timeout);
152
- result.bounced = true;
153
- eventDescription.history.unshift(result);
154
-
155
- _this.context.eventLog.unshift(result);
156
-
157
- resolve(result);
46
+ // no event
47
+ if (type.isUndefined(eventDescription)) {
48
+ return result;
49
+ }
50
+ eventDescription.loading = true;
51
+ this.block.update = true;
52
+ this.context._internal.update();
53
+ const actionHandle = async ()=>{
54
+ const res = await this.context._internal.Actions.callActions({
55
+ actions: eventDescription.actions,
56
+ arrayIndices: this.arrayIndices,
57
+ block: this.block,
58
+ catchActions: eventDescription.catchActions,
59
+ event,
60
+ eventName: name
61
+ });
62
+ eventDescription.history.unshift(res);
63
+ this.context.eventLog.unshift(res);
64
+ eventDescription.loading = false;
65
+ this.block.update = true;
66
+ this.context._internal.update();
67
+ return res;
158
68
  };
159
- });
160
- })();
161
- }
162
-
163
- }
164
-
165
- var _default = Events;
166
- exports.default = _default;
69
+ // no debounce
70
+ if (type.isNone(eventDescription.debounce)) {
71
+ return actionHandle();
72
+ }
73
+ const delay = !type.isNone(eventDescription.debounce.ms) ? eventDescription.debounce.ms : this.defaultDebounceMs;
74
+ // leading edge: bounce
75
+ if (this.timeouts[name] && eventDescription.debounce.immediate === true) {
76
+ result.bounced = true;
77
+ eventDescription.history.unshift(result);
78
+ this.context.eventLog.unshift(result);
79
+ return result;
80
+ }
81
+ // leading edge: trigger
82
+ if (eventDescription.debounce.immediate === true) {
83
+ this.timeouts[name] = setTimeout(()=>{
84
+ this.timeouts[name] = null;
85
+ }, delay);
86
+ return actionHandle();
87
+ }
88
+ // trailing edge
89
+ if (eventDescription.bouncer) {
90
+ eventDescription.bouncer();
91
+ }
92
+ return new Promise((resolve)=>{
93
+ const timeout = setTimeout(async ()=>{
94
+ eventDescription.bouncer = null;
95
+ const res = await actionHandle();
96
+ resolve(res);
97
+ }, delay);
98
+ eventDescription.bouncer = ()=>{
99
+ clearTimeout(timeout);
100
+ result.bounced = true;
101
+ eventDescription.history.unshift(result);
102
+ this.context.eventLog.unshift(result);
103
+ resolve(result);
104
+ };
105
+ });
106
+ }
107
+ constructor({ arrayIndices , block , context }){
108
+ this.defaultDebounceMs = 300;
109
+ this.events = {};
110
+ this.timeouts = {};
111
+ this.arrayIndices = arrayIndices;
112
+ this.block = block;
113
+ this.context = context;
114
+ this.init = this.init.bind(this);
115
+ this.triggerEvent = this.triggerEvent.bind(this);
116
+ this.registerEvent = this.registerEvent.bind(this);
117
+ this.initEvent = this.initEvent.bind(this);
118
+ this.init();
119
+ }
120
+ };
121
+ export default Events;
package/dist/Requests.js CHANGED
@@ -1,147 +1,111 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _graphqlTag = _interopRequireDefault(require("graphql-tag"));
9
-
10
- var _helpers = require("@lowdefy/helpers");
11
-
12
- var _templateObject;
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
17
-
18
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
19
-
20
- function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
21
-
22
- var CALL_REQUEST = (0, _graphqlTag.default)(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n query callRequest($input: RequestInput!) {\n request(input: $input) {\n id\n type\n success\n response\n }\n }\n"])));
23
-
24
- class Requests {
25
- constructor(context) {
26
- this.context = context;
27
- this.requestList = this.context.rootBlock.requests || [];
28
- this.callRequests = this.callRequests.bind(this);
29
- this.callRequest = this.callRequest.bind(this);
30
- this.fetch = this.fetch.bind(this);
31
- }
32
-
33
- callRequests() {
34
- var {
35
- requestIds,
36
- event,
37
- arrayIndices
38
- } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
39
-
40
- if (!requestIds) {
41
- return Promise.all(this.requestList.map(request => this.callRequest({
42
- requestId: request.requestId,
43
- event,
44
- arrayIndices
45
- })));
1
+ /*
2
+ Copyright 2020-2021 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { get, serializer, type } from '@lowdefy/helpers';
16
+ let Requests = class Requests {
17
+ callRequests({ actions , arrayIndices , event , params } = {}) {
18
+ if (params.all === true) {
19
+ return Promise.all(Object.keys(this.requestConfig).map((requestId)=>this.callRequest({
20
+ requestId,
21
+ event,
22
+ arrayIndices
23
+ })
24
+ ));
25
+ }
26
+ let requestIds = [];
27
+ if (type.isString(params)) requestIds = [
28
+ params
29
+ ];
30
+ if (type.isArray(params)) requestIds = params;
31
+ return Promise.all(requestIds.map((requestId)=>this.callRequest({
32
+ actions,
33
+ requestId,
34
+ event,
35
+ arrayIndices
36
+ })
37
+ ));
46
38
  }
47
-
48
- return Promise.all(requestIds.map(requestId => this.callRequest({
49
- requestId,
50
- event,
51
- arrayIndices
52
- })));
53
- }
54
-
55
- callRequest(_ref) {
56
- var {
57
- requestId,
58
- event,
59
- arrayIndices
60
- } = _ref;
61
-
62
- if (!this.context.requests[requestId]) {
63
- var request = this.requestList.find(req => req.requestId === requestId);
64
-
65
- if (!request) {
66
- this.context.requests[requestId] = {
67
- loading: false,
68
- response: null,
69
- error: [new Error("Configuration Error: Request ".concat(requestId, " not defined on context."))]
70
- };
71
- return Promise.reject(new Error("Configuration Error: Request ".concat(requestId, " not defined on context.")));
72
- }
73
-
74
- this.context.requests[requestId] = {
75
- loading: true,
76
- response: null,
77
- error: []
78
- };
39
+ callRequest({ actions , arrayIndices , event , requestId }) {
40
+ const request = this.requestConfig[requestId];
41
+ if (!request) {
42
+ const error = new Error(`Configuration Error: Request ${requestId} not defined on page.`);
43
+ this.context.requests[requestId] = {
44
+ loading: false,
45
+ response: null,
46
+ error: [
47
+ error
48
+ ]
49
+ };
50
+ return Promise.reject(error);
51
+ }
52
+ if (!this.context.requests[requestId]) {
53
+ this.context.requests[requestId] = {
54
+ loading: true,
55
+ response: null,
56
+ error: []
57
+ };
58
+ }
59
+ const { output: payload , errors: parserErrors } = this.context._internal.parser.parse({
60
+ actions,
61
+ event,
62
+ arrayIndices,
63
+ input: request.payload,
64
+ location: requestId
65
+ });
66
+ // TODO: We are throwing this error differently to the request does not exist error
67
+ if (parserErrors.length > 0) {
68
+ throw parserErrors[0];
69
+ }
70
+ return this.fetch({
71
+ requestId,
72
+ payload
73
+ });
79
74
  }
80
-
81
- return this.fetch({
82
- requestId,
83
- event,
84
- arrayIndices
85
- });
86
- }
87
-
88
- fetch(_ref2) {
89
- var _this = this;
90
-
91
- return _asyncToGenerator(function* () {
92
- var {
93
- requestId,
94
- event,
95
- arrayIndices
96
- } = _ref2;
97
- _this.context.requests[requestId].loading = true;
98
-
99
- if (_this.context.RootBlocks) {
100
- _this.context.RootBlocks.setBlocksLoadingCache();
101
- }
102
-
103
- try {
104
- var gqlResponse = yield _this.context.lowdefy.client.query({
105
- query: CALL_REQUEST,
106
- fetchPolicy: 'network-only',
107
- variables: {
108
- input: {
109
- arrayIndices,
110
- requestId,
111
- blockId: _this.context.blockId,
112
- event: _helpers.serializer.serialize(event) || {},
113
- input: _helpers.serializer.serialize(_this.context.lowdefy.inputs[_this.context.id]),
114
- lowdefyGlobal: _helpers.serializer.serialize(_this.context.lowdefy.lowdefyGlobal),
115
- pageId: _this.context.pageId,
116
- state: _helpers.serializer.serialize(_this.context.state),
117
- urlQuery: _helpers.serializer.serialize(_this.context.lowdefy.urlQuery)
118
- }
119
- }
75
+ async fetch({ requestId , payload }) {
76
+ this.context.requests[requestId].loading = true;
77
+ if (this.context._internal.RootBlocks) {
78
+ this.context._internal.RootBlocks.setBlocksLoadingCache();
79
+ }
80
+ try {
81
+ const response = await this.context._internal.lowdefy._internal.callRequest({
82
+ pageId: this.context.pageId,
83
+ payload: serializer.serialize(payload),
84
+ requestId
85
+ });
86
+ const deserializedResponse = serializer.deserialize(get(response, 'response', {
87
+ default: null
88
+ }));
89
+ this.context.requests[requestId].response = deserializedResponse;
90
+ this.context.requests[requestId].loading = false;
91
+ this.context._internal.update();
92
+ return deserializedResponse;
93
+ } catch (error) {
94
+ this.context.requests[requestId].error.unshift(error);
95
+ this.context.requests[requestId].loading = false;
96
+ this.context._internal.update();
97
+ throw error;
98
+ }
99
+ }
100
+ constructor(context){
101
+ this.context = context;
102
+ this.callRequests = this.callRequests.bind(this);
103
+ this.callRequest = this.callRequest.bind(this);
104
+ this.fetch = this.fetch.bind(this);
105
+ this.requestConfig = {};
106
+ (this.context._internal.rootBlock.requests || []).forEach((request)=>{
107
+ this.requestConfig[request.requestId] = request;
120
108
  });
121
-
122
- var response = _helpers.serializer.deserialize((0, _helpers.get)(gqlResponse, 'data.request.response', {
123
- default: null
124
- }));
125
-
126
- _this.context.requests[requestId].response = response;
127
- _this.context.requests[requestId].loading = false;
128
-
129
- _this.context.update();
130
-
131
- return response;
132
- } catch (error) {
133
- _this.context.requests[requestId].error.unshift(error);
134
-
135
- _this.context.requests[requestId].loading = false;
136
-
137
- _this.context.update();
138
-
139
- throw error;
140
- }
141
- })();
142
- }
143
-
144
- }
145
-
146
- var _default = Requests;
147
- exports.default = _default;
109
+ }
110
+ };
111
+ export default Requests;