jazz-tools 0.7.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/.eslintrc.cjs +24 -0
  2. package/.turbo/turbo-build.log +24 -0
  3. package/CHANGELOG.md +42 -0
  4. package/LICENSE.txt +19 -0
  5. package/README.md +3 -0
  6. package/dist/coValueInterfaces.js +8 -0
  7. package/dist/coValueInterfaces.js.map +1 -0
  8. package/dist/coValues/account/account.js +11 -0
  9. package/dist/coValues/account/account.js.map +1 -0
  10. package/dist/coValues/account/accountOf.js +150 -0
  11. package/dist/coValues/account/accountOf.js.map +1 -0
  12. package/dist/coValues/account/migration.js +4 -0
  13. package/dist/coValues/account/migration.js.map +1 -0
  14. package/dist/coValues/coList/coList.js +2 -0
  15. package/dist/coValues/coList/coList.js.map +1 -0
  16. package/dist/coValues/coList/coListOf.js +235 -0
  17. package/dist/coValues/coList/coListOf.js.map +1 -0
  18. package/dist/coValues/coList/internalDocs.js +2 -0
  19. package/dist/coValues/coList/internalDocs.js.map +1 -0
  20. package/dist/coValues/coMap/coMap.js +2 -0
  21. package/dist/coValues/coMap/coMap.js.map +1 -0
  22. package/dist/coValues/coMap/coMapOf.js +262 -0
  23. package/dist/coValues/coMap/coMapOf.js.map +1 -0
  24. package/dist/coValues/coMap/internalDocs.js +2 -0
  25. package/dist/coValues/coMap/internalDocs.js.map +1 -0
  26. package/dist/coValues/coStream/coStream.js +2 -0
  27. package/dist/coValues/coStream/coStream.js.map +1 -0
  28. package/dist/coValues/coStream/coStreamOf.js +244 -0
  29. package/dist/coValues/coStream/coStreamOf.js.map +1 -0
  30. package/dist/coValues/construction.js +34 -0
  31. package/dist/coValues/construction.js.map +1 -0
  32. package/dist/coValues/extensions/imageDef.js +36 -0
  33. package/dist/coValues/extensions/imageDef.js.map +1 -0
  34. package/dist/coValues/group/group.js +2 -0
  35. package/dist/coValues/group/group.js.map +1 -0
  36. package/dist/coValues/group/groupOf.js +109 -0
  37. package/dist/coValues/group/groupOf.js.map +1 -0
  38. package/dist/coValues/resolution.js +66 -0
  39. package/dist/coValues/resolution.js.map +1 -0
  40. package/dist/errors.js +2 -0
  41. package/dist/errors.js.map +1 -0
  42. package/dist/index.js +31 -0
  43. package/dist/index.js.map +1 -0
  44. package/dist/refs.js +95 -0
  45. package/dist/refs.js.map +1 -0
  46. package/dist/schemaHelpers.js +14 -0
  47. package/dist/schemaHelpers.js.map +1 -0
  48. package/dist/subscriptionScope.js +81 -0
  49. package/dist/subscriptionScope.js.map +1 -0
  50. package/dist/tests/coList.test.js +207 -0
  51. package/dist/tests/coList.test.js.map +1 -0
  52. package/dist/tests/coMap.test.js +238 -0
  53. package/dist/tests/coMap.test.js.map +1 -0
  54. package/dist/tests/coStream.test.js +263 -0
  55. package/dist/tests/coStream.test.js.map +1 -0
  56. package/dist/tests/types.test.js +33 -0
  57. package/dist/tests/types.test.js.map +1 -0
  58. package/package.json +23 -0
  59. package/src/coValueInterfaces.ts +105 -0
  60. package/src/coValues/account/account.ts +106 -0
  61. package/src/coValues/account/accountOf.ts +284 -0
  62. package/src/coValues/account/migration.ts +12 -0
  63. package/src/coValues/coList/coList.ts +57 -0
  64. package/src/coValues/coList/coListOf.ts +377 -0
  65. package/src/coValues/coList/internalDocs.ts +1 -0
  66. package/src/coValues/coMap/coMap.ts +110 -0
  67. package/src/coValues/coMap/coMapOf.ts +451 -0
  68. package/src/coValues/coMap/internalDocs.ts +1 -0
  69. package/src/coValues/coStream/coStream.ts +63 -0
  70. package/src/coValues/coStream/coStreamOf.ts +404 -0
  71. package/src/coValues/construction.ts +110 -0
  72. package/src/coValues/extensions/imageDef.ts +51 -0
  73. package/src/coValues/group/group.ts +27 -0
  74. package/src/coValues/group/groupOf.ts +183 -0
  75. package/src/coValues/resolution.ts +111 -0
  76. package/src/errors.ts +1 -0
  77. package/src/index.ts +68 -0
  78. package/src/refs.ts +128 -0
  79. package/src/schemaHelpers.ts +72 -0
  80. package/src/subscriptionScope.ts +118 -0
  81. package/src/tests/coList.test.ts +283 -0
  82. package/src/tests/coMap.test.ts +357 -0
  83. package/src/tests/coStream.test.ts +415 -0
  84. package/src/tests/types.test.ts +37 -0
  85. package/tsconfig.json +15 -0
@@ -0,0 +1,109 @@
1
+ import { inspect, isCoValueSchema, } from "../../coValueInterfaces.js";
2
+ import * as S from "@effect/schema/Schema";
3
+ import { isControlledAccount, } from "../account/account.js";
4
+ import { AST, Schema } from "@effect/schema";
5
+ import { constructorOfSchemaSym } from "../resolution.js";
6
+ import { pipeArguments } from "effect/Pipeable";
7
+ import { ValueRef } from "../../refs.js";
8
+ import { controlledAccountFromNode } from "../account/accountOf.js";
9
+ import { SharedCoValueConstructor } from "../construction.js";
10
+ export function GroupOf(fields) {
11
+ class GroupOfProfileAndRoot extends SharedCoValueConstructor {
12
+ static get ast() {
13
+ return AST.setAnnotation(Schema.instanceOf(this).ast, constructorOfSchemaSym, this);
14
+ }
15
+ static pipe() {
16
+ // eslint-disable-next-line prefer-rest-params
17
+ return pipeArguments(this, arguments);
18
+ }
19
+ constructor(init, options) {
20
+ super();
21
+ let raw;
22
+ if (options && "fromRaw" in options) {
23
+ raw = options.fromRaw;
24
+ }
25
+ else {
26
+ const initOwner = options?.owner || init?.owner;
27
+ if (!initOwner)
28
+ throw new Error("No owner provided");
29
+ if (isControlledAccount(initOwner)) {
30
+ const rawOwner = initOwner._raw;
31
+ raw = rawOwner.createGroup();
32
+ }
33
+ else {
34
+ throw new Error("Can only construct group as a controlled account");
35
+ }
36
+ }
37
+ const refs = {
38
+ get profile() {
39
+ if (isCoValueSchema(fields.profile)) {
40
+ const profileID = raw.get("profile");
41
+ return (profileID &&
42
+ new ValueRef(profileID, controlledAccountFromNode(raw.core.node), fields.profile));
43
+ }
44
+ },
45
+ get root() {
46
+ if (isCoValueSchema(fields.root)) {
47
+ const rootID = raw.get("root");
48
+ return (rootID &&
49
+ new ValueRef(rootID, controlledAccountFromNode(raw.core.node), fields.root));
50
+ }
51
+ },
52
+ };
53
+ Object.defineProperties(this, {
54
+ id: { value: raw.id, enumerable: false },
55
+ _type: { value: "Group", enumerable: false },
56
+ _owner: { value: this, enumerable: false },
57
+ _refs: { value: refs, enumerable: false },
58
+ _raw: { value: raw, enumerable: false },
59
+ _loadedAs: {
60
+ get: () => controlledAccountFromNode(raw.core.node),
61
+ enumerable: false,
62
+ },
63
+ _schema: { value: GroupOfProfileAndRoot, enumerable: false },
64
+ });
65
+ }
66
+ static fromRaw(raw) {
67
+ return new GroupOfProfileAndRoot(undefined, {
68
+ fromRaw: raw,
69
+ });
70
+ }
71
+ get profile() {
72
+ return this._refs.profile.accessFrom(this);
73
+ }
74
+ get root() {
75
+ return this._refs.root.accessFrom(this);
76
+ }
77
+ addMember(member, role) {
78
+ this._raw.addMember(typeof member === "string" ? member : member._raw, role);
79
+ return this;
80
+ }
81
+ myRole() {
82
+ return this._raw.myRole();
83
+ }
84
+ toJSON() {
85
+ return {
86
+ co: {
87
+ id: this.id,
88
+ type: this._type,
89
+ },
90
+ profile: this.profile?.toJSON(),
91
+ root: this.root?.toJSON(),
92
+ };
93
+ }
94
+ [(Schema.TypeId, inspect)]() {
95
+ return this.toJSON();
96
+ }
97
+ static as() {
98
+ return this;
99
+ }
100
+ }
101
+ GroupOfProfileAndRoot.type = "Group";
102
+ return GroupOfProfileAndRoot;
103
+ }
104
+ export class Group extends GroupOf({
105
+ profile: S.null,
106
+ root: S.null,
107
+ }).as() {
108
+ }
109
+ //# sourceMappingURL=groupOf.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"groupOf.js","sourceRoot":"","sources":["../../../src/coValues/group/groupOf.ts"],"names":[],"mappings":"AAAA,OAAO,EAGH,OAAO,EACP,eAAe,GAClB,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,CAAC,MAAM,uBAAuB,CAAC;AAE3C,OAAO,EAGH,mBAAmB,GACtB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,GAAG,EAA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C,OAAO,qBAEN,CAAC;AACN,CAAC;AAED,MAAM,OAAO,KAAM,SAAQ,OAAO,CAAC;IAC/B,OAAO,EAAE,CAAC,CAAC,IAAI;IACf,IAAI,EAAE,CAAC,CAAC,IAAI;CACf,CAAC,CAAC,EAAE,EAAS;CAAG"}
@@ -0,0 +1,66 @@
1
+ // TODO: can we get rid of this because effect schema already has an annotation for the constructor?
2
+ export const constructorOfSchemaSym = Symbol.for("@jazz/constructorOfSymbol");
3
+ export function propertyIsCoValueSchema(prop) {
4
+ if ("propertySignatureAST" in prop) {
5
+ return astIsCoValueSchema(prop.propertySignatureAST.from);
6
+ }
7
+ else {
8
+ return astIsCoValueSchema(prop.ast);
9
+ }
10
+ }
11
+ function astIsCoValueSchema(ast) {
12
+ if ((ast._tag === "Declaration") &&
13
+ ast.annotations[constructorOfSchemaSym]) {
14
+ return true;
15
+ }
16
+ else if (ast._tag === "Union") {
17
+ return ast.types.every((member) => member._tag === "UndefinedKeyword" || astIsCoValueSchema(member));
18
+ }
19
+ else if (ast._tag === "BooleanKeyword" ||
20
+ ast._tag === "NumberKeyword" ||
21
+ ast._tag === "StringKeyword" ||
22
+ ast._tag === "Literal" ||
23
+ ast._tag === "Tuple") {
24
+ return false;
25
+ }
26
+ else if (ast._tag === "Refinement") {
27
+ return astIsCoValueSchema(ast.from);
28
+ }
29
+ else if (ast._tag === "Transform") {
30
+ return astIsCoValueSchema(ast.from);
31
+ }
32
+ else {
33
+ throw new Error(`astIsCoValueSchema can't yet handle ${ast._tag}: ${JSON.stringify(ast)}`);
34
+ }
35
+ }
36
+ export function getCoValueConstructorInProperty(prop, rawValue) {
37
+ if ("propertySignatureAST" in prop) {
38
+ return getCoValueConstructorInAST(prop.propertySignatureAST.from, rawValue);
39
+ }
40
+ else {
41
+ return getCoValueConstructorInAST(prop.ast, rawValue);
42
+ }
43
+ }
44
+ // TODO (optimization): make this meta, so this creates a tailor-made function that will take a RawCoValue at call time
45
+ function getCoValueConstructorInAST(ast, rawValue) {
46
+ if ((ast._tag === "Declaration") &&
47
+ ast.annotations[constructorOfSchemaSym]) {
48
+ return ast.annotations[constructorOfSchemaSym];
49
+ }
50
+ else if (ast._tag === "Union" && ast.types.length === 2) {
51
+ const [a, b] = ast.types;
52
+ if (a._tag === "UndefinedKeyword") {
53
+ return getCoValueConstructorInAST(b, rawValue);
54
+ }
55
+ else if (b._tag === "UndefinedKeyword") {
56
+ return getCoValueConstructorInAST(a, rawValue);
57
+ }
58
+ else {
59
+ throw new Error(`getCoValueConstructorInAST can't yet handle Union of: ${JSON.stringify(ast.types)}`);
60
+ }
61
+ }
62
+ else {
63
+ throw new Error(`getCoValueConstructorInAST can't yet handle ${ast._tag}: ${JSON.stringify(ast)}`);
64
+ }
65
+ }
66
+ //# sourceMappingURL=resolution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolution.js","sourceRoot":"","sources":["../../src/coValues/resolution.ts"],"names":[],"mappings":"AAIA,oGAAoG;AACpG,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC;AAG9E,MAAM,UAAU,uBAAuB,CACnC,IAEiE;IAEjE,IAAI,sBAAsB,IAAI,IAAI,EAAE,CAAC;QACjC,OAAO,kBAAkB,CACpB,IAAI,CAAC,oBAA0C,CAAC,IAAI,CACxD,CAAC;IACN,CAAC;SAAM,CAAC;QACJ,OAAO,kBAAkB,CAAE,IAA+B,CAAC,GAAG,CAAC,CAAC;IACpE,CAAC;AACL,CAAC;AAED,SAAS,kBAAkB,CAAC,GAAY;IACpC,IACI,CAAC,GAAG,CAAC,IAAI,KAAK,aAAa,CAAC;QAC5B,GAAG,CAAC,WAAW,CAAC,sBAAsB,CAAC,EACzC,CAAC;QACC,OAAO,IAAI,CAAC;IAChB,CAAC;SAAM,IAAI,GAAG,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;QAC9B,OAAO,GAAG,CAAC,KAAK,CAAC,KAAK,CAClB,CAAC,MAAM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AAW,CAAC,sBAAsB,CAGjC,CAAC;IACjB,CAAC;SAAM,IAAI,GAAG,CAAC,IAAI,KAAK,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxD,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC;QACzB,IAAI,CAAC,CAAC,IAAI,KAAK,kBAAkB,EAAE,CAAC;YAChC,OAAO,0BAA0B,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;QACnD,CAAC;aAAM,IAAI,CAAC,CAAC,IAAI,KAAK,kBAAkB,EAAE,CAAC;YACvC,OAAO,0BAA0B,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;QACnD,CAAC;aAAM,CAAC;YACJ,MAAM,IAAI,KAAK,CACX,yDAAyD,IAAI,CAAC,SAAS,CACnE,GAAG,CAAC,KAAK,CACZ,EAAE,CACN,CAAC;QACN,CAAC;IACL,CAAC;SAAM,CAAC;QACJ,MAAM,IAAI,KAAK,CACX,+CACI,GAAG,CAAC,IACR,KAAK,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAC7B,CAAC;IACN,CAAC;AACL,CAAC"}
package/dist/errors.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":""}
package/dist/index.js ADDED
@@ -0,0 +1,31 @@
1
+ import { AccountOf } from "./coValues/account/accountOf.js";
2
+ import { CoListOf } from "./coValues/coList/coListOf.js";
3
+ import { CoMapOf } from "./coValues/coMap/coMapOf.js";
4
+ import { BinaryCoStreamImpl, CoStreamOf, } from "./coValues/coStream/coStreamOf.js";
5
+ import { ImageDefinition } from "./coValues/extensions/imageDef.js";
6
+ import { GroupOf } from "./coValues/group/groupOf.js";
7
+ /** @hidden */
8
+ export * as S from "@effect/schema/Schema";
9
+ /** @category Schemas & CoValues - Schema definers */
10
+ export const Co = {
11
+ map: CoMapOf,
12
+ list: CoListOf,
13
+ stream: CoStreamOf,
14
+ binaryStream: BinaryCoStreamImpl,
15
+ account: AccountOf,
16
+ group: GroupOf,
17
+ media: {
18
+ imageDef: ImageDefinition
19
+ }
20
+ };
21
+ /** @category Internal types */
22
+ export { cojsonReady as jazzReady, cojsonInternals, MAX_RECOMMENDED_TX_SIZE, } from "cojson";
23
+ /** @category Schemas & CoValues - CoMap */
24
+ export * as CoMapInternals from "./coValues/coMap/internalDocs.js";
25
+ /** @category Schemas & CoValues - CoList */
26
+ export * as CoListInternals from "./coValues/coList/internalDocs.js";
27
+ export { controlledAccountSym, } from "./coValues/account/account.js";
28
+ export { Account, BaseProfile } from "./coValues/account/accountOf.js";
29
+ export { Group } from "./coValues/group/groupOf.js";
30
+ export { ImageDefinition } from "./coValues/extensions/imageDef.js";
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AACzD,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AACtD,OAAO,EACH,kBAAkB,EAClB,UAAU,GACb,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AACpE,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAEtD,cAAc;AACd,OAAO,KAAK,CAAC,MAAM,uBAAuB,CAAC;AAE3C,qDAAqD;AACrD,MAAM,CAAC,MAAM,EAAE,GAAG;IACd,GAAG,EAAE,OAAO;IACZ,IAAI,EAAE,QAAQ;IACd,MAAM,EAAE,UAAU;IAClB,YAAY,EAAE,kBAAkB;IAChC,OAAO,EAAE,SAAS;IAClB,KAAK,EAAE,OAAO;IACd,KAAK,EAAE;QACH,QAAQ,EAAE,eAAe;KAC5B;CACJ,CAAC;AAEF,+BAA+B;AAC/B,OAAO,EACH,WAAW,IAAI,SAAS,EAMxB,eAAe,EACf,uBAAuB,GAC1B,MAAM,QAAQ,CAAC;AAMhB,2CAA2C;AAC3C,OAAO,KAAK,cAAc,MAAM,kCAAkC,CAAC;AAGnE,4CAA4C;AAC5C,OAAO,KAAK,eAAe,MAAM,mCAAmC,CAAC;AAQrE,OAAO,EAIH,oBAAoB,GACvB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAEvE,OAAO,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAC;AAEpD,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC"}
package/dist/refs.js ADDED
@@ -0,0 +1,95 @@
1
+ import { Effect } from "effect";
2
+ import { getCoValueConstructorInProperty } from "./coValues/resolution.js";
3
+ import { subscriptionsScopes } from "./subscriptionScope.js";
4
+ export class ValueRef {
5
+ constructor(id, controlledAccount, propDef) {
6
+ this.id = id;
7
+ this.controlledAccount = controlledAccount;
8
+ this.propDef = propDef;
9
+ }
10
+ get value() {
11
+ if (this.cachedValue)
12
+ return this.cachedValue;
13
+ // TODO: cache it for object identity!!!
14
+ const raw = this.controlledAccount._raw.core.node.getLoaded(this.id);
15
+ if (raw) {
16
+ const Constructor = getCoValueConstructorInProperty(this.propDef, raw);
17
+ if (!Constructor) {
18
+ throw new Error("Couldn't extract CoValue constructor from property definition");
19
+ }
20
+ const value = new Constructor(undefined, { fromRaw: raw });
21
+ this.cachedValue = value;
22
+ return value;
23
+ }
24
+ }
25
+ loadEf() {
26
+ return Effect.async((fulfill) => {
27
+ this.loadHelper()
28
+ .then((value) => {
29
+ if (value === "unavailable") {
30
+ fulfill(Effect.fail("unavailable"));
31
+ }
32
+ else {
33
+ fulfill(Effect.succeed(value));
34
+ }
35
+ })
36
+ .catch((e) => {
37
+ fulfill(Effect.die(e));
38
+ });
39
+ });
40
+ }
41
+ async loadHelper(options) {
42
+ const raw = await this.controlledAccount._raw.core.node.load(this.id, options?.onProgress);
43
+ if (raw === "unavailable") {
44
+ return "unavailable";
45
+ }
46
+ else {
47
+ return new ValueRef(this.id, this.controlledAccount, this.propDef)
48
+ .value;
49
+ }
50
+ }
51
+ async load(options) {
52
+ const result = await this.loadHelper(options);
53
+ if (result === "unavailable") {
54
+ return undefined;
55
+ }
56
+ else {
57
+ return result;
58
+ }
59
+ }
60
+ accessFrom(fromScopeValue) {
61
+ const subScope = subscriptionsScopes.get(fromScopeValue);
62
+ subScope?.onRefAccessedOrSet(this.id);
63
+ if (this.value && subScope) {
64
+ subscriptionsScopes.set(this.value, subScope);
65
+ }
66
+ return this.value;
67
+ }
68
+ }
69
+ export function makeRefs(getIdForKey, getKeysWithIds, controlledAccount, propDefForKey) {
70
+ const refs = {};
71
+ return new Proxy(refs, {
72
+ get(target, key) {
73
+ if (key === Symbol.iterator) {
74
+ return function* () {
75
+ for (const key of getKeysWithIds()) {
76
+ yield new ValueRef(getIdForKey(key), controlledAccount, propDefForKey(key));
77
+ }
78
+ };
79
+ }
80
+ if (typeof key === "symbol")
81
+ return undefined;
82
+ if (key === "length") {
83
+ return getKeysWithIds().length;
84
+ }
85
+ const id = getIdForKey(key);
86
+ if (!id)
87
+ return undefined;
88
+ return new ValueRef(id, controlledAccount, propDefForKey(key));
89
+ },
90
+ ownKeys() {
91
+ return getKeysWithIds().map((key) => key.toString());
92
+ },
93
+ });
94
+ }
95
+ //# sourceMappingURL=refs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refs.js","sourceRoot":"","sources":["../src/refs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAKhC,OAAO,EAAE,+BAA+B,EAAE,MAAM,0BAA0B,CAAC;AAE3E,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAA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EACpC,aAA2D;IAE3D,MAAM,IAAI,GAAG,EAAwC,CAAC;IACtD,OAAO,IAAI,KAAK,CAAC,IAAI,EAAE;QACnB,GAAG,CAAC,MAAM,EAAE,GAAG;YACX,IAAI,GAAG,KAAK,MAAM,CAAC,QAAQ,EAAE,CAAC;gBAC1B,OAAO,QAAQ,CAAC;oBACZ,KAAK,MAAM,GAAG,IAAI,cAAc,EAAE,EAAE,CAAC;wBACjC,MAAM,IAAI,QAAQ,CACd,WAAW,CAAC,GAAG,CAAE,EACjB,iBAAiB,EACjB,aAAa,CAAC,GAAG,CAAC,CACrB,CAAC;oBACN,CAAC;gBACL,CAAC,CAAC;YACN,CAAC;YACD,IAAI,OAAO,GAAG,KAAK,QAAQ;gBAAE,OAAO,SAAS,CAAC;YAC9C,IAAI,GAAG,KAAK,QAAQ,EAAE,CAAC;gBACnB,OAAO,cAAc,EAAE,CAAC,MAAM,CAAC;YACnC,CAAC;YACD,MAAM,EAAE,GAAG,WAAW,CAAC,GAAc,CAAC,CAAC;YACvC,IAAI,CAAC,EAAE;gBAAE,OAAO,SAAS,CAAC;YAC1B,OAAO,IAAI,QAAQ,CACf,EAAuB,EACvB,iBAAiB,EACjB,aAAa,CAAC,GAAc,CAAC,CAChC,CAAC;QACN,CAAC;QACD,OAAO;YACH,OAAO,cAAc,EAAE,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;QACzD,CAAC;KACJ,CAAC,CAAC;AACP,CAAC"}
@@ -0,0 +1,14 @@
1
+ import * as S from "@effect/schema/Schema";
2
+ export function CoSchema(raw) {
3
+ return raw;
4
+ }
5
+ export function propSigToSchema(propSig) {
6
+ if ("ast" in propSig) {
7
+ return propSig;
8
+ }
9
+ else {
10
+ const ast = propSig.propertySignatureAST;
11
+ return S.make(ast.from);
12
+ }
13
+ }
14
+ //# sourceMappingURL=schemaHelpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schemaHelpers.js","sourceRoot":"","sources":["../src/schemaHelpers.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,CAAC,MAAM,uBAAuB,CAAC;AA4C3C,MAAM,UAAU,QAAQ,CACpB,GAAQ;IAER,OAAO,GAAU,CAAC;AACtB,CAAC;AAED,MAAM,UAAU,eAAe,CAG3B,OAAU;IAMV,IAAI,KAAK,IAAI,OAAO,EAAE,CAAC;QACnB,OAAO,OAAc,CAAC;IAC1B,CAAC;SAAM,CAAC;QACJ,MAAM,GAAG,GACL,OAGH,CAAC,oBAAoB,CAAC;QACvB,OAAO,CAAC,CAAC,IAAI,CAAsB,GAAG,CAAC,IAAI,CAAQ,CAAC;IACxD,CAAC;AACL,CAAC"}
@@ -0,0 +1,81 @@
1
+ export const subscriptionsScopes = new WeakMap();
2
+ export class SubscriptionScope {
3
+ constructor(root, rootSchema, onUpdate) {
4
+ this.scopeID = `scope-${Math.random().toString(36).slice(2)}`;
5
+ this.entries = new Map();
6
+ this.scheduledUpdate = false;
7
+ this.rootEntry = {
8
+ state: "loaded",
9
+ value: root,
10
+ rawUnsub: () => { }, // placeholder
11
+ };
12
+ this.entries.set(root.id, this.rootEntry);
13
+ subscriptionsScopes.set(root, this);
14
+ this.subscriber = root._loadedAs;
15
+ this.onUpdate = onUpdate;
16
+ this.rootEntry.rawUnsub = root._raw.core.subscribe((rawUpdate) => {
17
+ if (!rawUpdate)
18
+ return;
19
+ this.rootEntry.value = rootSchema.fromRaw(rawUpdate);
20
+ // console.log("root update", this.rootEntry.value.toJSON());
21
+ subscriptionsScopes.set(this.rootEntry.value, this);
22
+ this.scheduleUpdate();
23
+ });
24
+ }
25
+ scheduleUpdate() {
26
+ if (!this.scheduledUpdate) {
27
+ this.scheduledUpdate = true;
28
+ queueMicrotask(() => {
29
+ this.scheduledUpdate = false;
30
+ this.onUpdate(this.rootEntry.value);
31
+ });
32
+ }
33
+ }
34
+ onRefAccessedOrSet(accessedOrSetId) {
35
+ // console.log("onRefAccessedOrSet", this.scopeID, accessedOrSetId);
36
+ if (!accessedOrSetId) {
37
+ return;
38
+ }
39
+ if (!this.entries.has(accessedOrSetId)) {
40
+ const loadingEntry = {
41
+ state: "loading",
42
+ immediatelyUnsub: false,
43
+ };
44
+ this.entries.set(accessedOrSetId, loadingEntry);
45
+ this.subscriber._raw.core.node
46
+ .loadCoValueCore(accessedOrSetId)
47
+ .then((core) => {
48
+ if (loadingEntry.state === "loading" &&
49
+ loadingEntry.immediatelyUnsub) {
50
+ return;
51
+ }
52
+ if (core !== "unavailable") {
53
+ const entry = {
54
+ state: "loaded",
55
+ rawUnsub: () => { }, // placeholder
56
+ };
57
+ this.entries.set(accessedOrSetId, entry);
58
+ const rawUnsub = core.subscribe((rawUpdate) => {
59
+ // console.log("ref update", this.scopeID, accessedOrSetId, JSON.stringify(rawUpdate))
60
+ if (!rawUpdate)
61
+ return;
62
+ this.scheduleUpdate();
63
+ });
64
+ entry.rawUnsub = rawUnsub;
65
+ }
66
+ });
67
+ }
68
+ }
69
+ unsubscribeAll() {
70
+ for (const entry of this.entries.values()) {
71
+ if (entry.state === "loaded") {
72
+ entry.rawUnsub();
73
+ }
74
+ else {
75
+ entry.immediatelyUnsub = true;
76
+ }
77
+ }
78
+ this.entries.clear();
79
+ }
80
+ }
81
+ //# sourceMappingURL=subscriptionScope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subscriptionScope.js","sourceRoot":"","sources":["../src/subscriptionScope.ts"],"names":[],"mappings":"AAKA,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,OAAO,EAG3C,CAAC;AAEJ,MAAM,OAAO,iBAAiB;IAkB1B,YACI,IAA6B,EAC7B,UAAsB,EACtB,QAAoD;QAlBxD,YAAO,GAAW,SAAS,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;QAEjE,YAAO,GAAG,IAAI,GAAG,EAId,CAAC;QAOJ,oB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KAAK,CAAC,gBAAgB,GAAG,IAAI,CAAC;YAClC,CAAC;QACL,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;IACzB,CAAC;CACJ"}
@@ -0,0 +1,207 @@
1
+ import { expect, describe, test, beforeEach } from "vitest";
2
+ import { webcrypto } from "node:crypto";
3
+ import { connectedPeers } from "cojson/src/streamUtils.js";
4
+ import { newRandomSessionID } from "cojson/src/coValueCore.js";
5
+ import { Effect, Queue } from "effect";
6
+ import { Co, S, Account, jazzReady } from "..";
7
+ if (!("crypto" in globalThis)) {
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ globalThis.crypto = webcrypto;
10
+ }
11
+ beforeEach(async () => {
12
+ await jazzReady;
13
+ });
14
+ describe("Simple CoList operations", async () => {
15
+ const me = await Account.create({
16
+ name: "Hermes Puggington",
17
+ });
18
+ class TestList extends Co.list(S.string).as() {
19
+ }
20
+ const list = new TestList(["bread", "butter", "onion"], { owner: me });
21
+ test("Construction", () => {
22
+ expect(list[0]).toBe("bread");
23
+ expect(list[1]).toBe("butter");
24
+ expect(list[2]).toBe("onion");
25
+ expect(list._raw.asArray()).toEqual(["bread", "butter", "onion"]);
26
+ expect(list.length).toBe(3);
27
+ expect(list.map((item) => item.toUpperCase())).toEqual([
28
+ "BREAD",
29
+ "BUTTER",
30
+ "ONION",
31
+ ]);
32
+ });
33
+ describe("Mutation", () => {
34
+ test("assignment", () => {
35
+ const list = new TestList(["bread", "butter", "onion"], {
36
+ owner: me,
37
+ });
38
+ list[1] = "margarine";
39
+ expect(list._raw.asArray()).toEqual([
40
+ "bread",
41
+ "margarine",
42
+ "onion",
43
+ ]);
44
+ expect(list[1]).toBe("margarine");
45
+ });
46
+ test("push", () => {
47
+ const list = new TestList(["bread", "butter", "onion"], {
48
+ owner: me,
49
+ });
50
+ list.push("cheese");
51
+ expect(list[3]).toBe("cheese");
52
+ expect(list._raw.asArray()).toEqual([
53
+ "bread",
54
+ "butter",
55
+ "onion",
56
+ "cheese",
57
+ ]);
58
+ });
59
+ test("unshift", () => {
60
+ const list = new TestList(["bread", "butter", "onion"], {
61
+ owner: me,
62
+ });
63
+ list.unshift("lettuce");
64
+ expect(list[0]).toBe("lettuce");
65
+ expect(list._raw.asArray()).toEqual([
66
+ "lettuce",
67
+ "bread",
68
+ "butter",
69
+ "onion",
70
+ ]);
71
+ });
72
+ test("pop", () => {
73
+ const list = new TestList(["bread", "butter", "onion"], {
74
+ owner: me,
75
+ });
76
+ expect(list.pop()).toBe("onion");
77
+ expect(list.length).toBe(2);
78
+ expect(list._raw.asArray()).toEqual(["bread", "butter"]);
79
+ });
80
+ test("shift", () => {
81
+ const list = new TestList(["bread", "butter", "onion"], {
82
+ owner: me,
83
+ });
84
+ expect(list.shift()).toBe("bread");
85
+ expect(list.length).toBe(2);
86
+ expect(list._raw.asArray()).toEqual(["butter", "onion"]);
87
+ });
88
+ test("splice", () => {
89
+ const list = new TestList(["bread", "butter", "onion"], {
90
+ owner: me,
91
+ });
92
+ list.splice(1, 1, "salt", "pepper");
93
+ expect(list.length).toBe(4);
94
+ expect(list._raw.asArray()).toEqual([
95
+ "bread",
96
+ "salt",
97
+ "pepper",
98
+ "onion",
99
+ ]);
100
+ });
101
+ });
102
+ });
103
+ describe("CoList resolution", async () => {
104
+ class TwiceNestedList extends Co.list(S.string).as() {
105
+ joined() {
106
+ return this.join(",");
107
+ }
108
+ }
109
+ class NestedList extends Co.list(TwiceNestedList).as() {
110
+ }
111
+ class TestList extends Co.list(NestedList).as() {
112
+ }
113
+ const initNodeAndList = async () => {
114
+ const me = await Account.create({
115
+ name: "Hermes Puggington",
116
+ });
117
+ const list = new TestList([
118
+ new NestedList([new TwiceNestedList(["a", "b"], { owner: me })], { owner: me }),
119
+ new NestedList([new TwiceNestedList(["c", "d"], { owner: me })], { owner: me }),
120
+ ], { owner: me });
121
+ return { me, list };
122
+ };
123
+ test("Construction", async () => {
124
+ const { list } = await initNodeAndList();
125
+ expect(list[0]?.[0]?.[0]).toBe("a");
126
+ expect(list[0]?.[0]?.joined()).toBe("a,b");
127
+ expect(list[0]?.[0]?.id).toBeDefined();
128
+ expect(list[1]?.[0]?.[0]).toBe("c");
129
+ });
130
+ test("Loading and availability", async () => {
131
+ const { me, list } = await initNodeAndList();
132
+ const [initialAsPeer, secondPeer] = connectedPeers("initial", "second", { peer1role: "server", peer2role: "client" });
133
+ me._raw.core.node.syncManager.addPeer(secondPeer);
134
+ const meOnSecondPeer = await Account.become({
135
+ accountID: me.id,
136
+ accountSecret: me._raw.agentSecret,
137
+ peersToLoadFrom: [initialAsPeer],
138
+ sessionID: newRandomSessionID(me.id),
139
+ });
140
+ const loadedList = await TestList.load(list.id, { as: meOnSecondPeer });
141
+ expect(loadedList?.[0]).toBe(undefined);
142
+ expect(loadedList?._refs[0]?.id).toEqual(list[0].id);
143
+ const loadedNestedList = await NestedList.load(list[0].id, {
144
+ as: meOnSecondPeer,
145
+ });
146
+ expect(loadedList?.[0]).toBeDefined();
147
+ expect(loadedList?.[0]?.[0]).toBeUndefined();
148
+ expect(loadedList?.[0]?._refs[0]?.id).toEqual(list[0][0].id);
149
+ expect(loadedList?._refs[0]?.value).toEqual(loadedNestedList);
150
+ const loadedTwiceNestedList = await TwiceNestedList.load(list[0][0].id, { as: meOnSecondPeer });
151
+ expect(loadedList?.[0]?.[0]).toBeDefined();
152
+ expect(loadedList?.[0]?.[0]?.[0]).toBe("a");
153
+ expect(loadedList?.[0]?.[0]?.joined()).toBe("a,b");
154
+ expect(loadedList?.[0]?._refs[0]?.id).toEqual(list[0]?.[0]?.id);
155
+ expect(loadedList?.[0]?._refs[0]?.value).toEqual(loadedTwiceNestedList);
156
+ const otherNestedList = new NestedList([new TwiceNestedList(["e", "f"], { owner: meOnSecondPeer })], { owner: meOnSecondPeer });
157
+ loadedList[0] = otherNestedList;
158
+ expect(loadedList?.[0]).toEqual(otherNestedList);
159
+ expect(loadedList?._refs[0]?.id).toEqual(otherNestedList.id);
160
+ });
161
+ test("Subscription & auto-resolution", async () => {
162
+ const { me, list } = await initNodeAndList();
163
+ const [initialAsPeer, secondPeer] = connectedPeers("initial", "second", { peer1role: "server", peer2role: "client" });
164
+ me._raw.core.node.syncManager.addPeer(secondPeer);
165
+ const meOnSecondPeer = await Account.become({
166
+ accountID: me.id,
167
+ accountSecret: me._raw.agentSecret,
168
+ peersToLoadFrom: [initialAsPeer],
169
+ sessionID: newRandomSessionID(me.id),
170
+ });
171
+ await Effect.runPromise(Effect.gen(function* ($) {
172
+ const queue = yield* $(Queue.unbounded());
173
+ TestList.subscribe(list.id, { as: meOnSecondPeer }, (subscribedList) => {
174
+ console.log("subscribedList?.[0]?.[0]?.[0]", subscribedList?.[0]?.[0]?.[0]);
175
+ Effect.runPromise(Queue.offer(queue, subscribedList));
176
+ });
177
+ const update1 = yield* $(Queue.take(queue));
178
+ expect(update1?.[0]).toEqual(undefined);
179
+ const update2 = yield* $(Queue.take(queue));
180
+ expect(update2?.[0]).toBeDefined();
181
+ expect(update2?.[0]?.[0]).toBeUndefined();
182
+ const update3 = yield* $(Queue.take(queue));
183
+ expect(update3?.[0]?.[0]).toBeDefined();
184
+ expect(update3?.[0]?.[0]?.[0]).toBe("a");
185
+ expect(update3?.[0]?.[0]?.joined()).toBe("a,b");
186
+ update3[0][0][0] = "x";
187
+ const update4 = yield* $(Queue.take(queue));
188
+ expect(update4?.[0]?.[0]?.[0]).toBe("x");
189
+ // When assigning a new nested value, we get an update
190
+ const newTwiceNestedList = new TwiceNestedList(["y", "z"], {
191
+ owner: meOnSecondPeer,
192
+ });
193
+ const newNestedList = new NestedList([newTwiceNestedList], {
194
+ owner: meOnSecondPeer,
195
+ });
196
+ update4[0] = newNestedList;
197
+ const update5 = yield* $(Queue.take(queue));
198
+ expect(update5?.[0]?.[0]?.[0]).toBe("y");
199
+ expect(update5?.[0]?.[0]?.joined()).toBe("y,z");
200
+ // we get updates when the new nested value changes
201
+ newTwiceNestedList[0] = "w";
202
+ const update6 = yield* $(Queue.take(queue));
203
+ expect(update6?.[0]?.[0]?.[0]).toBe("w");
204
+ }));
205
+ });
206
+ });
207
+ //# sourceMappingURL=coList.test.js.map