-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(factory): add factory for State and DocService
- Loading branch information
1 parent
1093458
commit c0e75a3
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum Strategy { | ||
LOGOOTSPLIT = 100 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './Strategy' | ||
export * from './StateStrategy' | ||
export * from './DocServiceStrategy' |