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

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

31 Mar 2026 08:56AM UTC coverage: 74.971% (-8.3%) from 83.221%
23789194950

push

github

cr15p1
chore(release): bump prerelease to 0.29.0-rc.11 via changeset

947 of 1238 branches covered (76.49%)

Branch coverage included in aggregate %.

4235 of 5674 relevant lines covered (74.64%)

25.61 hits per line

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

56.6
/packages/sync/src/utils/entityRunContextMethods.ts
1
import { type Entity, type EntityId, type UpsertOptions } from '../entity.js';
2
import {
3
  setManyEntityMembership,
4
  setOneEntityMembership,
5
  type EntityMembershipState,
6
} from './entityMembership.js';
7

8
export interface EntityRunContextMethods<
9
  TEntity extends object,
10
  RResult,
11
  TEntityId extends EntityId,
12
> {
13
  upsertOne: (input: TEntity, options?: UpsertOptions) => TEntity;
14
  upsertMany: (input: readonly TEntity[], options?: UpsertOptions) => readonly TEntity[];
15
  deleteOne: (id: TEntityId) => boolean;
16
  deleteMany: (ids: readonly TEntityId[]) => readonly TEntityId[];
17
  getValue: () => RResult;
18
}
19

20
export interface CreateEntityRunContextMethodsInput<
21
  TEntity extends object,
22
  RResult,
23
  TEntityId extends EntityId,
24
> {
25
  entity: Entity<TEntity, TEntityId>;
26
  state: EntityMembershipState<TEntityId>;
27
  isActive: () => boolean;
28
  refreshValue: () => void;
29
  readValue: () => RResult;
30
  refreshOnGet?: boolean;
31
  refreshStrategy?: EntityRunContextRefreshStrategy;
32
  upsertOneOperation?: string;
33
  upsertManyOperation?: string;
34
}
35

36
const DEFAULT_UPSERT_ONE_OPERATION = 'runContext.upsertOne()';
18✔
37
const DEFAULT_UPSERT_MANY_OPERATION = 'runContext.upsertMany()';
18✔
38
const DEFAULT_REFRESH_STRATEGY: EntityRunContextRefreshStrategy = 'eager';
18✔
39

40
export type EntityRunContextRefreshStrategy = 'eager' | 'lazy';
41

42
export const createEntityRunContextMethods = <
18✔
43
  TEntity extends object,
44
  RResult,
45
  TEntityId extends EntityId,
46
>({
47
  entity,
48
  state,
49
  isActive,
50
  refreshValue,
51
  readValue,
52
  refreshOnGet = false,
53
  refreshStrategy = DEFAULT_REFRESH_STRATEGY,
54
  upsertOneOperation = DEFAULT_UPSERT_ONE_OPERATION,
55
  upsertManyOperation = DEFAULT_UPSERT_MANY_OPERATION,
56
}: CreateEntityRunContextMethodsInput<
57
  TEntity,
58
  RResult,
59
  TEntityId
60
>): EntityRunContextMethods<TEntity, RResult, TEntityId> => {
61
  let hasPendingRefresh = false;
51✔
62
  const refreshIfNeeded = (): void => {
51✔
63
    if (!hasPendingRefresh) {
×
64
      return;
×
65
    }
66

67
    refreshValue();
×
68
    hasPendingRefresh = false;
×
69
  };
70
  const markValueDirty = (): void => {
51✔
71
    if (refreshStrategy === 'eager') {
34✔
72
      refreshValue();
19✔
73
      return;
19✔
74
    }
75

76
    hasPendingRefresh = true;
15✔
77
  };
78

79
  const upsertOne = (input: TEntity, options?: UpsertOptions): TEntity => {
51✔
80
    if (!isActive()) {
26✔
81
      return input;
×
82
    }
83

84
    const updated = entity.upsertOne(input, options);
26✔
85
    setOneEntityMembership(state, {
26✔
86
      entity,
87
      value: updated,
88
      operation: upsertOneOperation,
89
    });
90
    markValueDirty();
26✔
91
    return updated;
26✔
92
  };
93

94
  const upsertMany = (
51✔
95
    input: readonly TEntity[],
96
    options?: UpsertOptions,
97
  ): readonly TEntity[] => {
98
    if (!isActive()) {
8✔
99
      return input;
×
100
    }
101

102
    const updated = entity.upsertMany(input, options);
8✔
103
    setManyEntityMembership(state, {
8✔
104
      entity,
105
      values: updated,
106
      operation: upsertManyOperation,
107
    });
108
    markValueDirty();
8✔
109
    return updated;
8✔
110
  };
111

112
  const deleteOne = (id: TEntityId): boolean => {
51✔
113
    if (!isActive()) {
×
114
      return false;
×
115
    }
116

117
    const removed = entity.deleteOne(id);
×
118
    markValueDirty();
×
119
    return removed;
×
120
  };
121

122
  const deleteMany = (ids: readonly TEntityId[]): readonly TEntityId[] => {
51✔
123
    if (!isActive()) {
×
124
      return [];
×
125
    }
126

127
    const removedIds = entity.deleteMany(ids);
×
128
    markValueDirty();
×
129
    return removedIds;
×
130
  };
131

132
  const getValue = (): RResult => {
51✔
133
    if (refreshOnGet || hasPendingRefresh) {
×
134
      refreshIfNeeded();
×
135
    }
136

137
    return readValue();
×
138
  };
139

140
  return {
51✔
141
    upsertOne,
142
    upsertMany,
143
    deleteOne,
144
    deleteMany,
145
    getValue,
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