Skip to content

Commit

Permalink
feat(factory): add factory for State and DocService
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricenclos committed Nov 30, 2018
1 parent 1093458 commit c0e75a3
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/CrdtImpl/DocServiceStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Observable, Subject } from 'rxjs'
import { StateTypes } from '.'
import { CollaboratorsService } from '../collaborators'
import { DocService, State } from '../core'
import { IMessageIn, IMessageOut } from '../misc'
import { LSSync } from '../syncImpl'
import { LSDocService, LSDocument, LSState, LSSyncMessage } from './LogootSplit'
import { Strategy } from './Strategy'

export type DocServiceStrategyMethod<Seq, Op> = (
strat: Strategy,
messageIn$: Observable<IMessageIn>,
messageOut$: Subject<IMessageOut>,
id: number,
state: StateTypes,
collaboratorService: CollaboratorsService
) => DocService<Seq, Op>

export class DocServiceStrategy {
static createDocService<Seq, Op>(
strat: Strategy,
messageIn$: Observable<IMessageIn>,
messageOut$: Subject<IMessageOut>,
id: number,
state: State<Seq, Op>,
collaboratorService: CollaboratorsService
) {
switch (strat) {
case Strategy.LOGOOTSPLIT:
if (state instanceof LSState) {
const document = new LSDocument(state.sequenceCRDT)
const sync = new LSSync(
id,
state.networkClock,
state.vector,
state.remoteOperations,
collaboratorService
)
const syncMessage = new LSSyncMessage(messageIn$, messageOut$)
return new LSDocService(id, collaboratorService, document, sync, syncMessage)
} else {
throw new Error('State is not an instanceof LSState')
}
default:
throw new Error("This strategy doesn't exist")
}
}
}
47 changes: 47 additions & 0 deletions src/CrdtImpl/StateStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { SafeAny } from 'safe-any'
import { StateJSON } from '../core'
import { LSState } from './LogootSplit/LSState'
import { Strategy } from './Strategy'

export type StateTypes = LSState

interface IStateFactory<T> {
fromPlain(o: any): T
emptyState(): T
}

function createState<T>(s: IStateFactory<T>, o: SafeAny): T | null {
return s.fromPlain(o)
}

function createEmptyState<T>(s: IStateFactory<T>): T | null {
return s.emptyState()
}

export class StateStrategy {
static fromPlain<Seq, Op>(strat: Strategy, o: SafeAny<StateJSON<Seq, Op>>) {
let state
switch (strat) {
case Strategy.LOGOOTSPLIT:
state = createState(LSState, o)
break
default:
state = null
break
}
return state
}

static emptyState(strat: Strategy) {
let state
switch (strat) {
case Strategy.LOGOOTSPLIT:
state = createEmptyState(LSState)
break
default:
state = null
break
}
return state
}
}
3 changes: 3 additions & 0 deletions src/CrdtImpl/Strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum Strategy {
LOGOOTSPLIT = 100
}
3 changes: 3 additions & 0 deletions src/CrdtImpl/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './Strategy'
export * from './StateStrategy'
export * from './DocServiceStrategy'

0 comments on commit c0e75a3

Please sign in to comment.