Skip to content

Commit

Permalink
app; Wrap applyChanges in a mobx action
Browse files Browse the repository at this point in the history
  • Loading branch information
evanpurkhiser committed Jan 13, 2021
1 parent fcff5fc commit c6de567
Showing 1 changed file with 64 additions and 61 deletions.
125 changes: 64 additions & 61 deletions src/shared/store/ipc.ts
@@ -1,6 +1,7 @@
import {ipcMain, ipcRenderer} from 'electron';
import settings from 'electron-settings';
import {
action,
get,
IArrayDidChange,
IMapDidChange,
Expand Down Expand Up @@ -100,78 +101,80 @@ function getAtPath(obj: any, path: string) {
return target;
}

function applyChanges(obj: any, {path, change, serializerModel}: SerializedChange) {
const target = getAtPath(obj, path);
const applyChanges = action(
(obj: any, {path, change, serializerModel}: SerializedChange) => {
const target = getAtPath(obj, path);

if (target === undefined) {
// TODO: Race. Remove this and sometimes things happen
return;
}
if (target === undefined) {
// TODO: Race. Remove this and sometimes things happen
return;
}

const model = serializerModelMap.get(serializerModel ?? '');
const model = serializerModelMap.get(serializerModel ?? '');

// deserialization of our chnage object is a bit of a dance, we keep that
// dance here
const getNewValue = (override?: any) => {
const update = change as any;
const newValue = override ?? update.newValue;
// deserialization of our chnage object is a bit of a dance, we keep that
// dance here
const getNewValue = (override?: any) => {
const update = change as any;
const newValue = override ?? update.newValue;

// First attempt to use the specified serializer model if it maps to a
// serializableClass
if (model !== undefined) {
return deserialize(model as any, newValue);
}
// First attempt to use the specified serializer model if it maps to a
// serializableClass
if (model !== undefined) {
return deserialize(model as any, newValue);
}

// No deserializer found, assume it is a plain object
return newValue;
};

// A bit of a TS hack, cast each expected change type
const objChange = change as IObjectDidChange;
const mapChange = change as IMapDidChange;
const arrChange = change as IArrayDidChange;

// We can update the target object if it has serializeInfo available
const serializeSchema = target?.constructor?.serializeInfo;

if (
serializeSchema &&
!model &&
isObservableObject(target) &&
(objChange.type === 'add' || objChange.type === 'update') &&
objChange.newValue !== null &&
objChange.newValue !== undefined
) {
// eslint-disable-next-line @typescript-eslint/no-empty-function
update(serializeSchema, target, {[objChange.name]: objChange.newValue}, () => {});
return;
}
// No deserializer found, assume it is a plain object
return newValue;
};

// Update arrays
if (isObservableArray(target)) {
if (arrChange.type === 'update') {
set(arrChange.index, getNewValue());
// A bit of a TS hack, cast each expected change type
const objChange = change as IObjectDidChange;
const mapChange = change as IMapDidChange;
const arrChange = change as IArrayDidChange;

// We can update the target object if it has serializeInfo available
const serializeSchema = target?.constructor?.serializeInfo;

if (
serializeSchema &&
!model &&
isObservableObject(target) &&
(objChange.type === 'add' || objChange.type === 'update') &&
objChange.newValue !== null &&
objChange.newValue !== undefined
) {
// eslint-disable-next-line @typescript-eslint/no-empty-function
update(serializeSchema, target, {[objChange.name]: objChange.newValue}, () => {});
return;
}
if (arrChange.type === 'splice') {
// TODO This neds deserialization toooooo
target.splice(
arrChange.index,
arrChange.removedCount,
...arrChange.added.map(getNewValue)
);
return;

// Update arrays
if (isObservableArray(target)) {
if (arrChange.type === 'update') {
set(arrChange.index, getNewValue());
return;
}
if (arrChange.type === 'splice') {
// TODO This neds deserialization toooooo
target.splice(
arrChange.index,
arrChange.removedCount,
...arrChange.added.map(getNewValue)
);
return;
}
}
}

if (objChange.type === 'add' || objChange.type === 'update') {
set(target, objChange.name, getNewValue());
return;
}
if (objChange.type === 'remove' || mapChange.type === 'delete') {
remove(target, objChange.name as string);
if (objChange.type === 'add' || objChange.type === 'update') {
set(target, objChange.name, getNewValue());
return;
}
if (objChange.type === 'remove' || mapChange.type === 'delete') {
remove(target, objChange.name as string);
}
}
}
);

type ObserverStoreOpts = {
/**
Expand Down

1 comment on commit c6de567

@vercel
Copy link

@vercel vercel bot commented on c6de567 Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.