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.
- package/CHANGELOG.md +276 -269
- 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 +1 -2
- 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 +1 -2
- 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 -26
- package/src/implementation/createContext.ts +206 -213
- 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
@@ -3,109 +3,89 @@
|
|
3
3
|
import { ItemsSym } from "./symbols.js";
|
4
4
|
|
5
5
|
(globalThis as any).devtoolsFormatters = [
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
6
|
+
{
|
7
|
+
header: (object: any) => {
|
8
|
+
if (object._type === "CoMap") {
|
9
|
+
return ["div", {}, ["span", {}, object.constructor.name]];
|
10
|
+
} else if (object._type === "CoList") {
|
11
|
+
return [
|
12
|
+
"div",
|
13
|
+
{},
|
14
|
+
["span", {}, object.constructor.name + "(" + object.length + ") "],
|
15
|
+
];
|
16
|
+
} else if (object._type === "Account") {
|
17
|
+
return [
|
18
|
+
"div",
|
19
|
+
{},
|
20
|
+
[
|
21
|
+
"span",
|
22
|
+
{},
|
23
|
+
object.constructor.name +
|
24
|
+
"(" +
|
25
|
+
object._refs.profile.value?.name +
|
26
|
+
(object.isMe ? " ME" : "") +
|
27
|
+
")",
|
28
|
+
],
|
29
|
+
];
|
30
|
+
} else {
|
31
|
+
return null;
|
32
|
+
}
|
33
|
+
},
|
34
|
+
hasBody: function () {
|
35
|
+
return true;
|
36
|
+
},
|
37
|
+
body: function (object: any) {
|
38
|
+
if (object._type === "CoMap" || object._type === "Account") {
|
39
|
+
return [
|
40
|
+
"div",
|
41
|
+
{ style: "margin-left: 15px" },
|
42
|
+
["div", "id: ", ["object", { object: object.id }]],
|
43
|
+
...Object.entries(object).map(([k, v]) => [
|
44
|
+
"div",
|
45
|
+
{ style: "white-space: nowrap;" },
|
46
|
+
["span", { style: "font-weight: bold; opacity: 0.6" }, k, ": "],
|
47
|
+
["object", { object: v }],
|
48
|
+
...(typeof object._schema[k] === "function"
|
49
|
+
? v === null
|
50
|
+
? [
|
14
51
|
[
|
15
|
-
|
16
|
-
|
17
|
-
|
52
|
+
"span",
|
53
|
+
{ style: "opacity: 0.5" },
|
54
|
+
` (pending ${object._schema[k].name} `,
|
55
|
+
["object", { object: object._refs[k] }],
|
56
|
+
")",
|
18
57
|
],
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
58
|
+
]
|
59
|
+
: []
|
60
|
+
: []),
|
61
|
+
]),
|
62
|
+
];
|
63
|
+
} else if (object._type === "CoList") {
|
64
|
+
return [
|
65
|
+
"div",
|
66
|
+
{ style: "margin-left: 15px" },
|
67
|
+
["div", "id: ", ["object", { object: object.id }]],
|
68
|
+
...(object as any[]).map((v, i) => [
|
69
|
+
"div",
|
70
|
+
{ style: "white-space: nowrap;" },
|
71
|
+
["span", { style: "font-weight: bold; opacity: 0.6" }, i, ": "],
|
72
|
+
["object", { object: v }],
|
73
|
+
...(typeof object._schema[ItemsSym] === "function"
|
74
|
+
? v === null
|
75
|
+
? [
|
24
76
|
[
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
(object.isMe ? " ME" : "") +
|
31
|
-
")",
|
77
|
+
"span",
|
78
|
+
{ style: "opacity: 0.5" },
|
79
|
+
` (pending ${object._schema[ItemsSym].name} `,
|
80
|
+
["object", { object: object._refs[i] }],
|
81
|
+
")",
|
32
82
|
],
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
return true;
|
40
|
-
},
|
41
|
-
body: function (object: any) {
|
42
|
-
if (object._type === "CoMap" || object._type === "Account") {
|
43
|
-
return [
|
44
|
-
"div",
|
45
|
-
{ style: "margin-left: 15px" },
|
46
|
-
["div", "id: ", ["object", { object: object.id }]],
|
47
|
-
...Object.entries(object).map(([k, v]) => [
|
48
|
-
"div",
|
49
|
-
{ style: "white-space: nowrap;" },
|
50
|
-
[
|
51
|
-
"span",
|
52
|
-
{ style: "font-weight: bold; opacity: 0.6" },
|
53
|
-
k,
|
54
|
-
": ",
|
55
|
-
],
|
56
|
-
["object", { object: v }],
|
57
|
-
...(typeof object._schema[k] === "function"
|
58
|
-
? v === null
|
59
|
-
? [
|
60
|
-
[
|
61
|
-
"span",
|
62
|
-
{ style: "opacity: 0.5" },
|
63
|
-
` (pending ${object._schema[k].name} `,
|
64
|
-
[
|
65
|
-
"object",
|
66
|
-
{ object: object._refs[k] },
|
67
|
-
],
|
68
|
-
")",
|
69
|
-
],
|
70
|
-
]
|
71
|
-
: []
|
72
|
-
: []),
|
73
|
-
]),
|
74
|
-
];
|
75
|
-
} else if (object._type === "CoList") {
|
76
|
-
return [
|
77
|
-
"div",
|
78
|
-
{ style: "margin-left: 15px" },
|
79
|
-
["div", "id: ", ["object", { object: object.id }]],
|
80
|
-
...(object as any[]).map((v, i) => [
|
81
|
-
"div",
|
82
|
-
{ style: "white-space: nowrap;" },
|
83
|
-
[
|
84
|
-
"span",
|
85
|
-
{ style: "font-weight: bold; opacity: 0.6" },
|
86
|
-
i,
|
87
|
-
": ",
|
88
|
-
],
|
89
|
-
["object", { object: v }],
|
90
|
-
...(typeof object._schema[ItemsSym] === "function"
|
91
|
-
? v === null
|
92
|
-
? [
|
93
|
-
[
|
94
|
-
"span",
|
95
|
-
{ style: "opacity: 0.5" },
|
96
|
-
` (pending ${object._schema[ItemsSym].name} `,
|
97
|
-
[
|
98
|
-
"object",
|
99
|
-
{ object: object._refs[i] },
|
100
|
-
],
|
101
|
-
")",
|
102
|
-
],
|
103
|
-
]
|
104
|
-
: []
|
105
|
-
: []),
|
106
|
-
]),
|
107
|
-
];
|
108
|
-
}
|
109
|
-
},
|
83
|
+
]
|
84
|
+
: []
|
85
|
+
: []),
|
86
|
+
]),
|
87
|
+
];
|
88
|
+
}
|
110
89
|
},
|
90
|
+
},
|
111
91
|
];
|
@@ -1,171 +1,159 @@
|
|
1
1
|
import type { CoID, RawCoValue } from "cojson";
|
2
2
|
import type {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
Account,
|
4
|
+
AnonymousJazzAgent,
|
5
|
+
CoValue,
|
6
|
+
ID,
|
7
|
+
RefEncoded,
|
8
|
+
UnCo,
|
9
9
|
} from "../internal.js";
|
10
10
|
import {
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
instantiateRefEncoded,
|
12
|
+
isRefEncoded,
|
13
|
+
subscriptionsScopes,
|
14
14
|
} from "../internal.js";
|
15
15
|
import { coValuesCache } from "../lib/cache.js";
|
16
16
|
|
17
17
|
const TRACE_ACCESSES = false;
|
18
18
|
|
19
19
|
export class Ref<out V extends CoValue> {
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
}
|
20
|
+
constructor(
|
21
|
+
readonly id: ID<V>,
|
22
|
+
readonly controlledAccount: Account | AnonymousJazzAgent,
|
23
|
+
readonly schema: RefEncoded<V>,
|
24
|
+
) {
|
25
|
+
if (!isRefEncoded(schema)) {
|
26
|
+
throw new Error("Ref must be constructed with a ref schema");
|
28
27
|
}
|
28
|
+
}
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
}
|
30
|
+
get value() {
|
31
|
+
const node =
|
32
|
+
"node" in this.controlledAccount
|
33
|
+
? this.controlledAccount.node
|
34
|
+
: this.controlledAccount._raw.core.node;
|
35
|
+
const raw = node.getLoaded(this.id as unknown as CoID<RawCoValue>);
|
36
|
+
if (raw) {
|
37
|
+
return coValuesCache.get(raw, () =>
|
38
|
+
instantiateRefEncoded(this.schema, raw),
|
39
|
+
);
|
40
|
+
} else {
|
41
|
+
return null;
|
43
42
|
}
|
43
|
+
}
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
} else {
|
56
|
-
return new Ref(this.id, this.controlledAccount, this.schema).value!;
|
57
|
-
}
|
45
|
+
private async loadHelper(): Promise<V | "unavailable"> {
|
46
|
+
const node =
|
47
|
+
"node" in this.controlledAccount
|
48
|
+
? this.controlledAccount.node
|
49
|
+
: this.controlledAccount._raw.core.node;
|
50
|
+
const raw = await node.load(this.id as unknown as CoID<RawCoValue>);
|
51
|
+
if (raw === "unavailable") {
|
52
|
+
return "unavailable";
|
53
|
+
} else {
|
54
|
+
return new Ref(this.id, this.controlledAccount, this.schema).value!;
|
58
55
|
}
|
56
|
+
}
|
59
57
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
}
|
58
|
+
async load(): Promise<V | undefined> {
|
59
|
+
const result = await this.loadHelper();
|
60
|
+
if (result === "unavailable") {
|
61
|
+
return undefined;
|
62
|
+
} else {
|
63
|
+
return result;
|
67
64
|
}
|
65
|
+
}
|
68
66
|
|
69
|
-
|
70
|
-
|
71
|
-
key: string | number | symbol,
|
72
|
-
): V | null {
|
73
|
-
const subScope = subscriptionsScopes.get(fromScopeValue);
|
67
|
+
accessFrom(fromScopeValue: CoValue, key: string | number | symbol): V | null {
|
68
|
+
const subScope = subscriptionsScopes.get(fromScopeValue);
|
74
69
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
subScope?.scopeID,
|
79
|
-
"accessing",
|
80
|
-
fromScopeValue,
|
81
|
-
key,
|
82
|
-
this.id,
|
83
|
-
);
|
70
|
+
subScope?.onRefAccessedOrSet(fromScopeValue.id, this.id);
|
71
|
+
TRACE_ACCESSES &&
|
72
|
+
console.log(subScope?.scopeID, "accessing", fromScopeValue, key, this.id);
|
84
73
|
|
85
|
-
|
86
|
-
|
87
|
-
|
74
|
+
if (this.value && subScope) {
|
75
|
+
subscriptionsScopes.set(this.value, subScope);
|
76
|
+
}
|
88
77
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
return this.value;
|
109
|
-
}
|
78
|
+
if (subScope) {
|
79
|
+
const cached = subScope.cachedValues[this.id];
|
80
|
+
if (cached) {
|
81
|
+
TRACE_ACCESSES && console.log("cached", cached);
|
82
|
+
return cached as V;
|
83
|
+
} else if (this.value !== null) {
|
84
|
+
const freshValueInstance = instantiateRefEncoded(
|
85
|
+
this.schema,
|
86
|
+
this.value?._raw,
|
87
|
+
);
|
88
|
+
TRACE_ACCESSES && console.log("freshValueInstance", freshValueInstance);
|
89
|
+
subScope.cachedValues[this.id] = freshValueInstance;
|
90
|
+
subscriptionsScopes.set(freshValueInstance, subScope);
|
91
|
+
return freshValueInstance as V;
|
92
|
+
} else {
|
93
|
+
return null;
|
94
|
+
}
|
95
|
+
} else {
|
96
|
+
return this.value;
|
110
97
|
}
|
98
|
+
}
|
111
99
|
}
|
112
100
|
|
113
101
|
export function makeRefs<Keys extends string | number>(
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
102
|
+
getIdForKey: (key: Keys) => ID<CoValue> | undefined,
|
103
|
+
getKeysWithIds: () => Keys[],
|
104
|
+
controlledAccount: Account | AnonymousJazzAgent,
|
105
|
+
refSchemaForKey: (key: Keys) => RefEncoded<CoValue>,
|
118
106
|
): { [K in Keys]: Ref<CoValue> } & {
|
107
|
+
[Symbol.iterator]: () => IterableIterator<Ref<CoValue>>;
|
108
|
+
length: number;
|
109
|
+
} {
|
110
|
+
const refs = {} as { [K in Keys]: Ref<CoValue> } & {
|
119
111
|
[Symbol.iterator]: () => IterableIterator<Ref<CoValue>>;
|
120
112
|
length: number;
|
121
|
-
}
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
yield new Ref(
|
132
|
-
getIdForKey(key)!,
|
133
|
-
controlledAccount,
|
134
|
-
refSchemaForKey(key),
|
135
|
-
);
|
136
|
-
}
|
137
|
-
};
|
138
|
-
}
|
139
|
-
if (typeof key === "symbol") return undefined;
|
140
|
-
if (key === "length") {
|
141
|
-
return getKeysWithIds().length;
|
142
|
-
}
|
143
|
-
const id = getIdForKey(key as Keys);
|
144
|
-
if (!id) return undefined;
|
145
|
-
return new Ref(
|
146
|
-
id as ID<CoValue>,
|
147
|
-
controlledAccount,
|
148
|
-
refSchemaForKey(key as Keys),
|
113
|
+
};
|
114
|
+
return new Proxy(refs, {
|
115
|
+
get(_target, key) {
|
116
|
+
if (key === Symbol.iterator) {
|
117
|
+
return function* () {
|
118
|
+
for (const key of getKeysWithIds()) {
|
119
|
+
yield new Ref(
|
120
|
+
getIdForKey(key)!,
|
121
|
+
controlledAccount,
|
122
|
+
refSchemaForKey(key),
|
149
123
|
);
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
124
|
+
}
|
125
|
+
};
|
126
|
+
}
|
127
|
+
if (typeof key === "symbol") return undefined;
|
128
|
+
if (key === "length") {
|
129
|
+
return getKeysWithIds().length;
|
130
|
+
}
|
131
|
+
const id = getIdForKey(key as Keys);
|
132
|
+
if (!id) return undefined;
|
133
|
+
return new Ref(
|
134
|
+
id as ID<CoValue>,
|
135
|
+
controlledAccount,
|
136
|
+
refSchemaForKey(key as Keys),
|
137
|
+
);
|
138
|
+
},
|
139
|
+
ownKeys() {
|
140
|
+
return getKeysWithIds().map((key) => key.toString());
|
141
|
+
},
|
142
|
+
getOwnPropertyDescriptor(target, key) {
|
143
|
+
const id = getIdForKey(key as Keys);
|
144
|
+
if (id) {
|
145
|
+
return {
|
146
|
+
enumerable: true,
|
147
|
+
configurable: true,
|
148
|
+
writable: true,
|
149
|
+
};
|
150
|
+
} else {
|
151
|
+
return Reflect.getOwnPropertyDescriptor(target, key);
|
152
|
+
}
|
153
|
+
},
|
154
|
+
});
|
167
155
|
}
|
168
156
|
|
169
157
|
export type RefIfCoValue<V> = NonNullable<V> extends CoValue
|
170
|
-
|
171
|
-
|
158
|
+
? Ref<UnCo<NonNullable<V>>>
|
159
|
+
: never;
|