@lowdefy/engine 3.23.0-alpha.0 → 4.0.0-alpha.4

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,124 @@
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,
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]);
29
+ });
30
+ }
31
+ registerEvent({ name , actions: actions1 }) {
32
+ this.events[name] = this.initEvent(actions1);
33
+ }
34
+ async triggerEvent({ name: name1 , event }) {
35
+ const eventDescription = this.events[name1];
36
+ let result = {
37
+ blockId: this.block.blockId,
94
38
  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;
39
+ eventName: name1,
40
+ responses: {
41
+ },
42
+ endTimestamp: new Date(),
43
+ startTimestamp: new Date(),
44
+ success: true,
45
+ bounced: false
46
+ };
47
+ // no event
48
+ if (type.isUndefined(eventDescription)) {
49
+ return result;
50
+ }
51
+ eventDescription.loading = true;
52
+ this.block.update = true;
53
+ this.context._internal.update();
54
+ const actionHandle = async ()=>{
55
+ const res = await this.context._internal.Actions.callActions({
56
+ actions: eventDescription.actions,
57
+ arrayIndices: this.arrayIndices,
58
+ block: this.block,
59
+ catchActions: eventDescription.catchActions,
60
+ event,
61
+ eventName: name1
62
+ });
63
+ eventDescription.history.unshift(res);
64
+ this.context.eventLog.unshift(res);
65
+ eventDescription.loading = false;
66
+ this.block.update = true;
67
+ this.context._internal.update();
68
+ return res;
69
+ };
70
+ // no debounce
71
+ if (type.isNone(eventDescription.debounce)) {
72
+ return actionHandle();
73
+ }
74
+ const delay = !type.isNone(eventDescription.debounce.ms) ? eventDescription.debounce.ms : this.defaultDebounceMs;
75
+ // leading edge: bounce
76
+ if (this.timeouts[name1] && eventDescription.debounce.immediate === true) {
77
+ result.bounced = true;
78
+ eventDescription.history.unshift(result);
79
+ this.context.eventLog.unshift(result);
80
+ return result;
81
+ }
82
+ // leading edge: trigger
83
+ if (eventDescription.debounce.immediate === true) {
84
+ this.timeouts[name1] = setTimeout(()=>{
85
+ this.timeouts[name1] = null;
86
+ }, delay);
87
+ return actionHandle();
88
+ }
89
+ // trailing edge
90
+ if (eventDescription.bouncer) {
91
+ eventDescription.bouncer();
92
+ }
93
+ return new Promise((resolve)=>{
94
+ const timeout = setTimeout(async ()=>{
95
+ eventDescription.bouncer = null;
96
+ const res = await actionHandle();
97
+ resolve(res);
98
+ }, delay);
99
+ eventDescription.bouncer = ()=>{
100
+ clearTimeout(timeout);
101
+ result.bounced = true;
102
+ eventDescription.history.unshift(result);
103
+ this.context.eventLog.unshift(result);
104
+ resolve(result);
105
+ };
107
106
  });
108
-
109
- return function actionHandle() {
110
- return _ref4.apply(this, arguments);
107
+ }
108
+ constructor({ arrayIndices , block , context }){
109
+ this.defaultDebounceMs = 300;
110
+ this.events = {
111
111
  };
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);
112
+ this.timeouts = {
158
113
  };
159
- });
160
- })();
161
- }
162
-
163
- }
164
-
165
- var _default = Events;
166
- exports.default = _default;
114
+ this.arrayIndices = arrayIndices;
115
+ this.block = block;
116
+ this.context = context;
117
+ this.init = this.init.bind(this);
118
+ this.triggerEvent = this.triggerEvent.bind(this);
119
+ this.registerEvent = this.registerEvent.bind(this);
120
+ this.initEvent = this.initEvent.bind(this);
121
+ this.init();
122
+ }
123
+ };
124
+ export default Events;
package/dist/Requests.js CHANGED
@@ -1,147 +1,113 @@
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
+ }) {
19
+ if (params.all === true) {
20
+ return Promise.all(Object.keys(this.requestConfig).map((requestId)=>this.callRequest({
21
+ requestId,
22
+ event,
23
+ arrayIndices
24
+ })
25
+ ));
26
+ }
27
+ let requestIds = [];
28
+ if (type.isString(params)) requestIds = [
29
+ params
30
+ ];
31
+ if (type.isArray(params)) requestIds = params;
32
+ return Promise.all(requestIds.map((requestId)=>this.callRequest({
33
+ actions,
34
+ requestId,
35
+ event,
36
+ arrayIndices
37
+ })
38
+ ));
46
39
  }
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
- };
40
+ callRequest({ actions: actions1 , arrayIndices: arrayIndices1 , event: event1 , requestId }) {
41
+ const request = this.requestConfig[requestId];
42
+ if (!request) {
43
+ const error = new Error(`Configuration Error: Request ${requestId} not defined on page.`);
44
+ this.context.requests[requestId] = {
45
+ loading: false,
46
+ response: null,
47
+ error: [
48
+ error
49
+ ]
50
+ };
51
+ return Promise.reject(error);
52
+ }
53
+ if (!this.context.requests[requestId]) {
54
+ this.context.requests[requestId] = {
55
+ loading: true,
56
+ response: null,
57
+ error: []
58
+ };
59
+ }
60
+ const { output: payload , errors: parserErrors } = this.context._internal.parser.parse({
61
+ actions: actions1,
62
+ event: event1,
63
+ arrayIndices: arrayIndices1,
64
+ input: request.payload,
65
+ location: requestId
66
+ });
67
+ // TODO: We are throwing this error differently to the request does not exist error
68
+ if (parserErrors.length > 0) {
69
+ throw parserErrors[0];
70
+ }
71
+ return this.fetch({
72
+ requestId,
73
+ payload
74
+ });
79
75
  }
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
- }
76
+ async fetch({ requestId: requestId1 , payload }) {
77
+ this.context.requests[requestId1].loading = true;
78
+ if (this.context._internal.RootBlocks) {
79
+ this.context._internal.RootBlocks.setBlocksLoadingCache();
80
+ }
81
+ try {
82
+ const response = await this.context._internal.lowdefy._internal.callRequest({
83
+ pageId: this.context.pageId,
84
+ payload: serializer.serialize(payload),
85
+ requestId: requestId1
86
+ });
87
+ const deserializedResponse = serializer.deserialize(get(response, 'response', {
88
+ default: null
89
+ }));
90
+ this.context.requests[requestId1].response = deserializedResponse;
91
+ this.context.requests[requestId1].loading = false;
92
+ this.context._internal.update();
93
+ return deserializedResponse;
94
+ } catch (error) {
95
+ this.context.requests[requestId1].error.unshift(error);
96
+ this.context.requests[requestId1].loading = false;
97
+ this.context._internal.update();
98
+ throw error;
99
+ }
100
+ }
101
+ constructor(context){
102
+ this.context = context;
103
+ this.callRequests = this.callRequests.bind(this);
104
+ this.callRequest = this.callRequest.bind(this);
105
+ this.fetch = this.fetch.bind(this);
106
+ this.requestConfig = {
107
+ };
108
+ (this.context._internal.rootBlock.requests || []).forEach((request)=>{
109
+ this.requestConfig[request.requestId] = request;
120
110
  });
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;
111
+ }
112
+ };
113
+ export default Requests;