@xuda.io/xuda-worker-bundle-min 1.3.1185 → 1.3.1187

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 (2) hide show
  1. package/index.js +364 -139
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -3217,6 +3217,12 @@ func.datasource.run_events_functions = async function (
3217
3217
  calling_job,
3218
3218
  async_event
3219
3219
  ) {
3220
+
3221
+ if (typeof dataSourceSession === "undefined" || dataSourceSession === null) {
3222
+ console.warn(`Event ${event_id} not exist or not found`)
3223
+ return
3224
+ }
3225
+
3220
3226
  // return new Promise(async (resolve, reject) => {
3221
3227
  var _ds = SESSION_OBJ[SESSION_ID].DS_GLB[dataSourceSession];
3222
3228
  var args = _ds.args;
@@ -3262,20 +3268,7 @@ func.datasource.run_events_functions = async function (
3262
3268
  if (job_promises.length) {
3263
3269
  await Promise.all(job_promises)
3264
3270
  }
3265
- // console.log(job_promises)
3266
- // resolve()
3267
3271
 
3268
- // if (job_promises.length) {
3269
- // Promise.all(job_promises)
3270
- // .then((e) => {
3271
- // resolve()
3272
- // });
3273
-
3274
- // } else {
3275
- // resolve()
3276
- // }
3277
-
3278
- // })
3279
3272
 
3280
3273
  };
3281
3274
 
@@ -4101,11 +4094,15 @@ func.datasource.del = function (SESSION_ID, dsP) {
4101
4094
  func.datasource.update = async function (
4102
4095
  SESSION_ID,
4103
4096
  datasource_changes,
4104
- from_worker
4097
+ update_local_scope_only
4105
4098
  ) {
4106
4099
  return new Promise(async (resolve, reject) => {
4107
4100
  var _session = SESSION_OBJ[SESSION_ID];
4108
4101
 
4102
+ if (_session.IS_API || typeof IS_MASTER_WEBSOCKET !== "undefined" || typeof IS_PROCESS_SERVER !== "undefined") {
4103
+ update_local_scope_only = true
4104
+ }
4105
+
4109
4106
  if (typeof glb.GLOBAL_VARS === "undefined") {
4110
4107
  glb.GLOBAL_VARS = (
4111
4108
  await func.common.get_module(
@@ -4115,68 +4112,81 @@ func.datasource.update = async function (
4115
4112
  ).system_globals;
4116
4113
  }
4117
4114
 
4118
- const set_fieldComputed_dependencies = async function (dsNo, record_id, field_id, value) {
4119
- // check if field has fieldComputed property
4120
- const _progFields = await func.datasource.get_progFields(
4121
- SESSION_ID,
4122
- dataSource
4123
- );
4115
+ const set_fieldComputed_dependencies = async function (dsNo, field_id, parent_ds) {
4124
4116
 
4125
- let propExpressions
4126
- for await (const val of _progFields) {
4117
+ // iterate child ds
4118
+ for (const [dsSession, _ds] of Object.entries(_session.DS_GLB)) {
4119
+ if (parent_ds !== null) {
4120
+ if (_ds.parentDataSourceNo != parent_ds) continue
4121
+ } else {
4122
+ if (dsSession != dsNo) continue
4123
+ }
4124
+ let tree_ret = await func.utils.TREE_OBJ.get(SESSION_ID, _ds.prog_id);
4125
+ if (tree_ret.menuType === "component" || tree_ret.menuType === "globals") {
4126
+ // check if field has fieldComputed property
4127
+ const _progFields = await func.datasource.get_progFields(
4128
+ SESSION_ID,
4129
+ dsSession
4130
+ );
4131
+ // find if field is computed
4132
+ let fieldComputed_propExpressions, fieldComputed_id
4133
+ for await (const val of _progFields) {
4127
4134
 
4128
- const fieldId = val.data.field_id;
4135
+ const fieldId = val.data.field_id;
4129
4136
 
4130
- if (fieldId !== field_id) continue
4131
- if (val.data.type !== "virtual" || !val.props.fieldComputed) break
4137
+ // if (fieldId !== field_id) continue
4138
+ if (val.data.type !== "virtual" || !val.props.fieldComputed) continue
4132
4139
 
4133
- propExpressions = val.props?.propExpressions?.fieldValue
4140
+ const _propExpressions = val.props?.propExpressions?.fieldValue
4141
+ if (_propExpressions && JSON.stringify(_propExpressions).includes(field_id)) {
4142
+ fieldComputed_propExpressions = _propExpressions
4143
+ fieldComputed_id = fieldId
4144
+ }
4145
+ }
4134
4146
 
4135
- }
4147
+ if (!fieldComputed_id) return
4136
4148
 
4137
- if (!propExpressions) return
4138
4149
 
4139
- let new_datasource_changes = {}
4140
- for (const _ds of _session.DS_GLB) {
4141
- if (_ds.parentDataSourceNo !== dsNo) continue
4150
+ // iterate ds rows
4151
+ for (const row of _ds.data_feed?.rows || []) {
4152
+ // iterate row fields
4153
+ for (const [key, val] of Object.entries(row)) {
4154
+ if (key !== fieldComputed_id) continue
4155
+ try {
4142
4156
 
4143
- if (!new_datasource_changes[_ds.dsSession]) {
4144
- new_datasource_changes[_ds.dsSession] = {}
4145
- }
4146
- for (const row of _ds.data_feed?.rows || []) {
4147
- for (const [key, val] of Object.entries(row)) {
4148
- if (key !== field_id) return
4149
- try {
4150
- if (!new_datasource_changes[_ds.dsSession][row._ROWID]) {
4151
- new_datasource_changes[_ds.dsSession][row._ROWID] = {}
4152
- }
4153
- let ret = await func.expression.get(
4154
- SESSION_ID,
4155
- propExpressions,
4156
- dsNo,
4157
- "update",
4158
- row._ROWID
4159
- );
4160
- new_datasource_changes[_ds.dsSession][row._ROWID][field_id] = ret.result
4161
- // const row_idx = func.common.find_ROWID_idx(_ds, record_id);
4157
+ let ret = await func.expression.get(
4158
+ SESSION_ID,
4159
+ fieldComputed_propExpressions,
4160
+ dsNo,
4161
+ "update",
4162
+ row._ROWID
4163
+ );
4162
4164
 
4163
- // _ds.data_feed.rows[row_idx][field_id] = ret.result;
4164
- } catch (err) {
4165
- console.error(err);
4165
+ const row_idx = func.common.find_ROWID_idx(_ds, row._ROWID);
4166
+ if (_ds.data_feed.rows[row_idx][fieldComputed_id] !== ret.result) {
4167
+ _ds.data_feed.rows[row_idx][fieldComputed_id] = ret.result;
4168
+ if (!fields_changed.includes(fieldComputed_id)) {
4169
+ fields_changed.push(fieldComputed_id)
4170
+ }
4171
+ if (!datasource_changed.includes(dsSession)) {
4172
+ datasource_changed.push(dsSession)
4173
+ }
4174
+ }
4175
+ } catch (err) {
4176
+ console.error(err);
4177
+ }
4166
4178
  }
4167
4179
  }
4168
4180
  }
4169
-
4181
+ await set_fieldComputed_dependencies(dsNo, field_id, dsSession)
4170
4182
  }
4171
- debugger
4172
- await func.datasource.update(SESSION_ID,
4173
- new_datasource_changes,
4174
- from_worker)
4175
4183
 
4176
4184
  }
4177
4185
 
4178
4186
  var fields_changed = [];
4179
4187
  var datasource_changed = [];
4188
+ let client_datasource_changes = {}
4189
+ let server_datasource_changes = {}
4180
4190
  // iterate changes datasource
4181
4191
  for await (const [dataSource, row_data] of Object.entries(
4182
4192
  datasource_changes
@@ -4205,12 +4215,51 @@ func.datasource.update = async function (
4205
4215
  try {
4206
4216
  const row_idx = func.common.find_ROWID_idx(_ds, record_id);
4207
4217
  _ds.data_feed.rows[row_idx][field_id] = value;
4218
+ await set_fieldComputed_dependencies(dataSource, field_id, null)
4219
+
4220
+ if (!update_local_scope_only) {
4221
+ let tree_ret = await func.utils.TREE_OBJ.get(SESSION_ID, _ds.prog_id);
4222
+ if (glb.IS_WORKER) {
4223
+ // RUN AT SERVER
4224
+ if (tree_ret.menuType === "globals" || tree_ret.menuType === "component") {
4225
+
4226
+ const _progFields = await func.datasource.get_progFields(
4227
+ SESSION_ID,
4228
+ dataSource
4229
+ );
4230
+ let view_field_obj = func.common.find_item_by_key(
4231
+ _progFields,
4232
+ "field_id",
4233
+ field_id
4234
+ );
4235
+ if (!view_field_obj?.data?.serverField && record_id !== "data_system") {
4236
+ if (!client_datasource_changes[dataSource]) {
4237
+ client_datasource_changes[dataSource] = {}
4238
+ }
4239
+ if (!client_datasource_changes[dataSource][record_id]) {
4240
+ client_datasource_changes[dataSource][record_id] = {}
4241
+ }
4242
+ client_datasource_changes[dataSource][record_id][field_id] = value;
4243
+ }
4244
+
4245
+ }
4246
+ } else {
4247
+ // RUN AT CLIENT
4248
+ if ((tree_ret.menuType === "component" && _ds._run_at !== "client") || tree_ret.menuType === "globals") {
4249
+ if (!server_datasource_changes[dataSource]) {
4250
+ server_datasource_changes[dataSource] = {}
4251
+ }
4252
+ if (!server_datasource_changes[dataSource][record_id]) {
4253
+ server_datasource_changes[dataSource][record_id] = {}
4254
+ }
4255
+ server_datasource_changes[dataSource][record_id][field_id] = value;
4256
+ }
4257
+ }
4258
+ }
4208
4259
  } catch (err) {
4209
4260
  console.error(err);
4210
4261
  }
4211
4262
 
4212
-
4213
-
4214
4263
  if (!fields_changed.includes(field_id)) {
4215
4264
  fields_changed.push(field_id);
4216
4265
  }
@@ -4221,102 +4270,45 @@ func.datasource.update = async function (
4221
4270
  _ds.currentRecordId = record_id;
4222
4271
  }
4223
4272
 
4224
- if (!from_worker) {
4225
- if (glb.IS_WORKER) {
4226
- let tree_ret = await func.utils.TREE_OBJ.get(SESSION_ID, _ds.prog_id);
4227
4273
 
4228
- if (tree_ret.menuType === "globals") {
4229
-
4230
- const _progFields = await func.datasource.get_progFields(
4231
- SESSION_ID,
4232
- dataSource
4233
- );
4234
- let view_field_obj = func.common.find_item_by_key(
4235
- _progFields,
4236
- "field_id",
4237
- field_id
4238
- );
4239
- if (view_field_obj?.data?.serverField) {
4240
- delete datasource_changes[dataSource][record_id][field_id]
4241
- }
4242
-
4243
- if (record_id === "data_system") {
4244
- delete datasource_changes[dataSource][record_id]
4245
- }
4246
- }
4247
- }
4248
- }
4249
4274
  }
4250
4275
  if (!_ds.data_feed.rows_changed) {
4251
4276
  _ds.data_feed.rows_changed = [];
4252
4277
  }
4253
4278
  if (!_ds.data_feed.rows_changed.includes(record_id))
4254
4279
  _ds.data_feed.rows_changed.push(record_id);
4280
+ }
4255
4281
 
4256
4282
 
4257
4283
 
4258
- }
4259
4284
 
4260
- if (!from_worker) {
4261
- if (glb.IS_WORKER) {
4262
- let tree_ret = await func.utils.TREE_OBJ.get(SESSION_ID, _ds.prog_id);
4263
- let server_field = false
4264
- // if (tree_ret.menuType === "globals") {
4265
4285
 
4266
- // const _progFields = await func.datasource.get_progFields(
4267
- // SESSION_ID,
4268
- // dataSource
4269
- // );
4270
- // let view_field_obj = func.common.find_item_by_key(
4271
- // _progFields,
4272
- // "field_id",
4273
- // field_id
4274
- // );
4275
- // server_field = view_field_obj.serverField
4276
- // }
4286
+ }
4277
4287
 
4288
+ if (glb.IS_WORKER) {
4278
4289
 
4279
- if (_session.IS_API || typeof IS_MASTER_WEBSOCKET !== "undefined" || typeof IS_PROCESS_SERVER !== "undefined") {
4280
- continue;
4281
- }
4290
+ if (!update_local_scope_only && !_.isEmpty(client_datasource_changes)) {
4291
+ func.utils.post_back_to_client(
4292
+ SESSION_ID,
4293
+ "update_client_eventChangesResults_from_worker",
4294
+ _session.worker_id,
4295
+ client_datasource_changes
4296
+ );
4282
4297
 
4283
- if (tree_ret.menuType !== "component" && tree_ret.menuType !== "globals") {
4284
- // if (tree_ret.menuType !== "globals" || server_field) {
4285
- continue;
4286
- // }
4287
- }
4288
- // debugger;
4289
- // update client
4298
+ }
4290
4299
 
4291
- func.utils.post_back_to_client(
4292
- SESSION_ID,
4293
- "update_client_eventChangesResults_from_worker",
4294
- _session.worker_id,
4295
- datasource_changes
4296
- );
4297
- return resolve();
4298
- }
4300
+ } else {
4301
+ if (!update_local_scope_only && !_.isEmpty(server_datasource_changes)) {
4299
4302
 
4300
- if (!glb.IS_WORKER) {
4301
- if (_ds._run_at !== "client" || _ds.tree_obj.menuType === "globals") {
4302
- // no need to update worker
4303
- // return resolve();
4304
- // continue;
4305
- // }
4306
-
4307
- const ret = await func.index.call_worker(SESSION_ID, {
4308
- service: "update_datasource_changes_from_client",
4309
- data: {
4310
- session_id: SESSION_ID,
4311
- datasource_changes,
4312
- },
4313
- id: _ds.worker_id,
4314
- });
4315
- }
4316
- }
4303
+ const ret = await func.index.call_worker(SESSION_ID, {
4304
+ service: "update_datasource_changes_from_client",
4305
+ data: {
4306
+ session_id: SESSION_ID,
4307
+ datasource_changes: server_datasource_changes,
4308
+ },
4309
+ id: _ds.worker_id,
4310
+ });
4317
4311
  }
4318
- }
4319
- if (!glb.IS_WORKER) {
4320
4312
  ///// REFRESH SCREEN
4321
4313
  if (fields_changed.length) {
4322
4314
  await func.UI.screen.refresh_xu_attributes(SESSION_ID, fields_changed);
@@ -4328,13 +4320,246 @@ func.datasource.update = async function (
4328
4320
  );
4329
4321
  }
4330
4322
  }
4331
- return resolve();
4323
+ resolve();
4332
4324
  });
4325
+
4333
4326
  };
4334
4327
 
4335
4328
 
4329
+ // func.datasource.update = async function (
4330
+ // SESSION_ID,
4331
+ // datasource_changes,
4332
+ // update_local_scope_only
4333
+ // ) {
4334
+ // return new Promise(async (resolve, reject) => {
4335
+ // var _session = SESSION_OBJ[SESSION_ID];
4336
+
4337
+ // if (typeof glb.GLOBAL_VARS === "undefined") {
4338
+ // glb.GLOBAL_VARS = (
4339
+ // await func.common.get_module(
4340
+ // SESSION_ID,
4341
+ // "xuda-system-globals-module.mjs"
4342
+ // )
4343
+ // ).system_globals;
4344
+ // }
4345
+
4346
+ // let fieldComputed_datasource_changes = {};
4347
+
4348
+ // const set_fieldComputed_dependencies = async function (dsNo, field_id, parent_ds) {
4349
+
4350
+ // // iterate child ds
4351
+ // for (const [dsSession, _ds] of Object.entries(_session.DS_GLB)) {
4352
+ // if (parent_ds !== null) {
4353
+ // if (_ds.parentDataSourceNo != parent_ds) continue
4354
+ // } else {
4355
+ // if (dsSession != dsNo) continue
4356
+ // }
4357
+ // let tree_ret = await func.utils.TREE_OBJ.get(SESSION_ID, _ds.prog_id);
4358
+ // if (tree_ret.menuType === "component" || tree_ret.menuType === "globals") {
4359
+ // // check if field has fieldComputed property
4360
+ // const _progFields = await func.datasource.get_progFields(
4361
+ // SESSION_ID,
4362
+ // dsSession
4363
+ // );
4364
+ // // find if field is computed
4365
+ // let propExpressions
4366
+ // for await (const val of _progFields) {
4367
+
4368
+ // const fieldId = val.data.field_id;
4369
+
4370
+ // if (fieldId !== field_id) continue
4371
+ // if (val.data.type !== "virtual" || !val.props.fieldComputed) break
4372
+
4373
+ // const _propExpressions = val.props?.propExpressions?.fieldValue
4374
+ // if (_propExpressions && JSON.stringify(_propExpressions).includes(field_id)) {
4375
+ // propExpressions = _propExpressions
4376
+ // }
4377
+ // }
4378
+
4379
+ // if (!propExpressions) return
4380
+
4381
+ // if (!fieldComputed_datasource_changes[dsSession]) {
4382
+ // fieldComputed_datasource_changes[dsSession] = {}
4383
+ // }
4384
+ // // iterate ds rows
4385
+ // for (const row of _ds.data_feed?.rows || []) {
4386
+ // // iterate row fields
4387
+ // for (const [key, val] of Object.entries(row)) {
4388
+ // if (key !== field_id) return
4389
+ // try {
4390
+ // if (!fieldComputed_datasource_changes[dsSession][row._ROWID]) {
4391
+ // fieldComputed_datasource_changes[dsSession][row._ROWID] = {}
4392
+ // }
4393
+ // let ret = await func.expression.get(
4394
+ // SESSION_ID,
4395
+ // propExpressions,
4396
+ // dsNo,
4397
+ // "update",
4398
+ // row._ROWID
4399
+ // );
4400
+ // fieldComputed_datasource_changes[dsSession][row._ROWID][field_id] = ret.result
4401
+ // // const row_idx = func.common.find_ROWID_idx(_ds, record_id);
4402
+
4403
+ // // _ds.data_feed.rows[row_idx][field_id] = ret.result;
4404
+ // } catch (err) {
4405
+ // console.error(err);
4406
+ // }
4407
+ // }
4408
+ // }
4409
+ // }
4410
+ // await set_fieldComputed_dependencies(dsNo, field_id, dsSession)
4411
+ // }
4412
+ // debugger
4413
+
4414
+
4415
+
4416
+ // }
4417
+
4418
+ // var fields_changed = [];
4419
+ // var datasource_changed = [];
4420
+ // let client_datasource_changes={}
4421
+ // // iterate changes datasource
4422
+ // for await (const [dataSource, row_data] of Object.entries(
4423
+ // datasource_changes
4424
+ // )) {
4425
+ // var _ds = _session.DS_GLB[dataSource];
4426
+ // if (!_ds) {
4427
+ // continue;
4428
+ // }
4429
+ // // iterate changes records
4430
+ // for (const [record_id, fields_data] of Object.entries(row_data)) {
4431
+ // // iterate changes fields
4432
+ // for (const [field_id, value] of Object.entries(fields_data)) {
4433
+ // // mechanism to make update directly on the datasource object
4434
+ // if (record_id === "datasource_main") {
4435
+ // _.set(_ds, field_id, value);
4436
+ // continue;
4437
+ // }
4438
+
4439
+ // if (typeof fields_data === "object") {
4440
+ // if (glb.GLOBAL_VARS[field_id]) {
4441
+ // _ds.data_system[field_id] = value;
4442
+
4443
+ // continue;
4444
+ // }
4445
+
4446
+ // try {
4447
+ // const row_idx = func.common.find_ROWID_idx(_ds, record_id);
4448
+ // _ds.data_feed.rows[row_idx][field_id] = value;
4449
+ // await set_fieldComputed_dependencies(dataSource, field_id, null)
4450
+ // } catch (err) {
4451
+ // console.error(err);
4452
+ // }
4453
+
4454
+
4455
+
4456
+ // if (!fields_changed.includes(field_id)) {
4457
+ // fields_changed.push(field_id);
4458
+ // }
4459
+ // if (!datasource_changed.includes(dataSource)) {
4460
+ // datasource_changed.push(dataSource);
4461
+ // }
4462
+ // } else if (fields_data === "set") {
4463
+ // _ds.currentRecordId = record_id;
4464
+ // }
4465
+
4466
+ // if (!update_local_scope_only) {
4467
+ // if (glb.IS_WORKER) {
4468
+ // let tree_ret = await func.utils.TREE_OBJ.get(SESSION_ID, _ds.prog_id);
4469
+
4470
+ // if (tree_ret.menuType === "globals") {
4471
+
4472
+ // const _progFields = await func.datasource.get_progFields(
4473
+ // SESSION_ID,
4474
+ // dataSource
4475
+ // );
4476
+ // let view_field_obj = func.common.find_item_by_key(
4477
+ // _progFields,
4478
+ // "field_id",
4479
+ // field_id
4480
+ // );
4481
+ // if (view_field_obj?.data?.serverField) {
4482
+ // delete datasource_changes[dataSource][record_id][field_id]
4483
+ // }
4484
+
4485
+ // if (record_id === "data_system") {
4486
+ // delete datasource_changes[dataSource][record_id]
4487
+ // }
4488
+ // }
4489
+ // }
4490
+ // }
4491
+ // }
4492
+ // if (!_ds.data_feed.rows_changed) {
4493
+ // _ds.data_feed.rows_changed = [];
4494
+ // }
4495
+ // if (!_ds.data_feed.rows_changed.includes(record_id))
4496
+ // _ds.data_feed.rows_changed.push(record_id);
4497
+ // }
4498
+
4499
+
4500
+ // let new_datasource_changes = { ...datasource_changes, ...fieldComputed_datasource_changes }
4501
+
4502
+ // if (!update_local_scope_only) {
4503
+ // if (glb.IS_WORKER) {
4504
+ // let tree_ret = await func.utils.TREE_OBJ.get(SESSION_ID, _ds.prog_id);
4336
4505
 
4337
4506
 
4507
+ // if (_session.IS_API || typeof IS_MASTER_WEBSOCKET !== "undefined" || typeof IS_PROCESS_SERVER !== "undefined") {
4508
+ // continue;
4509
+ // }
4510
+
4511
+ // if (tree_ret.menuType !== "component" && tree_ret.menuType !== "globals") {
4512
+
4513
+ // continue;
4514
+
4515
+ // }
4516
+
4517
+ // // update client
4518
+
4519
+ // func.utils.post_back_to_client(
4520
+ // SESSION_ID,
4521
+ // "update_client_eventChangesResults_from_worker",
4522
+ // _session.worker_id,
4523
+ // new_datasource_changes
4524
+ // );
4525
+ // return resolve();
4526
+ // }
4527
+
4528
+ // if (!glb.IS_WORKER) {
4529
+ // if (_ds._run_at !== "client" || _ds.tree_obj.menuType === "globals") {
4530
+ // // no need to update worker
4531
+ // // return resolve();
4532
+ // // continue;
4533
+ // // }
4534
+
4535
+ // const ret = await func.index.call_worker(SESSION_ID, {
4536
+ // service: "update_datasource_changes_from_client",
4537
+ // data: {
4538
+ // session_id: SESSION_ID,
4539
+ // new_datasource_changes,
4540
+ // },
4541
+ // id: _ds.worker_id,
4542
+ // });
4543
+ // }
4544
+ // }
4545
+ // }
4546
+ // }
4547
+ // if (!glb.IS_WORKER) {
4548
+ // ///// REFRESH SCREEN
4549
+ // if (fields_changed.length) {
4550
+ // await func.UI.screen.refresh_xu_attributes(SESSION_ID, fields_changed);
4551
+ // await func.UI.screen.refresh_screen(
4552
+ // SESSION_ID,
4553
+ // fields_changed,
4554
+ // null,
4555
+ // datasource_changed[0] // refresh the current datasource only
4556
+ // );
4557
+ // }
4558
+ // }
4559
+ // return resolve();
4560
+ // });
4561
+ // };
4562
+
4338
4563
  func.datasource.callback = function (
4339
4564
  SESSION_ID,
4340
4565
  dsSessionP,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/xuda-worker-bundle-min",
3
- "version": "1.3.1185",
3
+ "version": "1.3.1187",
4
4
  "description": "xuda framework min",
5
5
  "main": "index.js",
6
6
  "scripts": {