@amityco/ts-sdk-react-native 6.30.5-d50bb81.0 → 6.31.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/.env +26 -26
- package/dist/@types/domains/client.d.ts +1 -0
- package/dist/@types/domains/client.d.ts.map +1 -1
- package/dist/client/api/createClient.d.ts.map +1 -1
- package/dist/core/model/index.d.ts +1 -0
- package/dist/core/model/index.d.ts.map +1 -1
- package/dist/core/model/localReferenceId.d.ts +2 -0
- package/dist/core/model/localReferenceId.d.ts.map +1 -0
- package/dist/index.cjs.js +123 -28
- package/dist/index.esm.js +123 -28
- package/dist/index.umd.js +3 -3
- package/dist/messageRepository/api/createMessage.d.ts.map +1 -1
- package/dist/messageRepository/observers/getMessages/MessageLiveCollectionController.d.ts.map +1 -1
- package/dist/messageRepository/observers/getMessages/MessageQueryStreamController.d.ts.map +1 -1
- package/dist/messageRepository/utils/getMessageFromMainDB.d.ts.map +1 -1
- package/dist/messageRepository/utils/getMessageIdentifierIds.d.ts +2 -0
- package/dist/messageRepository/utils/getMessageIdentifierIds.d.ts.map +1 -0
- package/dist/messageRepository/utils/index.d.ts +1 -0
- package/dist/messageRepository/utils/index.d.ts.map +1 -1
- package/dist/messageRepository/utils/prepareMessagePayload.d.ts.map +1 -1
- package/dist/utils/event.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/@types/domains/client.ts +2 -0
- package/src/client/api/createClient.ts +26 -0
- package/src/core/model/index.ts +1 -0
- package/src/core/model/localReferenceId.ts +12 -0
- package/src/messageRepository/api/createMessage.ts +9 -0
- package/src/messageRepository/observers/getMessages/MessageLiveCollectionController.ts +1 -0
- package/src/messageRepository/observers/getMessages/MessageQueryStreamController.ts +4 -4
- package/src/messageRepository/utils/getMessageFromMainDB.ts +9 -6
- package/src/messageRepository/utils/getMessageIdentifierIds.ts +37 -0
- package/src/messageRepository/utils/index.ts +1 -0
- package/src/messageRepository/utils/prepareMessagePayload.ts +22 -18
- package/src/utils/event.ts +20 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createMessage.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/api/createMessage.ts"],"names":[],"mappings":"AAmBA,aAAK,kBAAkB,CAAC,CAAC,SAAS,KAAK,CAAC,kBAAkB,IAAI,IAAI,CAChE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAChB,cAAc,GAAG,UAAU,GAAG,UAAU,GAAG,MAAM,GAAG,UAAU,GAAG,YAAY,CAC9E,GAAG;IACF,IAAI,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IAChC,MAAM,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;
|
|
1
|
+
{"version":3,"file":"createMessage.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/api/createMessage.ts"],"names":[],"mappings":"AAmBA,aAAK,kBAAkB,CAAC,CAAC,SAAS,KAAK,CAAC,kBAAkB,IAAI,IAAI,CAChE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAChB,cAAc,GAAG,UAAU,GAAG,UAAU,GAAG,MAAM,GAAG,UAAU,GAAG,YAAY,CAC9E,GAAG;IACF,IAAI,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IAChC,MAAM,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAoEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,aAAa,yEAEvB,QAAQ,MAAM,MAAM,CAAC,MAAM,OAAO,CAAC,CA2CrC,CAAC"}
|
package/dist/messageRepository/observers/getMessages/MessageLiveCollectionController.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MessageLiveCollectionController.d.ts","sourceRoot":"","sources":["../../../../src/messageRepository/observers/getMessages/MessageLiveCollectionController.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gDAAgD,CAAC;AAM1F,OAAO,EAAE,2BAA2B,EAAE,MAAM,+BAA+B,CAAC;AAG5E,qBAAa,+BAAgC,SAAQ,wBAAwB,CAC3E,SAAS,EACT,KAAK,CAAC,sBAAsB,EAC5B,KAAK,CAAC,OAAO,EACb,2BAA2B,CAC5B;IACC,OAAO,CAAC,qBAAqB,CAA+B;IAE5D,OAAO,CAAC,KAAK,CAA+B;gBAG1C,KAAK,EAAE,KAAK,CAAC,sBAAsB,EACnC,QAAQ,EAAE,KAAK,CAAC,sBAAsB,CAAC,KAAK,CAAC,OAAO,CAAC;IAqBvD,iBAAiB;IAmBjB,YAAY,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,KAAK,CAAC,0BAA0B;
|
|
1
|
+
{"version":3,"file":"MessageLiveCollectionController.d.ts","sourceRoot":"","sources":["../../../../src/messageRepository/observers/getMessages/MessageLiveCollectionController.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gDAAgD,CAAC;AAM1F,OAAO,EAAE,2BAA2B,EAAE,MAAM,+BAA+B,CAAC;AAG5E,qBAAa,+BAAgC,SAAQ,wBAAwB,CAC3E,SAAS,EACT,KAAK,CAAC,sBAAsB,EAC5B,KAAK,CAAC,OAAO,EACb,2BAA2B,CAC5B;IACC,OAAO,CAAC,qBAAqB,CAA+B;IAE5D,OAAO,CAAC,KAAK,CAA+B;gBAG1C,KAAK,EAAE,KAAK,CAAC,sBAAsB,EACnC,QAAQ,EAAE,KAAK,CAAC,sBAAsB,CAAC,KAAK,CAAC,OAAO,CAAC;IAqBvD,iBAAiB;IAmBjB,YAAY,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,KAAK,CAAC,0BAA0B;IAyBzE,WAAW,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,EAAE;IA4CjC,SAAS,CAAC,KAAK;cAUC,YAAY,CAAC,QAAQ,EAAE,KAAK,CAAC,cAAc,GAAG,KAAK,CAAC,UAAU;IAI9E,SAAS,CAAC,kBAAkB,CAAC,EAC3B,QAAQ,EACR,SAAS,EACT,OAAO,GACR,EAAE,KAAK,CAAC,sCAAsC,CAAC,SAAS,CAAC;CAG3D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MessageQueryStreamController.d.ts","sourceRoot":"","sources":["../../../../src/messageRepository/observers/getMessages/MessageQueryStreamController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,6CAA6C,CAAC;
|
|
1
|
+
{"version":3,"file":"MessageQueryStreamController.d.ts","sourceRoot":"","sources":["../../../../src/messageRepository/observers/getMessages/MessageQueryStreamController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,6CAA6C,CAAC;AAIpF,OAAO,EAAE,2BAA2B,EAAE,MAAM,+BAA+B,CAAC;AAG5E,qBAAa,4BAA6B,SAAQ,qBAAqB,CACrE,KAAK,CAAC,cAAc,EACpB,KAAK,CAAC,sBAAsB,CAC7B;IACC,OAAO,CAAC,YAAY,CAAqD;IAEzE,OAAO,CAAC,cAAc,CAEsB;IAE5C,OAAO,CAAC,oBAAoB,CAA8B;gBAGxD,KAAK,EAAE,KAAK,CAAC,sBAAsB,EACnC,QAAQ,EAAE,MAAM,EAAE,EAClB,YAAY,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,0BAA0B,KAAK,IAAI,EAChE,cAAc,EAAE,CAAC,QAAQ,EAAE,KAAK,CAAC,cAAc,KAAK,OAAO,CAAC,KAAK,CAAC,uBAAuB,CAAC,EAC1F,oBAAoB,EAAE,2BAA2B;IAS7C,YAAY,CAAC,QAAQ,EAAE,KAAK,CAAC,cAAc;IAWjD,mBAAmB,CACjB,QAAQ,EAAE,KAAK,CAAC,cAAc,GAAG,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,EAC1D,SAAS,EAAE,KAAK,CAAC,2BAA2B,EAC5C,OAAO,UAAQ;IAsBjB,OAAO,CAAC,MAAM,EAAE,MAAM,aACH,MAAM,eAAe;IA6DxC,YAAY,CACV,gBAAgB,EAAE;QAChB,EAAE,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,eAAe,CAAC,KAAK,KAAK,CAAC,YAAY,CAAC;QAC3E,MAAM,EAAE,MAAM,CAAC;KAChB,EAAE;CAIN"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getMessageFromMainDB.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/getMessageFromMainDB.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"getMessageFromMainDB.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/getMessageFromMainDB.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,oBAAoB,cAAe,MAAM,KAAG,MAAM,eAAe,GAAG,SAShF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getMessageIdentifierIds.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/getMessageIdentifierIds.ts"],"names":[],"mappings":"AAoBA,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,KAAK,CAAC,UAAU,EAAE,YAgBnE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prepareMessagePayload.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/prepareMessagePayload.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"prepareMessagePayload.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/prepareMessagePayload.ts"],"names":[],"mappings":"AAiCA,wBAAgB,cAAc,CAC5B,OAAO,EAAE,KAAK,CAAC,UAAU,EACzB,QAAQ,CAAC,EAAE,KAAK,CAAC,eAAe,EAAE,EAClC,KAAK,CAAC,EAAE,MAAM,KAAK,CAAC,iBAAiB,GACpC,KAAK,CAAC,eAAe,CAqEvB;AAWD,eAAO,MAAM,qBAAqB,YACvB,MAAM,cAAc,UACrB,MAAM,MAAM,iBAAiB,KACpC,QAAQ,MAAM,uBAAuB,CA0CvC,CAAC;AAEF,aAAK,gBAAgB,GAAG,IAAI,CAC1B,KAAK,CAAC,aAAa,EAIjB,MAAM,GACN,QAAQ,GACR,cAAc,GACd,MAAM,GACN,gBAAgB,GAChB,iBAAiB,GACjB,UAAU,GACV,eAAe,GACf,eAAe,CAClB,GAAG;IACF,WAAW,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC;IACnD,WAAW,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC;IACnD,SAAS,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IACvC,aAAa,EAAE,KAAK,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC;IACnD,QAAQ,CAAC,EAAE,KAAK,CAAC,kBAAkB,CAAC;IACpC,OAAO,EAAE;QACP,MAAM,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QACvC,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;KACrC,CAAC;CACH,CAAC;AAEF,wBAAgB,aAAa,CAAC,EAC5B,YAAY,EACZ,UAAU,EACV,QAAQ,EACR,IAAI,EACJ,GAAG,IAAI,EACR,EAAE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAc9C;AAED,wBAAgB,kBAAkB,CAAC,EACjC,MAAM,EACN,YAAY,EACZ,IAAI,EACJ,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,KAAK,EACL,IAAI,EACJ,GAAG,IAAI,EACR,EAAE,KAAK,CAAC,sBAAsB,GAAG,gBAAgB,CA6BjD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../../src/utils/event.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../../src/utils/event.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,mBAAmB,qMAM6B,MAAM,YAAY,iJA+BzE,CAAC"}
|
package/package.json
CHANGED
|
@@ -77,6 +77,8 @@ export const createClient = (
|
|
|
77
77
|
|
|
78
78
|
const isUnreadCountEnabled = false;
|
|
79
79
|
|
|
80
|
+
const objectSyncMap = new Map<string, string>();
|
|
81
|
+
|
|
80
82
|
const client = {
|
|
81
83
|
version: `${VERSION}`,
|
|
82
84
|
apiKey,
|
|
@@ -114,6 +116,30 @@ export const createClient = (
|
|
|
114
116
|
isUnreadCountEnabled,
|
|
115
117
|
|
|
116
118
|
getMarkerSyncConsistentMode,
|
|
119
|
+
|
|
120
|
+
/*
|
|
121
|
+
* The objectSyncMap is used to keep the map between local referenceId and server id in two ways mapping.
|
|
122
|
+
*
|
|
123
|
+
* For objects created locally (optimistic creation), the SDK stores them with their local ID as the cache key, which is used throughout the entire SDK session (in-memory) for syncing updates.
|
|
124
|
+
* If the payload or response only contains the remote ID of the same object, the SDK will look up the corresponding local ID to retrieve the object through ObjectSyncMap.
|
|
125
|
+
*
|
|
126
|
+
* This is useful for syncing updates to the object, as the SDK can find the local ID from the server ID and update the object in the cache correctly.
|
|
127
|
+
*
|
|
128
|
+
* Example:
|
|
129
|
+
* In the case of message optimistically creation. The referenceId is created locally.
|
|
130
|
+
*
|
|
131
|
+
* 1. When creating a message optimistically, a local referenceId is created. The objectSyncMap will have the following structure.
|
|
132
|
+
* {
|
|
133
|
+
* "LOCAL_uuid": undefined,
|
|
134
|
+
* }
|
|
135
|
+
*
|
|
136
|
+
* 2. After the message was created on the server and SDK received the response from server, the objectSyncMap will be updated to
|
|
137
|
+
* {
|
|
138
|
+
* "LOCAL_uuid": "server_message_id",
|
|
139
|
+
* "server_message_id": "LOCAL_uuid"
|
|
140
|
+
* }
|
|
141
|
+
*/
|
|
142
|
+
objectSyncMap,
|
|
117
143
|
};
|
|
118
144
|
|
|
119
145
|
try {
|
package/src/core/model/index.ts
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* To check if an id is a local referenceId, the id must start with 'LOCAL_'.
|
|
3
|
+
*
|
|
4
|
+
* Example: LOCAL_155f5158-281a-4a9d-a445-9243138d2041
|
|
5
|
+
*
|
|
6
|
+
* @param id as string
|
|
7
|
+
* @returns true if the id is a local referenceId, false otherwise
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export const isLocalId = (id: string) => {
|
|
11
|
+
return id.startsWith('LOCAL_');
|
|
12
|
+
};
|
|
@@ -30,6 +30,7 @@ const createMessageOptimistic = <T extends Amity.MessageContentType>(
|
|
|
30
30
|
bundle: createMessageParam<T>,
|
|
31
31
|
): Amity.InternalMessage | undefined => {
|
|
32
32
|
const client = getActiveClient();
|
|
33
|
+
const { objectSyncMap } = client;
|
|
33
34
|
|
|
34
35
|
if (!client.cache) return;
|
|
35
36
|
|
|
@@ -84,6 +85,9 @@ const createMessageOptimistic = <T extends Amity.MessageContentType>(
|
|
|
84
85
|
const cachedAt = UNSYNCED_OBJECT_CACHED_AT_VALUE;
|
|
85
86
|
|
|
86
87
|
pushToCache(['message', 'get', message.messageId], message, { cachedAt });
|
|
88
|
+
|
|
89
|
+
objectSyncMap.set(message.messageId, undefined);
|
|
90
|
+
|
|
87
91
|
fireEvent('local.message.created', { messages: [message] });
|
|
88
92
|
|
|
89
93
|
return message;
|
|
@@ -114,6 +118,8 @@ export const createMessage = async <T extends Amity.MessageContentType>(
|
|
|
114
118
|
bundle: createMessageParam<T>,
|
|
115
119
|
): Promise<Amity.Cached<Amity.Message>> => {
|
|
116
120
|
const client = getActiveClient();
|
|
121
|
+
|
|
122
|
+
const { objectSyncMap } = client;
|
|
117
123
|
client.log('message/createMessage', bundle);
|
|
118
124
|
|
|
119
125
|
const optimisticData = createMessageOptimistic<T>(bundle);
|
|
@@ -127,6 +133,9 @@ export const createMessage = async <T extends Amity.MessageContentType>(
|
|
|
127
133
|
referenceId,
|
|
128
134
|
});
|
|
129
135
|
|
|
136
|
+
objectSyncMap.set(referenceId, payload.messages[0].messageId);
|
|
137
|
+
objectSyncMap.set(payload.messages[0].messageId, referenceId);
|
|
138
|
+
|
|
130
139
|
const data = await prepareMessagePayload(payload);
|
|
131
140
|
const { messages } = data;
|
|
132
141
|
|
|
@@ -75,6 +75,7 @@ export class MessageLiveCollectionController extends LiveCollectionController<
|
|
|
75
75
|
|
|
76
76
|
notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) {
|
|
77
77
|
const collection = pullFromCache<Amity.MessageLiveCollectionCache>(this.cacheKey)?.data;
|
|
78
|
+
|
|
78
79
|
if (!collection) return;
|
|
79
80
|
|
|
80
81
|
const data = this.applyFilter(
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
import { QueryStreamController } from '~/core/liveCollection/QueryStreamController';
|
|
3
3
|
import { pullFromCache, pushToCache } from '~/cache/api';
|
|
4
4
|
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
5
|
-
import { getResolver } from '~/core/model';
|
|
6
5
|
import { getActiveClient } from '~/client';
|
|
7
6
|
import { MessagePaginationController } from './MessagePaginationController';
|
|
7
|
+
import { getMessageIdentifierIds } from '~/messageRepository/utils';
|
|
8
8
|
|
|
9
9
|
export class MessageQueryStreamController extends QueryStreamController<
|
|
10
10
|
Amity.MessagePayload,
|
|
@@ -50,7 +50,7 @@ export class MessageQueryStreamController extends QueryStreamController<
|
|
|
50
50
|
) {
|
|
51
51
|
if (refresh) {
|
|
52
52
|
pushToCache(this.cacheKey, {
|
|
53
|
-
data: response.messages
|
|
53
|
+
data: getMessageIdentifierIds(response.messages),
|
|
54
54
|
query: this.query,
|
|
55
55
|
});
|
|
56
56
|
} else {
|
|
@@ -62,8 +62,8 @@ export class MessageQueryStreamController extends QueryStreamController<
|
|
|
62
62
|
...collection,
|
|
63
63
|
data:
|
|
64
64
|
direction === 'next'
|
|
65
|
-
? [...new Set([...messages, ...response.messages
|
|
66
|
-
: [...new Set([...response.messages
|
|
65
|
+
? [...new Set([...messages, ...getMessageIdentifierIds(response.messages)])]
|
|
66
|
+
: [...new Set([...getMessageIdentifierIds(response.messages), ...messages])],
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
69
|
}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { pullFromCache } from '~/cache/api/pullFromCache';
|
|
2
|
-
import {
|
|
2
|
+
import { getActiveClient } from '~/client/api/activeClient';
|
|
3
|
+
import { isLocalId } from '~/core/model';
|
|
3
4
|
|
|
4
5
|
export const getMessageFromMainDB = (messageId: string): Amity.InternalMessage | undefined => {
|
|
5
|
-
const
|
|
6
|
-
|
|
6
|
+
const client = getActiveClient();
|
|
7
|
+
const { objectSyncMap } = client;
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
const resolvedMessageId = isLocalId(messageId)
|
|
10
|
+
? messageId
|
|
11
|
+
: objectSyncMap.get(messageId) ?? messageId;
|
|
12
|
+
|
|
13
|
+
return pullFromCache<Amity.InternalMessage>(['message', 'get', resolvedMessageId])?.data;
|
|
11
14
|
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { getActiveClient } from '~/client/api/activeClient';
|
|
2
|
+
import { isLocalId } from '~/core/model';
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* To get message identifier ids used as a cache key for the each message.
|
|
6
|
+
* These ids are used to store in the query stream and use to get messages from cache.
|
|
7
|
+
*
|
|
8
|
+
* The query stream data keeps the message identifier ids as follows:
|
|
9
|
+
* ['server_message_id_1', 'server_message_id_2', 'LOCAL_message_id_1', 'LOCAL_message_id_2']
|
|
10
|
+
*
|
|
11
|
+
* Example use case: Message created locally and then created on the server.
|
|
12
|
+
*
|
|
13
|
+
* 1. Message created locally has `LOCAL_message_id_2` as referenceId.
|
|
14
|
+
* 2. The `message.created` event will be fired from server with message payload that has no the referenceId.
|
|
15
|
+
* 3. SDK use this function to find indentifer id for each message in the payload. If the message is created locally, the identifier id will be the local reference id.
|
|
16
|
+
* 4. The SDK use this identifier ids to check if the message is already in the query stream before appending it.
|
|
17
|
+
*
|
|
18
|
+
* @param The raw message payload from server response
|
|
19
|
+
* @returns The identifier ids of the messages
|
|
20
|
+
*/
|
|
21
|
+
export function getMessageIdentifierIds(messages: Amity.RawMessage[]) {
|
|
22
|
+
const client = getActiveClient();
|
|
23
|
+
const { objectSyncMap } = client;
|
|
24
|
+
|
|
25
|
+
return messages
|
|
26
|
+
.map(message => {
|
|
27
|
+
/* NOTE: This logic is used to get identifier id for each message.
|
|
28
|
+
*
|
|
29
|
+
* if messageId is a local id, use it as identifier id else get identifier id from objectSyncMap.
|
|
30
|
+
* if find referenceId in objectSyncMap, this means this message is a local created message. The identifier id will be local message id.
|
|
31
|
+
* if cannot find identifier id in objectSyncMap, this means this message is not a local created message. The identifier id will be server message id.
|
|
32
|
+
*/
|
|
33
|
+
if (isLocalId(message.messageId)) return message.messageId;
|
|
34
|
+
return objectSyncMap.get(message.messageId) ?? message.messageId;
|
|
35
|
+
})
|
|
36
|
+
.filter(Boolean);
|
|
37
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { pullFromCache
|
|
1
|
+
import { pullFromCache } from '~/cache/api';
|
|
2
2
|
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
3
3
|
import { getMessageMarkers } from '~/marker/api/getMessageMarkers';
|
|
4
4
|
import { prepareMessagePayloadForCache } from '~/reactionRepository/utils/prepareMessagePayloadForCache';
|
|
@@ -8,23 +8,27 @@ import { MessageContentType } from '~/@types';
|
|
|
8
8
|
import { convertFromRaw as convertSubChannelFromRaw } from '~/subChannelRepository/utils/convertSubChannelFromRaw';
|
|
9
9
|
import { updateSubChannelCache } from '~/subChannelRepository/utils/updateSubChannelCache';
|
|
10
10
|
import { COLLECTION_DEFAULT_PAGINATION_LIMIT } from '~/utils/constants';
|
|
11
|
+
import { getActiveClient } from '~/client/api/activeClient';
|
|
12
|
+
import { isLocalId } from '~/core/model';
|
|
11
13
|
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
)?.data;
|
|
14
|
+
const addLocalReferenceId = (payload: Amity.RawMessage) => {
|
|
15
|
+
const client = getActiveClient();
|
|
16
|
+
const { objectSyncMap } = client;
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
return {
|
|
19
|
+
...payload,
|
|
20
|
+
/* NOTE: This logic is used to get local referenceId for each message.
|
|
21
|
+
*
|
|
22
|
+
* if messageId is a local reference id, use it as referenceId else get referenceId from objectSyncMap.
|
|
23
|
+
* if find referenceId in objectSyncMap, this means this message is a local created message. The referenceId will be local reference id.
|
|
24
|
+
* if cannot find referenceId in objectSyncMap, referenceId will be undefined.
|
|
25
|
+
*
|
|
26
|
+
* The referenceId is undefined means this message is not a local created message (optimistic creation message).
|
|
27
|
+
*/
|
|
28
|
+
referenceId: isLocalId(payload.messageId)
|
|
29
|
+
? payload.messageId
|
|
30
|
+
: objectSyncMap.get(payload.messageId),
|
|
31
|
+
};
|
|
28
32
|
};
|
|
29
33
|
|
|
30
34
|
export function convertFromRaw(
|
|
@@ -32,7 +36,7 @@ export function convertFromRaw(
|
|
|
32
36
|
reactors?: Amity.InternalReactor[],
|
|
33
37
|
event?: keyof Amity.MqttMessageEvents,
|
|
34
38
|
): Amity.InternalMessage {
|
|
35
|
-
const
|
|
39
|
+
const messageWithReferenceId = addLocalReferenceId(message);
|
|
36
40
|
|
|
37
41
|
const {
|
|
38
42
|
channelPublicId,
|
|
@@ -48,7 +52,7 @@ export function convertFromRaw(
|
|
|
48
52
|
messageId,
|
|
49
53
|
creatorId,
|
|
50
54
|
...rest
|
|
51
|
-
} =
|
|
55
|
+
} = messageWithReferenceId;
|
|
52
56
|
|
|
53
57
|
let cache: Amity.CacheEntry<Amity.Message> | undefined;
|
|
54
58
|
|
package/src/utils/event.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { pullFromCache } from '~/cache/api';
|
|
1
|
+
import { pullFromCache } from '~/cache/api/pullFromCache';
|
|
2
|
+
import { getActiveClient } from '~/client/api/activeClient';
|
|
3
|
+
import { isLocalId } from '~/core/model';
|
|
2
4
|
|
|
3
5
|
export const convertEventPayload =
|
|
4
6
|
<
|
|
@@ -16,8 +18,23 @@ export const convertEventPayload =
|
|
|
16
18
|
return sourceModel;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
+
const client = getActiveClient();
|
|
22
|
+
const { objectSyncMap } = client;
|
|
23
|
+
|
|
24
|
+
/*
|
|
25
|
+
* NOTE: For objects created locally (optimistic creation), the SDK stores them with their local ID as the cache key, which is used throughout the entire SDK session (in-memory) for syncing updates.
|
|
26
|
+
* If the payload or response only contains the remote ID of the same object, the SDK will look up the corresponding local ID to retrieve the object.
|
|
27
|
+
*/
|
|
28
|
+
const resolvedId = isLocalId(sourceModel[sourceModelProp] as unknown as string)
|
|
29
|
+
? sourceModel[sourceModelProp]
|
|
30
|
+
: objectSyncMap.get(sourceModel[sourceModelProp] as unknown as string) ??
|
|
31
|
+
sourceModel[sourceModelProp];
|
|
32
|
+
|
|
33
|
+
const model = pullFromCache<DestinationModel>([
|
|
34
|
+
destinationDomain,
|
|
35
|
+
'get',
|
|
36
|
+
`${resolvedId}`,
|
|
37
|
+
])?.data;
|
|
21
38
|
|
|
22
39
|
if (!model) return;
|
|
23
40
|
|