@pradip1995/segment-primitives 0.4.19 → 0.4.20

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.
@@ -1,32 +1,32 @@
1
- "use client"
2
-
3
- import { useEffect, useRef, useState } from "react"
4
- import {
5
- WISHLIST_UPDATED_EVENT,
6
- nextWishlistCount,
7
- type WishlistUpdatedDetail,
8
- } from "./wishlist-count"
9
-
10
- export function useWishlistCount(serverCount: number) {
11
- const initial = nextWishlistCount(serverCount)
12
- const [count, setCount] = useState(initial)
13
- const lastServerCount = useRef(initial)
14
-
15
- useEffect(() => {
16
- const next = nextWishlistCount(serverCount)
17
- if (next === lastServerCount.current) return
18
- lastServerCount.current = next
19
- setCount(next)
20
- }, [serverCount])
21
-
22
- useEffect(() => {
23
- const onUpdate = (event: Event) => {
24
- const detail = (event as CustomEvent<WishlistUpdatedDetail>).detail
25
- setCount((current) => nextWishlistCount(current, detail))
26
- }
27
- window.addEventListener(WISHLIST_UPDATED_EVENT, onUpdate)
28
- return () => window.removeEventListener(WISHLIST_UPDATED_EVENT, onUpdate)
29
- }, [])
30
-
31
- return count
32
- }
1
+ "use client"
2
+
3
+ import { useEffect, useRef, useState } from "react"
4
+ import {
5
+ WISHLIST_UPDATED_EVENT,
6
+ nextWishlistCount,
7
+ type WishlistUpdatedDetail,
8
+ } from "./wishlist-count"
9
+
10
+ export function useWishlistCount(serverCount: number) {
11
+ const initial = nextWishlistCount(serverCount)
12
+ const [count, setCount] = useState(initial)
13
+ const lastServerCount = useRef(initial)
14
+
15
+ useEffect(() => {
16
+ const next = nextWishlistCount(serverCount)
17
+ if (next === lastServerCount.current) return
18
+ lastServerCount.current = next
19
+ setCount(next)
20
+ }, [serverCount])
21
+
22
+ useEffect(() => {
23
+ const onUpdate = (event: Event) => {
24
+ const detail = (event as CustomEvent<WishlistUpdatedDetail>).detail
25
+ setCount((current) => nextWishlistCount(current, detail))
26
+ }
27
+ window.addEventListener(WISHLIST_UPDATED_EVENT, onUpdate)
28
+ return () => window.removeEventListener(WISHLIST_UPDATED_EVENT, onUpdate)
29
+ }, [])
30
+
31
+ return count
32
+ }
@@ -1,96 +1,96 @@
1
- import assert from "node:assert/strict"
2
- import { describe, it } from "node:test"
3
- import { nextWishlistCount, type WishlistUpdatedDetail } from "./wishlist-count.ts"
4
-
5
- const CASES: Array<{
6
- name: string
7
- current: number
8
- detail?: WishlistUpdatedDetail | null
9
- want: number
10
- }> = [
11
- {
12
- name: "removing one item decrements the navbar badge",
13
- current: 2,
14
- detail: { delta: -1 },
15
- want: 1,
16
- },
17
- {
18
- name: "removing the last item clears the badge",
19
- current: 1,
20
- detail: { delta: -1 },
21
- want: 0,
22
- },
23
- {
24
- name: "removing from an empty wishlist stays at zero",
25
- current: 0,
26
- detail: { delta: -1 },
27
- want: 0,
28
- },
29
- {
30
- name: "adding an item increments the badge",
31
- current: 0,
32
- detail: { delta: 1 },
33
- want: 1,
34
- },
35
- {
36
- name: "adding onto an existing count increments the badge",
37
- current: 2,
38
- detail: { delta: 1 },
39
- want: 3,
40
- },
41
- {
42
- name: "an explicit count replaces the current badge",
43
- current: 4,
44
- detail: { count: 1 },
45
- want: 1,
46
- },
47
- {
48
- name: "an explicit count wins when delta is also present",
49
- current: 4,
50
- detail: { count: 0, delta: 2 },
51
- want: 0,
52
- },
53
- {
54
- name: "missing detail keeps the current badge",
55
- current: 3,
56
- want: 3,
57
- },
58
- {
59
- name: "null detail keeps the current badge",
60
- current: 3,
61
- detail: null,
62
- want: 3,
63
- },
64
- {
65
- name: "empty detail keeps the current badge",
66
- current: 3,
67
- detail: {},
68
- want: 3,
69
- },
70
- {
71
- name: "non-finite current is treated as zero before applying delta",
72
- current: Number.NaN,
73
- detail: { delta: 2 },
74
- want: 2,
75
- },
76
- {
77
- name: "non-finite delta is ignored",
78
- current: 5,
79
- detail: { delta: Number.NaN },
80
- want: 5,
81
- },
82
- {
83
- name: "fractional values are truncated",
84
- current: 2.9,
85
- detail: { delta: -1.8 },
86
- want: 1,
87
- },
88
- ]
89
-
90
- describe("nextWishlistCount", () => {
91
- for (const row of CASES) {
92
- it(row.name, () => {
93
- assert.equal(nextWishlistCount(row.current, row.detail), row.want)
94
- })
95
- }
96
- })
1
+ import assert from "node:assert/strict"
2
+ import { describe, it } from "node:test"
3
+ import { nextWishlistCount, type WishlistUpdatedDetail } from "./wishlist-count.ts"
4
+
5
+ const CASES: Array<{
6
+ name: string
7
+ current: number
8
+ detail?: WishlistUpdatedDetail | null
9
+ want: number
10
+ }> = [
11
+ {
12
+ name: "removing one item decrements the navbar badge",
13
+ current: 2,
14
+ detail: { delta: -1 },
15
+ want: 1,
16
+ },
17
+ {
18
+ name: "removing the last item clears the badge",
19
+ current: 1,
20
+ detail: { delta: -1 },
21
+ want: 0,
22
+ },
23
+ {
24
+ name: "removing from an empty wishlist stays at zero",
25
+ current: 0,
26
+ detail: { delta: -1 },
27
+ want: 0,
28
+ },
29
+ {
30
+ name: "adding an item increments the badge",
31
+ current: 0,
32
+ detail: { delta: 1 },
33
+ want: 1,
34
+ },
35
+ {
36
+ name: "adding onto an existing count increments the badge",
37
+ current: 2,
38
+ detail: { delta: 1 },
39
+ want: 3,
40
+ },
41
+ {
42
+ name: "an explicit count replaces the current badge",
43
+ current: 4,
44
+ detail: { count: 1 },
45
+ want: 1,
46
+ },
47
+ {
48
+ name: "an explicit count wins when delta is also present",
49
+ current: 4,
50
+ detail: { count: 0, delta: 2 },
51
+ want: 0,
52
+ },
53
+ {
54
+ name: "missing detail keeps the current badge",
55
+ current: 3,
56
+ want: 3,
57
+ },
58
+ {
59
+ name: "null detail keeps the current badge",
60
+ current: 3,
61
+ detail: null,
62
+ want: 3,
63
+ },
64
+ {
65
+ name: "empty detail keeps the current badge",
66
+ current: 3,
67
+ detail: {},
68
+ want: 3,
69
+ },
70
+ {
71
+ name: "non-finite current is treated as zero before applying delta",
72
+ current: Number.NaN,
73
+ detail: { delta: 2 },
74
+ want: 2,
75
+ },
76
+ {
77
+ name: "non-finite delta is ignored",
78
+ current: 5,
79
+ detail: { delta: Number.NaN },
80
+ want: 5,
81
+ },
82
+ {
83
+ name: "fractional values are truncated",
84
+ current: 2.9,
85
+ detail: { delta: -1.8 },
86
+ want: 1,
87
+ },
88
+ ]
89
+
90
+ describe("nextWishlistCount", () => {
91
+ for (const row of CASES) {
92
+ it(row.name, () => {
93
+ assert.equal(nextWishlistCount(row.current, row.detail), row.want)
94
+ })
95
+ }
96
+ })
@@ -1,26 +1,26 @@
1
- export const WISHLIST_UPDATED_EVENT = "wishlist:updated"
2
-
3
- export type WishlistUpdatedDetail = {
4
- delta?: number
5
- count?: number
6
- }
7
-
8
- export function nextWishlistCount(
9
- current: number,
10
- detail?: WishlistUpdatedDetail | null
11
- ): number {
12
- const base = Number.isFinite(current) ? Math.max(0, Math.trunc(current)) : 0
13
- if (!detail) return base
14
- if (typeof detail.count === "number" && Number.isFinite(detail.count)) {
15
- return Math.max(0, Math.trunc(detail.count))
16
- }
17
- if (typeof detail.delta === "number" && Number.isFinite(detail.delta)) {
18
- return Math.max(0, base + Math.trunc(detail.delta))
19
- }
20
- return base
21
- }
22
-
23
- export function dispatchWishlistUpdated(detail: WishlistUpdatedDetail) {
24
- if (typeof window === "undefined") return
25
- window.dispatchEvent(new CustomEvent(WISHLIST_UPDATED_EVENT, { detail }))
26
- }
1
+ export const WISHLIST_UPDATED_EVENT = "wishlist:updated"
2
+
3
+ export type WishlistUpdatedDetail = {
4
+ delta?: number
5
+ count?: number
6
+ }
7
+
8
+ export function nextWishlistCount(
9
+ current: number,
10
+ detail?: WishlistUpdatedDetail | null
11
+ ): number {
12
+ const base = Number.isFinite(current) ? Math.max(0, Math.trunc(current)) : 0
13
+ if (!detail) return base
14
+ if (typeof detail.count === "number" && Number.isFinite(detail.count)) {
15
+ return Math.max(0, Math.trunc(detail.count))
16
+ }
17
+ if (typeof detail.delta === "number" && Number.isFinite(detail.delta)) {
18
+ return Math.max(0, base + Math.trunc(detail.delta))
19
+ }
20
+ return base
21
+ }
22
+
23
+ export function dispatchWishlistUpdated(detail: WishlistUpdatedDetail) {
24
+ if (typeof window === "undefined") return
25
+ window.dispatchEvent(new CustomEvent(WISHLIST_UPDATED_EVENT, { detail }))
26
+ }
@@ -1,83 +1,83 @@
1
- "use client"
2
-
3
- import { getWishlistProductIds } from "@pradip1995/commerce-core/client/actions/wishlist"
4
- import { WISHLIST_UPDATED_EVENT } from "./wishlist-count"
5
-
6
- const WINDOW_KEY = "__storefrontWishlistIds"
7
- const WINDOW_INFLIGHT = "__storefrontWishlistIdsInflight"
8
-
9
- type WishlistIdsWindow = Window & {
10
- [WINDOW_KEY]?: string[]
11
- [WINDOW_INFLIGHT]?: Promise<string[]>
12
- }
13
-
14
- function win(): WishlistIdsWindow | null {
15
- if (typeof window === "undefined") return null
16
- return window as WishlistIdsWindow
17
- }
18
-
19
- function normalizeIds(ids: unknown): string[] {
20
- if (!Array.isArray(ids)) return []
21
- return ids.filter((id): id is string => typeof id === "string" && id.length > 0)
22
- }
23
-
24
- function fetchWishlistIds(w: WishlistIdsWindow): Promise<string[]> {
25
- w[WINDOW_INFLIGHT] = getWishlistProductIds()
26
- .then((ids) => {
27
- const normalized = normalizeIds(ids)
28
- w[WINDOW_KEY] = normalized
29
- delete w[WINDOW_INFLIGHT]
30
- return normalized
31
- })
32
- .catch(() => {
33
- w[WINDOW_KEY] = []
34
- delete w[WINDOW_INFLIGHT]
35
- return [] as string[]
36
- })
37
- return w[WINDOW_INFLIGHT]!
38
- }
39
-
40
- /** Drop cached IDs so the next loadSharedWishlistIds() hits the server again. */
41
- export function clearSharedWishlistIdsCache() {
42
- const w = win()
43
- if (!w) return
44
- delete w[WINDOW_KEY]
45
- delete w[WINDOW_INFLIGHT]
46
- }
47
-
48
- /**
49
- * Shared wishlist ID loader (window-scoped so shop cards + wishlist page share
50
- * one cache even when the bundler duplicates the module).
51
- *
52
- * `force: true` drops settled cache but reuses an in-flight fetch so N product
53
- * cards mounting together only hit the server once.
54
- */
55
- export function loadSharedWishlistIds(options?: { force?: boolean }): Promise<string[]> {
56
- const w = win()
57
- if (!w) {
58
- return getWishlistProductIds()
59
- .then(normalizeIds)
60
- .catch(() => [] as string[])
61
- }
62
-
63
- if (options?.force) {
64
- delete w[WINDOW_KEY]
65
- if (w[WINDOW_INFLIGHT]) return w[WINDOW_INFLIGHT]
66
- return fetchWishlistIds(w)
67
- }
68
-
69
- if (w[WINDOW_KEY]) return Promise.resolve(w[WINDOW_KEY]!)
70
- if (w[WINDOW_INFLIGHT]) return w[WINDOW_INFLIGHT]
71
- return fetchWishlistIds(w)
72
- }
73
-
74
- let listening = false
75
-
76
- /** Ensure wishlist:updated always invalidates the window cache (once per runtime). */
77
- export function ensureWishlistIdsInvalidationListener() {
78
- if (listening || typeof window === "undefined") return
79
- listening = true
80
- window.addEventListener(WISHLIST_UPDATED_EVENT, () => {
81
- clearSharedWishlistIdsCache()
82
- })
83
- }
1
+ "use client"
2
+
3
+ import { getWishlistProductIds } from "@pradip1995/commerce-core/client/actions/wishlist"
4
+ import { WISHLIST_UPDATED_EVENT } from "./wishlist-count"
5
+
6
+ const WINDOW_KEY = "__storefrontWishlistIds"
7
+ const WINDOW_INFLIGHT = "__storefrontWishlistIdsInflight"
8
+
9
+ type WishlistIdsWindow = Window & {
10
+ [WINDOW_KEY]?: string[]
11
+ [WINDOW_INFLIGHT]?: Promise<string[]>
12
+ }
13
+
14
+ function win(): WishlistIdsWindow | null {
15
+ if (typeof window === "undefined") return null
16
+ return window as WishlistIdsWindow
17
+ }
18
+
19
+ function normalizeIds(ids: unknown): string[] {
20
+ if (!Array.isArray(ids)) return []
21
+ return ids.filter((id): id is string => typeof id === "string" && id.length > 0)
22
+ }
23
+
24
+ function fetchWishlistIds(w: WishlistIdsWindow): Promise<string[]> {
25
+ w[WINDOW_INFLIGHT] = getWishlistProductIds()
26
+ .then((ids) => {
27
+ const normalized = normalizeIds(ids)
28
+ w[WINDOW_KEY] = normalized
29
+ delete w[WINDOW_INFLIGHT]
30
+ return normalized
31
+ })
32
+ .catch(() => {
33
+ w[WINDOW_KEY] = []
34
+ delete w[WINDOW_INFLIGHT]
35
+ return [] as string[]
36
+ })
37
+ return w[WINDOW_INFLIGHT]!
38
+ }
39
+
40
+ /** Drop cached IDs so the next loadSharedWishlistIds() hits the server again. */
41
+ export function clearSharedWishlistIdsCache() {
42
+ const w = win()
43
+ if (!w) return
44
+ delete w[WINDOW_KEY]
45
+ delete w[WINDOW_INFLIGHT]
46
+ }
47
+
48
+ /**
49
+ * Shared wishlist ID loader (window-scoped so shop cards + wishlist page share
50
+ * one cache even when the bundler duplicates the module).
51
+ *
52
+ * `force: true` drops settled cache but reuses an in-flight fetch so N product
53
+ * cards mounting together only hit the server once.
54
+ */
55
+ export function loadSharedWishlistIds(options?: { force?: boolean }): Promise<string[]> {
56
+ const w = win()
57
+ if (!w) {
58
+ return getWishlistProductIds()
59
+ .then(normalizeIds)
60
+ .catch(() => [] as string[])
61
+ }
62
+
63
+ if (options?.force) {
64
+ delete w[WINDOW_KEY]
65
+ if (w[WINDOW_INFLIGHT]) return w[WINDOW_INFLIGHT]
66
+ return fetchWishlistIds(w)
67
+ }
68
+
69
+ if (w[WINDOW_KEY]) return Promise.resolve(w[WINDOW_KEY]!)
70
+ if (w[WINDOW_INFLIGHT]) return w[WINDOW_INFLIGHT]
71
+ return fetchWishlistIds(w)
72
+ }
73
+
74
+ let listening = false
75
+
76
+ /** Ensure wishlist:updated always invalidates the window cache (once per runtime). */
77
+ export function ensureWishlistIdsInvalidationListener() {
78
+ if (listening || typeof window === "undefined") return
79
+ listening = true
80
+ window.addEventListener(WISHLIST_UPDATED_EVENT, () => {
81
+ clearSharedWishlistIdsCache()
82
+ })
83
+ }