jazz-tools 0.7.0-alpha.0 → 0.7.0-alpha.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. package/.eslintrc.cjs +8 -1
  2. package/.turbo/turbo-build.log +79 -9
  3. package/CHANGELOG.md +76 -0
  4. package/dist/coValues/account.js +171 -0
  5. package/dist/coValues/account.js.map +1 -0
  6. package/dist/coValues/coList.js +246 -0
  7. package/dist/coValues/coList.js.map +1 -0
  8. package/dist/coValues/coMap.js +235 -0
  9. package/dist/coValues/coMap.js.map +1 -0
  10. package/dist/coValues/coStream.js +263 -0
  11. package/dist/coValues/coStream.js.map +1 -0
  12. package/dist/coValues/extensions/imageDef.js +14 -11
  13. package/dist/coValues/extensions/imageDef.js.map +1 -1
  14. package/dist/coValues/group.js +71 -0
  15. package/dist/coValues/group.js.map +1 -0
  16. package/dist/coValues/interfaces.js +61 -0
  17. package/dist/coValues/interfaces.js.map +1 -0
  18. package/dist/implementation/errors.js.map +1 -0
  19. package/dist/implementation/inspect.js +2 -0
  20. package/dist/implementation/inspect.js.map +1 -0
  21. package/dist/{refs.js → implementation/refs.js} +16 -15
  22. package/dist/implementation/refs.js.map +1 -0
  23. package/dist/implementation/schema.js +41 -0
  24. package/dist/implementation/schema.js.map +1 -0
  25. package/dist/{subscriptionScope.js → implementation/subscriptionScope.js} +1 -1
  26. package/dist/implementation/subscriptionScope.js.map +1 -0
  27. package/dist/index.js +10 -29
  28. package/dist/index.js.map +1 -1
  29. package/dist/internal.js +13 -0
  30. package/dist/internal.js.map +1 -0
  31. package/dist/tests/coList.test.js +22 -22
  32. package/dist/tests/coList.test.js.map +1 -1
  33. package/dist/tests/coMap.test.js +140 -46
  34. package/dist/tests/coMap.test.js.map +1 -1
  35. package/dist/tests/coStream.test.js +55 -49
  36. package/dist/tests/coStream.test.js.map +1 -1
  37. package/package.json +11 -4
  38. package/src/coValues/account.ts +327 -0
  39. package/src/coValues/coList.ts +391 -0
  40. package/src/coValues/coMap.ts +413 -0
  41. package/src/coValues/coStream.ts +404 -0
  42. package/src/coValues/extensions/imageDef.ts +20 -16
  43. package/src/coValues/group.ts +139 -0
  44. package/src/coValues/interfaces.ts +218 -0
  45. package/src/implementation/inspect.ts +2 -0
  46. package/src/{refs.ts → implementation/refs.ts} +53 -39
  47. package/src/implementation/schema.ts +124 -0
  48. package/src/{subscriptionScope.ts → implementation/subscriptionScope.ts} +11 -13
  49. package/src/index.ts +14 -57
  50. package/src/internal.ts +15 -0
  51. package/src/tests/coList.test.ts +23 -25
  52. package/src/tests/coMap.test.ts +140 -53
  53. package/src/tests/coStream.test.ts +76 -68
  54. package/dist/coValueInterfaces.js +0 -8
  55. package/dist/coValueInterfaces.js.map +0 -1
  56. package/dist/coValues/account/account.js +0 -11
  57. package/dist/coValues/account/account.js.map +0 -1
  58. package/dist/coValues/account/accountOf.js +0 -150
  59. package/dist/coValues/account/accountOf.js.map +0 -1
  60. package/dist/coValues/account/migration.js +0 -4
  61. package/dist/coValues/account/migration.js.map +0 -1
  62. package/dist/coValues/coList/coList.js +0 -2
  63. package/dist/coValues/coList/coList.js.map +0 -1
  64. package/dist/coValues/coList/coListOf.js +0 -235
  65. package/dist/coValues/coList/coListOf.js.map +0 -1
  66. package/dist/coValues/coList/internalDocs.js +0 -2
  67. package/dist/coValues/coList/internalDocs.js.map +0 -1
  68. package/dist/coValues/coMap/coMap.js +0 -2
  69. package/dist/coValues/coMap/coMap.js.map +0 -1
  70. package/dist/coValues/coMap/coMapOf.js +0 -262
  71. package/dist/coValues/coMap/coMapOf.js.map +0 -1
  72. package/dist/coValues/coMap/internalDocs.js +0 -2
  73. package/dist/coValues/coMap/internalDocs.js.map +0 -1
  74. package/dist/coValues/coStream/coStream.js +0 -2
  75. package/dist/coValues/coStream/coStream.js.map +0 -1
  76. package/dist/coValues/coStream/coStreamOf.js +0 -244
  77. package/dist/coValues/coStream/coStreamOf.js.map +0 -1
  78. package/dist/coValues/construction.js +0 -34
  79. package/dist/coValues/construction.js.map +0 -1
  80. package/dist/coValues/group/group.js +0 -2
  81. package/dist/coValues/group/group.js.map +0 -1
  82. package/dist/coValues/group/groupOf.js +0 -109
  83. package/dist/coValues/group/groupOf.js.map +0 -1
  84. package/dist/coValues/resolution.js +0 -66
  85. package/dist/coValues/resolution.js.map +0 -1
  86. package/dist/errors.js.map +0 -1
  87. package/dist/refs.js.map +0 -1
  88. package/dist/schemaHelpers.js +0 -14
  89. package/dist/schemaHelpers.js.map +0 -1
  90. package/dist/subscriptionScope.js.map +0 -1
  91. package/dist/tests/types.test.js +0 -33
  92. package/dist/tests/types.test.js.map +0 -1
  93. package/src/coValueInterfaces.ts +0 -105
  94. package/src/coValues/account/account.ts +0 -106
  95. package/src/coValues/account/accountOf.ts +0 -284
  96. package/src/coValues/account/migration.ts +0 -12
  97. package/src/coValues/coList/coList.ts +0 -57
  98. package/src/coValues/coList/coListOf.ts +0 -377
  99. package/src/coValues/coList/internalDocs.ts +0 -1
  100. package/src/coValues/coMap/coMap.ts +0 -110
  101. package/src/coValues/coMap/coMapOf.ts +0 -451
  102. package/src/coValues/coMap/internalDocs.ts +0 -1
  103. package/src/coValues/coStream/coStream.ts +0 -63
  104. package/src/coValues/coStream/coStreamOf.ts +0 -404
  105. package/src/coValues/construction.ts +0 -110
  106. package/src/coValues/group/group.ts +0 -27
  107. package/src/coValues/group/groupOf.ts +0 -183
  108. package/src/coValues/resolution.ts +0 -111
  109. package/src/schemaHelpers.ts +0 -72
  110. package/src/tests/types.test.ts +0 -37
  111. /package/dist/{errors.js → implementation/errors.js} +0 -0
  112. /package/src/{errors.ts → implementation/errors.ts} +0 -0
@@ -0,0 +1,404 @@
1
+ import type {
2
+ AccountID,
3
+ AgentID,
4
+ BinaryStreamInfo,
5
+ CojsonInternalTypes,
6
+ JsonValue,
7
+ RawBinaryCoStream,
8
+ RawCoStream,
9
+ SessionID,
10
+ } from "cojson";
11
+ import { cojsonInternals } from "cojson";
12
+ import type {
13
+ CoValue,
14
+ ValidItem,
15
+ Schema,
16
+ SchemaFor,
17
+ Group,
18
+ ID,
19
+ Me,
20
+ IfCo,
21
+ } from "../internal.js";
22
+ import {
23
+ ItemsSym,
24
+ Account,
25
+ CoValueBase,
26
+ Ref,
27
+ inspect,
28
+ co,
29
+ InitValues,
30
+ SchemaInit,
31
+ isRefEncoded,
32
+ } from "../internal.js";
33
+ import { Schema as EffectSchema } from "@effect/schema";
34
+
35
+ export type CoStreamEntry<Item> = {
36
+ value: NonNullable<Item> extends CoValue ? NonNullable<Item> | null : Item;
37
+ ref?: NonNullable<Item> extends CoValue ? Ref<NonNullable<Item>> : never;
38
+ by?: Account;
39
+ madeAt: Date;
40
+ tx: CojsonInternalTypes.TransactionID;
41
+ };
42
+
43
+ export class CoStream<Item extends ValidItem<Item, "CoStream"> = any>
44
+ extends CoValueBase
45
+ implements CoValue<"CoStream", RawCoStream>
46
+ {
47
+ static Of<Item extends ValidItem<Item, "CoStream"> = any>(
48
+ item: IfCo<Item, Item>
49
+ ): typeof CoStream<Item> {
50
+ return class CoStreamOf extends CoStream<Item> {
51
+ [co.items] = item;
52
+ };
53
+ }
54
+
55
+ declare id: ID<this>;
56
+ declare _type: "CoStream";
57
+ static {
58
+ this.prototype._type = "CoStream";
59
+ }
60
+ declare _raw: RawCoStream;
61
+
62
+ /** @internal This is only a marker type and doesn't exist at runtime */
63
+ [ItemsSym]!: Item;
64
+ static _schema: any;
65
+ get _schema(): {
66
+ [ItemsSym]: SchemaFor<Item>;
67
+ } {
68
+ return (this.constructor as typeof CoStream)._schema;
69
+ }
70
+
71
+ [key: ID<Account>]: CoStreamEntry<Item>;
72
+
73
+ get byMe(): CoStreamEntry<Item> | undefined {
74
+ return this[this._loadedAs.id];
75
+ }
76
+ perSession!: {
77
+ [key: SessionID]: CoStreamEntry<Item>;
78
+ };
79
+ get inCurrentSession(): CoStreamEntry<Item> | undefined {
80
+ return this.perSession[this._loadedAs.sessionID];
81
+ }
82
+
83
+ [InitValues]?: {
84
+ init?: Item[];
85
+ owner: Account | Group;
86
+ };
87
+
88
+ constructor(_init: undefined, options: { fromRaw: RawCoStream });
89
+ constructor(init: Item[], options: { owner: Account | Group });
90
+ constructor(
91
+ init: Item[] | undefined,
92
+ options: { owner: Account | Group } | { fromRaw: RawCoStream }
93
+ ) {
94
+ super();
95
+
96
+ if ("fromRaw" in options) {
97
+ Object.defineProperties(this, {
98
+ id: {
99
+ value: options.fromRaw.id,
100
+ enumerable: false,
101
+ },
102
+ _raw: { value: options.fromRaw, enumerable: false },
103
+ });
104
+ } else {
105
+ this[InitValues] = {
106
+ init,
107
+ owner: options.owner,
108
+ };
109
+ }
110
+
111
+ return new Proxy(this, CoStreamProxyHandler as ProxyHandler<this>);
112
+ }
113
+
114
+ push(...items: Item[]) {
115
+ for (const item of items) {
116
+ this.pushItem(item);
117
+ }
118
+ }
119
+
120
+ private pushItem(item: Item) {
121
+ const itemDescriptor = this._schema[ItemsSym] as Schema;
122
+
123
+ if (itemDescriptor === "json") {
124
+ this._raw.push(item as JsonValue);
125
+ } else if ("encoded" in itemDescriptor) {
126
+ this._raw.push(EffectSchema.encodeSync(itemDescriptor.encoded)(item));
127
+ } else if (isRefEncoded(itemDescriptor)) {
128
+ this._raw.push((item as unknown as CoValue).id);
129
+ }
130
+ }
131
+
132
+ toJSON() {
133
+ const itemDescriptor = this._schema[ItemsSym] as Schema;
134
+ const mapper =
135
+ itemDescriptor === "json"
136
+ ? (v: unknown) => v
137
+ : "encoded" in itemDescriptor
138
+ ? EffectSchema.encodeSync(itemDescriptor.encoded)
139
+ : (v: unknown) => v && (v as CoValue).id;
140
+
141
+ return {
142
+ id: this.id,
143
+ _type: this._type,
144
+ ...Object.fromEntries(
145
+ Object.entries(this).map(([account, entry]) => [
146
+ account,
147
+ mapper(entry.value),
148
+ ])
149
+ ),
150
+ in: Object.fromEntries(
151
+ Object.entries(this.perSession).map(([session, entry]) => [
152
+ session,
153
+ mapper(entry.value),
154
+ ])
155
+ ),
156
+ };
157
+ }
158
+
159
+ [inspect]() {
160
+ return this.toJSON();
161
+ }
162
+
163
+ static schema<V extends CoStream>(
164
+ this: { new (...args: any): V } & typeof CoStream,
165
+ def: { [ItemsSym]: V["_schema"][ItemsSym] }
166
+ ) {
167
+ this._schema ||= {};
168
+ Object.assign(this._schema, def);
169
+ }
170
+ }
171
+
172
+ function entryFromRawEntry<Item>(
173
+ accessFrom: CoValue,
174
+ rawEntry: {
175
+ by: AccountID | AgentID;
176
+ tx: CojsonInternalTypes.TransactionID;
177
+ at: Date;
178
+ value: JsonValue;
179
+ },
180
+ loadedAs: Account & Me,
181
+ accountID: ID<Account> | undefined,
182
+ itemField: Schema
183
+ ) {
184
+ return {
185
+ get value(): Item | undefined {
186
+ if (itemField === "json") {
187
+ return rawEntry.value as Item;
188
+ } else if ("encoded" in itemField) {
189
+ return EffectSchema.decodeSync(itemField.encoded)(rawEntry.value);
190
+ } else if (isRefEncoded(itemField)) {
191
+ return this.ref?.accessFrom(accessFrom) as Item;
192
+ }
193
+ },
194
+ get ref() {
195
+ if (itemField !== "json" && isRefEncoded(itemField)) {
196
+ const rawId = rawEntry.value;
197
+ return new Ref(
198
+ rawId as unknown as ID<CoValue>,
199
+ loadedAs,
200
+ itemField
201
+ );
202
+ }
203
+ },
204
+ get by() {
205
+ return (
206
+ accountID &&
207
+ new Ref(
208
+ accountID as unknown as ID<Account>,
209
+ loadedAs,
210
+ Account
211
+ )?.accessFrom(accessFrom)
212
+ );
213
+ },
214
+ madeAt: rawEntry.at,
215
+ tx: rawEntry.tx,
216
+ };
217
+ }
218
+
219
+ function init(stream: CoStream) {
220
+ const init = stream[InitValues];
221
+ if (!init) return;
222
+
223
+ const raw = init.owner._raw.createStream();
224
+
225
+ Object.defineProperties(stream, {
226
+ id: {
227
+ value: raw.id,
228
+ enumerable: false,
229
+ },
230
+ _raw: { value: raw, enumerable: false },
231
+ });
232
+
233
+ if (init.init) {
234
+ stream.push(...init.init);
235
+ }
236
+
237
+ delete stream[InitValues];
238
+ }
239
+
240
+ export const CoStreamProxyHandler: ProxyHandler<CoStream> = {
241
+ get(target, key, receiver) {
242
+ if (typeof key === "string" && key.startsWith("co_")) {
243
+ const rawEntry = target._raw.lastItemBy(key as AccountID);
244
+
245
+ if (!rawEntry) return;
246
+ return entryFromRawEntry(
247
+ receiver,
248
+ rawEntry,
249
+ target._loadedAs,
250
+ key as unknown as ID<Account>,
251
+ target._schema[ItemsSym]
252
+ );
253
+ } else if (key === "perSession") {
254
+ return new Proxy(receiver, CoStreamPerSessionProxyHandler);
255
+ } else {
256
+ return Reflect.get(target, key, receiver);
257
+ }
258
+ },
259
+ set(target, key, value, receiver) {
260
+ if (
261
+ key === ItemsSym &&
262
+ typeof value === "object" &&
263
+ SchemaInit in value
264
+ ) {
265
+ (target.constructor as typeof CoStream)._schema ||= {};
266
+ (target.constructor as typeof CoStream)._schema[ItemsSym] =
267
+ value[SchemaInit];
268
+ init(target);
269
+ return true;
270
+ } else {
271
+ return Reflect.set(target, key, value, receiver);
272
+ }
273
+ },
274
+ defineProperty(target, key, descriptor) {
275
+ if (
276
+ descriptor.value &&
277
+ key === ItemsSym &&
278
+ typeof descriptor.value === "object" &&
279
+ SchemaInit in descriptor.value
280
+ ) {
281
+ (target.constructor as typeof CoStream)._schema ||= {};
282
+ (target.constructor as typeof CoStream)._schema[ItemsSym] =
283
+ descriptor.value[SchemaInit];
284
+ init(target);
285
+ return true;
286
+ } else {
287
+ return Reflect.defineProperty(target, key, descriptor);
288
+ }
289
+ },
290
+ ownKeys(target) {
291
+ const keys = Reflect.ownKeys(target);
292
+
293
+ for (const accountID of target._raw.accounts()) {
294
+ keys.push(accountID);
295
+ }
296
+
297
+ return keys;
298
+ },
299
+ getOwnPropertyDescriptor(target, key) {
300
+ if (typeof key === "string" && key.startsWith("co_")) {
301
+ return {
302
+ configurable: true,
303
+ enumerable: true,
304
+ writable: false,
305
+ };
306
+ } else {
307
+ return Reflect.getOwnPropertyDescriptor(target, key);
308
+ }
309
+ },
310
+ };
311
+
312
+ const CoStreamPerSessionProxyHandler: ProxyHandler<CoStream> = {
313
+ get(target, key, receiver) {
314
+ if (typeof key === "string" && key.includes("session")) {
315
+ const sessionID = key as SessionID;
316
+ const rawEntry = target._raw.lastItemIn(sessionID);
317
+
318
+ if (!rawEntry) return;
319
+ const by = cojsonInternals.accountOrAgentIDfromSessionID(sessionID);
320
+ return entryFromRawEntry(
321
+ target,
322
+ rawEntry,
323
+ target._loadedAs,
324
+ cojsonInternals.isAccountID(by)
325
+ ? (by as unknown as ID<Account>)
326
+ : undefined,
327
+ target._schema[ItemsSym]
328
+ );
329
+ } else {
330
+ return Reflect.get(target, key, receiver);
331
+ }
332
+ },
333
+ };
334
+
335
+ export class BinaryCoStream
336
+ extends CoValueBase
337
+ implements CoValue<"BinaryCoStream", RawBinaryCoStream>
338
+ {
339
+ declare id: ID<this>;
340
+ declare _type: "BinaryCoStream";
341
+ declare _raw: RawBinaryCoStream;
342
+
343
+ constructor(
344
+ _init: [] | undefined,
345
+ options:
346
+ | {
347
+ owner: Account | Group;
348
+ }
349
+ | {
350
+ fromRaw: RawBinaryCoStream;
351
+ }
352
+ ) {
353
+ super();
354
+
355
+ let raw: RawBinaryCoStream;
356
+
357
+ if ("fromRaw" in options) {
358
+ raw = options.fromRaw;
359
+ } else {
360
+ const rawOwner = options.owner._raw;
361
+ raw = rawOwner.createBinaryStream();
362
+ }
363
+
364
+ Object.defineProperties(this, {
365
+ id: {
366
+ value: raw.id,
367
+ enumerable: false,
368
+ },
369
+ _raw: { value: raw, enumerable: false },
370
+ });
371
+ }
372
+
373
+ getChunks(options?: {
374
+ allowUnfinished?: boolean;
375
+ }):
376
+ | (BinaryStreamInfo & { chunks: Uint8Array[]; finished: boolean })
377
+ | undefined {
378
+ return this._raw.getBinaryChunks(options?.allowUnfinished);
379
+ }
380
+
381
+ start(options: BinaryStreamInfo): void {
382
+ this._raw.startBinaryStream(options);
383
+ }
384
+
385
+ push(data: Uint8Array): void {
386
+ this._raw.pushBinaryStreamChunk(data);
387
+ }
388
+
389
+ end(): void {
390
+ this._raw.endBinaryStream();
391
+ }
392
+
393
+ toJSON() {
394
+ return {
395
+ id: this.id,
396
+ _type: this._type,
397
+ ...this.getChunks(),
398
+ };
399
+ }
400
+
401
+ [inspect]() {
402
+ return this.toJSON();
403
+ }
404
+ }
@@ -1,19 +1,17 @@
1
- import * as S from "@effect/schema/Schema";
2
- import { CoMapOf } from "../coMap/coMapOf.js";
3
- import { BinaryCoStreamImpl } from "../coStream/coStreamOf.js";
4
- import { BinaryCoStream } from "../coStream/coStream.js";
5
- import { subscriptionsScopes } from "../../subscriptionScope.js";
1
+ import {
2
+ BinaryCoStream,
3
+ CoMap,
4
+ co,
5
+ subscriptionsScopes,
6
+ } from "../../internal.js";
7
+
8
+ export class ImageDefinition extends CoMap<ImageDefinition> {
9
+ originalSize = co.json<[number, number]>();
10
+ placeholderDataURL? = co.string;
11
+
12
+ [co.items] = co.ref(BinaryCoStream);
13
+ [res: `${number}x${number}`]: co<BinaryCoStream | null>;
6
14
 
7
- export class ImageDefinition extends CoMapOf(
8
- {
9
- originalSize: S.tuple(S.number, S.number),
10
- placeholderDataURL: S.optional(S.string),
11
- },
12
- {
13
- key: S.templateLiteral(S.string, S.literal("x"), S.string),
14
- value: BinaryCoStreamImpl,
15
- }
16
- ).as<ImageDefinition>() {
17
15
  get highestResAvailable():
18
16
  | { res: `${number}x${number}`; stream: BinaryCoStream }
19
17
  | undefined {
@@ -47,5 +45,11 @@ export class ImageDefinition extends CoMapOf(
47
45
  );
48
46
  }
49
47
  }
48
+ return (
49
+ highestAvailableResolution && {
50
+ res: highestAvailableResolution,
51
+ stream: this[highestAvailableResolution]!,
52
+ }
53
+ );
50
54
  }
51
- }
55
+ }
@@ -0,0 +1,139 @@
1
+ import type { Everyone, RawGroup, Role } from "cojson";
2
+ import type { CoValue, ID, JsonEncoded, RefEncoded } from "../internal.js";
3
+ import {
4
+ Account,
5
+ CoMap,
6
+ CoValueBase,
7
+ Ref,
8
+ co,
9
+ isControlledAccount,
10
+ AccountAndGroupProxyHandler,
11
+ } from "../internal.js";
12
+
13
+ export class Profile extends CoMap<{ name: co<string> }> {
14
+ name = co.string;
15
+ }
16
+
17
+ export class Group<
18
+ Def extends { profile: Profile | null; root: CoMap | null } = {
19
+ profile: Profile | null;
20
+ root: CoMap | null;
21
+ },
22
+ >
23
+ extends CoValueBase
24
+ implements CoValue<"Group", RawGroup>
25
+ {
26
+ declare id: ID<this>;
27
+ declare _type: "Group";
28
+ static {
29
+ this.prototype._type = "Group";
30
+ }
31
+ declare _raw: RawGroup;
32
+
33
+ static _schema: any;
34
+ get _schema(): {
35
+ profile: Def["profile"] extends CoValue
36
+ ? RefEncoded<Def["profile"]>
37
+ : JsonEncoded;
38
+ root: Def["root"] extends CoValue
39
+ ? RefEncoded<Def["root"]>
40
+ : JsonEncoded;
41
+ } {
42
+ return (this.constructor as typeof Group)._schema;
43
+ }
44
+ static {
45
+ this._schema = {
46
+ profile: { json: true },
47
+ root: { json: true },
48
+ } as any;
49
+ Object.defineProperty(this.prototype, "_schema", {
50
+ get: () => this._schema,
51
+ });
52
+ }
53
+
54
+ profile!: Def["profile"] extends Profile
55
+ ? Def["profile"] | null
56
+ : undefined;
57
+
58
+ root!: Def["root"] extends CoMap ? Def["root"] | null : undefined;
59
+
60
+ get _refs(): {
61
+ profile: Def["profile"] extends Profile ? Ref<Def["profile"]> : never;
62
+ root: Def["root"] extends CoMap ? Ref<Def["root"]> : never;
63
+ } {
64
+ const profileID = this._raw.get("profile") as unknown as
65
+ | ID<NonNullable<Def["profile"]>>
66
+ | undefined;
67
+ const rootID = this._raw.get("root") as unknown as
68
+ | ID<NonNullable<Def["root"]>>
69
+ | undefined;
70
+ return {
71
+ profile:
72
+ profileID &&
73
+ (new Ref(
74
+ profileID,
75
+ this._loadedAs,
76
+ this._schema.profile as RefEncoded<
77
+ NonNullable<Def["profile"]>
78
+ >
79
+ ) as any),
80
+ root:
81
+ rootID &&
82
+ (new Ref(
83
+ rootID,
84
+ this._loadedAs,
85
+ this._schema.root as RefEncoded<NonNullable<Def["root"]>>
86
+ ) as any),
87
+ };
88
+ }
89
+
90
+ constructor(options: { owner: Account | Group });
91
+ constructor(init: any, options: { fromRaw: RawGroup });
92
+ constructor(init: undefined, options: { owner: Account | Group });
93
+ constructor(
94
+ init: undefined | { owner: Account | Group },
95
+ options?: { fromRaw: RawGroup } | { owner: Account | Group }
96
+ ) {
97
+ super();
98
+ let raw: RawGroup;
99
+
100
+ if (options && "fromRaw" in options) {
101
+ raw = options.fromRaw;
102
+ } else {
103
+ const initOwner = options?.owner || init?.owner;
104
+ if (!initOwner) throw new Error("No owner provided");
105
+ if (
106
+ initOwner instanceof Account &&
107
+ isControlledAccount(initOwner)
108
+ ) {
109
+ const rawOwner = initOwner._raw;
110
+ raw = rawOwner.createGroup();
111
+ } else {
112
+ throw new Error(
113
+ "Can only construct group as a controlled account"
114
+ );
115
+ }
116
+ }
117
+
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(
127
+ this,
128
+ AccountAndGroupProxyHandler as ProxyHandler<this>
129
+ );
130
+ }
131
+
132
+ myRole(): Role | undefined {
133
+ return this._raw.myRole();
134
+ }
135
+
136
+ addMember(member: Everyone | Account, role: Role) {
137
+ this._raw.addMember(member === "everyone" ? member : member._raw, role);
138
+ }
139
+ }