@jcbuisson/express-x-client 3.0.1 → 3.0.3
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 +1 -1
- package/src/client.mts +36 -27
package/package.json
CHANGED
package/src/client.mts
CHANGED
|
@@ -8,20 +8,6 @@ import { tryOnScopeDispose } from '@vueuse/core';
|
|
|
8
8
|
import { useSessionStorage } from '@vueuse/core'
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
////////////////////////// UTILITIES //////////////////////////
|
|
12
|
-
|
|
13
|
-
function generateUID(length) {
|
|
14
|
-
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
15
|
-
let uid = ''
|
|
16
|
-
|
|
17
|
-
for (let i = 0; i < length; i++) {
|
|
18
|
-
const randomIndex = Math.floor(Math.random() * characters.length)
|
|
19
|
-
uid += characters.charAt(randomIndex)
|
|
20
|
-
}
|
|
21
|
-
return uid
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
11
|
////////////////////////// EXPRESSX //////////////////////////
|
|
26
12
|
|
|
27
13
|
export function createClient(socket, options={}) {
|
|
@@ -150,8 +136,6 @@ export function createClient(socket, options={}) {
|
|
|
150
136
|
return new Proxy(service, handler)
|
|
151
137
|
}
|
|
152
138
|
|
|
153
|
-
//-------------------- APPLICATION-LEVEL EVENTS --------------------
|
|
154
|
-
|
|
155
139
|
// There is a need for application-wide events sent outside any service method call, for example when backend state changes
|
|
156
140
|
// without front-end interactions
|
|
157
141
|
socket.on('app-event', ({ type, value }) => {
|
|
@@ -166,6 +150,16 @@ export function createClient(socket, options={}) {
|
|
|
166
150
|
type2appHandler[type] = handler
|
|
167
151
|
}
|
|
168
152
|
|
|
153
|
+
function connect() {
|
|
154
|
+
if (options.debug) console.log('connecting...')
|
|
155
|
+
socket.connect()
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function disconnect() {
|
|
159
|
+
if (options.debug) console.log('disconnecting...')
|
|
160
|
+
socket.disconnect()
|
|
161
|
+
}
|
|
162
|
+
|
|
169
163
|
const app = {
|
|
170
164
|
configure,
|
|
171
165
|
addConnectListener,
|
|
@@ -177,6 +171,7 @@ export function createClient(socket, options={}) {
|
|
|
177
171
|
|
|
178
172
|
service,
|
|
179
173
|
on,
|
|
174
|
+
connect, disconnect,
|
|
180
175
|
}
|
|
181
176
|
|
|
182
177
|
return app
|
|
@@ -370,9 +365,9 @@ export function offlinePlugin(app) {
|
|
|
370
365
|
return removeSynchroDBWhere(where, db.whereList)
|
|
371
366
|
}
|
|
372
367
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
368
|
+
async function synchronizeAll() {
|
|
369
|
+
await synchronizeModelWhereList(modelName, db.values, db.metadata, app.disconnectedDate, db.whereList)
|
|
370
|
+
}
|
|
376
371
|
|
|
377
372
|
// Automatically clean up when the component using this composable unmounts
|
|
378
373
|
tryOnScopeDispose(async () => {
|
|
@@ -388,7 +383,7 @@ export function offlinePlugin(app) {
|
|
|
388
383
|
create, update, remove,
|
|
389
384
|
findByUID, findWhere,
|
|
390
385
|
getObservable,
|
|
391
|
-
|
|
386
|
+
synchronizeAll,
|
|
392
387
|
addSynchroWhere,
|
|
393
388
|
}
|
|
394
389
|
}
|
|
@@ -411,7 +406,7 @@ export function offlinePlugin(app) {
|
|
|
411
406
|
const mutex = new Mutex()
|
|
412
407
|
|
|
413
408
|
// ex: where = { uid: 'azer' }
|
|
414
|
-
async function synchronize(
|
|
409
|
+
async function synchronize(modelName, idbValues, idbMetadata, where, cutoffDate) {
|
|
415
410
|
await mutex.acquire()
|
|
416
411
|
console.log('synchronize', modelName, where)
|
|
417
412
|
|
|
@@ -566,12 +561,12 @@ export function offlinePlugin(app) {
|
|
|
566
561
|
}
|
|
567
562
|
}
|
|
568
563
|
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
564
|
+
async function synchronizeModelWhereList(modelName, idbValues, idbMetadata, cutoffDate, whereDb) {
|
|
565
|
+
const whereList = await getWhereList(whereDb)
|
|
566
|
+
for (const where of whereList) {
|
|
567
|
+
await synchronize(modelName, idbValues, idbMetadata, where, cutoffDate)
|
|
568
|
+
}
|
|
569
|
+
}
|
|
575
570
|
|
|
576
571
|
// Singleton map to reuse Dexie instances per database name
|
|
577
572
|
const dbInstances = new Map();
|
|
@@ -596,6 +591,20 @@ export function offlinePlugin(app) {
|
|
|
596
591
|
}
|
|
597
592
|
|
|
598
593
|
|
|
594
|
+
////////////////////////// UTILITIES //////////////////////////
|
|
595
|
+
|
|
596
|
+
function generateUID(length) {
|
|
597
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
598
|
+
let uid = ''
|
|
599
|
+
|
|
600
|
+
for (let i = 0; i < length; i++) {
|
|
601
|
+
const randomIndex = Math.floor(Math.random() * characters.length)
|
|
602
|
+
uid += characters.charAt(randomIndex)
|
|
603
|
+
}
|
|
604
|
+
return uid
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
|
|
599
608
|
function stringifyWithSortedKeys(obj, space = null) {
|
|
600
609
|
return JSON.stringify(obj, (key, value) => {
|
|
601
610
|
// If the value is a plain object (not an array, null, or other object type like Date)
|