Beispiel #1
0
// This runs in the browser and is compiled to public/bundle.js

import {I, otDoc, registerType, subValues} from '@statecraft/core'
import {connectToWS, reconnectingclient} from '@statecraft/net'
import {type as texttype, TextOp} from 'ot-text-unicode'

registerType(texttype)

declare const config: {
  key: string,
  initialValue: string | null,
  initialVersions: (number[] | null)[],
}

// document.body.appendChild(html`<h1>oh hi</h1>`)

const elem = document.getElementById('content') as HTMLTextAreaElement | null
if (elem == null) throw Error('Could not find document #content div')


// *** Text editing operations
interface TextCtx {
  insert(pos: number, val: string): void
  remove(pos: number, len: number): void
}


const id = <T>(x: T) => x
let prevvalue: string = ''

// Replace the content of the text area with newText, and transform the
Beispiel #2
0
import {I, stores, rmKV, setKV, subValues, registerType, resultTypes, catchupStateMachine} from '@statecraft/core'
import {wrapWebSocket, connectMux, BothMsg, tcpserver} from '@statecraft/net'
import express from 'express'
import WebSocket from 'ws'
import http from 'http'

// import kvMem from '../../lib/stores/kvmem'
import State from './state'
import {type as jsonType, JSONOp} from 'ot-json1'
registerType(jsonType)

const {kvmem} = stores

process.on('unhandledRejection', err => {
  console.error((err as any).stack)
  process.exit(1)
})

;(async () => {
  // The store is a kv store mapping from client ID (incrementing numbers) => latest position.
  const store = await kvmem<State>()

  const app = express()
  app.use(express.static(`${__dirname}/public`))

  const server = http.createServer(app)
  const wss = new WebSocket.Server({server})

  let nextId = 1000

  wss.on('connection', async (socket, req) => {
Beispiel #3
0
import choo, { IState } from 'choo'
import html from 'choo/html'

import {I, subValues, subResults, sel, registerType, queryTypes, bitHas, version} from '@statecraft/core'
import {reconnectingclient, connectToWS} from '@statecraft/net'

import {type as texttype} from 'ot-text-unicode'
import {type as jsontype} from 'ot-json1'


registerType(texttype)
registerType(jsontype)

type Op = {v: (I.Version | null)[], op?: I.Op<any>, replace?: any}
type Ops = Op[]

interface State extends IState {
  sources: I.Source[]
  uid: string,
  versions: I.FullVersion
  data: any
  // selectedKey?: string
  connectionStatus: string
  prefer: 'pre' | 'html'
  opsForKey: Map<string, Ops>
}

const hex = (v: Uint8Array): string => (
  Array.prototype.map.call(v, (x: number) => ('00' + x.toString(16)).slice(-2)).join('')
)