jazz-tools 0.8.15 → 0.8.17

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 +283 -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,226 +1,212 @@
1
- import type { RawAccountID, Everyone, RawGroup, Role } from "cojson";
1
+ import type { Everyone, RawAccountID, RawGroup, Role } from "cojson";
2
2
  import type {
3
- CoValue,
4
- ID,
5
- RefEncoded,
6
- Schema,
7
- CoValueClass,
8
- DeeplyLoaded,
9
- DepthsIn,
3
+ CoValue,
4
+ CoValueClass,
5
+ DeeplyLoaded,
6
+ DepthsIn,
7
+ ID,
8
+ RefEncoded,
9
+ Schema,
10
10
  } from "../internal.js";
11
11
  import {
12
- Account,
13
- CoMap,
14
- CoValueBase,
15
- Ref,
16
- co,
17
- isControlledAccount,
18
- AccountAndGroupProxyHandler,
19
- MembersSym,
20
- loadCoValue,
21
- subscribeToCoValue,
22
- ensureCoValueLoaded,
23
- subscribeToExistingCoValue,
12
+ Account,
13
+ AccountAndGroupProxyHandler,
14
+ CoMap,
15
+ CoValueBase,
16
+ MembersSym,
17
+ Ref,
18
+ co,
19
+ ensureCoValueLoaded,
20
+ isControlledAccount,
21
+ loadCoValue,
22
+ subscribeToCoValue,
23
+ subscribeToExistingCoValue,
24
24
  } from "../internal.js";
25
25
 
26
26
  /** @category Identity & Permissions */
27
27
  export class Profile extends CoMap {
28
- name = co.string;
28
+ name = co.string;
29
29
  }
30
30
 
31
31
  /** @category Identity & Permissions */
32
32
  export class Group extends CoValueBase implements CoValue {
33
- declare id: ID<this>;
34
- declare _type: "Group";
35
- static {
36
- this.prototype._type = "Group";
37
- }
38
- declare _raw: RawGroup;
39
-
40
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
- static _schema: any;
42
- get _schema(): {
43
- profile: Schema;
44
- root: Schema;
45
- [MembersSym]: RefEncoded<Account>;
46
- } {
47
- return (this.constructor as typeof Group)._schema;
48
- }
49
- static {
50
- this._schema = {
51
- profile: "json" satisfies Schema,
52
- root: "json" satisfies Schema,
53
- [MembersSym]: {
54
- ref: () => Account,
55
- optional: false,
56
- } satisfies RefEncoded<Account>,
57
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
- } as any;
59
- Object.defineProperty(this.prototype, "_schema", {
60
- get: () => this._schema,
61
- });
33
+ declare id: ID<this>;
34
+ declare _type: "Group";
35
+ static {
36
+ this.prototype._type = "Group";
37
+ }
38
+ declare _raw: RawGroup;
39
+
40
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
+ static _schema: any;
42
+ get _schema(): {
43
+ profile: Schema;
44
+ root: Schema;
45
+ [MembersSym]: RefEncoded<Account>;
46
+ } {
47
+ return (this.constructor as typeof Group)._schema;
48
+ }
49
+ static {
50
+ this._schema = {
51
+ profile: "json" satisfies Schema,
52
+ root: "json" satisfies Schema,
53
+ [MembersSym]: {
54
+ ref: () => Account,
55
+ optional: false,
56
+ } satisfies RefEncoded<Account>,
57
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
+ } as any;
59
+ Object.defineProperty(this.prototype, "_schema", {
60
+ get: () => this._schema,
61
+ });
62
+ }
63
+
64
+ declare profile: Profile | null;
65
+ declare root: CoMap | null;
66
+ declare [MembersSym]: Account | null;
67
+
68
+ get _refs(): {
69
+ profile: Ref<Profile> | undefined;
70
+ root: Ref<CoMap> | undefined;
71
+ } {
72
+ const profileID = this._raw.get("profile") as unknown as
73
+ | ID<NonNullable<this["profile"]>>
74
+ | undefined;
75
+ const rootID = this._raw.get("root") as unknown as
76
+ | ID<NonNullable<this["root"]>>
77
+ | undefined;
78
+ return {
79
+ profile:
80
+ profileID &&
81
+ (new Ref(
82
+ profileID,
83
+ this._loadedAs,
84
+ this._schema.profile as RefEncoded<NonNullable<this["profile"]>>,
85
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
86
+ ) as any as this["profile"] extends Profile
87
+ ? Ref<this["profile"]>
88
+ : never),
89
+ root:
90
+ rootID &&
91
+ (new Ref(
92
+ rootID,
93
+ this._loadedAs,
94
+ this._schema.root as RefEncoded<NonNullable<this["root"]>>,
95
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
96
+ ) as any as this["root"] extends CoMap ? Ref<this["root"]> : never),
97
+ };
98
+ }
99
+
100
+ /** @deprecated Don't use constructor directly, use .create */
101
+ constructor(options: { fromRaw: RawGroup } | { owner: Account | Group }) {
102
+ super();
103
+ let raw: RawGroup;
104
+
105
+ if (options && "fromRaw" in options) {
106
+ raw = options.fromRaw;
107
+ } else {
108
+ const initOwner = options.owner;
109
+ if (!initOwner) throw new Error("No owner provided");
110
+ if (initOwner._type === "Account" && isControlledAccount(initOwner)) {
111
+ const rawOwner = initOwner._raw;
112
+ raw = rawOwner.createGroup();
113
+ } else {
114
+ throw new Error("Can only construct group as a controlled account");
115
+ }
62
116
  }
63
117
 
64
- declare profile: Profile | null;
65
- declare root: CoMap | null;
66
- declare [MembersSym]: Account | null;
67
-
68
- get _refs(): {
69
- profile: Ref<Profile> | undefined;
70
- root: Ref<CoMap> | undefined;
71
- } {
72
- const profileID = this._raw.get("profile") as unknown as
73
- | ID<NonNullable<this["profile"]>>
74
- | undefined;
75
- const rootID = this._raw.get("root") as unknown as
76
- | ID<NonNullable<this["root"]>>
77
- | undefined;
118
+ Object.defineProperties(this, {
119
+ id: {
120
+ value: raw.id,
121
+ enumerable: false,
122
+ },
123
+ _raw: { value: raw, enumerable: false },
124
+ });
125
+
126
+ return new Proxy(this, AccountAndGroupProxyHandler as ProxyHandler<this>);
127
+ }
128
+
129
+ static create<G extends Group>(
130
+ this: CoValueClass<G>,
131
+ options: { owner: Account },
132
+ ) {
133
+ return new this(options);
134
+ }
135
+
136
+ myRole(): Role | undefined {
137
+ return this._raw.myRole();
138
+ }
139
+
140
+ addMember(member: Everyone | Account, role: Role) {
141
+ this._raw.addMember(member === "everyone" ? member : member._raw, role);
142
+ return this;
143
+ }
144
+
145
+ get members() {
146
+ return this._raw
147
+ .keys()
148
+ .filter((key) => {
149
+ return key === "everyone" || key.startsWith("co_");
150
+ })
151
+ .map((id) => {
152
+ const role = this._raw.get(id as Everyone | RawAccountID);
153
+ const accountID =
154
+ id === "everyone" ? undefined : (id as unknown as ID<Account>);
155
+ const ref =
156
+ accountID &&
157
+ new Ref<NonNullable<this[MembersSym]>>(
158
+ accountID,
159
+ this._loadedAs,
160
+ this._schema[MembersSym],
161
+ );
162
+ const accessRef = () => ref?.accessFrom(this, "members." + id);
163
+
78
164
  return {
79
- profile:
80
- profileID &&
81
- (new Ref(
82
- profileID,
83
- this._loadedAs,
84
- this._schema.profile as RefEncoded<
85
- NonNullable<this["profile"]>
86
- >,
87
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
88
- ) as any as this["profile"] extends Profile
89
- ? Ref<this["profile"]>
90
- : never),
91
- root:
92
- rootID &&
93
- (new Ref(
94
- rootID,
95
- this._loadedAs,
96
- this._schema.root as RefEncoded<NonNullable<this["root"]>>,
97
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
- ) as any as this["root"] extends CoMap
99
- ? Ref<this["root"]>
100
- : never),
165
+ id: id as unknown as Everyone | ID<this[MembersSym]>,
166
+ role,
167
+ ref,
168
+ get account() {
169
+ return accessRef();
170
+ },
101
171
  };
102
- }
103
-
104
- /** @deprecated Don't use constructor directly, use .create */
105
- constructor(options: { fromRaw: RawGroup } | { owner: Account | Group }) {
106
- super();
107
- let raw: RawGroup;
108
-
109
- if (options && "fromRaw" in options) {
110
- raw = options.fromRaw;
111
- } else {
112
- const initOwner = options.owner;
113
- if (!initOwner) throw new Error("No owner provided");
114
- if (
115
- initOwner._type === "Account" &&
116
- isControlledAccount(initOwner)
117
- ) {
118
- const rawOwner = initOwner._raw;
119
- raw = rawOwner.createGroup();
120
- } else {
121
- throw new Error(
122
- "Can only construct group as a controlled account",
123
- );
124
- }
125
- }
126
-
127
- Object.defineProperties(this, {
128
- id: {
129
- value: raw.id,
130
- enumerable: false,
131
- },
132
- _raw: { value: raw, enumerable: false },
133
- });
134
-
135
- return new Proxy(
136
- this,
137
- AccountAndGroupProxyHandler as ProxyHandler<this>,
138
- );
139
- }
140
-
141
- static create<G extends Group>(
142
- this: CoValueClass<G>,
143
- options: { owner: Account },
144
- ) {
145
- return new this(options);
146
- }
147
-
148
- myRole(): Role | undefined {
149
- return this._raw.myRole();
150
- }
151
-
152
- addMember(member: Everyone | Account, role: Role) {
153
- this._raw.addMember(member === "everyone" ? member : member._raw, role);
154
- return this;
155
- }
156
-
157
- get members() {
158
- return this._raw
159
- .keys()
160
- .filter((key) => {
161
- return key === "everyone" || key.startsWith("co_");
162
- })
163
- .map((id) => {
164
- const role = this._raw.get(id as Everyone | RawAccountID);
165
- const accountID =
166
- id === "everyone"
167
- ? undefined
168
- : (id as unknown as ID<Account>);
169
- const ref =
170
- accountID &&
171
- new Ref<NonNullable<this[MembersSym]>>(
172
- accountID,
173
- this._loadedAs,
174
- this._schema[MembersSym],
175
- );
176
- const accessRef = () => ref?.accessFrom(this, "members." + id);
177
-
178
- return {
179
- id: id as unknown as Everyone | ID<this[MembersSym]>,
180
- role,
181
- ref,
182
- get account() {
183
- return accessRef();
184
- },
185
- };
186
- });
187
- }
188
-
189
- /** @category Subscription & Loading */
190
- static load<G extends Group, Depth>(
191
- this: CoValueClass<G>,
192
- id: ID<G>,
193
- as: Account,
194
- depth: Depth & DepthsIn<G>,
195
- ): Promise<DeeplyLoaded<G, Depth> | undefined> {
196
- return loadCoValue(this, id, as, depth);
197
- }
198
-
199
- /** @category Subscription & Loading */
200
- static subscribe<G extends Group, Depth>(
201
- this: CoValueClass<G>,
202
- id: ID<G>,
203
- as: Account,
204
- depth: Depth & DepthsIn<G>,
205
- listener: (value: DeeplyLoaded<G, Depth>) => void,
206
- ): () => void {
207
- return subscribeToCoValue<G, Depth>(this, id, as, depth, listener);
208
- }
209
-
210
- /** @category Subscription & Loading */
211
- ensureLoaded<G extends Group, Depth>(
212
- this: G,
213
- depth: Depth & DepthsIn<G>,
214
- ): Promise<DeeplyLoaded<G, Depth> | undefined> {
215
- return ensureCoValueLoaded(this, depth);
216
- }
217
-
218
- /** @category Subscription & Loading */
219
- subscribe<G extends Group, Depth>(
220
- this: G,
221
- depth: Depth & DepthsIn<G>,
222
- listener: (value: DeeplyLoaded<G, Depth>) => void,
223
- ): () => void {
224
- return subscribeToExistingCoValue(this, depth, listener);
225
- }
172
+ });
173
+ }
174
+
175
+ /** @category Subscription & Loading */
176
+ static load<G extends Group, Depth>(
177
+ this: CoValueClass<G>,
178
+ id: ID<G>,
179
+ as: Account,
180
+ depth: Depth & DepthsIn<G>,
181
+ ): Promise<DeeplyLoaded<G, Depth> | undefined> {
182
+ return loadCoValue(this, id, as, depth);
183
+ }
184
+
185
+ /** @category Subscription & Loading */
186
+ static subscribe<G extends Group, Depth>(
187
+ this: CoValueClass<G>,
188
+ id: ID<G>,
189
+ as: Account,
190
+ depth: Depth & DepthsIn<G>,
191
+ listener: (value: DeeplyLoaded<G, Depth>) => void,
192
+ ): () => void {
193
+ return subscribeToCoValue<G, Depth>(this, id, as, depth, listener);
194
+ }
195
+
196
+ /** @category Subscription & Loading */
197
+ ensureLoaded<G extends Group, Depth>(
198
+ this: G,
199
+ depth: Depth & DepthsIn<G>,
200
+ ): Promise<DeeplyLoaded<G, Depth> | undefined> {
201
+ return ensureCoValueLoaded(this, depth);
202
+ }
203
+
204
+ /** @category Subscription & Loading */
205
+ subscribe<G extends Group, Depth>(
206
+ this: G,
207
+ depth: Depth & DepthsIn<G>,
208
+ listener: (value: DeeplyLoaded<G, Depth>) => void,
209
+ ): () => void {
210
+ return subscribeToExistingCoValue(this, depth, listener);
211
+ }
226
212
  }