@nocobase/plugin-workflow 0.7.0-alpha.34 → 0.7.0-alpha.57

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 (132) hide show
  1. package/lib/actions/index.js +31 -32
  2. package/lib/actions/nodes.js +310 -182
  3. package/lib/actions/workflows.js +260 -157
  4. package/lib/calculators/index.js +142 -90
  5. package/lib/collections/executions.js +40 -43
  6. package/lib/collections/flow_nodes.js +60 -65
  7. package/lib/collections/jobs.js +45 -47
  8. package/lib/collections/workflows.js +78 -87
  9. package/lib/constants.js +22 -17
  10. package/lib/index.js +71 -22
  11. package/lib/instructions/calculation.js +34 -29
  12. package/lib/instructions/condition.js +94 -87
  13. package/lib/instructions/create.js +43 -26
  14. package/lib/instructions/destroy.js +42 -25
  15. package/lib/instructions/index.js +46 -25
  16. package/lib/instructions/parallel.js +99 -84
  17. package/lib/instructions/prompt.js +21 -13
  18. package/lib/instructions/query.js +47 -29
  19. package/lib/instructions/update.js +44 -25
  20. package/lib/models/Execution.js +401 -248
  21. package/lib/models/FlowNode.js +18 -5
  22. package/lib/models/Job.js +18 -5
  23. package/lib/models/Workflow.js +132 -88
  24. package/lib/server.js +93 -66
  25. package/lib/triggers/collection.js +113 -51
  26. package/lib/triggers/index.js +25 -11
  27. package/package.json +7 -12
  28. package/esm/actions/index.d.ts +0 -1
  29. package/esm/actions/index.js +0 -14
  30. package/esm/actions/index.js.map +0 -1
  31. package/esm/actions/nodes.d.ts +0 -4
  32. package/esm/actions/nodes.js +0 -187
  33. package/esm/actions/nodes.js.map +0 -1
  34. package/esm/actions/workflows.d.ts +0 -3
  35. package/esm/actions/workflows.js +0 -163
  36. package/esm/actions/workflows.js.map +0 -1
  37. package/esm/calculators/index.d.ts +0 -38
  38. package/esm/calculators/index.js +0 -128
  39. package/esm/calculators/index.js.map +0 -1
  40. package/esm/collections/executions.d.ts +0 -3
  41. package/esm/collections/executions.js +0 -43
  42. package/esm/collections/executions.js.map +0 -1
  43. package/esm/collections/flow_nodes.d.ts +0 -3
  44. package/esm/collections/flow_nodes.js +0 -65
  45. package/esm/collections/flow_nodes.js.map +0 -1
  46. package/esm/collections/jobs.d.ts +0 -3
  47. package/esm/collections/jobs.js +0 -47
  48. package/esm/collections/jobs.js.map +0 -1
  49. package/esm/collections/workflows.d.ts +0 -3
  50. package/esm/collections/workflows.js +0 -87
  51. package/esm/collections/workflows.js.map +0 -1
  52. package/esm/constants.d.ts +0 -17
  53. package/esm/constants.js +0 -18
  54. package/esm/constants.js.map +0 -1
  55. package/esm/index.d.ts +0 -5
  56. package/esm/index.js +0 -6
  57. package/esm/index.js.map +0 -1
  58. package/esm/instructions/calculation.d.ts +0 -8
  59. package/esm/instructions/calculation.js +0 -55
  60. package/esm/instructions/calculation.js.map +0 -1
  61. package/esm/instructions/condition.d.ts +0 -5
  62. package/esm/instructions/condition.js +0 -99
  63. package/esm/instructions/condition.js.map +0 -1
  64. package/esm/instructions/create.d.ts +0 -8
  65. package/esm/instructions/create.js +0 -26
  66. package/esm/instructions/create.js.map +0 -1
  67. package/esm/instructions/destroy.d.ts +0 -8
  68. package/esm/instructions/destroy.js +0 -25
  69. package/esm/instructions/destroy.js.map +0 -1
  70. package/esm/instructions/index.d.ts +0 -15
  71. package/esm/instructions/index.js +0 -20
  72. package/esm/instructions/index.js.map +0 -1
  73. package/esm/instructions/parallel.d.ts +0 -13
  74. package/esm/instructions/parallel.js +0 -88
  75. package/esm/instructions/parallel.js.map +0 -1
  76. package/esm/instructions/prompt.d.ts +0 -7
  77. package/esm/instructions/prompt.js +0 -13
  78. package/esm/instructions/prompt.js.map +0 -1
  79. package/esm/instructions/query.d.ts +0 -8
  80. package/esm/instructions/query.js +0 -29
  81. package/esm/instructions/query.js.map +0 -1
  82. package/esm/instructions/update.d.ts +0 -9
  83. package/esm/instructions/update.js +0 -25
  84. package/esm/instructions/update.js.map +0 -1
  85. package/esm/models/Execution.d.ts +0 -51
  86. package/esm/models/Execution.js +0 -256
  87. package/esm/models/Execution.js.map +0 -1
  88. package/esm/models/FlowNode.d.ts +0 -17
  89. package/esm/models/FlowNode.js +0 -4
  90. package/esm/models/FlowNode.js.map +0 -1
  91. package/esm/models/Job.d.ts +0 -15
  92. package/esm/models/Job.js +0 -4
  93. package/esm/models/Job.js.map +0 -1
  94. package/esm/models/Workflow.d.ts +0 -29
  95. package/esm/models/Workflow.js +0 -88
  96. package/esm/models/Workflow.js.map +0 -1
  97. package/esm/server.d.ts +0 -5
  98. package/esm/server.js +0 -62
  99. package/esm/server.js.map +0 -1
  100. package/esm/triggers/collection.d.ts +0 -12
  101. package/esm/triggers/collection.js +0 -61
  102. package/esm/triggers/collection.js.map +0 -1
  103. package/esm/triggers/index.d.ts +0 -9
  104. package/esm/triggers/index.js +0 -6
  105. package/esm/triggers/index.js.map +0 -1
  106. package/lib/actions/index.js.map +0 -1
  107. package/lib/actions/nodes.js.map +0 -1
  108. package/lib/actions/workflows.js.map +0 -1
  109. package/lib/calculators/index.js.map +0 -1
  110. package/lib/collections/executions.js.map +0 -1
  111. package/lib/collections/flow_nodes.js.map +0 -1
  112. package/lib/collections/jobs.js.map +0 -1
  113. package/lib/collections/workflows.js.map +0 -1
  114. package/lib/constants.js.map +0 -1
  115. package/lib/index.js.map +0 -1
  116. package/lib/instructions/calculation.js.map +0 -1
  117. package/lib/instructions/condition.js.map +0 -1
  118. package/lib/instructions/create.js.map +0 -1
  119. package/lib/instructions/destroy.js.map +0 -1
  120. package/lib/instructions/index.js.map +0 -1
  121. package/lib/instructions/parallel.js.map +0 -1
  122. package/lib/instructions/prompt.js.map +0 -1
  123. package/lib/instructions/query.js.map +0 -1
  124. package/lib/instructions/update.js.map +0 -1
  125. package/lib/models/Execution.js.map +0 -1
  126. package/lib/models/FlowNode.js.map +0 -1
  127. package/lib/models/Job.js.map +0 -1
  128. package/lib/models/Workflow.js.map +0 -1
  129. package/lib/server.js.map +0 -1
  130. package/lib/triggers/collection.js.map +0 -1
  131. package/lib/triggers/index.js.map +0 -1
  132. package/tsconfig.build.json +0 -9
@@ -1,7 +1,20 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const database_1 = require("@nocobase/database");
4
- class FlowNodeModel extends database_1.Model {
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ function _database() {
9
+ const data = require("@nocobase/database");
10
+
11
+ _database = function _database() {
12
+ return data;
13
+ };
14
+
15
+ return data;
5
16
  }
6
- exports.default = FlowNodeModel;
7
- //# sourceMappingURL=FlowNode.js.map
17
+
18
+ class FlowNodeModel extends _database().Model {}
19
+
20
+ exports.default = FlowNodeModel;
package/lib/models/Job.js CHANGED
@@ -1,7 +1,20 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const database_1 = require("@nocobase/database");
4
- class JobModel extends database_1.Model {
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ function _database() {
9
+ const data = require("@nocobase/database");
10
+
11
+ _database = function _database() {
12
+ return data;
13
+ };
14
+
15
+ return data;
5
16
  }
6
- exports.default = JobModel;
7
- //# sourceMappingURL=Job.js.map
17
+
18
+ class JobModel extends _database().Model {}
19
+
20
+ exports.default = JobModel;
@@ -1,94 +1,138 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const database_1 = require("@nocobase/database");
16
- const triggers_1 = __importDefault(require("../triggers"));
17
- const constants_1 = require("../constants");
18
- class WorkflowModel extends database_1.Model {
19
- static mount() {
20
- return __awaiter(this, void 0, void 0, function* () {
21
- const collection = this.database.getCollection('workflows');
22
- const workflows = yield collection.repository.find({
23
- filter: { enabled: true },
24
- });
25
- workflows.forEach((workflow) => {
26
- workflow.toggle();
27
- });
28
- this.addHook('afterCreate', (model) => model.toggle());
29
- this.addHook('afterUpdate', (model) => model.toggle());
30
- this.addHook('afterDestroy', (model) => model.toggle(false));
31
- });
32
- }
33
- getHookId() {
34
- return `workflow-${this.get('id')}`;
35
- }
36
- getTransaction(options) {
37
- if (!this.useTransaction) {
38
- return undefined;
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ function _database() {
9
+ const data = require("@nocobase/database");
10
+
11
+ _database = function _database() {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
17
+
18
+ var _triggers = _interopRequireDefault(require("../triggers"));
19
+
20
+ var _constants = require("../constants");
21
+
22
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
+
24
+ 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); } }
25
+
26
+ 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); }); }; }
27
+
28
+ class WorkflowModel extends _database().Model {
29
+ static mount() {
30
+ var _this = this;
31
+
32
+ return _asyncToGenerator(function* () {
33
+ const collection = _this.database.getCollection('workflows');
34
+
35
+ const workflows = yield collection.repository.find({
36
+ filter: {
37
+ enabled: true
39
38
  }
40
- return options.transaction && !options.transaction.finished
41
- ? options.transaction
42
- : this.constructor.database.sequelize.transaction();
39
+ });
40
+ workflows.forEach(workflow => {
41
+ workflow.toggle();
42
+ });
43
+
44
+ _this.addHook('afterCreate', model => model.toggle());
45
+
46
+ _this.addHook('afterUpdate', model => model.toggle());
47
+
48
+ _this.addHook('afterDestroy', model => model.toggle(false));
49
+ })();
50
+ }
51
+
52
+ getHookId() {
53
+ return `workflow-${this.get('id')}`;
54
+ }
55
+
56
+ getTransaction(options) {
57
+ if (!this.useTransaction) {
58
+ return undefined;
43
59
  }
44
- toggle(enable) {
45
- return __awaiter(this, void 0, void 0, function* () {
46
- const type = this.get('type');
47
- const { on, off } = triggers_1.default.get(type);
48
- if (typeof enable !== 'undefined' ? enable : this.get('enabled')) {
49
- on.call(this, this.trigger.bind(this));
50
- }
51
- else {
52
- off.call(this);
53
- }
60
+
61
+ return options.transaction && !options.transaction.finished ? options.transaction : this.constructor.database.sequelize.transaction();
62
+ }
63
+
64
+ toggle(enable) {
65
+ var _this2 = this;
66
+
67
+ return _asyncToGenerator(function* () {
68
+ const type = _this2.get('type');
69
+
70
+ const _triggers$get = _triggers.default.get(type),
71
+ on = _triggers$get.on,
72
+ off = _triggers$get.off;
73
+
74
+ if (typeof enable !== 'undefined' ? enable : _this2.get('enabled')) {
75
+ on.call(_this2, _this2.trigger.bind(_this2));
76
+ } else {
77
+ off.call(_this2);
78
+ }
79
+ })();
80
+ }
81
+
82
+ trigger(context, options) {
83
+ var _this3 = this;
84
+
85
+ return _asyncToGenerator(function* () {
86
+ // `null` means not to trigger
87
+ if (context === null) {
88
+ return;
89
+ }
90
+
91
+ const transaction = yield _this3.getTransaction(options);
92
+
93
+ if (_this3.useTransaction) {
94
+ const existed = yield _this3.countExecutions({
95
+ where: {
96
+ transaction: transaction.id
97
+ },
98
+ transaction
54
99
  });
55
- }
56
- trigger(context, options) {
57
- return __awaiter(this, void 0, void 0, function* () {
58
- // `null` means not to trigger
59
- if (context === null) {
60
- return;
61
- }
62
- const transaction = yield this.getTransaction(options);
63
- if (this.useTransaction) {
64
- const existed = yield this.countExecutions({
65
- where: {
66
- transaction: transaction.id
67
- },
68
- transaction
69
- });
70
- if (existed) {
71
- console.warn(`workflow ${this.id} has already been triggered in same execution (${transaction.id}), and newly triggering will be skipped.`);
72
- return;
73
- }
74
- }
75
- const execution = yield this.createExecution({
76
- context,
77
- status: constants_1.EXECUTION_STATUS.STARTED,
78
- useTransaction: this.useTransaction,
79
- transaction: transaction.id
80
- }, { transaction });
81
- execution.workflow = this;
82
- yield execution.start({ transaction });
83
- if (!this.executed) {
84
- yield this.update({ executed: true }, { transaction });
85
- }
86
- if (transaction && (!options.transaction || options.transaction.finished)) {
87
- yield transaction.commit();
88
- }
89
- return execution;
100
+
101
+ if (existed) {
102
+ console.warn(`workflow ${_this3.id} has already been triggered in same execution (${transaction.id}), and newly triggering will be skipped.`);
103
+ return;
104
+ }
105
+ }
106
+
107
+ const execution = yield _this3.createExecution({
108
+ context,
109
+ status: _constants.EXECUTION_STATUS.STARTED,
110
+ useTransaction: _this3.useTransaction,
111
+ transaction: transaction.id
112
+ }, {
113
+ transaction
114
+ });
115
+ execution.workflow = _this3;
116
+ yield execution.start({
117
+ transaction
118
+ });
119
+
120
+ if (!_this3.executed) {
121
+ yield _this3.update({
122
+ executed: true
123
+ }, {
124
+ transaction
90
125
  });
91
- }
126
+ }
127
+
128
+ if (transaction && (!options.transaction || options.transaction.finished)) {
129
+ yield transaction.commit();
130
+ }
131
+
132
+ return execution;
133
+ })();
134
+ }
135
+
92
136
  }
93
- exports.default = WorkflowModel;
94
- //# sourceMappingURL=Workflow.js.map
137
+
138
+ exports.default = WorkflowModel;
package/lib/server.js CHANGED
@@ -1,68 +1,95 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const path_1 = __importDefault(require("path"));
16
- const server_1 = require("@nocobase/server");
17
- const Workflow_1 = __importDefault(require("./models/Workflow"));
18
- const Execution_1 = __importDefault(require("./models/Execution"));
19
- const actions_1 = __importDefault(require("./actions"));
20
- class WorkflowPlugin extends server_1.Plugin {
21
- load(options = {}) {
22
- return __awaiter(this, void 0, void 0, function* () {
23
- const { db } = this.app;
24
- db.registerModels({
25
- WorkflowModel: Workflow_1.default,
26
- ExecutionModel: Execution_1.default,
27
- });
28
- yield db.import({
29
- directory: path_1.default.resolve(__dirname, 'collections'),
30
- });
31
- (0, actions_1.default)(this.app);
32
- // [Life Cycle]:
33
- // * load all workflows in db
34
- // * add all hooks for enabled workflows
35
- // * add hooks for create/update[enabled]/delete workflow to add/remove specific hooks
36
- this.app.on('beforeStart', () => __awaiter(this, void 0, void 0, function* () {
37
- const { model } = db.getCollection('workflows');
38
- yield model.mount();
39
- }));
40
- // [Life Cycle]: initialize all necessary seed data
41
- this.app.on('db.init', () => __awaiter(this, void 0, void 0, function* () { }));
42
- // const [Automation, AutomationJob] = database.getModels(['automations', 'automations_jobs']);
43
- // Automation.addHook('afterCreate', async (model: AutomationModel) => {
44
- // model.get('enabled') && await model.loadJobs();
45
- // });
46
- // Automation.addHook('afterUpdate', async (model: AutomationModel) => {
47
- // if (!model.changed('enabled' as any)) {
48
- // return;
49
- // }
50
- // model.get('enabled') ? await model.loadJobs() : await model.cancelJobs();
51
- // });
52
- // Automation.addHook('beforeDestroy', async (model: AutomationModel) => {
53
- // await model.cancelJobs();
54
- // });
55
- // AutomationJob.addHook('afterCreate', async (model: AutomationJobModel) => {
56
- // await model.bootstrap();
57
- // });
58
- // AutomationJob.addHook('beforeDestroy', async (model: AutomationJobModel) => {
59
- // await model.cancel();
60
- // });
61
- });
62
- }
63
- getName() {
64
- return this.getPackageName(__dirname);
65
- }
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ function _path() {
9
+ const data = _interopRequireDefault(require("path"));
10
+
11
+ _path = function _path() {
12
+ return data;
13
+ };
14
+
15
+ return data;
66
16
  }
67
- exports.default = WorkflowPlugin;
68
- //# sourceMappingURL=server.js.map
17
+
18
+ function _server() {
19
+ const data = require("@nocobase/server");
20
+
21
+ _server = function _server() {
22
+ return data;
23
+ };
24
+
25
+ return data;
26
+ }
27
+
28
+ var _Workflow = _interopRequireDefault(require("./models/Workflow"));
29
+
30
+ var _Execution = _interopRequireDefault(require("./models/Execution"));
31
+
32
+ var _actions = _interopRequireDefault(require("./actions"));
33
+
34
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
+
36
+ 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); } }
37
+
38
+ 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); }); }; }
39
+
40
+ class WorkflowPlugin extends _server().Plugin {
41
+ load(options = {}) {
42
+ var _this = this;
43
+
44
+ return _asyncToGenerator(function* () {
45
+ const db = _this.app.db;
46
+ db.registerModels({
47
+ WorkflowModel: _Workflow.default,
48
+ ExecutionModel: _Execution.default
49
+ });
50
+ yield db.import({
51
+ directory: _path().default.resolve(__dirname, 'collections')
52
+ });
53
+ (0, _actions.default)(_this.app); // [Life Cycle]:
54
+ // * load all workflows in db
55
+ // * add all hooks for enabled workflows
56
+ // * add hooks for create/update[enabled]/delete workflow to add/remove specific hooks
57
+
58
+ _this.app.on('beforeStart', /*#__PURE__*/_asyncToGenerator(function* () {
59
+ const _db$getCollection = db.getCollection('workflows'),
60
+ model = _db$getCollection.model;
61
+
62
+ yield model.mount();
63
+ })); // [Life Cycle]: initialize all necessary seed data
64
+
65
+
66
+ _this.app.on('db.init', /*#__PURE__*/_asyncToGenerator(function* () {})); // const [Automation, AutomationJob] = database.getModels(['automations', 'automations_jobs']);
67
+ // Automation.addHook('afterCreate', async (model: AutomationModel) => {
68
+ // model.get('enabled') && await model.loadJobs();
69
+ // });
70
+ // Automation.addHook('afterUpdate', async (model: AutomationModel) => {
71
+ // if (!model.changed('enabled' as any)) {
72
+ // return;
73
+ // }
74
+ // model.get('enabled') ? await model.loadJobs() : await model.cancelJobs();
75
+ // });
76
+ // Automation.addHook('beforeDestroy', async (model: AutomationModel) => {
77
+ // await model.cancelJobs();
78
+ // });
79
+ // AutomationJob.addHook('afterCreate', async (model: AutomationJobModel) => {
80
+ // await model.bootstrap();
81
+ // });
82
+ // AutomationJob.addHook('beforeDestroy', async (model: AutomationJobModel) => {
83
+ // await model.cancel();
84
+ // });
85
+
86
+ })();
87
+ }
88
+
89
+ getName() {
90
+ return this.getPackageName(__dirname);
91
+ }
92
+
93
+ }
94
+
95
+ exports.default = WorkflowPlugin;
@@ -1,63 +1,125 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
9
+
10
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
11
+
12
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
13
+
14
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
15
+
16
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
17
+
18
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
19
+
20
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
21
+
3
22
  const MODE_BITMAP = {
4
- CREATE: 1,
5
- UPDATE: 2,
6
- DESTROY: 4
23
+ CREATE: 1,
24
+ UPDATE: 2,
25
+ DESTROY: 4
7
26
  };
8
27
  const MODE_BITMAP_EVENTS = new Map();
9
28
  MODE_BITMAP_EVENTS.set(MODE_BITMAP.CREATE, 'afterCreate');
10
29
  MODE_BITMAP_EVENTS.set(MODE_BITMAP.UPDATE, 'afterUpdate');
11
- MODE_BITMAP_EVENTS.set(MODE_BITMAP.DESTROY, 'afterDestroy');
12
- // async function, should return promise
30
+ MODE_BITMAP_EVENTS.set(MODE_BITMAP.DESTROY, 'afterDestroy'); // async function, should return promise
31
+
13
32
  function bindHandler(callback) {
14
- const { condition, changed } = this.config;
15
- return (data, options) => {
16
- // NOTE: if no configured fields changed, do not trigger
17
- if (changed && changed.length && changed.every(name => !data.changed(name))) {
18
- return;
19
- }
20
- // NOTE: if no configured condition match, do not trigger
21
- if (condition && condition.$and.length) {
22
- // TODO: check all conditions in condition against data
23
- // const calculation = toCalculation(condition);
24
- }
25
- return callback({ data: data.get() }, options);
26
- };
33
+ const _this$config = this.config,
34
+ condition = _this$config.condition,
35
+ changed = _this$config.changed;
36
+ return (data, options) => {
37
+ // NOTE: if no configured fields changed, do not trigger
38
+ if (changed && changed.length && changed.every(name => !data.changed(name))) {
39
+ return;
40
+ } // NOTE: if no configured condition match, do not trigger
41
+
42
+
43
+ if (condition && condition.$and.length) {// TODO: check all conditions in condition against data
44
+ // const calculation = toCalculation(condition);
45
+ }
46
+
47
+ return callback({
48
+ data: data.get()
49
+ }, options);
50
+ };
27
51
  }
28
- exports.default = {
29
- name: 'collection',
30
- on(callback) {
31
- var _a;
32
- const { database } = this.constructor;
33
- const { collection, mode } = this.config;
34
- const Collection = database.getCollection(collection);
35
- if (!Collection) {
36
- return;
37
- }
38
- for (let [key, event] of MODE_BITMAP_EVENTS.entries()) {
39
- if (mode & key) {
40
- if (!((_a = Collection.model.options.hooks[event]) === null || _a === void 0 ? void 0 : _a.find(item => item.name && item.name === this.getHookId()))) {
41
- Collection.model.addHook(event, this.getHookId(), bindHandler.call(this, callback));
42
- }
43
- }
44
- else {
45
- Collection.model.removeHook(event, this.getHookId());
46
- }
47
- }
48
- },
49
- off() {
50
- const { database } = this.constructor;
51
- const { collection, mode } = this.config;
52
- const Collection = database.getCollection(collection);
53
- if (!Collection) {
54
- return;
52
+
53
+ var _default = {
54
+ name: 'collection',
55
+
56
+ on(callback) {
57
+ const database = this.constructor.database;
58
+ const _this$config2 = this.config,
59
+ collection = _this$config2.collection,
60
+ mode = _this$config2.mode;
61
+ const Collection = database.getCollection(collection);
62
+
63
+ if (!Collection) {
64
+ return;
65
+ }
66
+
67
+ var _iterator = _createForOfIteratorHelper(MODE_BITMAP_EVENTS.entries()),
68
+ _step;
69
+
70
+ try {
71
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
72
+ let _step$value = _slicedToArray(_step.value, 2),
73
+ key = _step$value[0],
74
+ event = _step$value[1];
75
+
76
+ if (mode & key) {
77
+ var _Collection$model$opt;
78
+
79
+ if (!((_Collection$model$opt = Collection.model.options.hooks[event]) === null || _Collection$model$opt === void 0 ? void 0 : _Collection$model$opt.find(item => item.name && item.name === this.getHookId()))) {
80
+ Collection.model.addHook(event, this.getHookId(), bindHandler.call(this, callback));
81
+ }
82
+ } else {
83
+ Collection.model.removeHook(event, this.getHookId());
55
84
  }
56
- for (let [key, event] of MODE_BITMAP_EVENTS.entries()) {
57
- if (mode & key) {
58
- Collection.model.removeHook(event, this.getHookId());
59
- }
85
+ }
86
+ } catch (err) {
87
+ _iterator.e(err);
88
+ } finally {
89
+ _iterator.f();
90
+ }
91
+ },
92
+
93
+ off() {
94
+ const database = this.constructor.database;
95
+ const _this$config3 = this.config,
96
+ collection = _this$config3.collection,
97
+ mode = _this$config3.mode;
98
+ const Collection = database.getCollection(collection);
99
+
100
+ if (!Collection) {
101
+ return;
102
+ }
103
+
104
+ var _iterator2 = _createForOfIteratorHelper(MODE_BITMAP_EVENTS.entries()),
105
+ _step2;
106
+
107
+ try {
108
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
109
+ let _step2$value = _slicedToArray(_step2.value, 2),
110
+ key = _step2$value[0],
111
+ event = _step2$value[1];
112
+
113
+ if (mode & key) {
114
+ Collection.model.removeHook(event, this.getHookId());
60
115
  }
116
+ }
117
+ } catch (err) {
118
+ _iterator2.e(err);
119
+ } finally {
120
+ _iterator2.f();
61
121
  }
122
+ }
123
+
62
124
  };
63
- //# sourceMappingURL=collection.js.map
125
+ exports.default = _default;
@@ -1,12 +1,26 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.triggers = void 0;
7
- const utils_1 = require("@nocobase/utils");
8
- const collection_1 = __importDefault(require("./collection"));
9
- exports.triggers = new utils_1.Registry();
10
- exports.default = exports.triggers;
11
- exports.triggers.register(collection_1.default.name, collection_1.default);
12
- //# sourceMappingURL=index.js.map
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.triggers = exports.default = void 0;
7
+
8
+ function _utils() {
9
+ const data = require("@nocobase/utils");
10
+
11
+ _utils = function _utils() {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
17
+
18
+ var _collection = _interopRequireDefault(require("./collection"));
19
+
20
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
+
22
+ const triggers = new (_utils().Registry)();
23
+ exports.triggers = triggers;
24
+ var _default = triggers;
25
+ exports.default = _default;
26
+ triggers.register(_collection.default.name, _collection.default);