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/group.ts
CHANGED
@@ -1,226 +1,212 @@
|
|
1
|
-
import type {
|
1
|
+
import type { Everyone, RawAccountID, RawGroup, Role } from "cojson";
|
2
2
|
import type {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
CoValue,
|
4
|
+
CoValueClass,
|
5
|
+
DeeplyLoaded,
|
6
|
+
DepthsIn,
|
7
|
+
ID,
|
8
|
+
RefEncoded,
|
9
|
+
Schema,
|
10
10
|
} from "../internal.js";
|
11
11
|
import {
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
28
|
+
name = co.string;
|
29
29
|
}
|
30
30
|
|
31
31
|
/** @category Identity & Permissions */
|
32
32
|
export class Group extends CoValueBase implements CoValue {
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
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
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
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
|
}
|