@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.
package/cjs/index.js CHANGED
@@ -1145,28 +1145,39 @@ var createLiveStateMiddleware = function (config) {
1145
1145
  if (!liveStateDataId) {
1146
1146
  liveStateDataId = VELT_DEFAULT_LIVE_STATE_ID;
1147
1147
  }
1148
- return function (store) {
1149
- var isSdkInitialized = false;
1150
- var getDataSubscriptionSet = false;
1151
- var setupGetDataSubscription = function () {
1152
- var _a;
1153
- if (!getDataSubscriptionSet) {
1154
- getDataSubscriptionSet = true;
1155
- var liveStateSyncElement = (_a = window.Velt) === null || _a === void 0 ? void 0 : _a.getLiveStateSyncElement();
1156
- liveStateSyncElement.getLiveStateData(liveStateDataId).subscribe(function (data) {
1157
- // Dispatch action to update local state, marking it as remote
1158
- if (data) {
1159
- var sessionId_1 = getSessionId();
1160
- if (data.id === sessionId_1) {
1161
- return;
1162
- }
1163
- else {
1164
- store.dispatch(__assign(__assign({}, data.action), { isRemote: true }));
1165
- }
1148
+ var internalStore; // This variable will hold the reference to the store
1149
+ var dataSubscription; // This variable will hold the reference to the data subscription
1150
+ var isSdkInitialized = false;
1151
+ var setupGetDataSubscription = function () {
1152
+ var _a, _b;
1153
+ if (!dataSubscription && window.Velt && ((_a = window.Velt) === null || _a === void 0 ? void 0 : _a.getLiveStateSyncElement)) {
1154
+ var liveStateSyncElement = (_b = window.Velt) === null || _b === void 0 ? void 0 : _b.getLiveStateSyncElement();
1155
+ dataSubscription = liveStateSyncElement === null || liveStateSyncElement === void 0 ? void 0 : liveStateSyncElement.getLiveStateData(liveStateDataId).subscribe(function (data) {
1156
+ // Dispatch action to update local state, marking it as remote
1157
+ if (data) {
1158
+ var sessionId_1 = getSessionId();
1159
+ if (data.id === sessionId_1) {
1160
+ return;
1166
1161
  }
1167
- });
1168
- }
1169
- };
1162
+ else {
1163
+ internalStore === null || internalStore === void 0 ? void 0 : internalStore.dispatch(__assign(__assign({}, data.action), { isRemote: true }));
1164
+ }
1165
+ }
1166
+ });
1167
+ }
1168
+ };
1169
+ var updateLiveStateDataId = function (newId) {
1170
+ liveStateDataId = newId || VELT_DEFAULT_LIVE_STATE_ID;
1171
+ if (dataSubscription) {
1172
+ dataSubscription.unsubscribe();
1173
+ dataSubscription = null;
1174
+ }
1175
+ if (isSdkInitialized) {
1176
+ setupGetDataSubscription();
1177
+ }
1178
+ };
1179
+ var middleware = function (store) {
1180
+ internalStore = store;
1170
1181
  var veltSdkInitializedListener = function () {
1171
1182
  isSdkInitialized = true;
1172
1183
  setupGetDataSubscription();
@@ -1185,6 +1196,10 @@ var createLiveStateMiddleware = function (config) {
1185
1196
  return result;
1186
1197
  }; };
1187
1198
  };
1199
+ return {
1200
+ middleware: middleware,
1201
+ updateLiveStateDataId: updateLiveStateDataId,
1202
+ };
1188
1203
  };
1189
1204
  var logLiveState = function (action, liveStateDataId) {
1190
1205
  var _a;