@veltdev/react 1.0.97 → 1.0.98

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.
@@ -4,5 +4,8 @@ declare type LiveStateMiddlewareConfig = {
4
4
  allowAction?: (action: any) => boolean;
5
5
  liveStateDataId?: string;
6
6
  };
7
- export declare const createLiveStateMiddleware: (config?: LiveStateMiddlewareConfig) => (store: any) => (next: any) => (action: any) => any;
7
+ export declare const createLiveStateMiddleware: (config?: LiveStateMiddlewareConfig) => {
8
+ middleware: (store: any) => (next: any) => (action: any) => any;
9
+ updateLiveStateDataId: (newId?: string) => void;
10
+ };
8
11
  export {};
package/esm/index.js CHANGED
@@ -1137,28 +1137,39 @@ var createLiveStateMiddleware = function (config) {
1137
1137
  if (!liveStateDataId) {
1138
1138
  liveStateDataId = VELT_DEFAULT_LIVE_STATE_ID;
1139
1139
  }
1140
- return function (store) {
1141
- var isSdkInitialized = false;
1142
- var getDataSubscriptionSet = false;
1143
- var setupGetDataSubscription = function () {
1144
- var _a;
1145
- if (!getDataSubscriptionSet) {
1146
- getDataSubscriptionSet = true;
1147
- var liveStateSyncElement = (_a = window.Velt) === null || _a === void 0 ? void 0 : _a.getLiveStateSyncElement();
1148
- liveStateSyncElement.getLiveStateData(liveStateDataId).subscribe(function (data) {
1149
- // Dispatch action to update local state, marking it as remote
1150
- if (data) {
1151
- var sessionId_1 = getSessionId();
1152
- if (data.id === sessionId_1) {
1153
- return;
1154
- }
1155
- else {
1156
- store.dispatch(__assign(__assign({}, data.action), { isRemote: true }));
1157
- }
1140
+ var internalStore; // This variable will hold the reference to the store
1141
+ var dataSubscription; // This variable will hold the reference to the data subscription
1142
+ var isSdkInitialized = false;
1143
+ var setupGetDataSubscription = function () {
1144
+ var _a, _b;
1145
+ if (!dataSubscription && window.Velt && ((_a = window.Velt) === null || _a === void 0 ? void 0 : _a.getLiveStateSyncElement)) {
1146
+ var liveStateSyncElement = (_b = window.Velt) === null || _b === void 0 ? void 0 : _b.getLiveStateSyncElement();
1147
+ dataSubscription = liveStateSyncElement === null || liveStateSyncElement === void 0 ? void 0 : liveStateSyncElement.getLiveStateData(liveStateDataId).subscribe(function (data) {
1148
+ // Dispatch action to update local state, marking it as remote
1149
+ if (data) {
1150
+ var sessionId_1 = getSessionId();
1151
+ if (data.id === sessionId_1) {
1152
+ return;
1158
1153
  }
1159
- });
1160
- }
1161
- };
1154
+ else {
1155
+ internalStore === null || internalStore === void 0 ? void 0 : internalStore.dispatch(__assign(__assign({}, data.action), { isRemote: true }));
1156
+ }
1157
+ }
1158
+ });
1159
+ }
1160
+ };
1161
+ var updateLiveStateDataId = function (newId) {
1162
+ liveStateDataId = newId || VELT_DEFAULT_LIVE_STATE_ID;
1163
+ if (dataSubscription) {
1164
+ dataSubscription.unsubscribe();
1165
+ dataSubscription = null;
1166
+ }
1167
+ if (isSdkInitialized) {
1168
+ setupGetDataSubscription();
1169
+ }
1170
+ };
1171
+ var middleware = function (store) {
1172
+ internalStore = store;
1162
1173
  var veltSdkInitializedListener = function () {
1163
1174
  isSdkInitialized = true;
1164
1175
  setupGetDataSubscription();
@@ -1177,6 +1188,10 @@ var createLiveStateMiddleware = function (config) {
1177
1188
  return result;
1178
1189
  }; };
1179
1190
  };
1191
+ return {
1192
+ middleware: middleware,
1193
+ updateLiveStateDataId: updateLiveStateDataId,
1194
+ };
1180
1195
  };
1181
1196
  var logLiveState = function (action, liveStateDataId) {
1182
1197
  var _a;