Skip to content

Commit

Permalink
pass editable and legacy through to ManagedObject and ManagedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
richgt committed Sep 25, 2024
1 parent 91ca6b3 commit 53c82b6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
23 changes: 19 additions & 4 deletions packages/schema-record/src/-private/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export function computeArray(
identifier: StableRecordIdentifier,
field: ArrayField | SchemaArrayField,
path: string[],
isSchemaArray: boolean
isSchemaArray: boolean,
editable: boolean,
legacy: boolean
) {
// the thing we hand out needs to know its owner and path in a private manner
// its "address" is the parent identifier (identifier) + field name (field.name)
Expand All @@ -110,7 +112,19 @@ export function computeArray(
if (!rawValue) {
return null;
}
managedArray = new ManagedArray(store, schema, cache, field, rawValue, identifier, path, record, isSchemaArray);
managedArray = new ManagedArray(
store,
schema,
cache,
field,
rawValue,
identifier,
path,
record,
isSchemaArray,
editable,
legacy
);
if (!managedArrayMapForRecord) {
ManagedArrayMap.set(record, new Map([[field, managedArray]]));
} else {
Expand All @@ -127,7 +141,8 @@ export function computeObject(
identifier: StableRecordIdentifier,
field: ObjectField,
path: string[],
editable: boolean
editable: boolean,
legacy: boolean
) {
const managedObjectMapForRecord = ManagedObjectMap.get(record);
let managedObject;
Expand All @@ -145,7 +160,7 @@ export function computeObject(
const transform = schema.transformation(field);
rawValue = transform.hydrate(rawValue as ObjectValue, field.options ?? null, record) as object;
}
managedObject = new ManagedObject(schema, cache, field, rawValue, identifier, path, record, editable);
managedObject = new ManagedObject(schema, cache, field, rawValue, identifier, path, record, editable, legacy);

if (!managedObjectMapForRecord) {
ManagedObjectMap.set(record, new Map([[field, managedObject]]));
Expand Down
8 changes: 7 additions & 1 deletion packages/schema-record/src/-private/managed-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export class ManagedArray {
declare path: string[];
declare owner: SchemaRecord;
declare [ARRAY_SIGNAL]: Signal;
declare [Editable]: boolean;
declare [Legacy]: boolean;

constructor(
store: Store,
Expand All @@ -119,12 +121,16 @@ export class ManagedArray {
identifier: StableRecordIdentifier,
path: string[],
owner: SchemaRecord,
isSchemaArray: boolean
isSchemaArray: boolean,
editable: boolean,
legacy: boolean
) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
this[SOURCE] = data?.slice();
this[ARRAY_SIGNAL] = createSignal(this, 'length');
this[Editable] = editable;
this[Legacy] = legacy;
const _SIGNAL = this[ARRAY_SIGNAL];
const boundFns = new Map<KeyType, ProxiedMethod>();
this.identifier = identifier;
Expand Down
7 changes: 5 additions & 2 deletions packages/schema-record/src/-private/managed-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { ObjectField, SchemaObjectField } from '@warp-drive/core-types/sche

import type { SchemaRecord } from '../record';
import type { SchemaService } from '../schema';
import { Editable, EmbeddedPath, MUTATE, OBJECT_SIGNAL, Parent, SOURCE } from '../symbols';
import { Editable, EmbeddedPath, Legacy, MUTATE, OBJECT_SIGNAL, Parent, SOURCE } from '../symbols';

export function notifyObject(obj: ManagedObject) {
addToTransaction(obj[OBJECT_SIGNAL]);
Expand Down Expand Up @@ -37,6 +37,7 @@ export class ManagedObject {
declare [EmbeddedPath]: string[];
declare [OBJECT_SIGNAL]: Signal;
declare [Editable]: boolean;
declare [Legacy]: boolean;

constructor(
schema: SchemaService,
Expand All @@ -46,13 +47,15 @@ export class ManagedObject {
identifier: StableRecordIdentifier,
path: string[],
owner: SchemaRecord,
editable: boolean
editable: boolean,
legacy: boolean
) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
this[SOURCE] = { ...data };
this[OBJECT_SIGNAL] = createSignal(this, 'length');
this[Editable] = editable;
this[Legacy] = legacy;
this[Parent] = identifier;
this[EmbeddedPath] = path;

Expand Down
28 changes: 25 additions & 3 deletions packages/schema-record/src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,21 +253,43 @@ export class SchemaRecord {
return computeDerivation(schema, receiver as unknown as SchemaRecord, identifier, field, prop as string);
case 'schema-array':
entangleSignal(signals, receiver, field.name);
return computeArray(store, schema, cache, target, identifier, field, propArray, true);
return computeArray(
store,
schema,
cache,
target,
identifier,
field,
propArray,
true,
Mode[Editable],
Mode[Legacy]
);
case 'array':
assert(
`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`,
!target[Legacy]
);
entangleSignal(signals, receiver, field.name);
return computeArray(store, schema, cache, target, identifier, field, propArray, false);
return computeArray(
store,
schema,
cache,
target,
identifier,
field,
propArray,
false,
Mode[Editable],
Mode[Legacy]
);
case 'object':
assert(
`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`,
!target[Legacy]
);
entangleSignal(signals, receiver, field.name);
return computeObject(schema, cache, target, identifier, field, propArray, Mode[Editable]);
return computeObject(schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
case 'schema-object':
assert(
`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`,
Expand Down

0 comments on commit 53c82b6

Please sign in to comment.