jazz-tools 0.8.14 → 0.8.16
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +281 -268
- package/dist/native/coValues/account.js +3 -6
- package/dist/native/coValues/account.js.map +1 -1
- package/dist/native/coValues/coList.js +3 -7
- package/dist/native/coValues/coList.js.map +1 -1
- package/dist/native/coValues/coMap.js +8 -14
- package/dist/native/coValues/coMap.js.map +1 -1
- package/dist/native/coValues/coStream.js +7 -12
- package/dist/native/coValues/coStream.js.map +1 -1
- package/dist/native/coValues/deepLoading.js +6 -3
- package/dist/native/coValues/deepLoading.js.map +1 -1
- package/dist/native/coValues/extensions/imageDef.js.map +1 -1
- package/dist/native/coValues/group.js +3 -6
- package/dist/native/coValues/group.js.map +1 -1
- package/dist/native/coValues/interfaces.js +4 -3
- package/dist/native/coValues/interfaces.js.map +1 -1
- package/dist/native/exports.js +3 -9
- package/dist/native/exports.js.map +1 -1
- package/dist/native/implementation/createContext.js +7 -0
- package/dist/native/implementation/createContext.js.map +1 -1
- package/dist/native/implementation/devtoolsFormatters.js +5 -25
- package/dist/native/implementation/devtoolsFormatters.js.map +1 -1
- package/dist/native/implementation/refs.js +1 -2
- package/dist/native/implementation/refs.js.map +1 -1
- package/dist/native/implementation/schema.js +1 -1
- package/dist/native/implementation/schema.js.map +1 -1
- package/dist/native/implementation/subscriptionScope.js +2 -4
- package/dist/native/implementation/subscriptionScope.js.map +1 -1
- package/dist/native/index.native.js +1 -1
- package/dist/native/index.native.js.map +1 -1
- package/dist/native/lib/cache.js.map +1 -1
- package/dist/native/lib/cache.test.js +1 -1
- package/dist/native/lib/cache.test.js.map +1 -1
- package/dist/web/coValues/account.js +3 -6
- package/dist/web/coValues/account.js.map +1 -1
- package/dist/web/coValues/coList.js +3 -7
- package/dist/web/coValues/coList.js.map +1 -1
- package/dist/web/coValues/coMap.js +8 -14
- package/dist/web/coValues/coMap.js.map +1 -1
- package/dist/web/coValues/coStream.js +7 -12
- package/dist/web/coValues/coStream.js.map +1 -1
- package/dist/web/coValues/deepLoading.js +6 -3
- package/dist/web/coValues/deepLoading.js.map +1 -1
- package/dist/web/coValues/extensions/imageDef.js.map +1 -1
- package/dist/web/coValues/group.js +3 -6
- package/dist/web/coValues/group.js.map +1 -1
- package/dist/web/coValues/interfaces.js +4 -3
- package/dist/web/coValues/interfaces.js.map +1 -1
- package/dist/web/exports.js +3 -9
- package/dist/web/exports.js.map +1 -1
- package/dist/web/implementation/createContext.js +7 -0
- package/dist/web/implementation/createContext.js.map +1 -1
- package/dist/web/implementation/devtoolsFormatters.js +5 -25
- package/dist/web/implementation/devtoolsFormatters.js.map +1 -1
- package/dist/web/implementation/refs.js +1 -2
- package/dist/web/implementation/refs.js.map +1 -1
- package/dist/web/implementation/schema.js +1 -1
- package/dist/web/implementation/schema.js.map +1 -1
- package/dist/web/implementation/subscriptionScope.js +2 -4
- package/dist/web/implementation/subscriptionScope.js.map +1 -1
- package/dist/web/lib/cache.js.map +1 -1
- package/dist/web/lib/cache.test.js +1 -1
- package/dist/web/lib/cache.test.js.map +1 -1
- package/package.json +5 -9
- package/src/coValues/account.ts +330 -339
- package/src/coValues/coList.ts +474 -495
- package/src/coValues/coMap.ts +584 -604
- package/src/coValues/coStream.ts +624 -650
- package/src/coValues/deepLoading.ts +184 -200
- package/src/coValues/extensions/imageDef.ts +44 -44
- package/src/coValues/group.ts +196 -210
- package/src/coValues/interfaces.ts +197 -199
- package/src/exports.ts +38 -25
- package/src/implementation/createContext.ts +210 -204
- package/src/implementation/devtoolsFormatters.ts +80 -100
- package/src/implementation/refs.ts +127 -139
- package/src/implementation/schema.ts +124 -128
- package/src/implementation/subscriptionScope.ts +111 -121
- package/src/index.native.ts +3 -3
- package/src/lib/cache.test.ts +48 -48
- package/src/lib/cache.ts +9 -9
- package/src/tests/coList.test.ts +264 -283
- package/src/tests/coMap.test.ts +741 -761
- package/src/tests/coStream.test.ts +405 -438
- package/src/tests/deepLoading.test.ts +251 -256
- package/src/tests/groupsAndAccounts.test.ts +70 -74
- package/src/tests/schema.test.ts +198 -198
- package/src/tests/subscribe.test.ts +312 -299
- package/tsconfig.json +2 -4
- package/tsconfig.native.json +4 -10
- package/tsconfig.web.json +4 -10
- package/.eslintrc.cjs +0 -24
- package/.prettierrc.js +0 -9
package/src/coValues/account.ts
CHANGED
@@ -1,375 +1,366 @@
|
|
1
|
-
import { LocalNode, cojsonInternals } from "cojson";
|
2
1
|
import {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
2
|
+
AgentSecret,
|
3
|
+
CoID,
|
4
|
+
CryptoProvider,
|
5
|
+
InviteSecret,
|
6
|
+
LocalNode,
|
7
|
+
Peer,
|
8
|
+
RawAccount,
|
9
|
+
RawCoMap,
|
10
|
+
RawCoValue,
|
11
|
+
RawControlledAccount,
|
12
|
+
SessionID,
|
13
|
+
cojsonInternals,
|
13
14
|
} from "cojson";
|
14
15
|
import {
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
ensureCoValueLoaded,
|
38
|
-
subscribeToExistingCoValue,
|
16
|
+
AnonymousJazzAgent,
|
17
|
+
CoMap,
|
18
|
+
type CoValue,
|
19
|
+
CoValueBase,
|
20
|
+
CoValueClass,
|
21
|
+
DeeplyLoaded,
|
22
|
+
DepthsIn,
|
23
|
+
Group,
|
24
|
+
ID,
|
25
|
+
MembersSym,
|
26
|
+
Profile,
|
27
|
+
Ref,
|
28
|
+
type RefEncoded,
|
29
|
+
RefIfCoValue,
|
30
|
+
type Schema,
|
31
|
+
SchemaInit,
|
32
|
+
ensureCoValueLoaded,
|
33
|
+
inspect,
|
34
|
+
loadCoValue,
|
35
|
+
subscribeToCoValue,
|
36
|
+
subscribeToExistingCoValue,
|
37
|
+
subscriptionsScopes,
|
39
38
|
} from "../internal.js";
|
40
39
|
import { coValuesCache } from "../lib/cache.js";
|
41
40
|
|
42
41
|
/** @category Identity & Permissions */
|
43
42
|
export class Account extends CoValueBase implements CoValue {
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
43
|
+
declare id: ID<this>;
|
44
|
+
declare _type: "Account";
|
45
|
+
declare _raw: RawAccount | RawControlledAccount;
|
46
|
+
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
48
|
+
static _schema: any;
|
49
|
+
get _schema(): {
|
50
|
+
profile: Schema;
|
51
|
+
root: Schema;
|
52
|
+
} {
|
53
|
+
return (this.constructor as typeof Account)._schema;
|
54
|
+
}
|
55
|
+
static {
|
56
|
+
this._schema = {
|
57
|
+
profile: {
|
58
|
+
ref: () => Profile,
|
59
|
+
optional: false,
|
60
|
+
} satisfies RefEncoded<Profile>,
|
61
|
+
root: {
|
62
|
+
ref: () => CoMap,
|
63
|
+
optional: true,
|
64
|
+
} satisfies RefEncoded<CoMap>,
|
65
|
+
};
|
66
|
+
}
|
67
|
+
|
68
|
+
get _owner(): Account {
|
69
|
+
return this as Account;
|
70
|
+
}
|
71
|
+
get _loadedAs(): Account | AnonymousJazzAgent {
|
72
|
+
if (this.isMe) return this;
|
73
|
+
|
74
|
+
const rawAccount = this._raw.core.node.account;
|
75
|
+
|
76
|
+
if (rawAccount instanceof RawAccount) {
|
77
|
+
return coValuesCache.get(rawAccount, () => Account.fromRaw(rawAccount));
|
71
78
|
}
|
72
|
-
get _loadedAs(): Account | AnonymousJazzAgent {
|
73
|
-
if (this.isMe) return this;
|
74
|
-
|
75
|
-
const rawAccount = this._raw.core.node.account;
|
76
|
-
|
77
|
-
if (rawAccount instanceof RawAccount) {
|
78
|
-
return coValuesCache.get(rawAccount, () => Account.fromRaw(rawAccount)) ;
|
79
|
-
}
|
80
79
|
|
81
|
-
|
80
|
+
return new AnonymousJazzAgent(this._raw.core.node);
|
81
|
+
}
|
82
|
+
|
83
|
+
declare profile: Profile | null;
|
84
|
+
declare root: CoMap | null;
|
85
|
+
|
86
|
+
get _refs(): {
|
87
|
+
profile: RefIfCoValue<Profile> | undefined;
|
88
|
+
root: RefIfCoValue<CoMap> | undefined;
|
89
|
+
} {
|
90
|
+
const profileID = this._raw.get("profile") as unknown as
|
91
|
+
| ID<NonNullable<this["profile"]>>
|
92
|
+
| undefined;
|
93
|
+
const rootID = this._raw.get("root") as unknown as
|
94
|
+
| ID<NonNullable<this["root"]>>
|
95
|
+
| undefined;
|
96
|
+
|
97
|
+
return {
|
98
|
+
profile:
|
99
|
+
profileID &&
|
100
|
+
(new Ref(
|
101
|
+
profileID,
|
102
|
+
this._loadedAs,
|
103
|
+
this._schema.profile as RefEncoded<
|
104
|
+
NonNullable<this["profile"]> & CoValue
|
105
|
+
>,
|
106
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
107
|
+
) as any as RefIfCoValue<this["profile"]>),
|
108
|
+
root:
|
109
|
+
rootID &&
|
110
|
+
(new Ref(
|
111
|
+
rootID,
|
112
|
+
this._loadedAs,
|
113
|
+
this._schema.root as RefEncoded<NonNullable<this["root"]> & CoValue>,
|
114
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
115
|
+
) as any as RefIfCoValue<this["root"]>),
|
116
|
+
};
|
117
|
+
}
|
118
|
+
|
119
|
+
isMe: boolean;
|
120
|
+
sessionID: SessionID | undefined;
|
121
|
+
|
122
|
+
constructor(options: { fromRaw: RawAccount | RawControlledAccount }) {
|
123
|
+
super();
|
124
|
+
if (!("fromRaw" in options)) {
|
125
|
+
throw new Error("Can only construct account from raw or with .create()");
|
82
126
|
}
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
| undefined;
|
97
|
-
|
98
|
-
return {
|
99
|
-
profile:
|
100
|
-
profileID &&
|
101
|
-
(new Ref(
|
102
|
-
profileID,
|
103
|
-
this._loadedAs,
|
104
|
-
this._schema.profile as RefEncoded<
|
105
|
-
NonNullable<this["profile"]> & CoValue
|
106
|
-
>,
|
107
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
108
|
-
) as any as RefIfCoValue<this["profile"]>),
|
109
|
-
root:
|
110
|
-
rootID &&
|
111
|
-
(new Ref(
|
112
|
-
rootID,
|
113
|
-
this._loadedAs,
|
114
|
-
this._schema.root as RefEncoded<
|
115
|
-
NonNullable<this["root"]> & CoValue
|
116
|
-
>,
|
117
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
118
|
-
) as any as RefIfCoValue<this["root"]>),
|
119
|
-
};
|
127
|
+
this.isMe = options.fromRaw.id == options.fromRaw.core.node.account.id;
|
128
|
+
|
129
|
+
Object.defineProperties(this, {
|
130
|
+
id: {
|
131
|
+
value: options.fromRaw.id,
|
132
|
+
enumerable: false,
|
133
|
+
},
|
134
|
+
_raw: { value: options.fromRaw, enumerable: false },
|
135
|
+
_type: { value: "Account", enumerable: false },
|
136
|
+
});
|
137
|
+
|
138
|
+
if (this.isMe) {
|
139
|
+
this.sessionID = options.fromRaw.core.node.currentSessionID;
|
120
140
|
}
|
121
141
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
constructor(options: { fromRaw: RawAccount | RawControlledAccount }) {
|
126
|
-
super();
|
127
|
-
if (!("fromRaw" in options)) {
|
128
|
-
throw new Error(
|
129
|
-
"Can only construct account from raw or with .create()",
|
130
|
-
);
|
131
|
-
}
|
132
|
-
this.isMe = options.fromRaw.id == options.fromRaw.core.node.account.id;
|
133
|
-
|
134
|
-
Object.defineProperties(this, {
|
135
|
-
id: {
|
136
|
-
value: options.fromRaw.id,
|
137
|
-
enumerable: false,
|
138
|
-
},
|
139
|
-
_raw: { value: options.fromRaw, enumerable: false },
|
140
|
-
_type: { value: "Account", enumerable: false },
|
141
|
-
});
|
142
|
-
|
143
|
-
if (this.isMe) {
|
144
|
-
this.sessionID = options.fromRaw.core.node.currentSessionID;
|
145
|
-
}
|
146
|
-
|
147
|
-
return new Proxy(
|
148
|
-
this,
|
149
|
-
AccountAndGroupProxyHandler as ProxyHandler<this>,
|
150
|
-
);
|
151
|
-
}
|
142
|
+
return new Proxy(this, AccountAndGroupProxyHandler as ProxyHandler<this>);
|
143
|
+
}
|
152
144
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
}
|
145
|
+
myRole(): "admin" | undefined {
|
146
|
+
if (this.isMe) {
|
147
|
+
return "admin";
|
157
148
|
}
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
await (this._raw as RawControlledAccount).acceptInvite(
|
169
|
-
valueID as unknown as CoID<RawCoValue>,
|
170
|
-
inviteSecret,
|
171
|
-
);
|
172
|
-
|
173
|
-
return loadCoValue(coValueClass, valueID, this as Account, []);
|
174
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
175
|
-
}
|
176
|
-
|
177
|
-
/** @private */
|
178
|
-
static async create<A extends Account>(
|
179
|
-
this: CoValueClass<A> & typeof Account,
|
180
|
-
options: {
|
181
|
-
creationProps: { name: string };
|
182
|
-
initialAgentSecret?: AgentSecret;
|
183
|
-
peersToLoadFrom?: Peer[];
|
184
|
-
crypto: CryptoProvider;
|
185
|
-
},
|
186
|
-
): Promise<A> {
|
187
|
-
const { node } = await LocalNode.withNewlyCreatedAccount({
|
188
|
-
...options,
|
189
|
-
migration: async (rawAccount, _node, creationProps) => {
|
190
|
-
const account = new this({
|
191
|
-
fromRaw: rawAccount,
|
192
|
-
}) as A;
|
193
|
-
|
194
|
-
await account.migrate?.(creationProps);
|
195
|
-
},
|
196
|
-
});
|
197
|
-
|
198
|
-
return this.fromNode(node) as A;
|
199
|
-
}
|
200
|
-
|
201
|
-
static async createAs<A extends Account>(
|
202
|
-
this: CoValueClass<A> & typeof Account,
|
203
|
-
as: Account,
|
204
|
-
options: {
|
205
|
-
creationProps: { name: string };
|
206
|
-
},
|
207
|
-
) {
|
208
|
-
// TODO: is there a cleaner way to do this?
|
209
|
-
const connectedPeers = cojsonInternals.connectedPeers(
|
210
|
-
"creatingAccount",
|
211
|
-
"createdAccount",
|
212
|
-
{ peer1role: "server", peer2role: "client" },
|
213
|
-
);
|
214
|
-
|
215
|
-
as._raw.core.node.syncManager.addPeer(connectedPeers[1]);
|
216
|
-
|
217
|
-
return this.create<A>({
|
218
|
-
creationProps: options.creationProps,
|
219
|
-
crypto: as._raw.core.node.crypto,
|
220
|
-
peersToLoadFrom: [connectedPeers[0]],
|
221
|
-
});
|
149
|
+
}
|
150
|
+
|
151
|
+
async acceptInvite<V extends CoValue>(
|
152
|
+
valueID: ID<V>,
|
153
|
+
inviteSecret: InviteSecret,
|
154
|
+
coValueClass: CoValueClass<V>,
|
155
|
+
) {
|
156
|
+
if (!this.isMe) {
|
157
|
+
throw new Error("Only a controlled account can accept invites");
|
222
158
|
}
|
223
159
|
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
)
|
228
|
-
return new this({
|
229
|
-
fromRaw: node.account as RawControlledAccount,
|
230
|
-
}) as A;
|
231
|
-
}
|
160
|
+
await (this._raw as RawControlledAccount).acceptInvite(
|
161
|
+
valueID as unknown as CoID<RawCoValue>,
|
162
|
+
inviteSecret,
|
163
|
+
);
|
232
164
|
|
165
|
+
return loadCoValue(coValueClass, valueID, this as Account, []);
|
233
166
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
167
|
+
}
|
168
|
+
|
169
|
+
/** @private */
|
170
|
+
static async create<A extends Account>(
|
171
|
+
this: CoValueClass<A> & typeof Account,
|
172
|
+
options: {
|
173
|
+
creationProps: { name: string };
|
174
|
+
initialAgentSecret?: AgentSecret;
|
175
|
+
peersToLoadFrom?: Peer[];
|
176
|
+
crypto: CryptoProvider;
|
177
|
+
},
|
178
|
+
): Promise<A> {
|
179
|
+
const { node } = await LocalNode.withNewlyCreatedAccount({
|
180
|
+
...options,
|
181
|
+
migration: async (rawAccount, _node, creationProps) => {
|
182
|
+
const account = new this({
|
183
|
+
fromRaw: rawAccount,
|
184
|
+
}) as A;
|
240
185
|
|
241
|
-
|
242
|
-
|
243
|
-
}
|
186
|
+
await account.migrate?.(creationProps);
|
187
|
+
},
|
188
|
+
});
|
244
189
|
|
245
|
-
|
246
|
-
|
247
|
-
creationProps?: { name: string },
|
248
|
-
): void | Promise<void> {
|
249
|
-
if (creationProps) {
|
250
|
-
const profileGroup = Group.create({ owner: this });
|
251
|
-
profileGroup.addMember("everyone", "reader");
|
252
|
-
this.profile = Profile.create(
|
253
|
-
{ name: creationProps.name },
|
254
|
-
{ owner: profileGroup },
|
255
|
-
);
|
256
|
-
}
|
257
|
-
}
|
190
|
+
return this.fromNode(node) as A;
|
191
|
+
}
|
258
192
|
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
193
|
+
static async createAs<A extends Account>(
|
194
|
+
this: CoValueClass<A> & typeof Account,
|
195
|
+
as: Account,
|
196
|
+
options: {
|
197
|
+
creationProps: { name: string };
|
198
|
+
},
|
199
|
+
) {
|
200
|
+
// TODO: is there a cleaner way to do this?
|
201
|
+
const connectedPeers = cojsonInternals.connectedPeers(
|
202
|
+
"creatingAccount",
|
203
|
+
"createdAccount",
|
204
|
+
{ peer1role: "server", peer2role: "client" },
|
205
|
+
);
|
206
|
+
|
207
|
+
as._raw.core.node.syncManager.addPeer(connectedPeers[1]);
|
208
|
+
|
209
|
+
return this.create<A>({
|
210
|
+
creationProps: options.creationProps,
|
211
|
+
crypto: as._raw.core.node.crypto,
|
212
|
+
peersToLoadFrom: [connectedPeers[0]],
|
213
|
+
});
|
214
|
+
}
|
215
|
+
|
216
|
+
static fromNode<A extends Account>(
|
217
|
+
this: CoValueClass<A>,
|
218
|
+
node: LocalNode,
|
219
|
+
): A {
|
220
|
+
return new this({
|
221
|
+
fromRaw: node.account as RawControlledAccount,
|
222
|
+
}) as A;
|
223
|
+
}
|
224
|
+
|
225
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
226
|
+
toJSON(): object | any[] {
|
227
|
+
return {
|
228
|
+
id: this.id,
|
229
|
+
_type: this._type,
|
230
|
+
};
|
231
|
+
}
|
232
|
+
|
233
|
+
[inspect]() {
|
234
|
+
return this.toJSON();
|
235
|
+
}
|
236
|
+
|
237
|
+
migrate(
|
238
|
+
this: Account,
|
239
|
+
creationProps?: { name: string },
|
240
|
+
): void | Promise<void> {
|
241
|
+
if (creationProps) {
|
242
|
+
const profileGroup = Group.create({ owner: this });
|
243
|
+
profileGroup.addMember("everyone", "reader");
|
244
|
+
this.profile = Profile.create(
|
245
|
+
{ name: creationProps.name },
|
246
|
+
{ owner: profileGroup },
|
247
|
+
);
|
267
248
|
}
|
249
|
+
}
|
250
|
+
|
251
|
+
/** @category Subscription & Loading */
|
252
|
+
static load<A extends Account, Depth>(
|
253
|
+
this: CoValueClass<A>,
|
254
|
+
id: ID<A>,
|
255
|
+
as: Account,
|
256
|
+
depth: Depth & DepthsIn<A>,
|
257
|
+
): Promise<DeeplyLoaded<A, Depth> | undefined> {
|
258
|
+
return loadCoValue(this, id, as, depth);
|
259
|
+
}
|
260
|
+
|
261
|
+
/** @category Subscription & Loading */
|
262
|
+
static subscribe<A extends Account, Depth>(
|
263
|
+
this: CoValueClass<A>,
|
264
|
+
id: ID<A>,
|
265
|
+
as: Account,
|
266
|
+
depth: Depth & DepthsIn<A>,
|
267
|
+
listener: (value: DeeplyLoaded<A, Depth>) => void,
|
268
|
+
): () => void {
|
269
|
+
return subscribeToCoValue<A, Depth>(this, id, as, depth, listener);
|
270
|
+
}
|
271
|
+
|
272
|
+
/** @category Subscription & Loading */
|
273
|
+
ensureLoaded<A extends Account, Depth>(
|
274
|
+
this: A,
|
275
|
+
depth: Depth & DepthsIn<A>,
|
276
|
+
): Promise<DeeplyLoaded<A, Depth> | undefined> {
|
277
|
+
return ensureCoValueLoaded(this, depth);
|
278
|
+
}
|
279
|
+
|
280
|
+
/** @category Subscription & Loading */
|
281
|
+
subscribe<A extends Account, Depth>(
|
282
|
+
this: A,
|
283
|
+
depth: Depth & DepthsIn<A>,
|
284
|
+
listener: (value: DeeplyLoaded<A, Depth>) => void,
|
285
|
+
): () => void {
|
286
|
+
return subscribeToExistingCoValue(this, depth, listener);
|
287
|
+
}
|
288
|
+
}
|
268
289
|
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
290
|
+
export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
|
291
|
+
get(target, key, receiver) {
|
292
|
+
if (key === "profile") {
|
293
|
+
const ref = target._refs.profile;
|
294
|
+
return ref
|
295
|
+
? ref.accessFrom(receiver, "profile")
|
296
|
+
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
297
|
+
(undefined as any);
|
298
|
+
} else if (key === "root") {
|
299
|
+
const ref = target._refs.root;
|
300
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
301
|
+
return ref ? ref.accessFrom(receiver, "root") : (undefined as any);
|
302
|
+
} else {
|
303
|
+
return Reflect.get(target, key, receiver);
|
278
304
|
}
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
305
|
+
},
|
306
|
+
set(target, key, value, receiver) {
|
307
|
+
if (
|
308
|
+
(key === "profile" || key === "root" || key === MembersSym) &&
|
309
|
+
typeof value === "object" &&
|
310
|
+
SchemaInit in value
|
311
|
+
) {
|
312
|
+
(target.constructor as typeof CoMap)._schema ||= {};
|
313
|
+
(target.constructor as typeof CoMap)._schema[key] = value[SchemaInit];
|
314
|
+
return true;
|
315
|
+
} else if (key === "profile") {
|
316
|
+
if (value) {
|
317
|
+
target._raw.set(
|
318
|
+
"profile",
|
319
|
+
value.id as unknown as CoID<RawCoMap>,
|
320
|
+
"trusting",
|
321
|
+
);
|
322
|
+
}
|
323
|
+
subscriptionsScopes
|
324
|
+
.get(receiver)
|
325
|
+
?.onRefAccessedOrSet(target.id, value.id);
|
326
|
+
return true;
|
327
|
+
} else if (key === "root") {
|
328
|
+
if (value) {
|
329
|
+
target._raw.set("root", value.id as unknown as CoID<RawCoMap>);
|
330
|
+
}
|
331
|
+
subscriptionsScopes
|
332
|
+
.get(receiver)
|
333
|
+
?.onRefAccessedOrSet(target.id, value.id);
|
334
|
+
return true;
|
335
|
+
} else {
|
336
|
+
return Reflect.set(target, key, value, receiver);
|
286
337
|
}
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
)
|
294
|
-
|
338
|
+
},
|
339
|
+
defineProperty(target, key, descriptor) {
|
340
|
+
if (
|
341
|
+
(key === "profile" || key === "root" || key === MembersSym) &&
|
342
|
+
typeof descriptor.value === "object" &&
|
343
|
+
SchemaInit in descriptor.value
|
344
|
+
) {
|
345
|
+
(target.constructor as typeof CoMap)._schema ||= {};
|
346
|
+
(target.constructor as typeof CoMap)._schema[key] =
|
347
|
+
descriptor.value[SchemaInit];
|
348
|
+
return true;
|
349
|
+
} else {
|
350
|
+
return Reflect.defineProperty(target, key, descriptor);
|
295
351
|
}
|
296
|
-
}
|
297
|
-
|
298
|
-
export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
|
299
|
-
get(target, key, receiver) {
|
300
|
-
if (key === "profile") {
|
301
|
-
const ref = target._refs.profile;
|
302
|
-
return ref
|
303
|
-
? ref.accessFrom(receiver, "profile")
|
304
|
-
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
305
|
-
(undefined as any);
|
306
|
-
} else if (key === "root") {
|
307
|
-
const ref = target._refs.root;
|
308
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
309
|
-
return ref ? ref.accessFrom(receiver, "root") : (undefined as any);
|
310
|
-
} else {
|
311
|
-
return Reflect.get(target, key, receiver);
|
312
|
-
}
|
313
|
-
},
|
314
|
-
set(target, key, value, receiver) {
|
315
|
-
if (
|
316
|
-
(key === "profile" || key === "root" || key === MembersSym) &&
|
317
|
-
typeof value === "object" &&
|
318
|
-
SchemaInit in value
|
319
|
-
) {
|
320
|
-
(target.constructor as typeof CoMap)._schema ||= {};
|
321
|
-
(target.constructor as typeof CoMap)._schema[key] =
|
322
|
-
value[SchemaInit];
|
323
|
-
return true;
|
324
|
-
} else if (key === "profile") {
|
325
|
-
if (value) {
|
326
|
-
target._raw.set(
|
327
|
-
"profile",
|
328
|
-
value.id as unknown as CoID<RawCoMap>,
|
329
|
-
"trusting",
|
330
|
-
);
|
331
|
-
}
|
332
|
-
subscriptionsScopes
|
333
|
-
.get(receiver)
|
334
|
-
?.onRefAccessedOrSet(target.id, value.id);
|
335
|
-
return true;
|
336
|
-
} else if (key === "root") {
|
337
|
-
if (value) {
|
338
|
-
target._raw.set("root", value.id as unknown as CoID<RawCoMap>);
|
339
|
-
}
|
340
|
-
subscriptionsScopes
|
341
|
-
.get(receiver)
|
342
|
-
?.onRefAccessedOrSet(target.id, value.id);
|
343
|
-
return true;
|
344
|
-
} else {
|
345
|
-
return Reflect.set(target, key, value, receiver);
|
346
|
-
}
|
347
|
-
},
|
348
|
-
defineProperty(target, key, descriptor) {
|
349
|
-
if (
|
350
|
-
(key === "profile" || key === "root" || key === MembersSym) &&
|
351
|
-
typeof descriptor.value === "object" &&
|
352
|
-
SchemaInit in descriptor.value
|
353
|
-
) {
|
354
|
-
(target.constructor as typeof CoMap)._schema ||= {};
|
355
|
-
(target.constructor as typeof CoMap)._schema[key] =
|
356
|
-
descriptor.value[SchemaInit];
|
357
|
-
return true;
|
358
|
-
} else {
|
359
|
-
return Reflect.defineProperty(target, key, descriptor);
|
360
|
-
}
|
361
|
-
},
|
352
|
+
},
|
362
353
|
};
|
363
354
|
|
364
355
|
/** @category Identity & Permissions */
|
365
356
|
export function isControlledAccount(account: Account): account is Account & {
|
366
|
-
|
367
|
-
|
368
|
-
|
357
|
+
isMe: true;
|
358
|
+
sessionID: SessionID;
|
359
|
+
_raw: RawControlledAccount;
|
369
360
|
} {
|
370
|
-
|
361
|
+
return account.isMe;
|
371
362
|
}
|
372
363
|
|
373
364
|
export type AccountClass<Acc extends Account> = CoValueClass<Acc> & {
|
374
|
-
|
365
|
+
fromNode: (typeof Account)["fromNode"];
|
375
366
|
};
|