From 81d82e170e9b2aec3ccbc652b4501757a36da828 Mon Sep 17 00:00:00 2001 From: Mario Nebl Date: Sun, 5 Aug 2018 21:38:12 +0200 Subject: [PATCH] feat: implement map deletions sync --- src/message/message.ts | 13 ++++++++ src/preview/preview.ts | 18 ++++++++++ src/renderer/create-change-notifiers.ts | 44 +++++++++++++++++++++---- src/sender/client.ts | 3 +- src/types/mobx.ts | 24 +++++++------- tslint.json | 9 ----- 6 files changed, 83 insertions(+), 28 deletions(-) diff --git a/src/message/message.ts b/src/message/message.ts index bcd869d6a..4063dc282 100644 --- a/src/message/message.ts +++ b/src/message/message.ts @@ -47,6 +47,7 @@ export enum MessageType { Log = 'log', Maximize = 'maximize', MobxAdd = 'mobx-add', + MobxDelete = 'mobx-delete', MobxUpdate = 'mobx-update', MobxSplice = 'mobx-splice', OpenExternalURL = 'open-external-url', @@ -120,6 +121,7 @@ export type Message = | Log | Maximize | MobxAddMessage + | MobxDeleteMessage | MobxUpdateMessage | MobxSpliceMessage | OpenExternalURL @@ -352,6 +354,16 @@ export interface MobxAddPayload { change: MobxAddChange; } +export interface MobxDeletePayload { + id: string; + name: string; + memberName: string; + change: { + type: Types.MobxChangeType.Delete; + key: string; + }; +} + export interface MobxSplicePayload { id: string; name: string; @@ -397,4 +409,5 @@ export interface MobxObjectUpdatePayload { export type MobxUpdateMessage = Envelope; export type MobxAddMessage = Envelope; +export type MobxDeleteMessage = Envelope; export type MobxSpliceMessage = Envelope; diff --git a/src/preview/preview.ts b/src/preview/preview.ts index 41486a48f..fdf0ab874 100644 --- a/src/preview/preview.ts +++ b/src/preview/preview.ts @@ -156,6 +156,24 @@ function main(): void { member.set(message.payload.change.key, value); }); + sender.match(Message.MessageType.MobxDelete, message => { + const parent = store.getObject(message.payload.name, message.payload.id); + + if (!parent) { + console.log(message); + return; + } + + const mayBeMember = parent[message.payload.memberName]; + + if (!mayBeMember) { + return; + } + + const member = mayBeMember as Map; + member.delete(message.payload.change.key); + }); + sender.match(Message.MessageType.MobxSplice, message => { const parent = store.getObject(message.payload.name, message.payload.id); diff --git a/src/renderer/create-change-notifiers.ts b/src/renderer/create-change-notifiers.ts index 85f4b1edf..c7c6b93ab 100644 --- a/src/renderer/create-change-notifiers.ts +++ b/src/renderer/create-change-notifiers.ts @@ -79,7 +79,8 @@ export function createChangeNotifiers({ app, store }: NotifierContext): void { switch (change.type) { case Types.MobxChangeType.Update: { if (change.hasOwnProperty('index')) { - const arrayChange = change as Types.MobxArrayUpdate; + console.log(change); + /* const arrayChange = change as Types.MobxArrayUpdate; sender.send({ id: uuid.v4(), @@ -93,18 +94,21 @@ export function createChangeNotifiers({ app, store }: NotifierContext): void { newValue: change.newValue } } - }); + }); */ } if (change.hasOwnProperty('key')) { - const objectChange = change as Types.MobxObjectUpdate | Types.MobxMapUpdate; + const objectChange = change as + | Types.MobxObjectUpdate + | Types.MobxMapUpdate; sender.send({ id: uuid.v4(), type: Message.MessageType.MobxUpdate, payload: { - id: change.object.id, - name: change.object.constructor.name, + // tslint:disable-next-line:no-any + id: (objectChange.object as any).id, + name: objectChange.object.constructor.name, change: { type: objectChange.type, key: objectChange.key, @@ -153,7 +157,7 @@ export function createChangeNotifiers({ app, store }: NotifierContext): void { valueModel: typeof newValue === 'object' ? newValue.model : undefined, change: { type: change.type, - key: change.key, + key: change.key as string, newValue } } @@ -162,6 +166,32 @@ export function createChangeNotifiers({ app, store }: NotifierContext): void { break; } + case Types.MobxChangeType.Delete: { + const parent = getParentByMember(change.object, { name: change.name, project }); + const name = parseChangeName(change.name); + const deletion = change as Types.MobxDelete; + + if (!parent) { + return; + } + + sender.send({ + id: uuid.v4(), + type: Message.MessageType.MobxDelete, + payload: { + id: parent.getId(), + name: name.parentName, + memberName: name.memberName, + change: { + type: deletion.type, + key: deletion.key + } + } + }); + + break; + } + case Types.MobxChangeType.Splice: { const parent = getParentByMember(change.object, { name: change.name, project }); const name = parseChangeName(change.name); @@ -185,6 +215,8 @@ export function createChangeNotifiers({ app, store }: NotifierContext): void { } } }); + + break; } } }); diff --git a/src/sender/client.ts b/src/sender/client.ts index e5a1c4379..85cd153a2 100644 --- a/src/sender/client.ts +++ b/src/sender/client.ts @@ -18,11 +18,11 @@ export class Sender { public async send(message: Message.Message): Promise { if (!isMessage(message)) { + console.error(`Tried to send invalid message: ${message}`); return; } await onReady(this.connection); - this.connection.send(Serde.serialize(message)); } @@ -50,6 +50,7 @@ export class Sender { const header = Serde.getMessageHeader(e.data); if (header.status === Serde.MessageHeaderStatus.Error) { + console.error(header); return; } diff --git a/src/types/mobx.ts b/src/types/mobx.ts index 26b2a40e0..b7f21a7fb 100644 --- a/src/types/mobx.ts +++ b/src/types/mobx.ts @@ -1,17 +1,13 @@ -export type MobxChange = +export type MobxChange = | MobxAction | MobxScheduledReaction | MobxReaction | MobxCompute - // tslint:disable-next-line:no-any - | MobxUpdate + | MobxUpdate | MobxSplice - // tslint:disable-next-line:no-any - | MobxAdd - // tslint:disable-next-line:no-any - | MobxDelete - // tslint:disable-next-line:no-any - | MobxCreate + | MobxAdd + | MobxDelete + | MobxCreate | MobxReportEnd; export interface MobxReportStart { @@ -73,21 +69,25 @@ export interface MobxMapAdd extends MobxReportStart { type: MobxChangeType.Add; } -export interface MobxDelete extends MobxReportStart { +export interface MobxDelete extends MobxReportStart { object: T; name: string; oldValue: T; + key: V; type: MobxChangeType.Delete; } -export interface MobxCreate extends MobxReportStart { +export interface MobxCreate extends MobxReportStart { object: T; name: string; newValue: T; type: MobxChangeType.Create; } -export type MobxUpdate = MobxArrayUpdate | MobxMapUpdate | MobxObjectUpdate; +export type MobxUpdate = + | MobxArrayUpdate + | MobxMapUpdate + | MobxObjectUpdate; export interface MobxArrayUpdate extends MobxReportStart { type: MobxChangeType.Update; diff --git a/tslint.json b/tslint.json index 2c0f42b14..c0b07515c 100644 --- a/tslint.json +++ b/tslint.json @@ -203,14 +203,6 @@ "destructuring": "any" } ], - "trailing-comma": [ - true, - { - "multiline": "never", - "singleline": "never", - "esSpecCompliant": true - } - ], "align": [ true, "elements", @@ -313,7 +305,6 @@ true, 0 ], - "switch-final-break": true, "type-literal-delimiter": true, "variable-name": [ true,