@amityco/ts-sdk 7.32.1-fb6342dc.0 → 7.33.1-c82094c.0
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/cache/api/dropFromCache.d.ts.map +1 -1
- package/dist/cache/api/pushToCache.d.ts.map +1 -1
- package/dist/cache/api/queryCache.d.ts.map +1 -1
- package/dist/cache/api/queryCacheByProperty.d.ts +3 -0
- package/dist/cache/api/queryCacheByProperty.d.ts.map +1 -0
- package/dist/cache/api/tests/queryCache.test.d.ts +2 -0
- package/dist/cache/api/tests/queryCache.test.d.ts.map +1 -0
- package/dist/cache/utils/cacheIndex.d.ts +15 -0
- package/dist/cache/utils/cacheIndex.d.ts.map +1 -0
- package/dist/index.cjs.js +131 -18
- package/dist/index.esm.js +131 -18
- package/dist/index.umd.js +2 -2
- package/dist/messageRepository/utils/getMessageFromMainDB.d.ts.map +1 -1
- package/dist/messageRepository/utils/prepareMessagePayload.d.ts.map +1 -1
- package/dist/utils/linkedObject/channelLinkedObject.d.ts.map +1 -1
- package/dist/utils/linkedObject/tests/channelCachePerformance.test.d.ts +2 -0
- package/dist/utils/linkedObject/tests/channelCachePerformance.test.d.ts.map +1 -0
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dropFromCache.d.ts","sourceRoot":"","sources":["../../../src/cache/api/dropFromCache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"dropFromCache.d.ts","sourceRoot":"","sources":["../../../src/cache/api/dropFromCache.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,aAAa,4CAAyC,OAqBlE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pushToCache.d.ts","sourceRoot":"","sources":["../../../src/cache/api/pushToCache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pushToCache.d.ts","sourceRoot":"","sources":["../../../src/cache/api/pushToCache.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,WAAW,8BAEhB,OAAO,YACJ,MAAM,YAAY,KAC1B,OAmBF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queryCache.d.ts","sourceRoot":"","sources":["../../../src/cache/api/queryCache.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,UAAU,+
|
|
1
|
+
{"version":3,"file":"queryCache.d.ts","sourceRoot":"","sources":["../../../src/cache/api/queryCache.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,UAAU,+EAWtB,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** Internal model lookup; deliberately excluded from the public Cache API. @hidden */
|
|
2
|
+
export declare const queryCacheByProperty: <T>(prefix: [string, string], property: keyof T & string, value: string) => Amity.CacheEntry<T>[] | undefined;
|
|
3
|
+
//# sourceMappingURL=queryCacheByProperty.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queryCacheByProperty.d.ts","sourceRoot":"","sources":["../../../src/cache/api/queryCacheByProperty.ts"],"names":[],"mappings":"AAGA,sFAAsF;AACtF,eAAO,MAAM,oBAAoB,cACvB,CAAC,MAAM,EAAE,MAAM,CAAC,qCAEjB,MAAM,sCAKd,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queryCache.test.d.ts","sourceRoot":"","sources":["../../../../src/cache/api/tests/queryCache.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare type IndexedKey = {
|
|
2
|
+
encodedKey: string;
|
|
3
|
+
key: Amity.CacheKey;
|
|
4
|
+
order: number;
|
|
5
|
+
};
|
|
6
|
+
/** @hidden */
|
|
7
|
+
export declare const getMatchingCacheKeys: (data: Amity.Cache['data'], key: Amity.CacheKey) => IndexedKey[];
|
|
8
|
+
/** Update only an index that has already been used; writes alone need no index build. @hidden */
|
|
9
|
+
export declare const updateCacheIndex: (data: Amity.Cache['data'], encodedKey: string) => void;
|
|
10
|
+
/** @hidden */
|
|
11
|
+
export declare const removeFromCacheIndex: (data: Amity.Cache['data'], encodedKey: string) => void;
|
|
12
|
+
/** Index a string model property within one domain/operation, retaining cache insertion order. @hidden */
|
|
13
|
+
export declare const getCacheEntriesByProperty: <T>(data: Amity.Cache['data'], prefix: [string, string], property: string, value: string) => Amity.CacheEntry<T>[];
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=cacheIndex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cacheIndex.d.ts","sourceRoot":"","sources":["../../../src/cache/utils/cacheIndex.ts"],"names":[],"mappings":"AAEA,aAAK,UAAU,GAAG;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,GAAG,EAAE,KAAK,CAAC,QAAQ,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAsFF,cAAc;AACd,eAAO,MAAM,oBAAoB,SAAU,WAAW,CAAC,MAAM,CAAC,sCACoC,CAAC;AAEnG,iGAAiG;AACjG,eAAO,MAAM,gBAAgB,SAAU,WAAW,CAAC,MAAM,CAAC,cAAc,MAAM,SAQ7E,CAAC;AAEF,cAAc;AACd,eAAO,MAAM,oBAAoB,SAAU,WAAW,CAAC,MAAM,CAAC,cAAc,MAAM,SAWjF,CAAC;AAEF,0GAA0G;AAC1G,eAAO,MAAM,yBAAyB,YAC9B,WAAW,CAAC,MAAM,CAAC,UACjB,CAAC,MAAM,EAAE,MAAM,CAAC,YACd,MAAM,SACT,MAAM,0BAed,CAAC"}
|
package/dist/index.cjs.js
CHANGED
|
@@ -364,8 +364,8 @@ exports.AmityLiveViewerCountMode = void 0;
|
|
|
364
364
|
|
|
365
365
|
function getVersion() {
|
|
366
366
|
try {
|
|
367
|
-
// the string ''v7.
|
|
368
|
-
return 'v7.
|
|
367
|
+
// the string ''v7.33.0-cjs'' should be replaced by actual value by @rollup/plugin-replace
|
|
368
|
+
return 'v7.33.0-cjs';
|
|
369
369
|
}
|
|
370
370
|
catch (error) {
|
|
371
371
|
return '__dev__';
|
|
@@ -1187,6 +1187,116 @@ const wipeCache = async (storageKey = 'amitySdk') => {
|
|
|
1187
1187
|
return true;
|
|
1188
1188
|
};
|
|
1189
1189
|
|
|
1190
|
+
// Indexes never enter persisted cache data. Replacing data (restore/wipe/enable) starts a new index.
|
|
1191
|
+
const indexes = new WeakMap();
|
|
1192
|
+
const prefixesFor = (key) => {
|
|
1193
|
+
if (typeof key[0] !== 'string')
|
|
1194
|
+
return [];
|
|
1195
|
+
const prefixes = [JSON.stringify([key[0]])];
|
|
1196
|
+
if (typeof key[1] === 'string')
|
|
1197
|
+
prefixes.push(JSON.stringify([key[0], key[1]]));
|
|
1198
|
+
return prefixes;
|
|
1199
|
+
};
|
|
1200
|
+
const removePropertyValue = (property, indexed) => {
|
|
1201
|
+
const previous = property.previousValues.get(indexed.encodedKey);
|
|
1202
|
+
if (previous === undefined)
|
|
1203
|
+
return;
|
|
1204
|
+
const matches = property.values.get(previous);
|
|
1205
|
+
matches.delete(indexed);
|
|
1206
|
+
if (!matches.size)
|
|
1207
|
+
property.values.delete(previous);
|
|
1208
|
+
property.previousValues.delete(indexed.encodedKey);
|
|
1209
|
+
};
|
|
1210
|
+
const addPropertyValue = (data, property, indexed) => {
|
|
1211
|
+
var _a;
|
|
1212
|
+
if (!partialMatch(property.prefix, indexed.key))
|
|
1213
|
+
return;
|
|
1214
|
+
const value = (_a = data[indexed.encodedKey].data) === null || _a === void 0 ? void 0 : _a[property.property];
|
|
1215
|
+
if (typeof value !== 'string')
|
|
1216
|
+
return;
|
|
1217
|
+
let matches = property.values.get(value);
|
|
1218
|
+
if (!matches) {
|
|
1219
|
+
matches = new Set();
|
|
1220
|
+
property.values.set(value, matches);
|
|
1221
|
+
}
|
|
1222
|
+
matches.add(indexed);
|
|
1223
|
+
property.previousValues.set(indexed.encodedKey, value);
|
|
1224
|
+
};
|
|
1225
|
+
const addKey = (index, encodedKey) => {
|
|
1226
|
+
const indexed = { encodedKey, key: decodeKey(encodedKey), order: index.nextOrder };
|
|
1227
|
+
index.nextOrder += 1; // eslint-disable-line no-param-reassign
|
|
1228
|
+
index.keys.set(encodedKey, indexed);
|
|
1229
|
+
prefixesFor(indexed.key).forEach(prefix => {
|
|
1230
|
+
let keys = index.prefixes.get(prefix);
|
|
1231
|
+
if (!keys) {
|
|
1232
|
+
keys = new Map();
|
|
1233
|
+
index.prefixes.set(prefix, keys);
|
|
1234
|
+
}
|
|
1235
|
+
keys.set(encodedKey, indexed);
|
|
1236
|
+
});
|
|
1237
|
+
return indexed;
|
|
1238
|
+
};
|
|
1239
|
+
const getIndex = (data) => {
|
|
1240
|
+
let index = indexes.get(data);
|
|
1241
|
+
if (!index) {
|
|
1242
|
+
index = { keys: new Map(), prefixes: new Map(), properties: new Map(), nextOrder: 0 };
|
|
1243
|
+
Object.keys(data).forEach(encodedKey => addKey(index, encodedKey));
|
|
1244
|
+
indexes.set(data, index);
|
|
1245
|
+
}
|
|
1246
|
+
return index;
|
|
1247
|
+
};
|
|
1248
|
+
const candidatesFor = (index, key) => {
|
|
1249
|
+
var _a, _b;
|
|
1250
|
+
const prefixes = prefixesFor(key);
|
|
1251
|
+
if (!prefixes.length)
|
|
1252
|
+
return index.keys.values();
|
|
1253
|
+
return (_b = (_a = index.prefixes.get(prefixes[prefixes.length - 1])) === null || _a === void 0 ? void 0 : _a.values()) !== null && _b !== void 0 ? _b : [];
|
|
1254
|
+
};
|
|
1255
|
+
/** @hidden */
|
|
1256
|
+
const getMatchingCacheKeys = (data, key) => Array.from(candidatesFor(getIndex(data), key)).filter(indexed => partialMatch(key, indexed.key));
|
|
1257
|
+
/** Update only an index that has already been used; writes alone need no index build. @hidden */
|
|
1258
|
+
const updateCacheIndex = (data, encodedKey) => {
|
|
1259
|
+
var _a;
|
|
1260
|
+
const index = indexes.get(data);
|
|
1261
|
+
if (!index)
|
|
1262
|
+
return;
|
|
1263
|
+
const indexed = (_a = index.keys.get(encodedKey)) !== null && _a !== void 0 ? _a : addKey(index, encodedKey);
|
|
1264
|
+
index.properties.forEach(property => {
|
|
1265
|
+
removePropertyValue(property, indexed);
|
|
1266
|
+
addPropertyValue(data, property, indexed);
|
|
1267
|
+
});
|
|
1268
|
+
};
|
|
1269
|
+
/** @hidden */
|
|
1270
|
+
const removeFromCacheIndex = (data, encodedKey) => {
|
|
1271
|
+
const index = indexes.get(data);
|
|
1272
|
+
const indexed = index === null || index === void 0 ? void 0 : index.keys.get(encodedKey);
|
|
1273
|
+
if (!index || !indexed)
|
|
1274
|
+
return;
|
|
1275
|
+
index.properties.forEach(property => removePropertyValue(property, indexed));
|
|
1276
|
+
prefixesFor(indexed.key).forEach(prefix => {
|
|
1277
|
+
const keys = index.prefixes.get(prefix);
|
|
1278
|
+
keys.delete(encodedKey);
|
|
1279
|
+
if (!keys.size)
|
|
1280
|
+
index.prefixes.delete(prefix);
|
|
1281
|
+
});
|
|
1282
|
+
index.keys.delete(encodedKey);
|
|
1283
|
+
};
|
|
1284
|
+
/** Index a string model property within one domain/operation, retaining cache insertion order. @hidden */
|
|
1285
|
+
const getCacheEntriesByProperty = (data, prefix, property, value) => {
|
|
1286
|
+
var _a;
|
|
1287
|
+
const index = getIndex(data);
|
|
1288
|
+
const identity = JSON.stringify([prefix, property]);
|
|
1289
|
+
let propertyIndex = index.properties.get(identity);
|
|
1290
|
+
if (!propertyIndex) {
|
|
1291
|
+
propertyIndex = { prefix, property, values: new Map(), previousValues: new Map() };
|
|
1292
|
+
index.properties.set(identity, propertyIndex);
|
|
1293
|
+
Array.from(candidatesFor(index, prefix)).forEach(indexed => addPropertyValue(data, propertyIndex, indexed));
|
|
1294
|
+
}
|
|
1295
|
+
return Array.from((_a = propertyIndex.values.get(value)) !== null && _a !== void 0 ? _a : [])
|
|
1296
|
+
.sort((a, b) => a.order - b.order)
|
|
1297
|
+
.map(({ encodedKey }) => data[encodedKey]);
|
|
1298
|
+
};
|
|
1299
|
+
|
|
1190
1300
|
/**
|
|
1191
1301
|
* ```js
|
|
1192
1302
|
* import { queryCache } from '@amityco/ts-sdk'
|
|
@@ -1207,12 +1317,7 @@ const queryCache = (key) => {
|
|
|
1207
1317
|
if (!cache)
|
|
1208
1318
|
return;
|
|
1209
1319
|
log('cache/api/queryCache', { key });
|
|
1210
|
-
return
|
|
1211
|
-
.filter(stringKey => {
|
|
1212
|
-
const decodedKey = decodeKey(stringKey);
|
|
1213
|
-
return partialMatch(key, decodedKey);
|
|
1214
|
-
})
|
|
1215
|
-
.map(stringKey => cache.data[stringKey]);
|
|
1320
|
+
return getMatchingCacheKeys(cache.data, key).map(({ encodedKey }) => cache.data[encodedKey]);
|
|
1216
1321
|
};
|
|
1217
1322
|
|
|
1218
1323
|
/**
|
|
@@ -1268,6 +1373,7 @@ const pushToCache = (key, data, options = { cachedAt: Date.now() }) => {
|
|
|
1268
1373
|
}
|
|
1269
1374
|
const encodedKey = encodeKey(key);
|
|
1270
1375
|
cache.data[encodedKey] = Object.assign({ key, data }, options);
|
|
1376
|
+
updateCacheIndex(cache.data, encodedKey);
|
|
1271
1377
|
return true;
|
|
1272
1378
|
};
|
|
1273
1379
|
|
|
@@ -1352,15 +1458,14 @@ const dropFromCache = (key, exact = false) => {
|
|
|
1352
1458
|
return false;
|
|
1353
1459
|
log('cache/api/dropFromCache', { key, exact });
|
|
1354
1460
|
if (!exact) {
|
|
1355
|
-
return
|
|
1356
|
-
.map(
|
|
1357
|
-
.filter(candidate => partialMatch(key, candidate))
|
|
1358
|
-
.map(filteredKey => dropFromCache(filteredKey, true))
|
|
1461
|
+
return getMatchingCacheKeys(cache.data, key)
|
|
1462
|
+
.map(({ key: filteredKey }) => dropFromCache(filteredKey, true))
|
|
1359
1463
|
.every(returned => returned);
|
|
1360
1464
|
}
|
|
1361
1465
|
const encodedKey = encodeKey(key);
|
|
1362
1466
|
if (encodedKey in cache.data) {
|
|
1363
1467
|
delete cache.data[encodedKey];
|
|
1468
|
+
removeFromCacheIndex(cache.data, encodedKey);
|
|
1364
1469
|
return true;
|
|
1365
1470
|
}
|
|
1366
1471
|
return false;
|
|
@@ -3749,13 +3854,20 @@ const getUserMarker = async () => {
|
|
|
3749
3854
|
return { data: latestUserMarker, cachedAt };
|
|
3750
3855
|
};
|
|
3751
3856
|
|
|
3857
|
+
/** Internal model lookup; deliberately excluded from the public Cache API. @hidden */
|
|
3858
|
+
const queryCacheByProperty = (prefix, property, value) => {
|
|
3859
|
+
const { cache } = getActiveClient();
|
|
3860
|
+
if (!cache)
|
|
3861
|
+
return;
|
|
3862
|
+
return getCacheEntriesByProperty(cache.data, prefix, property, value);
|
|
3863
|
+
};
|
|
3864
|
+
|
|
3752
3865
|
const getMessageFromMainDB = (messageId) => {
|
|
3753
|
-
var _a, _b;
|
|
3866
|
+
var _a, _b, _c;
|
|
3754
3867
|
const message = (_a = pullFromCache(['message', 'get', messageId])) === null || _a === void 0 ? void 0 : _a.data;
|
|
3755
3868
|
if (message)
|
|
3756
3869
|
return message;
|
|
3757
|
-
|
|
3758
|
-
return (_b = messages === null || messages === void 0 ? void 0 : messages.find(({ data }) => data.messageId === messageId)) === null || _b === void 0 ? void 0 : _b.data;
|
|
3870
|
+
return (_c = (_b = queryCacheByProperty(['message', 'get'], 'messageId', messageId)) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.data;
|
|
3759
3871
|
};
|
|
3760
3872
|
|
|
3761
3873
|
/** @hidden */
|
|
@@ -3897,7 +4009,8 @@ function convertFromRaw$2(_a) {
|
|
|
3897
4009
|
|
|
3898
4010
|
const mergePayloadWithLocal = (payload) => {
|
|
3899
4011
|
var _a, _b, _c;
|
|
3900
|
-
|
|
4012
|
+
// Cache keys may be local referenceIds even after a server messageId is assigned.
|
|
4013
|
+
const localMessage = (_b = (_a = queryCacheByProperty(['message', 'get'], 'messageId', payload.messageId)) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.data;
|
|
3901
4014
|
if (localMessage) {
|
|
3902
4015
|
return Object.assign(Object.assign(Object.assign({}, localMessage), payload), {
|
|
3903
4016
|
// NOTE: referenceId is missing in the some payload event. If we have local message data with referenceId, use it instead.
|
|
@@ -5464,9 +5577,9 @@ const channelLinkedObject = (channel) => {
|
|
|
5464
5577
|
var _a;
|
|
5465
5578
|
let previewMembers = [];
|
|
5466
5579
|
if (['conversation', 'community'].includes(channel.type)) {
|
|
5467
|
-
const channelUsers =
|
|
5580
|
+
const channelUsers = queryCacheByProperty(['channelUsers', 'get'], 'channelId', channel.channelId);
|
|
5468
5581
|
if (channelUsers && (channelUsers === null || channelUsers === void 0 ? void 0 : channelUsers.length) > 0) {
|
|
5469
|
-
previewMembers = ((_a = channelUsers === null || channelUsers === void 0 ? void 0 : channelUsers.filter(({ data }) => data.
|
|
5582
|
+
previewMembers = ((_a = channelUsers === null || channelUsers === void 0 ? void 0 : channelUsers.filter(({ data }) => data.membership !== 'none')) !== null && _a !== void 0 ? _a : [])
|
|
5470
5583
|
// sort in ascending order by userInternalId
|
|
5471
5584
|
.sort((a, b) => a.data.userInternalId.localeCompare(b.data.userInternalId))
|
|
5472
5585
|
// Select only first 4 members
|
package/dist/index.esm.js
CHANGED
|
@@ -348,8 +348,8 @@ var AmityLiveViewerCountMode;
|
|
|
348
348
|
|
|
349
349
|
function getVersion() {
|
|
350
350
|
try {
|
|
351
|
-
// the string ''v7.
|
|
352
|
-
return 'v7.
|
|
351
|
+
// the string ''v7.33.0-esm'' should be replaced by actual value by @rollup/plugin-replace
|
|
352
|
+
return 'v7.33.0-esm';
|
|
353
353
|
}
|
|
354
354
|
catch (error) {
|
|
355
355
|
return '__dev__';
|
|
@@ -1171,6 +1171,116 @@ const wipeCache = async (storageKey = 'amitySdk') => {
|
|
|
1171
1171
|
return true;
|
|
1172
1172
|
};
|
|
1173
1173
|
|
|
1174
|
+
// Indexes never enter persisted cache data. Replacing data (restore/wipe/enable) starts a new index.
|
|
1175
|
+
const indexes = new WeakMap();
|
|
1176
|
+
const prefixesFor = (key) => {
|
|
1177
|
+
if (typeof key[0] !== 'string')
|
|
1178
|
+
return [];
|
|
1179
|
+
const prefixes = [JSON.stringify([key[0]])];
|
|
1180
|
+
if (typeof key[1] === 'string')
|
|
1181
|
+
prefixes.push(JSON.stringify([key[0], key[1]]));
|
|
1182
|
+
return prefixes;
|
|
1183
|
+
};
|
|
1184
|
+
const removePropertyValue = (property, indexed) => {
|
|
1185
|
+
const previous = property.previousValues.get(indexed.encodedKey);
|
|
1186
|
+
if (previous === undefined)
|
|
1187
|
+
return;
|
|
1188
|
+
const matches = property.values.get(previous);
|
|
1189
|
+
matches.delete(indexed);
|
|
1190
|
+
if (!matches.size)
|
|
1191
|
+
property.values.delete(previous);
|
|
1192
|
+
property.previousValues.delete(indexed.encodedKey);
|
|
1193
|
+
};
|
|
1194
|
+
const addPropertyValue = (data, property, indexed) => {
|
|
1195
|
+
var _a;
|
|
1196
|
+
if (!partialMatch(property.prefix, indexed.key))
|
|
1197
|
+
return;
|
|
1198
|
+
const value = (_a = data[indexed.encodedKey].data) === null || _a === void 0 ? void 0 : _a[property.property];
|
|
1199
|
+
if (typeof value !== 'string')
|
|
1200
|
+
return;
|
|
1201
|
+
let matches = property.values.get(value);
|
|
1202
|
+
if (!matches) {
|
|
1203
|
+
matches = new Set();
|
|
1204
|
+
property.values.set(value, matches);
|
|
1205
|
+
}
|
|
1206
|
+
matches.add(indexed);
|
|
1207
|
+
property.previousValues.set(indexed.encodedKey, value);
|
|
1208
|
+
};
|
|
1209
|
+
const addKey = (index, encodedKey) => {
|
|
1210
|
+
const indexed = { encodedKey, key: decodeKey(encodedKey), order: index.nextOrder };
|
|
1211
|
+
index.nextOrder += 1; // eslint-disable-line no-param-reassign
|
|
1212
|
+
index.keys.set(encodedKey, indexed);
|
|
1213
|
+
prefixesFor(indexed.key).forEach(prefix => {
|
|
1214
|
+
let keys = index.prefixes.get(prefix);
|
|
1215
|
+
if (!keys) {
|
|
1216
|
+
keys = new Map();
|
|
1217
|
+
index.prefixes.set(prefix, keys);
|
|
1218
|
+
}
|
|
1219
|
+
keys.set(encodedKey, indexed);
|
|
1220
|
+
});
|
|
1221
|
+
return indexed;
|
|
1222
|
+
};
|
|
1223
|
+
const getIndex = (data) => {
|
|
1224
|
+
let index = indexes.get(data);
|
|
1225
|
+
if (!index) {
|
|
1226
|
+
index = { keys: new Map(), prefixes: new Map(), properties: new Map(), nextOrder: 0 };
|
|
1227
|
+
Object.keys(data).forEach(encodedKey => addKey(index, encodedKey));
|
|
1228
|
+
indexes.set(data, index);
|
|
1229
|
+
}
|
|
1230
|
+
return index;
|
|
1231
|
+
};
|
|
1232
|
+
const candidatesFor = (index, key) => {
|
|
1233
|
+
var _a, _b;
|
|
1234
|
+
const prefixes = prefixesFor(key);
|
|
1235
|
+
if (!prefixes.length)
|
|
1236
|
+
return index.keys.values();
|
|
1237
|
+
return (_b = (_a = index.prefixes.get(prefixes[prefixes.length - 1])) === null || _a === void 0 ? void 0 : _a.values()) !== null && _b !== void 0 ? _b : [];
|
|
1238
|
+
};
|
|
1239
|
+
/** @hidden */
|
|
1240
|
+
const getMatchingCacheKeys = (data, key) => Array.from(candidatesFor(getIndex(data), key)).filter(indexed => partialMatch(key, indexed.key));
|
|
1241
|
+
/** Update only an index that has already been used; writes alone need no index build. @hidden */
|
|
1242
|
+
const updateCacheIndex = (data, encodedKey) => {
|
|
1243
|
+
var _a;
|
|
1244
|
+
const index = indexes.get(data);
|
|
1245
|
+
if (!index)
|
|
1246
|
+
return;
|
|
1247
|
+
const indexed = (_a = index.keys.get(encodedKey)) !== null && _a !== void 0 ? _a : addKey(index, encodedKey);
|
|
1248
|
+
index.properties.forEach(property => {
|
|
1249
|
+
removePropertyValue(property, indexed);
|
|
1250
|
+
addPropertyValue(data, property, indexed);
|
|
1251
|
+
});
|
|
1252
|
+
};
|
|
1253
|
+
/** @hidden */
|
|
1254
|
+
const removeFromCacheIndex = (data, encodedKey) => {
|
|
1255
|
+
const index = indexes.get(data);
|
|
1256
|
+
const indexed = index === null || index === void 0 ? void 0 : index.keys.get(encodedKey);
|
|
1257
|
+
if (!index || !indexed)
|
|
1258
|
+
return;
|
|
1259
|
+
index.properties.forEach(property => removePropertyValue(property, indexed));
|
|
1260
|
+
prefixesFor(indexed.key).forEach(prefix => {
|
|
1261
|
+
const keys = index.prefixes.get(prefix);
|
|
1262
|
+
keys.delete(encodedKey);
|
|
1263
|
+
if (!keys.size)
|
|
1264
|
+
index.prefixes.delete(prefix);
|
|
1265
|
+
});
|
|
1266
|
+
index.keys.delete(encodedKey);
|
|
1267
|
+
};
|
|
1268
|
+
/** Index a string model property within one domain/operation, retaining cache insertion order. @hidden */
|
|
1269
|
+
const getCacheEntriesByProperty = (data, prefix, property, value) => {
|
|
1270
|
+
var _a;
|
|
1271
|
+
const index = getIndex(data);
|
|
1272
|
+
const identity = JSON.stringify([prefix, property]);
|
|
1273
|
+
let propertyIndex = index.properties.get(identity);
|
|
1274
|
+
if (!propertyIndex) {
|
|
1275
|
+
propertyIndex = { prefix, property, values: new Map(), previousValues: new Map() };
|
|
1276
|
+
index.properties.set(identity, propertyIndex);
|
|
1277
|
+
Array.from(candidatesFor(index, prefix)).forEach(indexed => addPropertyValue(data, propertyIndex, indexed));
|
|
1278
|
+
}
|
|
1279
|
+
return Array.from((_a = propertyIndex.values.get(value)) !== null && _a !== void 0 ? _a : [])
|
|
1280
|
+
.sort((a, b) => a.order - b.order)
|
|
1281
|
+
.map(({ encodedKey }) => data[encodedKey]);
|
|
1282
|
+
};
|
|
1283
|
+
|
|
1174
1284
|
/**
|
|
1175
1285
|
* ```js
|
|
1176
1286
|
* import { queryCache } from '@amityco/ts-sdk'
|
|
@@ -1191,12 +1301,7 @@ const queryCache = (key) => {
|
|
|
1191
1301
|
if (!cache)
|
|
1192
1302
|
return;
|
|
1193
1303
|
log('cache/api/queryCache', { key });
|
|
1194
|
-
return
|
|
1195
|
-
.filter(stringKey => {
|
|
1196
|
-
const decodedKey = decodeKey(stringKey);
|
|
1197
|
-
return partialMatch(key, decodedKey);
|
|
1198
|
-
})
|
|
1199
|
-
.map(stringKey => cache.data[stringKey]);
|
|
1304
|
+
return getMatchingCacheKeys(cache.data, key).map(({ encodedKey }) => cache.data[encodedKey]);
|
|
1200
1305
|
};
|
|
1201
1306
|
|
|
1202
1307
|
/**
|
|
@@ -1252,6 +1357,7 @@ const pushToCache = (key, data, options = { cachedAt: Date.now() }) => {
|
|
|
1252
1357
|
}
|
|
1253
1358
|
const encodedKey = encodeKey(key);
|
|
1254
1359
|
cache.data[encodedKey] = Object.assign({ key, data }, options);
|
|
1360
|
+
updateCacheIndex(cache.data, encodedKey);
|
|
1255
1361
|
return true;
|
|
1256
1362
|
};
|
|
1257
1363
|
|
|
@@ -1336,15 +1442,14 @@ const dropFromCache = (key, exact = false) => {
|
|
|
1336
1442
|
return false;
|
|
1337
1443
|
log('cache/api/dropFromCache', { key, exact });
|
|
1338
1444
|
if (!exact) {
|
|
1339
|
-
return
|
|
1340
|
-
.map(
|
|
1341
|
-
.filter(candidate => partialMatch(key, candidate))
|
|
1342
|
-
.map(filteredKey => dropFromCache(filteredKey, true))
|
|
1445
|
+
return getMatchingCacheKeys(cache.data, key)
|
|
1446
|
+
.map(({ key: filteredKey }) => dropFromCache(filteredKey, true))
|
|
1343
1447
|
.every(returned => returned);
|
|
1344
1448
|
}
|
|
1345
1449
|
const encodedKey = encodeKey(key);
|
|
1346
1450
|
if (encodedKey in cache.data) {
|
|
1347
1451
|
delete cache.data[encodedKey];
|
|
1452
|
+
removeFromCacheIndex(cache.data, encodedKey);
|
|
1348
1453
|
return true;
|
|
1349
1454
|
}
|
|
1350
1455
|
return false;
|
|
@@ -3749,13 +3854,20 @@ const getUserMarker = async () => {
|
|
|
3749
3854
|
return { data: latestUserMarker, cachedAt };
|
|
3750
3855
|
};
|
|
3751
3856
|
|
|
3857
|
+
/** Internal model lookup; deliberately excluded from the public Cache API. @hidden */
|
|
3858
|
+
const queryCacheByProperty = (prefix, property, value) => {
|
|
3859
|
+
const { cache } = getActiveClient();
|
|
3860
|
+
if (!cache)
|
|
3861
|
+
return;
|
|
3862
|
+
return getCacheEntriesByProperty(cache.data, prefix, property, value);
|
|
3863
|
+
};
|
|
3864
|
+
|
|
3752
3865
|
const getMessageFromMainDB = (messageId) => {
|
|
3753
|
-
var _a, _b;
|
|
3866
|
+
var _a, _b, _c;
|
|
3754
3867
|
const message = (_a = pullFromCache(['message', 'get', messageId])) === null || _a === void 0 ? void 0 : _a.data;
|
|
3755
3868
|
if (message)
|
|
3756
3869
|
return message;
|
|
3757
|
-
|
|
3758
|
-
return (_b = messages === null || messages === void 0 ? void 0 : messages.find(({ data }) => data.messageId === messageId)) === null || _b === void 0 ? void 0 : _b.data;
|
|
3870
|
+
return (_c = (_b = queryCacheByProperty(['message', 'get'], 'messageId', messageId)) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.data;
|
|
3759
3871
|
};
|
|
3760
3872
|
|
|
3761
3873
|
/** @hidden */
|
|
@@ -3897,7 +4009,8 @@ function convertFromRaw$2(_a) {
|
|
|
3897
4009
|
|
|
3898
4010
|
const mergePayloadWithLocal = (payload) => {
|
|
3899
4011
|
var _a, _b, _c;
|
|
3900
|
-
|
|
4012
|
+
// Cache keys may be local referenceIds even after a server messageId is assigned.
|
|
4013
|
+
const localMessage = (_b = (_a = queryCacheByProperty(['message', 'get'], 'messageId', payload.messageId)) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.data;
|
|
3901
4014
|
if (localMessage) {
|
|
3902
4015
|
return Object.assign(Object.assign(Object.assign({}, localMessage), payload), {
|
|
3903
4016
|
// NOTE: referenceId is missing in the some payload event. If we have local message data with referenceId, use it instead.
|
|
@@ -5464,9 +5577,9 @@ const channelLinkedObject = (channel) => {
|
|
|
5464
5577
|
var _a;
|
|
5465
5578
|
let previewMembers = [];
|
|
5466
5579
|
if (['conversation', 'community'].includes(channel.type)) {
|
|
5467
|
-
const channelUsers =
|
|
5580
|
+
const channelUsers = queryCacheByProperty(['channelUsers', 'get'], 'channelId', channel.channelId);
|
|
5468
5581
|
if (channelUsers && (channelUsers === null || channelUsers === void 0 ? void 0 : channelUsers.length) > 0) {
|
|
5469
|
-
previewMembers = ((_a = channelUsers === null || channelUsers === void 0 ? void 0 : channelUsers.filter(({ data }) => data.
|
|
5582
|
+
previewMembers = ((_a = channelUsers === null || channelUsers === void 0 ? void 0 : channelUsers.filter(({ data }) => data.membership !== 'none')) !== null && _a !== void 0 ? _a : [])
|
|
5470
5583
|
// sort in ascending order by userInternalId
|
|
5471
5584
|
.sort((a, b) => a.data.userInternalId.localeCompare(b.data.userInternalId))
|
|
5472
5585
|
// Select only first 4 members
|