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,218 @@
1
+ import { Effect, Sink, Stream } from "effect";
2
+ import type { CojsonInternalTypes, RawCoValue } from "cojson";
3
+ import { RawAccount } from "cojson";
4
+ import type { Me, UnavailableError } from "../internal.js";
5
+ import {
6
+ Account,
7
+ AccountCtx,
8
+ Group,
9
+ SubscriptionScope,
10
+ Ref,
11
+ inspect,
12
+ } from "../internal.js";
13
+
14
+ export type SubclassedConstructor<T> = {
15
+ new (...args: any[]): T;
16
+ };
17
+
18
+ export interface CoValueClass<Value extends CoValue = CoValue, Init = any> {
19
+ /** @category Construction and loading */
20
+ new (init: Init, options: { owner: Account | Group }): Value;
21
+
22
+ /** @ignore */
23
+ fromRaw(raw: Value["_raw"]): Value;
24
+
25
+ /** @category Construction and loading */
26
+ load<V extends Value>(
27
+ this: SubclassedConstructor<V>,
28
+ id: ID<V>,
29
+ options: {
30
+ as: Account & Me;
31
+ onProgress?: (progress: number) => void;
32
+ }
33
+ ): Promise<V | undefined>;
34
+
35
+ /** @category Construction and loading */
36
+ loadEf<V extends Value>(
37
+ this: SubclassedConstructor<V>,
38
+ id: ID<V>
39
+ ): Effect.Effect<V, UnavailableError, AccountCtx>;
40
+
41
+ /** @category Subscription */
42
+ subscribe<V extends Value, Acc extends Account>(
43
+ this: SubclassedConstructor<V>,
44
+ id: ID<V>,
45
+ options: { as: Acc & Me },
46
+ onUpdate: (value: V) => void
47
+ ): () => void;
48
+
49
+ /** @category Subscription */
50
+ subscribeEf<V extends Value>(
51
+ this: SubclassedConstructor<V>,
52
+ id: ID<V>
53
+ ): Stream.Stream<V, UnavailableError, AccountCtx>;
54
+ }
55
+
56
+ /** @category Schemas & CoValues - Abstract interfaces */
57
+ export interface CoValue<Type extends string = string, Raw = any> {
58
+ /** @category Value identity */
59
+ readonly id: ID<this>;
60
+ /** @category Value identity */
61
+ _type: Type;
62
+ /** @category Collaboration */
63
+ _owner: Account | Group;
64
+ /** @category Subscription */
65
+ subscribe(listener: (update: this) => void): () => void;
66
+ /** @category Subscription */
67
+ subscribeEf(): Stream.Stream<this, UnavailableError, never>;
68
+ /** @category Internals */
69
+ _raw: Raw;
70
+ /** @category Internals */
71
+ readonly _loadedAs: Account & Me;
72
+ /** @category Stringifying & inspection */
73
+ toJSON(): any[] | object;
74
+ /** @category Stringifying & inspection */
75
+ [inspect](): any;
76
+ }
77
+
78
+ export function isCoValue(value: any): value is CoValue {
79
+ return value && value._type !== undefined;
80
+ }
81
+ export function isCoValueClass(value: any): value is CoValueClass {
82
+ return typeof value === "function" && value.fromRaw !== undefined;
83
+ }
84
+
85
+ /** @category Schemas & CoValues - Abstract interfaces */
86
+ export type ID<T> = CojsonInternalTypes.RawCoID & {
87
+ readonly __type: T;
88
+ };
89
+
90
+ export class CoValueBase implements CoValue {
91
+ id!: ID<this>;
92
+ _type!: string;
93
+ _raw!: RawCoValue;
94
+
95
+ get _owner(): Account | Group {
96
+ return this._raw.group instanceof RawAccount
97
+ ? Account.fromRaw(this._raw.group)
98
+ : Group.fromRaw(this._raw.group);
99
+ }
100
+
101
+ get _loadedAs() {
102
+ return Account.fromNode(this._raw.core.node);
103
+ }
104
+
105
+ constructor(..._args: any) {}
106
+
107
+ static fromRaw<V extends CoValue>(
108
+ this: SubclassedConstructor<V>,
109
+ raw: RawCoValue
110
+ ): V {
111
+ return new this(undefined, { fromRaw: raw });
112
+ }
113
+
114
+ static loadEf<V extends CoValue>(
115
+ this: SubclassedConstructor<V> & typeof CoValueBase,
116
+ id: ID<V>
117
+ ): Effect.Effect<V, UnavailableError, AccountCtx> {
118
+ return Effect.gen(this, function* (_) {
119
+ const account = yield* _(AccountCtx);
120
+ return yield* _(
121
+ new Ref(id as ID<V>, account, this as CoValueClass<V>).loadEf()
122
+ );
123
+ });
124
+ }
125
+
126
+ static load<V extends CoValue>(
127
+ this: SubclassedConstructor<V> & typeof CoValueBase,
128
+ id: ID<V>,
129
+ options: {
130
+ as: Account & Me;
131
+ onProgress?: (progress: number) => void;
132
+ }
133
+ ): Promise<V | undefined> {
134
+ return new Ref(id as ID<V>, options.as, this as CoValueClass<V>).load(
135
+ options?.onProgress && { onProgress: options.onProgress }
136
+ );
137
+ }
138
+
139
+ static subscribe<V extends CoValue, Acc extends Account>(
140
+ this: SubclassedConstructor<V> & typeof CoValueBase,
141
+ id: ID<V>,
142
+ options: { as: Acc & Me },
143
+ onUpdate: (value: V) => void
144
+ ): () => void {
145
+ void Effect.runPromise(
146
+ Effect.provideService(
147
+ this.subscribeEf(id).pipe(
148
+ Stream.run(
149
+ Sink.forEach((update) =>
150
+ Effect.sync(() => onUpdate(update))
151
+ )
152
+ )
153
+ ),
154
+ AccountCtx,
155
+ options.as as Account & Me
156
+ )
157
+ );
158
+
159
+ return function unsubscribe() {};
160
+ }
161
+
162
+ static subscribeEf<V extends CoValue>(
163
+ this: SubclassedConstructor<V> & typeof CoValueBase,
164
+ id: ID<V>
165
+ ): Stream.Stream<V, UnavailableError, AccountCtx> {
166
+ return Stream.fromEffect(this.loadEf(id)).pipe(
167
+ Stream.flatMap((value) =>
168
+ Stream.asyncScoped<V, UnavailableError>((emit) =>
169
+ Effect.gen(this, function* (_) {
170
+ const subscription = new SubscriptionScope(
171
+ value,
172
+ this,
173
+ (update) => {
174
+ void emit.single(update as V);
175
+ }
176
+ );
177
+
178
+ yield* _(
179
+ Effect.addFinalizer(() =>
180
+ Effect.sync(() => subscription.unsubscribeAll())
181
+ )
182
+ );
183
+ })
184
+ )
185
+ )
186
+ );
187
+ }
188
+
189
+ subscribe(listener: (update: this) => void): () => void {
190
+ return (this.constructor as unknown as typeof CoValueBase).subscribe(
191
+ this.id as unknown as ID<CoValue>,
192
+ { as: this._loadedAs },
193
+ listener as unknown as (update: CoValue) => void
194
+ );
195
+ }
196
+
197
+ subscribeEf(): Stream.Stream<this, UnavailableError, never> {
198
+ return Stream.provideService(
199
+ (this.constructor as unknown as typeof CoValueBase).subscribeEf(
200
+ this.id as unknown as ID<CoValue>
201
+ ),
202
+ AccountCtx,
203
+ this._loadedAs
204
+ ) as unknown as Stream.Stream<this, UnavailableError, never>;
205
+ }
206
+
207
+ toJSON(): object | any[] {
208
+ return {
209
+ id: this.id,
210
+ type: this._type,
211
+ error: "unknown CoValue class",
212
+ };
213
+ }
214
+
215
+ [inspect]() {
216
+ return this.toJSON();
217
+ }
218
+ }
@@ -0,0 +1,2 @@
1
+ export const inspect = Symbol.for("nodejs.util.inspect.custom");
2
+ export type inspect = typeof inspect;
@@ -1,20 +1,31 @@
1
1
  import { Effect } from "effect";
2
- import { CoValue, ID } from "./coValueInterfaces.js";
3
- import { ControlledAccount } from "./coValues/account/account.js";
4
- import { CoID, RawCoValue } from "cojson";
5
- import { UnavailableError } from "./errors.js";
6
- import { getCoValueConstructorInProperty } from "./coValues/resolution.js";
7
- import { PropDef } from "./schemaHelpers.js";
8
- import { subscriptionsScopes } from "./subscriptionScope.js";
2
+ import type { CoID, RawCoValue } from "cojson";
3
+ import type {
4
+ Account,
5
+ CoValue,
6
+ ID,
7
+ Me,
8
+ RefEncoded,
9
+ UnavailableError,
10
+ } from "../internal.js";
11
+ import {
12
+ instantiateRefEncoded,
13
+ isRefEncoded,
14
+ subscriptionsScopes,
15
+ } from "../internal.js";
9
16
 
10
- export class ValueRef<V extends CoValue> {
17
+ export class Ref<V extends CoValue> {
11
18
  private cachedValue: V | undefined;
12
19
 
13
20
  constructor(
14
21
  readonly id: ID<V>,
15
- readonly controlledAccount: ControlledAccount,
16
- readonly propDef: PropDef<any>
17
- ) {}
22
+ readonly controlledAccount: Account & Me,
23
+ readonly schema: RefEncoded<V>
24
+ ) {
25
+ if (!isRefEncoded(schema)) {
26
+ throw new Error("Ref must be constructed with a ref schema");
27
+ }
28
+ }
18
29
 
19
30
  get value() {
20
31
  if (this.cachedValue) return this.cachedValue;
@@ -23,18 +34,11 @@ export class ValueRef<V extends CoValue> {
23
34
  this.id as unknown as CoID<RawCoValue>
24
35
  );
25
36
  if (raw) {
26
- const Constructor = getCoValueConstructorInProperty(
27
- this.propDef,
28
- raw
29
- );
30
- if (!Constructor) {
31
- throw new Error(
32
- "Couldn't extract CoValue constructor from property definition"
33
- );
34
- }
35
- const value = new Constructor(undefined, { fromRaw: raw }) as V;
37
+ const value = instantiateRefEncoded(this.schema, raw);
36
38
  this.cachedValue = value;
37
39
  return value;
40
+ } else {
41
+ return null;
38
42
  }
39
43
  }
40
44
 
@@ -54,7 +58,9 @@ export class ValueRef<V extends CoValue> {
54
58
  });
55
59
  }
56
60
 
57
- private async loadHelper(options?: {onProgress: (p: number) => void}): Promise<V | "unavailable"> {
61
+ private async loadHelper(options?: {
62
+ onProgress: (p: number) => void;
63
+ }): Promise<V | "unavailable"> {
58
64
  const raw = await this.controlledAccount._raw.core.node.load(
59
65
  this.id as unknown as CoID<RawCoValue>,
60
66
  options?.onProgress
@@ -62,12 +68,14 @@ export class ValueRef<V extends CoValue> {
62
68
  if (raw === "unavailable") {
63
69
  return "unavailable";
64
70
  } else {
65
- return new ValueRef(this.id, this.controlledAccount, this.propDef)
71
+ return new Ref(this.id, this.controlledAccount, this.schema)
66
72
  .value!;
67
73
  }
68
74
  }
69
75
 
70
- async load(options?: {onProgress: (p: number) => void}): Promise<V | undefined> {
76
+ async load(options?: {
77
+ onProgress: (p: number) => void;
78
+ }): Promise<V | undefined> {
71
79
  const result = await this.loadHelper(options);
72
80
  if (result === "unavailable") {
73
81
  return undefined;
@@ -76,7 +84,7 @@ export class ValueRef<V extends CoValue> {
76
84
  }
77
85
  }
78
86
 
79
- accessFrom(fromScopeValue: CoValue): V | undefined {
87
+ accessFrom(fromScopeValue: CoValue): V | null {
80
88
  const subScope = subscriptionsScopes.get(fromScopeValue);
81
89
 
82
90
  subScope?.onRefAccessedOrSet(this.id);
@@ -89,22 +97,28 @@ export class ValueRef<V extends CoValue> {
89
97
  }
90
98
  }
91
99
 
92
- export function makeRefs<F extends { [key: string | number]: CoValue }>(
93
- getIdForKey: <K extends keyof F>(key: K) => F[K]["id"] | undefined,
94
- getKeysWithIds: () => (keyof F)[],
95
- controlledAccount: ControlledAccount,
96
- propDefForKey: <K extends keyof F>(key: K) => PropDef<F[K]>
97
- ): { [K in keyof F]: ValueRef<F[K]> } {
98
- const refs = {} as { [K in keyof F]: ValueRef<F[K]> };
100
+ export function makeRefs<Keys extends string | number>(
101
+ getIdForKey: (key: Keys) => ID<CoValue> | undefined,
102
+ getKeysWithIds: () => Keys[],
103
+ controlledAccount: Account & Me,
104
+ refSchemaForKey: (key: Keys) => RefEncoded<CoValue>
105
+ ): { [K in Keys]: Ref<CoValue> } & {
106
+ [Symbol.iterator]: () => IterableIterator<Ref<CoValue>>;
107
+ length: number;
108
+ } {
109
+ const refs = {} as { [K in Keys]: Ref<CoValue> } & {
110
+ [Symbol.iterator]: () => IterableIterator<Ref<CoValue>>;
111
+ length: number;
112
+ };
99
113
  return new Proxy(refs, {
100
- get(target, key) {
114
+ get(_target, key) {
101
115
  if (key === Symbol.iterator) {
102
116
  return function* () {
103
117
  for (const key of getKeysWithIds()) {
104
- yield new ValueRef(
118
+ yield new Ref(
105
119
  getIdForKey(key)!,
106
120
  controlledAccount,
107
- propDefForKey(key)
121
+ refSchemaForKey(key)
108
122
  );
109
123
  }
110
124
  };
@@ -113,12 +127,12 @@ export function makeRefs<F extends { [key: string | number]: CoValue }>(
113
127
  if (key === "length") {
114
128
  return getKeysWithIds().length;
115
129
  }
116
- const id = getIdForKey(key as keyof F);
130
+ const id = getIdForKey(key as Keys);
117
131
  if (!id) return undefined;
118
- return new ValueRef(
119
- id as ID<F[typeof key]>,
132
+ return new Ref(
133
+ id as ID<CoValue>,
120
134
  controlledAccount,
121
- propDefForKey(key as keyof F)
135
+ refSchemaForKey(key as Keys)
122
136
  );
123
137
  },
124
138
  ownKeys() {
@@ -0,0 +1,124 @@
1
+ import type { JsonValue, RawCoValue } from "cojson";
2
+ import { type CoValue, type CoValueClass, isCoValueClass } from "../internal.js";
3
+ import type { Schema as EffectSchema, TypeId } from "@effect/schema/Schema";
4
+
5
+ export type CoMarker = { readonly __co: unique symbol };
6
+ export type co<T> = T | (T & CoMarker);
7
+ export type IfCo<C, R> = C extends infer _A | infer B
8
+ ? B extends CoMarker
9
+ ? R
10
+ : never
11
+ : never;
12
+
13
+ export const SchemaInit = Symbol.for("SchemaInit");
14
+ export type SchemaInit = typeof SchemaInit;
15
+
16
+ export const InitValues = Symbol.for("InitValues");
17
+ export type InitValues = typeof InitValues;
18
+
19
+ export const ItemsSym = Symbol.for("items");
20
+ export type ItemsSym = typeof ItemsSym;
21
+
22
+ export const co = {
23
+ string: {
24
+ [SchemaInit]: "json" satisfies Schema,
25
+ } as unknown as co<string>,
26
+ number: {
27
+ [SchemaInit]: "json" satisfies Schema,
28
+ } as unknown as co<number>,
29
+ boolean: {
30
+ [SchemaInit]: "json" satisfies Schema,
31
+ } as unknown as co<boolean>,
32
+ literal: <T extends string | number | boolean>(_lit: T): co<T> => {
33
+ return { [SchemaInit]: "json" satisfies Schema } as any;
34
+ },
35
+ json: <T extends JsonValue>(): co<T> => {
36
+ return { [SchemaInit]: "json" satisfies Schema } as any;
37
+ },
38
+ encoded: <T>(arg: Encoder<T>): co<T> => {
39
+ return { [SchemaInit]: { encoded: arg } satisfies Schema } as any;
40
+ },
41
+ ref: <C extends CoValueClass>(
42
+ arg: C | ((_raw: InstanceType<C>["_raw"]) => C)
43
+ ): co<InstanceType<C> | null> => {
44
+ return { [SchemaInit]: arg satisfies Schema } as any;
45
+ },
46
+ items: ItemsSym as ItemsSym,
47
+ };
48
+
49
+ export type JsonEncoded = "json";
50
+ export type EncodedAs<V> = { encoded: Encoder<V> };
51
+ export type RefEncoded<V extends CoValue> =
52
+ | CoValueClass<V>
53
+ | ((raw: RawCoValue) => CoValueClass<V>);
54
+
55
+ export function isRefEncoded<V extends CoValue>(
56
+ schema: Schema
57
+ ): schema is RefEncoded<V> {
58
+ return typeof schema === "function";
59
+ }
60
+
61
+ export function instantiateRefEncoded<V extends CoValue>(
62
+ schema: RefEncoded<V>,
63
+ raw: RawCoValue
64
+ ): V {
65
+ return isCoValueClass(schema)
66
+ ? schema.fromRaw(raw)
67
+ : (schema as (raw: RawCoValue) => CoValueClass<V>)(raw).fromRaw(raw);
68
+ }
69
+
70
+ export type Schema = JsonEncoded | RefEncoded<CoValue> | EncodedAs<any>;
71
+
72
+ export type SchemaFor<Field> = NonNullable<Field> extends CoValue
73
+ ? RefEncoded<NonNullable<Field>>
74
+ : NonNullable<Field> extends JsonValue
75
+ ? JsonEncoded
76
+ : EncodedAs<NonNullable<Field>>;
77
+
78
+ export type EffectSchemaWithInputAndOutput<A, I = A> = EffectSchema<
79
+ any,
80
+ any,
81
+ never
82
+ > & {
83
+ [TypeId]: {
84
+ _A: (_: any) => A;
85
+ _I: (_: any) => I;
86
+ };
87
+ };
88
+
89
+ export type Encoder<V> = EffectSchemaWithInputAndOutput<V, JsonValue>;
90
+
91
+ import { Date } from "@effect/schema/Schema";
92
+
93
+ export const Encoders = {
94
+ Date,
95
+ };
96
+
97
+ export type EnsureCoValueNullable<
98
+ V,
99
+ Key extends string | ItemsSym,
100
+ > = NonNullable<V> extends CoValue
101
+ ? null extends V
102
+ ? V
103
+ : Key extends string
104
+ ? [
105
+ `👋 CoMap fields that are CoValue references should be nullable, declare ${Key} as:`,
106
+ V | null,
107
+ ]
108
+ : [
109
+ `👋 CoMap fields that are CoValue references should be nullable, declare _item as:`,
110
+ V | null,
111
+ ]
112
+ : V;
113
+
114
+ export type ValidItem<
115
+ Item,
116
+ ContainerType extends string,
117
+ > = NonNullable<Item> extends CoValue
118
+ ? null extends Item
119
+ ? any
120
+ : [
121
+ `👋 CoList items that are CoValue references should be nullable, make sure the Item generic parameter of ${ContainerType} is:`,
122
+ Item | null,
123
+ ]
124
+ : any;
@@ -1,7 +1,5 @@
1
- import { ControlledAccount } from "./coValues/account/account.js";
2
- import { CoValueSchema, CoValue, ID } from "./coValueInterfaces.js";
3
- import * as S from "@effect/schema/Schema";
4
- import { RawCoValue } from "cojson";
1
+ import type { RawCoValue } from "cojson";
2
+ import type { Account, CoValue, CoValueBase, ID, Me, SubclassedConstructor } from "../internal.js";
5
3
 
6
4
  export const subscriptionsScopes = new WeakMap<
7
5
  CoValue,
@@ -9,10 +7,10 @@ export const subscriptionsScopes = new WeakMap<
9
7
  >();
10
8
 
11
9
  export class SubscriptionScope<
12
- RootSchema extends CoValueSchema = CoValueSchema,
10
+ Root extends CoValue
13
11
  > {
14
12
  scopeID: string = `scope-${Math.random().toString(36).slice(2)}`;
15
- subscriber: ControlledAccount;
13
+ subscriber: Account & Me;
16
14
  entries = new Map<
17
15
  ID<CoValue>,
18
16
  | { state: "loading"; immediatelyUnsub?: boolean }
@@ -20,16 +18,16 @@ export class SubscriptionScope<
20
18
  >();
21
19
  rootEntry: {
22
20
  state: "loaded";
23
- value: S.Schema.To<RootSchema>;
21
+ value: Root;
24
22
  rawUnsub: () => void;
25
23
  };
26
- onUpdate: (newRoot: S.Schema.To<RootSchema>) => void;
24
+ onUpdate: (newRoot: Root) => void;
27
25
  scheduledUpdate: boolean = false;
28
26
 
29
27
  constructor(
30
- root: S.Schema.To<RootSchema>,
31
- rootSchema: RootSchema,
32
- onUpdate: (newRoot: S.Schema.To<RootSchema>) => void
28
+ root: Root,
29
+ rootSchema: SubclassedConstructor<Root> & typeof CoValueBase,
30
+ onUpdate: (newRoot: Root) => void
33
31
  ) {
34
32
  this.rootEntry = {
35
33
  state: "loaded" as const,
@@ -47,7 +45,7 @@ export class SubscriptionScope<
47
45
  if (!rawUpdate) return;
48
46
  this.rootEntry.value = rootSchema.fromRaw(
49
47
  rawUpdate
50
- ) as S.Schema.To<RootSchema>;
48
+ ) as Root;
51
49
  // console.log("root update", this.rootEntry.value.toJSON());
52
50
  subscriptionsScopes.set(this.rootEntry.value, this);
53
51
  this.scheduleUpdate();
@@ -77,7 +75,7 @@ export class SubscriptionScope<
77
75
  immediatelyUnsub: false,
78
76
  } as const;
79
77
  this.entries.set(accessedOrSetId, loadingEntry);
80
- this.subscriber._raw.core.node
78
+ void this.subscriber._raw.core.node
81
79
  .loadCoValueCore(accessedOrSetId)
82
80
  .then((core) => {
83
81
  if (
package/src/index.ts CHANGED
@@ -1,68 +1,25 @@
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 {
5
- BinaryCoStreamImpl,
6
- CoStreamOf,
7
- } from "./coValues/coStream/coStreamOf.js";
8
- import { ImageDefinition } from "./coValues/extensions/imageDef.js";
9
- import { GroupOf } from "./coValues/group/groupOf.js";
10
-
11
- /** @hidden */
12
- export * as S from "@effect/schema/Schema";
13
-
14
- /** @category Schemas & CoValues - Schema definers */
15
- export const Co = {
16
- map: CoMapOf,
17
- list: CoListOf,
18
- stream: CoStreamOf,
19
- binaryStream: BinaryCoStreamImpl,
20
- account: AccountOf,
21
- group: GroupOf,
22
- media: {
23
- imageDef: ImageDefinition
24
- }
25
- };
26
-
27
- /** @category Internal types */
28
1
  export {
2
+ /** @category Internal types */
29
3
  cojsonReady as jazzReady,
4
+ cojsonInternals,
5
+ MAX_RECOMMENDED_TX_SIZE,
6
+ } from "cojson";
7
+ export type {
30
8
  InviteSecret,
31
9
  Peer,
32
10
  SessionID,
33
11
  AgentID,
34
12
  SyncMessage,
35
- cojsonInternals,
36
- MAX_RECOMMENDED_TX_SIZE,
37
13
  } from "cojson";
38
14
 
39
- export { ID, CoValue, CoValueSchema } from "./coValueInterfaces.js";
40
-
41
-
42
- export { CoMap, CoMapSchema } from "./coValues/coMap/coMap.js";
43
- /** @category Schemas & CoValues - CoMap */
44
- export * as CoMapInternals from "./coValues/coMap/internalDocs.js";
45
-
46
- export { CoList, CoListSchema } from "./coValues/coList/coList.js";
47
- /** @category Schemas & CoValues - CoList */
48
- export * as CoListInternals from "./coValues/coList/internalDocs.js";
49
-
50
- export {
51
- CoStream,
52
- CoStreamSchema,
53
- BinaryCoStream,
54
- } from "./coValues/coStream/coStream.js";
55
-
56
- export {
57
- AnyAccount,
58
- ControlledAccount,
59
- AccountSchema,
60
- controlledAccountSym,
61
- } from "./coValues/account/account.js";
15
+ export type { ID, CoValue } from "./internal.js";
62
16
 
63
- export { AccountMigration } from "./coValues/account/migration.js";
64
- export { Account, BaseProfile } from "./coValues/account/accountOf.js";
65
- export { AnyGroup } from "./coValues/group/group.js";
66
- export { Group } from "./coValues/group/groupOf.js";
17
+ export { Encoders, co } from "./internal.js";
67
18
 
68
- export { ImageDefinition } from "./coValues/extensions/imageDef.js";
19
+ export { CoMap } from "./internal.js";
20
+ export { CoList } from "./internal.js";
21
+ export { CoStream, BinaryCoStream } from "./internal.js";
22
+ export { Group, Profile } from "./internal.js";
23
+ export { Account, type Me } from "./internal.js";
24
+ export { ImageDefinition } from "./internal.js";
25
+ export { CoValueBase, type CoValueClass } from "./internal.js";
@@ -0,0 +1,15 @@
1
+ export * from './implementation/inspect.js';
2
+ export * from "./coValues/interfaces.js";
3
+
4
+ export * from "./coValues/coMap.js";
5
+ export * from "./coValues/account.js";
6
+ export * from "./coValues/coList.js";
7
+ export * from "./coValues/coStream.js";
8
+ export * from "./coValues/group.js";
9
+
10
+ export * from "./implementation/errors.js";
11
+ export * from "./implementation/refs.js";
12
+ export * from "./implementation/schema.js";
13
+ export * from "./implementation/subscriptionScope.js";
14
+
15
+ export * from "./coValues/extensions/imageDef.js";