• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

live-input-vector-output-node / livon-ts / 26078594527

19 May 2026 05:40AM UTC coverage: 69.413% (-5.6%) from 74.971%
26078594527

push

github

cr15p1
Remove remaining duplicated Sonar blocks

1990 of 3317 branches covered (59.99%)

Branch coverage included in aggregate %.

9 of 23 new or added lines in 3 files covered. (39.13%)

304 existing lines in 15 files now uncovered.

4321 of 5775 relevant lines covered (74.82%)

25.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.32
/packages/sync/src/entity/createMembershipMethods.ts
1
import type {
2
  EntityId,
3
  EntityUnitKeyInput,
4
  EntityUnitState,
5
  RegisterEntityUnitInput,
6
  SetEntityUnitMembershipInput,
7
} from './types.js';
8

9
interface CreateMembershipMethodsInput<TId extends EntityId> {
10
  unitStateByKey: Map<string, EntityUnitState<TId>>;
11
  sweepExpiredOrphansIfNeeded: () => void;
12
  removeUnitKeyFromId: (input: EntityUnitKeyInput<TId>) => void;
13
  addUnitKeyToId: (input: EntityUnitKeyInput<TId>) => void;
14
}
15

16
interface SetSingleUnitMembershipInput<TId extends EntityId> {
17
  currentMembershipIds: Set<TId>;
18
  key: string;
19
  nextSingleMembershipId: TId;
20
}
21

22
export interface MembershipMethods<TId extends EntityId> {
23
  clearUnitMembership: (key: string) => void;
24
  registerUnit: (input: RegisterEntityUnitInput) => () => void;
25
  setUnitMembership: (input: SetEntityUnitMembershipInput<TId>) => void;
26
}
27

28
export const createMembershipMethods = <TId extends EntityId>({
16✔
29
  unitStateByKey,
30
  sweepExpiredOrphansIfNeeded,
31
  removeUnitKeyFromId,
32
  addUnitKeyToId,
33
}: CreateMembershipMethodsInput<TId>): MembershipMethods<TId> => {
34
  const clearUnitMembership = (key: string): void => {
87✔
35
    const unitState = unitStateByKey.get(key);
61✔
36
    if (!unitState) {
61!
37
      return;
61✔
38
    }
39

UNCOV
40
    unitState.membershipIds.forEach((id) => {
×
UNCOV
41
      removeUnitKeyFromId({ id, key });
×
42
    });
UNCOV
43
    unitState.membershipIds.clear();
×
44
  };
45

46
  const unregisterUnit = (key: string): void => {
87✔
47
    clearUnitMembership(key);
61✔
48
    unitStateByKey.delete(key);
61✔
49
  };
50

51
  const registerUnit = ({
87✔
52
    key,
53
    onChange,
54
  }: RegisterEntityUnitInput): (() => void) => {
55
    sweepExpiredOrphansIfNeeded();
61✔
56
    unregisterUnit(key);
61✔
57
    unitStateByKey.set(key, {
61✔
58
      onChange,
59
      membershipIds: new Set<TId>(),
60
    });
61

62
    return () => {
61✔
UNCOV
63
      unregisterUnit(key);
×
64
    };
65
  };
66

67
  const setSingleUnitMembership = ({
87✔
68
    currentMembershipIds,
69
    key,
70
    nextSingleMembershipId,
71
  }: SetSingleUnitMembershipInput<TId>): void => {
72
    currentMembershipIds.forEach((id) => {
56✔
73
      if (id !== nextSingleMembershipId) {
3!
74
        removeUnitKeyFromId({ id, key });
3✔
75
      }
76
    });
77

78
    const hasNextSingleMembershipId = currentMembershipIds.has(nextSingleMembershipId);
56✔
79
    currentMembershipIds.clear();
56✔
80
    currentMembershipIds.add(nextSingleMembershipId);
56✔
81
    if (!hasNextSingleMembershipId) {
56!
82
      addUnitKeyToId({
56✔
83
        id: nextSingleMembershipId,
84
        key,
85
      });
86
    }
87
  };
88

89
  const setUnitMembership = ({
87✔
90
    key,
91
    ids,
92
  }: SetEntityUnitMembershipInput<TId>): void => {
93
    sweepExpiredOrphansIfNeeded();
70✔
94
    const unitState = unitStateByKey.get(key);
70✔
95
    if (!unitState) {
70✔
96
      return;
5✔
97
    }
98

99
    const currentMembershipIds = unitState.membershipIds;
65✔
100
    if (ids.length === 1) {
65✔
101
      const [nextSingleMembershipId] = ids;
56✔
102
      if (nextSingleMembershipId === undefined) {
56!
UNCOV
103
        return;
×
104
      }
105

106
      if (currentMembershipIds.size === 1 && currentMembershipIds.has(nextSingleMembershipId)) {
56!
UNCOV
107
        return;
×
108
      }
109

110
      setSingleUnitMembership({
56✔
111
        currentMembershipIds,
112
        key,
113
        nextSingleMembershipId,
114
      });
115
      return;
56✔
116
    }
117

118
    if (currentMembershipIds.size === ids.length) {
9!
UNCOV
119
      const hasDifferentId = ids.some((id) => !currentMembershipIds.has(id));
×
UNCOV
120
      if (!hasDifferentId) {
×
121
        return;
×
122
      }
123
    }
124

125
    const nextMembershipIds = new Set<TId>(ids);
9✔
126

127
    currentMembershipIds.forEach((id) => {
9✔
128
      if (!nextMembershipIds.has(id)) {
5!
129
        removeUnitKeyFromId({ id, key });
5✔
130
      }
131
    });
132

133
    nextMembershipIds.forEach((id) => {
9✔
134
      if (!currentMembershipIds.has(id)) {
160!
135
        addUnitKeyToId({ id, key });
160✔
136
      }
137
    });
138

139
    unitState.membershipIds = nextMembershipIds;
9✔
140
  };
141

142
  return {
87✔
143
    clearUnitMembership,
144
    registerUnit,
145
    setUnitMembership,
146
  };
147
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc