Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
refactor: adapt splice handling to available constructor information
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Sep 10, 2018
1 parent 13cf169 commit 5e28ee3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/message/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ export interface MobxSplicePayload<T = unknown> {
id: string;
name: string;
memberName: string;
valueModel: Types.ModelName | undefined;
change: MobxSpliceChange<T>;
}

Expand Down
31 changes: 16 additions & 15 deletions src/model/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,6 @@ export class Project {
const object = this.getObject(message.payload.name, message.payload.id);

if (!object) {
// console.log(message);
return;
}

Expand All @@ -688,7 +687,7 @@ export class Project {
const ValueModel = ModelTree.getModelByName(message.payload.valueModel);

if (!parent) {
console.log(message);
console.log('no parent', message);
return;
}

Expand All @@ -703,7 +702,7 @@ export class Project {
: message.payload.change.newValue;

if (typeof value === 'object' && !ValueModel) {
console.log(message);
console.log('no value model', message);
}

const member = mayBeMember as Map<unknown, unknown>;
Expand All @@ -730,30 +729,32 @@ export class Project {

sender.match<Message.MobxSpliceMessage>(Message.MessageType.MobxSplice, message => {
const parent = this.getObject(message.payload.name, message.payload.id);
const ValueModel = ModelTree.getModelByName(message.payload.valueModel);

if (!parent) {
console.log(message);
console.log('no parent', message);
return;
}

const changedData = parent.toJSON();
const target = changedData[message.payload.memberName];
const mayBeMember = parent[message.payload.memberName];

if (!Array.isArray(target)) {
console.log(message);
if (!mayBeMember) {
return;
}

if (message.payload.change.removed.length > 0) {
target.splice(message.payload.change.index, message.payload.change.removed.length);
}
const added = message.payload.change.added.map(
a => (ValueModel ? ValueModel.from(a, { project: this }) : a)
);
const removed = message.payload.change.removed;
const member = mayBeMember as unknown[];

if (message.payload.change.added.length > 0) {
target.splice(message.payload.change.index, 0, ...message.payload.change.added);
if (removed.length > 0) {
member.splice(message.payload.change.index, removed.length);
}

// tslint:disable-next-line:no-any
(parent.update as any)(changedData);
if (added.length > 0) {
member.splice(message.payload.change.index, 0, ...added);
}
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/model/user-store-action/user-store-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface UserStoreActionInit {
}

export class UserStoreAction {
public readonly model = 'UserStoreAction';
public readonly model = Types.ModelName.UserStoreAction;

private acceptsProperty: boolean;
private id: string;
Expand Down Expand Up @@ -94,7 +94,7 @@ export class UserStoreAction {

public toJSON(): Types.SerializedUserStoreAction {
return {
model: 'UserStoreAction',
model: Types.ModelName.UserStoreAction,
acceptsProperty: this.acceptsProperty,
id: this.id,
name: this.name,
Expand Down
13 changes: 0 additions & 13 deletions src/preview/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,6 @@ function main(): void {
store.setMetaDown(message.payload.metaDown);
});

sender.match<Message.ChangePages>(Message.MessageType.ChangePages, message => {
Mobx.transaction(() => {
const changes = computeDifference<Model.Page>({
after: message.payload.pages.map(p => Model.Page.from(p, { project })),
before: project.getPages()
});

changes.added.forEach(change => project.addPage(change.after));
changes.changed.forEach(change => change.before.update(change.after));
changes.removed.forEach(change => project.removePage(change.before));
});
});

sender.match<Message.ChangePatternLibraries>(
Message.MessageType.ChangePatternLibraries,
message => {
Expand Down
10 changes: 9 additions & 1 deletion src/renderer/create-change-notifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ export function createChangeNotifiers({ app, store }: NotifierContext): void {
id: parent.getId(),
name: name.parentName,
memberName: name.memberName,
// tslint:disable-next-line:no-any
valueModel: typeof newValue === 'object' ? newValue.model : undefined,
change: {
type: change.type,
Expand Down Expand Up @@ -200,13 +199,22 @@ export function createChangeNotifiers({ app, store }: NotifierContext): void {
return;
}

// tslint:disable-next-line:no-any
const exampleValue = (change.added || change.removed)[0] as Model.AnyModel;

const model =
exampleValue && typeof exampleValue.toJSON === 'function'
? exampleValue.model
: undefined;

sender.send({
id: uuid.v4(),
type: Message.MessageType.MobxSplice,
payload: {
id: parent.getId(),
name: name.parentName,
memberName: name.memberName,
valueModel: model,
change: {
type: change.type,
index: change.index,
Expand Down

0 comments on commit 5e28ee3

Please sign in to comment.