jazz-tools 0.7.0-alpha.35 → 0.7.0-alpha.37

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. package/.turbo/turbo-build.log +36 -30
  2. package/CHANGELOG.md +20 -0
  3. package/dist/coValues/account.js +5 -4
  4. package/dist/coValues/account.js.map +1 -1
  5. package/dist/coValues/coList.js +2 -1
  6. package/dist/coValues/coList.js.map +1 -1
  7. package/dist/coValues/coMap.js +6 -4
  8. package/dist/coValues/coMap.js.map +1 -1
  9. package/dist/coValues/coStream.js +2 -2
  10. package/dist/coValues/coStream.js.map +1 -1
  11. package/dist/coValues/extensions/imageDef.js +1 -1
  12. package/dist/coValues/extensions/imageDef.js.map +1 -1
  13. package/dist/coValues/group.js +1 -1
  14. package/dist/coValues/group.js.map +1 -1
  15. package/dist/coValues/interfaces.js +16 -9
  16. package/dist/coValues/interfaces.js.map +1 -1
  17. package/dist/implementation/devtoolsFormatters.js +113 -0
  18. package/dist/implementation/devtoolsFormatters.js.map +1 -0
  19. package/dist/implementation/refs.js +24 -4
  20. package/dist/implementation/refs.js.map +1 -1
  21. package/dist/implementation/subscriptionScope.js +14 -1
  22. package/dist/implementation/subscriptionScope.js.map +1 -1
  23. package/dist/internal.js +1 -0
  24. package/dist/internal.js.map +1 -1
  25. package/package.json +3 -3
  26. package/src/coValues/account.ts +5 -4
  27. package/src/coValues/coList.ts +11 -8
  28. package/src/coValues/coMap.ts +12 -4
  29. package/src/coValues/coStream.ts +2 -2
  30. package/src/coValues/extensions/imageDef.ts +1 -1
  31. package/src/coValues/group.ts +1 -1
  32. package/src/coValues/interfaces.ts +33 -20
  33. package/src/implementation/devtoolsFormatters.ts +109 -0
  34. package/src/implementation/refs.ts +22 -4
  35. package/src/implementation/subscriptionScope.ts +17 -1
  36. package/src/internal.ts +3 -1
@@ -0,0 +1,109 @@
1
+ import { ItemsSym } from "./symbols.js";
2
+
3
+ (globalThis as any).devtoolsFormatters = [
4
+ {
5
+ header: (object: any) => {
6
+ if (object._type === "CoMap") {
7
+ return ["div", {}, ["span", {}, object.constructor.name]];
8
+ } else if (object._type === "CoList") {
9
+ return [
10
+ "div",
11
+ {},
12
+ [
13
+ "span",
14
+ {},
15
+ object.constructor.name + "(" + object.length + ") ",
16
+ ],
17
+ ];
18
+ } else if (object._type === "Account") {
19
+ return [
20
+ "div",
21
+ {},
22
+ [
23
+ "span",
24
+ {},
25
+ object.constructor.name +
26
+ "(" +
27
+ object._refs.profile.value?.name +
28
+ (object.isMe ? " ME" : "") +
29
+ ")",
30
+ ],
31
+ ];
32
+ } else {
33
+ return null;
34
+ }
35
+ },
36
+ hasBody: function () {
37
+ return true;
38
+ },
39
+ body: function (object: any) {
40
+ if (object._type === "CoMap" || object._type === "Account") {
41
+ return [
42
+ "div",
43
+ { style: "margin-left: 15px" },
44
+ ["div", "id: ", ["object", { object: object.id }]],
45
+ ...Object.entries(object).map(([k, v]) => [
46
+ "div",
47
+ { style: "white-space: nowrap;" },
48
+ [
49
+ "span",
50
+ { style: "font-weight: bold; opacity: 0.6" },
51
+ k,
52
+ ": ",
53
+ ],
54
+ ["object", { object: v }],
55
+ ...(typeof object._schema[k] === "function"
56
+ ? v === null
57
+ ? [
58
+ [
59
+ "span",
60
+ { style: "opacity: 0.5" },
61
+ ` (pending ${object._schema[k].name} `,
62
+ [
63
+ "object",
64
+ { object: object._refs[k] },
65
+ ],
66
+ ")",
67
+ ],
68
+ ]
69
+ : []
70
+ : []),
71
+ ]),
72
+ ];
73
+ } else if (object._type === "CoList") {
74
+ return [
75
+ "div",
76
+ { style: "margin-left: 15px" },
77
+ ["div", "id: ", ["object", { object: object.id }]],
78
+ ...(object as any[]).map((v, i) => [
79
+ "div",
80
+ { style: "white-space: nowrap;" },
81
+ [
82
+ "span",
83
+ { style: "font-weight: bold; opacity: 0.6" },
84
+ i,
85
+ ": ",
86
+ ],
87
+ ["object", { object: v }],
88
+ ...(typeof object._schema[ItemsSym] === "function"
89
+ ? v === null
90
+ ? [
91
+ [
92
+ "span",
93
+ { style: "opacity: 0.5" },
94
+ ` (pending ${object._schema[ItemsSym].name} `,
95
+ [
96
+ "object",
97
+ { object: object._refs[i] },
98
+ ],
99
+ ")",
100
+ ],
101
+ ]
102
+ : []
103
+ : []),
104
+ ]),
105
+ ];
106
+ }
107
+ },
108
+ },
109
+ ];
@@ -16,6 +16,8 @@ import {
16
16
 
17
17
  const refCache = new WeakMap<RawCoValue, CoValue>();
18
18
 
19
+ const TRACE_ACCESSES = false;
20
+
19
21
  export class Ref<out V extends CoValue> {
20
22
  constructor(
21
23
  readonly id: ID<V>,
@@ -28,7 +30,6 @@ export class Ref<out V extends CoValue> {
28
30
  }
29
31
 
30
32
  get value() {
31
- // TODO: cache it for object identity!!!
32
33
  const raw = this.controlledAccount._raw.core.node.getLoaded(
33
34
  this.id as unknown as CoID<RawCoValue>
34
35
  );
@@ -87,16 +88,33 @@ export class Ref<out V extends CoValue> {
87
88
  }
88
89
  }
89
90
 
90
- accessFrom(fromScopeValue: CoValue): V | null {
91
+ accessFrom(fromScopeValue: CoValue, key: string | number | symbol): V | null {
91
92
  const subScope = subscriptionsScopes.get(fromScopeValue);
92
93
 
93
- subScope?.onRefAccessedOrSet(this.id);
94
+ subScope?.onRefAccessedOrSet(fromScopeValue.id, this.id);
95
+ TRACE_ACCESSES && console.log(subScope?.scopeID, "accessing", fromScopeValue, key, this.id);
94
96
 
95
97
  if (this.value && subScope) {
96
98
  subscriptionsScopes.set(this.value, subScope);
97
99
  }
98
100
 
99
- return this.value;
101
+ if (subScope) {
102
+ const cached = subScope.cachedValues[this.id];
103
+ if (cached) {
104
+ TRACE_ACCESSES && console.log("cached", cached);
105
+ return cached as V;
106
+ } else if (this.value !== null) {
107
+ const freshValueInstance = instantiateRefEncoded(this.schema, this.value?._raw);
108
+ TRACE_ACCESSES && console.log("freshValueInstance", freshValueInstance);
109
+ subScope.cachedValues[this.id] = freshValueInstance;
110
+ subscriptionsScopes.set(freshValueInstance, subScope);
111
+ return freshValueInstance as V;
112
+ } else {
113
+ return null
114
+ }
115
+ } else {
116
+ return this.value;
117
+ }
100
118
  }
101
119
  }
102
120
 
@@ -6,6 +6,8 @@ export const subscriptionsScopes = new WeakMap<
6
6
  SubscriptionScope<any>
7
7
  >();
8
8
 
9
+ const TRACE_INVALIDATIONS = false;
10
+
9
11
  export class SubscriptionScope<
10
12
  Root extends CoValue
11
13
  > {
@@ -23,6 +25,8 @@ export class SubscriptionScope<
23
25
  };
24
26
  onUpdate: (newRoot: Root) => void;
25
27
  scheduledUpdate: boolean = false;
28
+ cachedValues: {[id: ID<CoValue>]: CoValue} = {};
29
+ parents: {[id: ID<CoValue>]: Set<ID<CoValue>>} = {};
26
30
 
27
31
  constructor(
28
32
  root: Root,
@@ -63,12 +67,15 @@ export class SubscriptionScope<
63
67
  }
64
68
  }
65
69
 
66
- onRefAccessedOrSet(accessedOrSetId: ID<CoValue> | undefined) {
70
+ onRefAccessedOrSet(fromId: ID<CoValue>, accessedOrSetId: ID<CoValue> | undefined) {
67
71
  // console.log("onRefAccessedOrSet", this.scopeID, accessedOrSetId);
68
72
  if (!accessedOrSetId) {
69
73
  return;
70
74
  }
71
75
 
76
+ this.parents[accessedOrSetId] = this.parents[accessedOrSetId] || new Set();
77
+ this.parents[accessedOrSetId]!.add(fromId);
78
+
72
79
  if (!this.entries.has(accessedOrSetId)) {
73
80
  const loadingEntry = {
74
81
  state: "loading",
@@ -94,6 +101,7 @@ export class SubscriptionScope<
94
101
  const rawUnsub = core.subscribe((rawUpdate) => {
95
102
  // console.log("ref update", this.scopeID, accessedOrSetId, JSON.stringify(rawUpdate))
96
103
  if (!rawUpdate) return;
104
+ this.invalidate(accessedOrSetId);
97
105
  this.scheduleUpdate();
98
106
  });
99
107
 
@@ -103,6 +111,14 @@ export class SubscriptionScope<
103
111
  }
104
112
  }
105
113
 
114
+ invalidate(id: ID<CoValue>, fromChild?: ID<CoValue>) {
115
+ TRACE_INVALIDATIONS && console.log("invalidating", fromChild, "->", id, this.cachedValues[id]);
116
+ delete this.cachedValues[id];
117
+ for (const parent of this.parents[id] || []) {
118
+ this.invalidate(parent, id);
119
+ }
120
+ }
121
+
106
122
  unsubscribeAll() {
107
123
  for (const entry of this.entries.values()) {
108
124
  if (entry.state === "loaded") {
package/src/internal.ts CHANGED
@@ -13,4 +13,6 @@ export * from "./implementation/refs.js";
13
13
  export * from "./implementation/schema.js";
14
14
  export * from "./implementation/subscriptionScope.js";
15
15
 
16
- export * from "./coValues/extensions/imageDef.js";
16
+ export * from "./coValues/extensions/imageDef.js";
17
+
18
+ import "./implementation/devtoolsFormatters.js"