Skip to content

Commit

Permalink
refactor: using single notifier correctly in record-core
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Harley committed Nov 17, 2017
1 parent 12694da commit e491aab
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 77 deletions.
5 changes: 3 additions & 2 deletions dist/src/record/record-core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ export declare class RecordCore extends Emitter {
* Transition States
*/
private onSubscribing();
private handleReadResponse(message);
private onResubscribing();
private onOfflineLoading();
private onReady();
private onUnsubscribed();
private onDeleted();
handle(message: RecordMessage): boolean;
handle(message: RecordMessage): void;
private handleReadResponse(message);
private handleHeadResponse(message);
private sendRead();
private saveUpdate();
private sendUpdate(path, data, callback);
Expand Down
64 changes: 32 additions & 32 deletions dist/src/record/record-core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/src/record/record-core.js.map

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions dist/src/record/single-notifier.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/src/record/single-notifier.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 32 additions & 37 deletions src/record/record-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,8 @@ export class RecordCore extends Emitter {
})
}

private handleReadResponse (message: RecordMessage): void {
if (this.stateMachine.state === RECORD_STATE.MERGING) {
this.recoverRecord(message.version as number, message.parsedData, message)
return
}
this.version = message.version as number
this.applyChange(setPath(this.data, null, message.parsedData))
this.stateMachine.transition(RECORD_ACTION.READ_RESPONSE)
}

private onResubscribing (): void {
this.recordServices.headRegistry.register(this.name, this.handleHeadResponse.bind(this))
this.services.timeoutRegistry.add({
message: {
topic: TOPIC.RECORD,
Expand Down Expand Up @@ -442,28 +433,10 @@ export class RecordCore extends Emitter {
this.destroy()
}

public handle (message: RecordMessage): boolean {


if (message.action === RECORD_ACTION.HEAD_RESPONSE) {
if (this.version === message.version as number) {
this.stateMachine.transition(RECORD_OFFLINE_ACTIONS.RESUBSCRIBED)
return true
}
if (this.version + 1 === message.version as number) {
this.version = message.version as number
this.applyChange(setPath(this.data, null, message.parsedData))
this.stateMachine.transition(RECORD_OFFLINE_ACTIONS.RESUBSCRIBED)
return true
}
this.stateMachine.transition(RECORD_OFFLINE_ACTIONS.INVALID_VERSION)
this.sendRead()
return true
}

public handle (message: RecordMessage): void {
if (message.action === RECORD_ACTION.PATCH || message.action === RECORD_ACTION.UPDATE || message.action === RECORD_ACTION.ERASE) {
this.applyUpdate(message as RecordWriteMessage)
return true
return
}

if (message.action === RECORD_ACTION.DELETE_SUCCESS) {
Expand All @@ -473,18 +446,18 @@ export class RecordCore extends Emitter {
} else if (this.deleteResponse.resolve) {
this.deleteResponse.resolve()
}
return true
return
}

if (message.action === RECORD_ACTION.DELETED) {
this.stateMachine.transition(message.action)
return true
return
}

if (message.action === RECORD_ACTION.VERSION_EXISTS) {
// what kind of message is version exists?
// this.recoverRecord(message)
return true
return
}

if (message.action === RECORD_ACTION.MESSAGE_DENIED) {
Expand All @@ -495,7 +468,8 @@ export class RecordCore extends Emitter {
message.originalAction === RECORD_ACTION.DELETE ||
message.originalAction === RECORD_ACTION.CREATE ||
message.originalAction === RECORD_ACTION.READ ||
message.originalAction === RECORD_ACTION.SUBSCRIBECREATEANDREAD
message.originalAction === RECORD_ACTION.SUBSCRIBECREATEANDREAD ||
message.originalAction === RECORD_ACTION.SUBSCRIBEANDHEAD
) {
this.emit(EVENT.RECORD_ERROR, RECORD_ACTION[RECORD_ACTION.MESSAGE_DENIED], RECORD_ACTION[message.originalAction])
}
Expand All @@ -507,7 +481,7 @@ export class RecordCore extends Emitter {
this.deleteResponse.reject(RECORD_ACTION[RECORD_ACTION.MESSAGE_DENIED])
}
}
return true
return
}

if (
Expand All @@ -516,10 +490,31 @@ export class RecordCore extends Emitter {
) {
this.hasProvider = message.action === RECORD_ACTION.SUBSCRIPTION_HAS_PROVIDER
this.emit(EVENT.RECORD_HAS_PROVIDER_CHANGED, this.hasProvider)
return true
return
}
}

return false
private handleReadResponse (message: RecordMessage): void {
if (this.stateMachine.state === RECORD_STATE.MERGING) {
this.recoverRecord(message.version as number, message.parsedData, message)
return
}
this.version = message.version as number
this.applyChange(setPath(this.data, null, message.parsedData))
this.stateMachine.transition(RECORD_ACTION.READ_RESPONSE)
}

private handleHeadResponse (message: RecordMessage): void {
if (this.version === message.version as number) {
this.stateMachine.transition(RECORD_OFFLINE_ACTIONS.RESUBSCRIBED)
} else if (this.version + 1 === message.version as number) {
this.version = message.version as number
this.applyChange(setPath(this.data, null, message.parsedData))
this.stateMachine.transition(RECORD_OFFLINE_ACTIONS.RESUBSCRIBED)
} else {
this.stateMachine.transition(RECORD_OFFLINE_ACTIONS.INVALID_VERSION)
this.sendRead()
}
}

private sendRead () {
Expand Down
2 changes: 0 additions & 2 deletions src/record/single-notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,12 @@ export class SingleNotifier {
}

for (let i = 0; i < internalResponses.length; i++) {
console.log('calling')
internalResponses[i](message)
}
this.internalRequests.delete(name)

// todo we can clean this up and do cb = (error, data) => error ? reject(error) : resolve()
for (let i = 0; i < responses.length; i++) {
console.log('calling')
const response = responses[i]
if (response.callback) {
response.callback(error, data)
Expand Down

0 comments on commit e491aab

Please sign in to comment.