jazz-tools 0.8.15 → 0.8.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. package/CHANGELOG.md +276 -269
  2. package/dist/native/coValues/account.js +3 -6
  3. package/dist/native/coValues/account.js.map +1 -1
  4. package/dist/native/coValues/coList.js +3 -7
  5. package/dist/native/coValues/coList.js.map +1 -1
  6. package/dist/native/coValues/coMap.js +8 -14
  7. package/dist/native/coValues/coMap.js.map +1 -1
  8. package/dist/native/coValues/coStream.js +7 -12
  9. package/dist/native/coValues/coStream.js.map +1 -1
  10. package/dist/native/coValues/deepLoading.js +6 -3
  11. package/dist/native/coValues/deepLoading.js.map +1 -1
  12. package/dist/native/coValues/extensions/imageDef.js.map +1 -1
  13. package/dist/native/coValues/group.js +3 -6
  14. package/dist/native/coValues/group.js.map +1 -1
  15. package/dist/native/coValues/interfaces.js +4 -3
  16. package/dist/native/coValues/interfaces.js.map +1 -1
  17. package/dist/native/exports.js +3 -9
  18. package/dist/native/exports.js.map +1 -1
  19. package/dist/native/implementation/createContext.js +1 -2
  20. package/dist/native/implementation/createContext.js.map +1 -1
  21. package/dist/native/implementation/devtoolsFormatters.js +5 -25
  22. package/dist/native/implementation/devtoolsFormatters.js.map +1 -1
  23. package/dist/native/implementation/refs.js +1 -2
  24. package/dist/native/implementation/refs.js.map +1 -1
  25. package/dist/native/implementation/schema.js +1 -1
  26. package/dist/native/implementation/schema.js.map +1 -1
  27. package/dist/native/implementation/subscriptionScope.js +2 -4
  28. package/dist/native/implementation/subscriptionScope.js.map +1 -1
  29. package/dist/native/index.native.js +1 -1
  30. package/dist/native/index.native.js.map +1 -1
  31. package/dist/native/lib/cache.js.map +1 -1
  32. package/dist/native/lib/cache.test.js +1 -1
  33. package/dist/native/lib/cache.test.js.map +1 -1
  34. package/dist/web/coValues/account.js +3 -6
  35. package/dist/web/coValues/account.js.map +1 -1
  36. package/dist/web/coValues/coList.js +3 -7
  37. package/dist/web/coValues/coList.js.map +1 -1
  38. package/dist/web/coValues/coMap.js +8 -14
  39. package/dist/web/coValues/coMap.js.map +1 -1
  40. package/dist/web/coValues/coStream.js +7 -12
  41. package/dist/web/coValues/coStream.js.map +1 -1
  42. package/dist/web/coValues/deepLoading.js +6 -3
  43. package/dist/web/coValues/deepLoading.js.map +1 -1
  44. package/dist/web/coValues/extensions/imageDef.js.map +1 -1
  45. package/dist/web/coValues/group.js +3 -6
  46. package/dist/web/coValues/group.js.map +1 -1
  47. package/dist/web/coValues/interfaces.js +4 -3
  48. package/dist/web/coValues/interfaces.js.map +1 -1
  49. package/dist/web/exports.js +3 -9
  50. package/dist/web/exports.js.map +1 -1
  51. package/dist/web/implementation/createContext.js +1 -2
  52. package/dist/web/implementation/createContext.js.map +1 -1
  53. package/dist/web/implementation/devtoolsFormatters.js +5 -25
  54. package/dist/web/implementation/devtoolsFormatters.js.map +1 -1
  55. package/dist/web/implementation/refs.js +1 -2
  56. package/dist/web/implementation/refs.js.map +1 -1
  57. package/dist/web/implementation/schema.js +1 -1
  58. package/dist/web/implementation/schema.js.map +1 -1
  59. package/dist/web/implementation/subscriptionScope.js +2 -4
  60. package/dist/web/implementation/subscriptionScope.js.map +1 -1
  61. package/dist/web/lib/cache.js.map +1 -1
  62. package/dist/web/lib/cache.test.js +1 -1
  63. package/dist/web/lib/cache.test.js.map +1 -1
  64. package/package.json +5 -9
  65. package/src/coValues/account.ts +330 -339
  66. package/src/coValues/coList.ts +474 -495
  67. package/src/coValues/coMap.ts +584 -604
  68. package/src/coValues/coStream.ts +624 -650
  69. package/src/coValues/deepLoading.ts +184 -200
  70. package/src/coValues/extensions/imageDef.ts +44 -44
  71. package/src/coValues/group.ts +196 -210
  72. package/src/coValues/interfaces.ts +197 -199
  73. package/src/exports.ts +38 -26
  74. package/src/implementation/createContext.ts +206 -213
  75. package/src/implementation/devtoolsFormatters.ts +80 -100
  76. package/src/implementation/refs.ts +127 -139
  77. package/src/implementation/schema.ts +124 -128
  78. package/src/implementation/subscriptionScope.ts +111 -121
  79. package/src/index.native.ts +3 -3
  80. package/src/lib/cache.test.ts +48 -48
  81. package/src/lib/cache.ts +9 -9
  82. package/src/tests/coList.test.ts +264 -283
  83. package/src/tests/coMap.test.ts +741 -761
  84. package/src/tests/coStream.test.ts +405 -438
  85. package/src/tests/deepLoading.test.ts +251 -256
  86. package/src/tests/groupsAndAccounts.test.ts +70 -74
  87. package/src/tests/schema.test.ts +198 -198
  88. package/src/tests/subscribe.test.ts +312 -299
  89. package/tsconfig.json +2 -4
  90. package/tsconfig.native.json +4 -10
  91. package/tsconfig.web.json +4 -10
  92. package/.eslintrc.cjs +0 -24
  93. package/.prettierrc.js +0 -9
@@ -1,229 +1,213 @@
1
1
  import { SessionID } from "cojson";
2
2
  import {
3
- Account,
4
- CoList,
5
- CoStream,
6
- CoStreamEntry,
7
- ItemsSym,
8
- Ref,
9
- RefEncoded,
10
- UnCo,
3
+ Account,
4
+ CoList,
5
+ CoStream,
6
+ CoStreamEntry,
7
+ ItemsSym,
8
+ Ref,
9
+ RefEncoded,
10
+ UnCo,
11
11
  } from "../internal.js";
12
12
  import { CoKeys, CoMap } from "./coMap.js";
13
13
  import { CoValue, ID } from "./interfaces.js";
14
14
 
15
15
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
16
  export function fulfillsDepth(depth: any, value: CoValue): boolean {
17
- if (
18
- value._type === "CoMap" ||
19
- value._type === "Group" ||
20
- value._type === "Account"
21
- ) {
22
- if (Array.isArray(depth) && depth.length === 1) {
23
- return Object.entries(value).every(([key, item]) => {
24
- return (
25
- value as unknown as {
26
- _refs: { [key: string]: Ref<CoValue> | undefined };
27
- }
28
- )._refs[key]
29
- ? item && fulfillsDepth(depth[0], item)
30
- : ((value as CoMap)._schema[
31
- ItemsSym
32
- ] as RefEncoded<CoValue>)!.optional;
33
- });
34
- } else {
35
- for (const key of Object.keys(depth)) {
36
- const map = value as unknown as {
37
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
- [key: string]: any;
39
- _refs: { [key: string]: Ref<CoValue> | undefined };
40
- };
41
- if (!map._refs[key] && map._schema[key].optional) {
42
- continue;
43
- }
44
- if (!map[key]) {
45
- return false;
46
- }
47
- if (!fulfillsDepth(depth[key], map[key])) {
48
- return false;
49
- }
50
- }
51
- return true;
17
+ if (
18
+ value._type === "CoMap" ||
19
+ value._type === "Group" ||
20
+ value._type === "Account"
21
+ ) {
22
+ if (Array.isArray(depth) && depth.length === 1) {
23
+ return Object.entries(value).every(([key, item]) => {
24
+ return (
25
+ value as unknown as {
26
+ _refs: { [key: string]: Ref<CoValue> | undefined };
27
+ }
28
+ )._refs[key]
29
+ ? item && fulfillsDepth(depth[0], item)
30
+ : ((value as CoMap)._schema[ItemsSym] as RefEncoded<CoValue>)!
31
+ .optional;
32
+ });
33
+ } else {
34
+ for (const key of Object.keys(depth)) {
35
+ const map = value as unknown as {
36
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
+ [key: string]: any;
38
+ _refs: { [key: string]: Ref<CoValue> | undefined };
39
+ };
40
+ if (!map._refs[key] && map._schema[key].optional) {
41
+ continue;
52
42
  }
53
- } else if (value._type === "CoList") {
54
- if (depth.length === 0) {
55
- return true;
56
- } else {
57
- const itemDepth = depth[0];
58
- return (value as CoList).every((item, i) =>
59
- (value as CoList)._refs[i]
60
- ? item && fulfillsDepth(itemDepth, item)
61
- : (
62
- (value as CoList)._schema[
63
- ItemsSym
64
- ] as RefEncoded<CoValue>
65
- ).optional,
66
- );
43
+ if (!map[key]) {
44
+ return false;
67
45
  }
68
- } else if (value._type === "CoStream") {
69
- if (depth.length === 0) {
70
- return true;
71
- } else {
72
- const itemDepth = depth[0];
73
- return Object.values((value as CoStream).perSession).every(
74
- (entry) =>
75
- entry.ref
76
- ? entry.value && fulfillsDepth(itemDepth, entry.value)
77
- : (
78
- (value as CoStream)._schema[
79
- ItemsSym
80
- ] as RefEncoded<CoValue>
81
- ).optional,
82
- );
46
+ if (!fulfillsDepth(depth[key], map[key])) {
47
+ return false;
83
48
  }
84
- } else if (value._type === "BinaryCoStream") {
85
- return true;
49
+ }
50
+ return true;
51
+ }
52
+ } else if (value._type === "CoList") {
53
+ if (depth.length === 0) {
54
+ return true;
86
55
  } else {
87
- console.error(value);
88
- throw new Error("Unexpected value type: " + value._type);
56
+ const itemDepth = depth[0];
57
+ return (value as CoList).every((item, i) =>
58
+ (value as CoList)._refs[i]
59
+ ? item && fulfillsDepth(itemDepth, item)
60
+ : ((value as CoList)._schema[ItemsSym] as RefEncoded<CoValue>)
61
+ .optional,
62
+ );
89
63
  }
64
+ } else if (value._type === "CoStream") {
65
+ if (depth.length === 0) {
66
+ return true;
67
+ } else {
68
+ const itemDepth = depth[0];
69
+ return Object.values((value as CoStream).perSession).every((entry) =>
70
+ entry.ref
71
+ ? entry.value && fulfillsDepth(itemDepth, entry.value)
72
+ : ((value as CoStream)._schema[ItemsSym] as RefEncoded<CoValue>)
73
+ .optional,
74
+ );
75
+ }
76
+ } else if (value._type === "BinaryCoStream") {
77
+ return true;
78
+ } else {
79
+ console.error(value);
80
+ throw new Error("Unexpected value type: " + value._type);
81
+ }
90
82
  }
91
83
 
92
84
  type UnCoNotNull<T> = UnCo<Exclude<T, null>>;
93
85
  type Clean<T> = UnCo<NonNullable<T>>;
94
86
 
95
87
  export type DepthsIn<
96
- V,
97
- DepthLimit extends number = 5,
98
- CurrentDepth extends number[] = [],
88
+ V,
89
+ DepthLimit extends number = 5,
90
+ CurrentDepth extends number[] = [],
99
91
  > =
100
- | (DepthLimit extends CurrentDepth["length"]
101
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
102
- any
103
- : // Basically V extends CoList - but if we used that we'd introduce circularity into the definition of CoList itself
104
- V extends Array<infer Item>
105
- ?
106
- | [
107
- DepthsIn<
108
- UnCoNotNull<Item>,
109
- DepthLimit,
110
- [0, ...CurrentDepth]
111
- >,
92
+ | (DepthLimit extends CurrentDepth["length"]
93
+ ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
94
+ any
95
+ : // Basically V extends CoList - but if we used that we'd introduce circularity into the definition of CoList itself
96
+ V extends Array<infer Item>
97
+ ?
98
+ | [DepthsIn<UnCoNotNull<Item>, DepthLimit, [0, ...CurrentDepth]>]
99
+ | never[]
100
+ : // Basically V extends CoMap | Group | Account - but if we used that we'd introduce circularity into the definition of CoMap itself
101
+ V extends { _type: "CoMap" | "Group" | "Account" }
102
+ ?
103
+ | {
104
+ [Key in CoKeys<V> as Clean<V[Key]> extends CoValue
105
+ ? Key
106
+ : never]?: DepthsIn<
107
+ Clean<V[Key]>,
108
+ DepthLimit,
109
+ [0, ...CurrentDepth]
110
+ >;
111
+ }
112
+ | (ItemsSym extends keyof V
113
+ ? [
114
+ DepthsIn<
115
+ Clean<V[ItemsSym]>,
116
+ DepthLimit,
117
+ [0, ...CurrentDepth]
118
+ >,
112
119
  ]
113
- | never[]
114
- : // Basically V extends CoMap | Group | Account - but if we used that we'd introduce circularity into the definition of CoMap itself
115
- V extends { _type: "CoMap" | "Group" | "Account" }
116
- ?
117
- | {
118
- [Key in CoKeys<V> as Clean<V[Key]> extends CoValue
119
- ? Key
120
- : never]?: DepthsIn<
121
- Clean<V[Key]>,
122
- DepthLimit,
123
- [0, ...CurrentDepth]
124
- >;
125
- }
126
- | (ItemsSym extends keyof V
127
- ? [
128
- DepthsIn<
129
- Clean<V[ItemsSym]>,
130
- DepthLimit,
131
- [0, ...CurrentDepth]
132
- >,
133
- ]
134
- : never)
135
- | never[]
136
- : V extends {
137
- _type: "CoStream";
138
- byMe: CoStreamEntry<infer Item> | undefined;
139
- }
140
- ?
141
- | [
142
- DepthsIn<
143
- UnCoNotNull<Item>,
144
- DepthLimit,
145
- [0, ...CurrentDepth]
146
- >,
147
- ]
148
- | never[]
149
- : never[])
150
- | never[];
120
+ : never)
121
+ | never[]
122
+ : V extends {
123
+ _type: "CoStream";
124
+ byMe: CoStreamEntry<infer Item> | undefined;
125
+ }
126
+ ?
127
+ | [
128
+ DepthsIn<
129
+ UnCoNotNull<Item>,
130
+ DepthLimit,
131
+ [0, ...CurrentDepth]
132
+ >,
133
+ ]
134
+ | never[]
135
+ : never[])
136
+ | never[];
151
137
 
152
138
  export type DeeplyLoaded<
153
- V,
154
- Depth,
155
- DepthLimit extends number = 5,
156
- CurrentDepth extends number[] = [],
139
+ V,
140
+ Depth,
141
+ DepthLimit extends number = 5,
142
+ CurrentDepth extends number[] = [],
157
143
  > = DepthLimit extends CurrentDepth["length"]
158
- ? V
159
- : // Basically V extends CoList - but if we used that we'd introduce circularity into the definition of CoList itself
160
- [V] extends [Array<infer Item>]
161
- ? Depth extends never[] // []
162
- ? V
163
- : UnCoNotNull<Item> extends CoValue
164
- ? Depth extends Array<infer ItemDepth> // [item-depth]
165
- ? (UnCoNotNull<Item> &
166
- DeeplyLoaded<
167
- UnCoNotNull<Item>,
168
- ItemDepth,
169
- DepthLimit,
170
- [0, ...CurrentDepth]
171
- >)[] &
172
- V
173
- : never
174
- : V
175
- : // Basically V extends CoMap | Group | Account - but if we used that we'd introduce circularity into the definition of CoMap itself
176
- [V] extends [{ _type: "CoMap" | "Group" | "Account" }]
177
- ? Depth extends never[]
144
+ ? V
145
+ : // Basically V extends CoList - but if we used that we'd introduce circularity into the definition of CoList itself
146
+ [V] extends [Array<infer Item>]
147
+ ? Depth extends never[] // []
148
+ ? V
149
+ : UnCoNotNull<Item> extends CoValue
150
+ ? Depth extends Array<infer ItemDepth> // [item-depth]
151
+ ? (UnCoNotNull<Item> &
152
+ DeeplyLoaded<
153
+ UnCoNotNull<Item>,
154
+ ItemDepth,
155
+ DepthLimit,
156
+ [0, ...CurrentDepth]
157
+ >)[] &
158
+ V
159
+ : never
160
+ : V
161
+ : // Basically V extends CoMap | Group | Account - but if we used that we'd introduce circularity into the definition of CoMap itself
162
+ [V] extends [{ _type: "CoMap" | "Group" | "Account" }]
163
+ ? Depth extends never[]
164
+ ? V
165
+ : Depth extends Array<infer ItemDepth>
166
+ ? ItemsSym extends keyof V
167
+ ? V & {
168
+ [key: string]: DeeplyLoaded<
169
+ Clean<V[ItemsSym]>,
170
+ ItemDepth,
171
+ DepthLimit,
172
+ [0, ...CurrentDepth]
173
+ >;
174
+ }
175
+ : never
176
+ : keyof Depth extends never
178
177
  ? V
179
- : Depth extends Array<infer ItemDepth>
180
- ? ItemsSym extends keyof V
181
- ? V & {
182
- [key: string]: DeeplyLoaded<
183
- Clean<V[ItemsSym]>,
184
- ItemDepth,
178
+ : {
179
+ [Key in keyof Depth]-?: Key extends CoKeys<V>
180
+ ? Clean<V[Key]> extends CoValue
181
+ ?
182
+ | DeeplyLoaded<
183
+ Clean<V[Key]>,
184
+ Depth[Key],
185
185
  DepthLimit,
186
186
  [0, ...CurrentDepth]
187
- >;
188
- }
189
- : never
190
- : keyof Depth extends never
191
- ? V
192
- : {
193
- [Key in keyof Depth]-?: Key extends CoKeys<V>
194
- ? Clean<V[Key]> extends CoValue
195
- ?
196
- | DeeplyLoaded<
197
- Clean<V[Key]>,
198
- Depth[Key],
199
- DepthLimit,
200
- [0, ...CurrentDepth]
201
- >
202
- | (undefined extends V[Key]
203
- ? undefined
204
- : never)
205
- : never
206
- : never;
207
- } & V
187
+ >
188
+ | (undefined extends V[Key] ? undefined : never)
189
+ : never
190
+ : never;
191
+ } & V
192
+ : [V] extends [
193
+ {
194
+ _type: "CoStream";
195
+ byMe: CoStreamEntry<infer Item> | undefined;
196
+ },
197
+ ]
198
+ ? Depth extends never[]
199
+ ? V
200
+ : V & {
201
+ byMe?: { value: UnCoNotNull<Item> };
202
+ inCurrentSession?: { value: UnCoNotNull<Item> };
203
+ perSession: {
204
+ [key: SessionID]: { value: UnCoNotNull<Item> };
205
+ };
206
+ } & { [key: ID<Account>]: { value: UnCoNotNull<Item> } }
208
207
  : [V] extends [
209
- {
210
- _type: "CoStream";
211
- byMe: CoStreamEntry<infer Item> | undefined;
212
- },
208
+ {
209
+ _type: "BinaryCoStream";
210
+ },
213
211
  ]
214
- ? Depth extends never[]
215
- ? V
216
- : V & {
217
- byMe?: { value: UnCoNotNull<Item> };
218
- inCurrentSession?: { value: UnCoNotNull<Item> };
219
- perSession: {
220
- [key: SessionID]: { value: UnCoNotNull<Item> };
221
- };
222
- } & { [key: ID<Account>]: { value: UnCoNotNull<Item> } }
223
- : [V] extends [
224
- {
225
- _type: "BinaryCoStream";
226
- },
227
- ]
228
- ? V
229
- : never;
212
+ ? V
213
+ : never;
@@ -1,59 +1,59 @@
1
1
  import {
2
- BinaryCoStream,
3
- CoMap,
4
- co,
5
- subscriptionsScopes,
2
+ BinaryCoStream,
3
+ CoMap,
4
+ co,
5
+ subscriptionsScopes,
6
6
  } from "../../internal.js";
7
7
 
8
8
  /** @category Media */
9
9
  export class ImageDefinition extends CoMap {
10
- originalSize = co.json<[number, number]>();
11
- placeholderDataURL? = co.string;
10
+ originalSize = co.json<[number, number]>();
11
+ placeholderDataURL? = co.string;
12
12
 
13
- [co.items] = co.ref(BinaryCoStream);
14
- [res: `${number}x${number}`]: co<BinaryCoStream | null>;
13
+ [co.items] = co.ref(BinaryCoStream);
14
+ [res: `${number}x${number}`]: co<BinaryCoStream | null>;
15
15
 
16
- highestResAvailable(options?: {
17
- maxWidth?: number;
18
- }): { res: `${number}x${number}`; stream: BinaryCoStream } | undefined {
19
- if (!subscriptionsScopes.get(this)) {
20
- console.warn(
21
- "highestResAvailable() only makes sense when used within a subscription.",
22
- );
23
- }
16
+ highestResAvailable(options?: {
17
+ maxWidth?: number;
18
+ }): { res: `${number}x${number}`; stream: BinaryCoStream } | undefined {
19
+ if (!subscriptionsScopes.get(this)) {
20
+ console.warn(
21
+ "highestResAvailable() only makes sense when used within a subscription.",
22
+ );
23
+ }
24
24
 
25
- const resolutions = Object.keys(this).filter(
26
- (key) =>
27
- key.match(/^\d+x\d+$/) &&
28
- (options?.maxWidth === undefined ||
29
- Number(key.split("x")[0]) <= options.maxWidth),
30
- ) as `${number}x${number}`[];
25
+ const resolutions = Object.keys(this).filter(
26
+ (key) =>
27
+ key.match(/^\d+x\d+$/) &&
28
+ (options?.maxWidth === undefined ||
29
+ Number(key.split("x")[0]) <= options.maxWidth),
30
+ ) as `${number}x${number}`[];
31
31
 
32
- resolutions.sort((a, b) => {
33
- const aWidth = Number(a.split("x")[0]);
34
- const bWidth = Number(b.split("x")[0]);
35
- return aWidth - bWidth;
36
- });
32
+ resolutions.sort((a, b) => {
33
+ const aWidth = Number(a.split("x")[0]);
34
+ const bWidth = Number(b.split("x")[0]);
35
+ return aWidth - bWidth;
36
+ });
37
37
 
38
- let highestAvailableResolution: `${number}x${number}` | undefined;
38
+ let highestAvailableResolution: `${number}x${number}` | undefined;
39
39
 
40
- for (const resolution of resolutions) {
41
- if (this[resolution] && this[resolution]?.getChunks()) {
42
- highestAvailableResolution = resolution;
43
- } else {
44
- return (
45
- highestAvailableResolution && {
46
- res: highestAvailableResolution,
47
- stream: this[highestAvailableResolution]!,
48
- }
49
- );
50
- }
51
- }
40
+ for (const resolution of resolutions) {
41
+ if (this[resolution] && this[resolution]?.getChunks()) {
42
+ highestAvailableResolution = resolution;
43
+ } else {
52
44
  return (
53
- highestAvailableResolution && {
54
- res: highestAvailableResolution,
55
- stream: this[highestAvailableResolution]!,
56
- }
45
+ highestAvailableResolution && {
46
+ res: highestAvailableResolution,
47
+ stream: this[highestAvailableResolution]!,
48
+ }
57
49
  );
50
+ }
58
51
  }
52
+ return (
53
+ highestAvailableResolution && {
54
+ res: highestAvailableResolution,
55
+ stream: this[highestAvailableResolution]!,
56
+ }
57
+ );
58
+ }
59
59
  }