@fire-spark/realtime 0.15.3 → 0.16.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/index.d.ts CHANGED
@@ -41,6 +41,8 @@ interface RealtimeListenDeps {
41
41
  createClient?: (url: string, key: string) => RealtimeSocket;
42
42
  setIntervalFn?: typeof setInterval;
43
43
  clearIntervalFn?: typeof clearInterval;
44
+ /** Defaults to `auth.onCustomerChange`. */
45
+ onCustomerChange?: (listener: () => void) => () => void;
44
46
  }
45
47
  interface RealtimeListenOrderEvents {
46
48
  changed: (payload: OrderChangedPayload$1) => void;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { isStorefrontNotFound, services, storefrontJson } from "@fire-spark/core";
1
+ import { auth, isStorefrontNotFound, services, storefrontJson } from "@fire-spark/core";
2
2
  //#region ../../node_modules/.pnpm/@supabase+realtime-js@2.100.1/node_modules/@supabase/realtime-js/dist/module/lib/websocket-factory.js
3
3
  /**
4
4
  * Utilities for creating WebSocket instances across runtimes.
@@ -3999,6 +3999,9 @@ const listenOrder = (options, deps = {}) => {
3999
3999
  let socket = null;
4000
4000
  let channel = null;
4001
4001
  let refreshId = null;
4002
+ let followCustomer = false;
4003
+ let followingCustomer = false;
4004
+ const pinned = options.accessToken !== void 0;
4002
4005
  const http = {
4003
4006
  baseUrl: options.baseUrl,
4004
4007
  accessToken: options.accessToken,
@@ -4070,6 +4073,7 @@ const listenOrder = (options, deps = {}) => {
4070
4073
  return;
4071
4074
  }
4072
4075
  if (!socket) return;
4076
+ if (followCustomer) resubscribe();
4073
4077
  refreshId = setIntervalFn(() => {
4074
4078
  if (!socket) return;
4075
4079
  applyToken(socket).catch((error) => {
@@ -4079,6 +4083,31 @@ const listenOrder = (options, deps = {}) => {
4079
4083
  }, REALTIME_REFRESH_MS);
4080
4084
  if (stopped) teardown();
4081
4085
  };
4086
+ const resubscribe = () => {
4087
+ followCustomer = true;
4088
+ if (followingCustomer || stopped || !socket) return;
4089
+ followingCustomer = true;
4090
+ followCustomer = false;
4091
+ const current = socket;
4092
+ (async () => {
4093
+ try {
4094
+ await channel?.unsubscribe();
4095
+ channel = null;
4096
+ const token = await fetchRealtimeToken({}, http);
4097
+ if (stopped || socket !== current) return;
4098
+ await current.setAuth(token.access_token);
4099
+ channel = bindOrderEvents(current, token.customer_topic);
4100
+ } finally {
4101
+ followingCustomer = false;
4102
+ if (followCustomer) resubscribe();
4103
+ }
4104
+ })().catch((error) => {
4105
+ const message = error instanceof Error ? error.message : String(error);
4106
+ console.error("[realtime] customer resubscribe failed", message);
4107
+ });
4108
+ };
4109
+ const subscribeCustomer = deps.onCustomerChange ?? auth.onCustomerChange;
4110
+ const offCustomer = pinned ? () => void 0 : subscribeCustomer(resubscribe);
4082
4111
  start().catch((error) => {
4083
4112
  if (stopped) return;
4084
4113
  const message = error instanceof Error ? error.message : String(error);
@@ -4086,6 +4115,7 @@ const listenOrder = (options, deps = {}) => {
4086
4115
  });
4087
4116
  return () => {
4088
4117
  stopped = true;
4118
+ offCustomer();
4089
4119
  teardown();
4090
4120
  };
4091
4121
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fire-spark/realtime",
3
- "version": "0.15.3",
3
+ "version": "0.16.0",
4
4
  "description": "Fire Spark storefront realtime SDK",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -18,8 +18,8 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "@supabase/realtime-js": "^2.100.1",
21
- "@fire-spark/core": "^0.15.3",
22
- "@fire-spark/types": "^0.15.3"
21
+ "@fire-spark/core": "^0.16.0",
22
+ "@fire-spark/types": "^0.16.0"
23
23
  },
24
24
  "devDependencies": {
25
25
  "tsdown": "^0.22.14",