Skip to content

Commit

Permalink
Inline functions into "create*Accessor*" functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Mar 21, 2024
1 parent fb861a4 commit 6788980
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 199 deletions.
102 changes: 41 additions & 61 deletions packages/realm/src/Dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,36 @@ function createDictionaryAccessorForMixed<T>({
realm,
typeHelpers,
}: Pick<DictionaryAccessorFactoryOptions<T>, "realm" | "typeHelpers">): DictionaryAccessor<T> {
const { toBinding } = typeHelpers;
return {
get: (...args) => getMixed(realm, typeHelpers, ...args),
set: (...args) => setMixed(realm, typeHelpers.toBinding, ...args),
get(dictionary, key) {
const value = dictionary.tryGetAny(key);
switch (value) {
case binding.ListSentinel: {
const accessor = createListAccessor<T>({ realm, typeHelpers, itemType: binding.PropertyType.Mixed });
return new List<T>(realm, dictionary.getList(key), accessor) as T;
}
case binding.DictionarySentinel: {
const accessor = createDictionaryAccessor<T>({ realm, typeHelpers, itemType: binding.PropertyType.Mixed });
return new Dictionary<T>(realm, dictionary.getDictionary(key), accessor) as T;
}
default:
return typeHelpers.fromBinding(value) as T;
}
},
set(dictionary, key, value) {
assert.inTransaction(realm);

if (isJsOrRealmList(value)) {
dictionary.insertCollection(key, binding.CollectionType.List);
insertIntoListOfMixed(value, dictionary.getList(key), toBinding);
} else if (isJsOrRealmDictionary(value)) {
dictionary.insertCollection(key, binding.CollectionType.Dictionary);
insertIntoDictionaryOfMixed(value, dictionary.getDictionary(key), toBinding);
} else {
dictionary.insertAny(key, toBinding(value));
}
},
helpers: typeHelpers,
};
}
Expand All @@ -371,69 +398,22 @@ function createDictionaryAccessorForKnownType<T>({
}: Omit<DictionaryAccessorFactoryOptions<T>, "itemType">): DictionaryAccessor<T> {
const { fromBinding, toBinding } = typeHelpers;
return {
get: (...args) => getKnownType(fromBinding, ...args),
set: (...args) => setKnownType(realm, toBinding, !!isEmbedded, ...args),
get(dictionary, key) {
return fromBinding(dictionary.tryGetAny(key));
},
set(dictionary, key, value) {
assert.inTransaction(realm);

if (isEmbedded) {
toBinding(value, { createObj: () => [dictionary.insertEmbedded(key), true] });
} else {
dictionary.insertAny(key, toBinding(value));
}
},
helpers: typeHelpers,
};
}

function getKnownType<T>(fromBinding: TypeHelpers<T>["fromBinding"], dictionary: binding.Dictionary, key: string): T {
return fromBinding(dictionary.tryGetAny(key));
}

function getMixed<T>(realm: Realm, typeHelpers: TypeHelpers<T>, dictionary: binding.Dictionary, key: string): T {
const value = dictionary.tryGetAny(key);
switch (value) {
case binding.ListSentinel: {
const accessor = createListAccessor<T>({ realm, typeHelpers, itemType: binding.PropertyType.Mixed });
return new List<T>(realm, dictionary.getList(key), accessor) as T;
}
case binding.DictionarySentinel: {
const accessor = createDictionaryAccessor<T>({ realm, typeHelpers, itemType: binding.PropertyType.Mixed });
return new Dictionary<T>(realm, dictionary.getDictionary(key), accessor) as T;
}
default:
return typeHelpers.fromBinding(value) as T;
}
}

function setKnownType<T>(
realm: Realm,
toBinding: TypeHelpers<T>["toBinding"],
isEmbedded: boolean,
dictionary: binding.Dictionary,
key: string,
value: T,
): void {
assert.inTransaction(realm);

if (isEmbedded) {
toBinding(value, { createObj: () => [dictionary.insertEmbedded(key), true] });
} else {
dictionary.insertAny(key, toBinding(value));
}
}

function setMixed<T>(
realm: Realm,
toBinding: TypeHelpers<T>["toBinding"],
dictionary: binding.Dictionary,
key: string,
value: T,
): void {
assert.inTransaction(realm);

if (isJsOrRealmList(value)) {
dictionary.insertCollection(key, binding.CollectionType.List);
insertIntoListOfMixed(value, dictionary.getList(key), toBinding);
} else if (isJsOrRealmDictionary(value)) {
dictionary.insertCollection(key, binding.CollectionType.Dictionary);
insertIntoDictionaryOfMixed(value, dictionary.getDictionary(key), toBinding);
} else {
dictionary.insertAny(key, toBinding(value));
}
}

/** @internal */
export function insertIntoDictionaryOfMixed(
dictionary: Dictionary | Record<string, unknown>,
Expand Down
149 changes: 58 additions & 91 deletions packages/realm/src/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,49 @@ function createListAccessorForMixed<T>({
realm,
typeHelpers,
}: Pick<ListAccessorFactoryOptions<T>, "realm" | "typeHelpers">): ListAccessor<T> {
const { toBinding } = typeHelpers;
return {
get: (...args) => getMixed(realm, typeHelpers, ...args),
set: (...args) => setMixed(realm, typeHelpers.toBinding, ...args),
insert: (...args) => insertMixed(realm, typeHelpers.toBinding, ...args),
get(list, index) {
const value = list.getAny(index);
switch (value) {
case binding.ListSentinel: {
const accessor = createListAccessor<T>({ realm, typeHelpers, itemType: binding.PropertyType.Mixed });
return new List<T>(realm, list.getList(index), accessor) as T;
}
case binding.DictionarySentinel: {
const accessor = createDictionaryAccessor<T>({ realm, typeHelpers, itemType: binding.PropertyType.Mixed });
return new Dictionary<T>(realm, list.getDictionary(index), accessor) as T;
}
default:
return typeHelpers.fromBinding(value);
}
},
set(list, index, value) {
assert.inTransaction(realm);

if (isJsOrRealmList(value)) {
list.setCollection(index, binding.CollectionType.List);
insertIntoListOfMixed(value, list.getList(index), toBinding);
} else if (isJsOrRealmDictionary(value)) {
list.setCollection(index, binding.CollectionType.Dictionary);
insertIntoDictionaryOfMixed(value, list.getDictionary(index), toBinding);
} else {
list.setAny(index, toBinding(value));
}
},
insert(list, index, value) {
assert.inTransaction(realm);

if (isJsOrRealmList(value)) {
list.insertCollection(index, binding.CollectionType.List);
insertIntoListOfMixed(value, list.getList(index), toBinding);
} else if (isJsOrRealmDictionary(value)) {
list.insertCollection(index, binding.CollectionType.Dictionary);
insertIntoDictionaryOfMixed(value, list.getDictionary(index), toBinding);
} else {
list.insertAny(index, toBinding(value));
}
},
helpers: typeHelpers,
};
}
Expand All @@ -365,98 +404,26 @@ function createListAccessorForKnownType<T>({
const { fromBinding, toBinding } = typeHelpers;
return {
get: createDefaultGetter({ fromBinding, itemType }),
set: (...args) => setKnownType(realm, toBinding, !!isEmbedded, ...args),
insert: (...args) => insertKnownType(realm, toBinding, !!isEmbedded, ...args),
set(list, index, value) {
assert.inTransaction(realm);
list.setAny(
index,
toBinding(value, isEmbedded ? { createObj: () => [list.setEmbedded(index), true] } : undefined),
);
},
insert(list, index, value) {
assert.inTransaction(realm);
if (isEmbedded) {
// Simply transforming to binding will insert the embedded object
toBinding(value, { createObj: () => [list.insertEmbedded(index), true] });
} else {
list.insertAny(index, toBinding(value));
}
},
helpers: typeHelpers,
};
}

function getMixed<T>(realm: Realm, typeHelpers: TypeHelpers<T>, list: binding.List, index: number): T {
const value = list.getAny(index);
switch (value) {
case binding.ListSentinel: {
const accessor = createListAccessor<T>({ realm, typeHelpers, itemType: binding.PropertyType.Mixed });
return new List<T>(realm, list.getList(index), accessor) as T;
}
case binding.DictionarySentinel: {
const accessor = createDictionaryAccessor<T>({ realm, typeHelpers, itemType: binding.PropertyType.Mixed });
return new Dictionary<T>(realm, list.getDictionary(index), accessor) as T;
}
default:
return typeHelpers.fromBinding(value);
}
}

function setKnownType<T>(
realm: Realm,
toBinding: TypeHelpers<T>["toBinding"],
isEmbedded: boolean,
list: binding.List,
index: number,
value: T,
): void {
assert.inTransaction(realm);
list.setAny(index, toBinding(value, isEmbedded ? { createObj: () => [list.setEmbedded(index), true] } : undefined));
}

function setMixed<T>(
realm: Realm,
toBinding: TypeHelpers<T>["toBinding"],
list: binding.List,
index: number,
value: T,
): void {
assert.inTransaction(realm);

if (isJsOrRealmList(value)) {
list.setCollection(index, binding.CollectionType.List);
insertIntoListOfMixed(value, list.getList(index), toBinding);
} else if (isJsOrRealmDictionary(value)) {
list.setCollection(index, binding.CollectionType.Dictionary);
insertIntoDictionaryOfMixed(value, list.getDictionary(index), toBinding);
} else {
list.setAny(index, toBinding(value));
}
}

function insertKnownType<T>(
realm: Realm,
toBinding: TypeHelpers<T>["toBinding"],
isEmbedded: boolean,
list: binding.List,
index: number,
value: T,
): void {
assert.inTransaction(realm);

if (isEmbedded) {
// Simply transforming to binding will insert the embedded object
toBinding(value, { createObj: () => [list.insertEmbedded(index), true] });
} else {
list.insertAny(index, toBinding(value));
}
}

function insertMixed<T>(
realm: Realm,
toBinding: TypeHelpers<T>["toBinding"],
list: binding.List,
index: number,
value: T,
): void {
assert.inTransaction(realm);

if (isJsOrRealmList(value)) {
list.insertCollection(index, binding.CollectionType.List);
insertIntoListOfMixed(value, list.getList(index), toBinding);
} else if (isJsOrRealmDictionary(value)) {
list.insertCollection(index, binding.CollectionType.Dictionary);
insertIntoDictionaryOfMixed(value, list.getDictionary(index), toBinding);
} else {
list.insertAny(index, toBinding(value));
}
}

/** @internal */
export function insertIntoListOfMixed(
list: List | unknown[],
Expand Down
32 changes: 15 additions & 17 deletions packages/realm/src/Results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,21 @@ function createResultsAccessorForMixed<T>({
typeHelpers,
}: Omit<ResultsAccessorFactoryOptions<T>, "itemType">): ResultsAccessor<T> {
return {
get: (...args) => getMixed(realm, typeHelpers, ...args),
get(results, index) {
const value = results.getAny(index);
switch (value) {
case binding.ListSentinel: {
const accessor = createListAccessor<T>({ realm, typeHelpers, itemType: binding.PropertyType.Mixed });
return new List<T>(realm, results.getList(index), accessor) as T;
}
case binding.DictionarySentinel: {
const accessor = createDictionaryAccessor<T>({ realm, typeHelpers, itemType: binding.PropertyType.Mixed });
return new Dictionary<T>(realm, results.getDictionary(index), accessor) as T;
}
default:
return typeHelpers.fromBinding(value);
}
},
helpers: typeHelpers,
};
}
Expand All @@ -239,21 +253,5 @@ function createResultsAccessorForKnownType<T>({
};
}

function getMixed<T>(realm: Realm, typeHelpers: TypeHelpers<T>, results: binding.Results, index: number): T {
const value = results.getAny(index);
switch (value) {
case binding.ListSentinel: {
const accessor = createListAccessor<T>({ realm, typeHelpers, itemType: binding.PropertyType.Mixed });
return new List<T>(realm, results.getList(index), accessor) as T;
}
case binding.DictionarySentinel: {
const accessor = createDictionaryAccessor<T>({ realm, typeHelpers, itemType: binding.PropertyType.Mixed });
return new Dictionary<T>(realm, results.getDictionary(index), accessor) as T;
}
default:
return typeHelpers.fromBinding(value);
}
}

/* eslint-disable-next-line @typescript-eslint/no-explicit-any -- Useful for APIs taking any `Results` */
export type AnyResults = Results<any>;
Loading

0 comments on commit 6788980

Please sign in to comment.