@mixd-id/web-scaffold 0.1.230406275 → 0.1.230406277

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,7 +1,7 @@
1
1
  {
2
2
  "name": "@mixd-id/web-scaffold",
3
3
  "private": false,
4
- "version": "0.1.230406275",
4
+ "version": "0.1.230406277",
5
5
  "scripts": {
6
6
  "dev": "vite serve",
7
7
  "build": "vite build",
@@ -342,9 +342,6 @@ const getValue = (filter, opt) => {
342
342
  break
343
343
  }
344
344
 
345
-
346
- console.log('getValue', filter, key, type, operator, util.inspect(whereObj, false, null, true /* enable colors */))
347
-
348
345
  return whereObj
349
346
  }
350
347
 
@@ -366,6 +363,8 @@ const filtersToSequelizeWhere = async(filters, opt) => {
366
363
 
367
364
  const { key, value, operator } = filter
368
365
 
366
+ if(key.indexOf('.') >= 0) continue
367
+
369
368
  if(Array.isArray(value) && !operator){
370
369
  const { modifier = 'and' } = filter
371
370
 
@@ -393,7 +392,6 @@ const filtersToSequelizeWhere = async(filters, opt) => {
393
392
  ...getValue(filter, opt)
394
393
  }
395
394
  }
396
-
397
395
  }
398
396
 
399
397
  return {
@@ -808,6 +806,81 @@ const sortsToSequelizeWhere = async (sorts, opt) => {
808
806
  }
809
807
  }
810
808
 
809
+ const filtersToSequelizeInclude = async(filters, opt, includes) => {
810
+ if(!Array.isArray(filters) || filters.length < 1){
811
+ return includes
812
+ }
813
+
814
+ // TODO: Test on 2 level or more level includes
815
+
816
+ // Generate where object
817
+ let whereObj = {}
818
+ for(let filter of filters) {
819
+ if (filter.enabled === false) continue
820
+
821
+ const {key, value, operator} = filter
822
+
823
+ if(key.indexOf('.') < 0) continue
824
+
825
+ if(Array.isArray(value) && !operator){
826
+ const { modifier = 'and' } = filter
827
+
828
+ if(![ 'and', 'or' ].includes(modifier)) continue
829
+
830
+ const opModifierValue = []
831
+ for(let v of value){
832
+ opModifierValue.push(getValue({ ...v, key }, {
833
+ ...opt,
834
+ withoutKey: true
835
+ }))
836
+ }
837
+
838
+ whereObj = {
839
+ ...whereObj,
840
+ [key]: {
841
+ [Op[modifier]]: opModifierValue
842
+ }
843
+ }
844
+ }
845
+ else{
846
+ whereObj = {
847
+ ...whereObj,
848
+ ...getValue(filter, opt)
849
+ }
850
+ }
851
+ }
852
+ //console.log('whereObj', whereObj)
853
+
854
+ // Apply where to includes
855
+ for(let key in whereObj){
856
+
857
+ const keyParts = key.split('.')
858
+ if(keyParts.length < 2) continue
859
+
860
+ // Walk through the includes to find the correct include
861
+ let include
862
+ for(let i = 0 ; i < keyParts.length - 1 ; i++){
863
+ const as = keyParts[i]
864
+ include = !include ? includes.find(_ => _.as === as) : include.includes ?? []
865
+ }
866
+
867
+ // If include is not found, skip
868
+ if(!include){
869
+ continue
870
+ }
871
+
872
+ // Generate where object for the correct include
873
+ const currentKey = keyParts[keyParts.length - 1]
874
+ if(!include.where){
875
+ include.where = { [Op.and]: [] }
876
+ }
877
+ include.where[Op.and].push({ [currentKey]: whereObj[key] })
878
+ }
879
+
880
+ //console.log('includes', includes)
881
+ return includes
882
+ }
883
+
811
884
  module.exports = {
812
885
  filtersToSequelizeWhere,
813
886
  afterItemToSequelizeWhere,
@@ -815,4 +888,5 @@ module.exports = {
815
888
  chartToSequelizeWhere,
816
889
  pivotToSequelizeWhere,
817
890
  sortsToSequelizeWhere,
891
+ filtersToSequelizeInclude,
818
892
  }
package/src/utils/wss.js CHANGED
@@ -138,11 +138,14 @@ class WSS extends EventEmitter2{
138
138
  return socket.close(1002, e.message);
139
139
  }
140
140
 
141
- const { _requestId, path, params, pong } = obj
141
+ const { _requestId, path, params, ping, pong } = obj
142
142
 
143
143
  if(pong){
144
144
  delete socket.pinging
145
145
  }
146
+ else if(ping){
147
+ socket.send(await this.toBinaryData({ pong: 1 }))
148
+ }
146
149
  else{
147
150
  let status = 200
148
151
  let data
package/src/utils/wss.mjs CHANGED
@@ -45,6 +45,7 @@ class WSS extends EventEmitter2{
45
45
  _counter = 0
46
46
  _callbacks = {}
47
47
  _pendingSend = []
48
+ _pinging = false
48
49
 
49
50
  constructor(opt) {
50
51
  super();
@@ -58,6 +59,20 @@ class WSS extends EventEmitter2{
58
59
  }, opt ?? {})
59
60
 
60
61
  this.connect().then()
62
+
63
+ if(typeof window !== 'undefined'){
64
+ window.addEventListener('visibilitychange', () => {
65
+ if(document.visibilityState === 'visible'){
66
+ console.log('visibilitychange', this._instance.readyState)
67
+ this.emit('visibilitychange', this._instance.readyState, [])
68
+
69
+ this.ping().catch(e => {
70
+ console.log('ping error', e)
71
+ this.reconnect().then()
72
+ })
73
+ }
74
+ })
75
+ }
61
76
  }
62
77
 
63
78
  async fromBinaryData(binaryData){
@@ -77,6 +92,8 @@ class WSS extends EventEmitter2{
77
92
 
78
93
  async connect(reconnect = false){
79
94
 
95
+ console.log('connect', this._opt.token)
96
+
80
97
  this._instance = new WebSocket(this._opt.url, [ this._opt.token ])
81
98
 
82
99
  this._instance.binaryType = 'arraybuffer';
@@ -85,15 +102,18 @@ class WSS extends EventEmitter2{
85
102
 
86
103
  const obj = await this.fromBinaryData(event.data);
87
104
 
88
- const { _requestId, status, data, auth, ping } = obj
105
+ const { _requestId, status, data, auth, ping, pong } = obj
89
106
 
90
107
  if(ping){
91
108
  this._instance.send(await this.toBinaryData({ pong:1 }))
92
109
  }
110
+ else if(pong){
111
+ this._pinging = false
112
+ }
93
113
  else if(auth){
94
114
  this._instance.isAuth = auth
95
115
 
96
- reconnect ? this.emit('reconnect', null, []) : this.emit('connect', null, [])
116
+ this.emit('connect', reconnect, [])
97
117
 
98
118
  for(let sendParams of this._pendingSend){
99
119
  this.sendSync(sendParams.path, sendParams.params, sendParams.cb, sendParams.err)
@@ -139,6 +159,9 @@ class WSS extends EventEmitter2{
139
159
  this.emit('connect_error', e, [])
140
160
  break
141
161
 
162
+ case 1005:
163
+ break
164
+
142
165
  default:
143
166
  this.emit('disconnect', null, [])
144
167
  break
@@ -158,7 +181,6 @@ class WSS extends EventEmitter2{
158
181
 
159
182
  sendSync(path, params, cb, err, override){
160
183
  if(this._instance.readyState > 1){
161
- console.log('unable to send, ready state not 1', this._instance.readyState)
162
184
  return
163
185
  }
164
186
  else if(!this._instance.isAuth){
@@ -191,14 +213,12 @@ class WSS extends EventEmitter2{
191
213
  t1: new Date().getTime()
192
214
  }
193
215
 
194
- /*setTimeout(() => {
216
+ setTimeout(() => {
195
217
  if(this._callbacks[_requestId]){
196
218
  err({ message: 'Timeout' })
197
219
  delete this._callbacks[_requestId]
198
-
199
- this.reconnect()
200
220
  }
201
- }, (override ?? {}).timeout ?? this._opt.timeout)*/
221
+ }, (override ?? {}).timeout ?? this._opt.timeout)
202
222
  })
203
223
  }
204
224
 
@@ -208,6 +228,45 @@ class WSS extends EventEmitter2{
208
228
  })
209
229
  }
210
230
 
231
+ get readyState(){
232
+ const state = (this._instance ?? {}).readyState
233
+
234
+ if(state === 1 && !this._instance.isAuth){
235
+ return 0
236
+ }
237
+ if(!this._instance.isAuth)
238
+ return 0
239
+ return (this._instance ?? {}).readyState
240
+ }
241
+
242
+ async ping(timeout = 3000){
243
+ return new Promise(async (resolve, reject) => {
244
+
245
+ this._pinging = true
246
+ this._instance.send(await this.toBinaryData({ ping:1 }))
247
+
248
+ const t1 = new Date().getTime()
249
+
250
+ const pingCheck = () => {
251
+ if(!this._pinging){
252
+ resolve()
253
+ }
254
+ else{
255
+ const t2 = new Date().getTime()
256
+
257
+ if(t2 - t1 > timeout){
258
+ reject({ message: 'Ping timeout' })
259
+ }
260
+ else{
261
+ setTimeout(pingCheck, 100)
262
+ }
263
+ }
264
+ }
265
+
266
+ setTimeout(pingCheck, 100)
267
+ })
268
+ }
269
+
211
270
  }
212
271
 
213
272
  export {