@live-change/prosemirror-service 0.2.47 → 0.2.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +60 -4
  2. package/model.js +11 -6
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -4,7 +4,7 @@ const app = App.app()
4
4
  const definition = require('./definition.js')
5
5
  const config = definition.config
6
6
 
7
- const { Document, StepsBucket, schemas } = require("./model.js")
7
+ const { Document, StepsBucket, schemas, getDocument } = require("./model.js")
8
8
 
9
9
  const { testLatency } = config
10
10
  const sleep = ms => new Promise(r => setTimeout(r, ms))
@@ -78,6 +78,48 @@ definition.action({
78
78
  type: 'documentCreated',
79
79
  document, documentType: type, purpose, content, lastModified: new Date(), created: new Date()
80
80
  })
81
+ return {
82
+ id: document,
83
+ type, purpose, content, version: 0
84
+ }
85
+ }
86
+ })
87
+
88
+ definition.action({
89
+ name: 'createDocumentIfNotExists',
90
+ waitForEvents: true,
91
+ properties: {
92
+ document: {
93
+ type: String,
94
+ validation: ['nonEmpty']
95
+ },
96
+ type: {
97
+ type: String,
98
+ validation: ['nonEmpty']
99
+ },
100
+ purpose: {
101
+ type: String,
102
+ validation: ['nonEmpty']
103
+ },
104
+ content: {
105
+ type: Object
106
+ }
107
+ },
108
+ async execute({ document, type, purpose, content }, { client, service }, emit) {
109
+ if(testLatency) await sleep(testLatency)
110
+ if(!schemas[type]) throw new Error(`schema not found for document type ${type}`)
111
+ const documentData = await Document.get(document)
112
+ if(documentData) {
113
+ return documentData
114
+ }
115
+ emit({
116
+ type: 'documentCreated',
117
+ document, documentType: type, purpose, content, lastModified: new Date(), created: new Date()
118
+ })
119
+ return {
120
+ id: document,
121
+ type, purpose, content, version: 0
122
+ }
81
123
  }
82
124
  })
83
125
 
@@ -103,17 +145,31 @@ definition.action({
103
145
  window: {
104
146
  type: String,
105
147
  validation: ['nonEmpty']
148
+ },
149
+ continuation: {
150
+ type: Boolean
106
151
  }
107
152
  },
108
153
  queuedBy: (command) => command.client.document,
109
- async execute({ document, type, version, steps, window }, { client, service }, emit) {
154
+ async execute({ document, type, version, steps, window, continuation }, { client, service }, emit) {
110
155
  if(testLatency) await sleep(testLatency)
111
156
  if(!schemas[type]) throw new Error(`schema not found for document type ${type}`)
112
- const documentData = await Document.get(document)
157
+ const documentData = await getDocument(document, type)
113
158
  if(!documentData) throw new Error('document not found')
114
- if(document.version > version) return 'ignored'
159
+ if(documentData.version != version) return 'ignored'
115
160
  const [sessionOrUserType, sessionOrUser] =
116
161
  client.user ? ['user_User', client.user] : ['session_Session', client.session]
162
+ if(continuation) {
163
+ console.log("DOC DATA", documentData)
164
+ console.log("CONTINUATION", documentData.lastStepsBucket, sessionOrUserType, sessionOrUser)
165
+ if(!documentData.lastStepsBucket
166
+ || documentData.lastStepsBucket.sessionOrUserType != sessionOrUserType
167
+ || documentData.lastStepsBucket.sessionOrUser != sessionOrUser) {
168
+ console.log("CONTINUATION IGNORED!!")
169
+ return [] // ignore, client will rebase
170
+ }
171
+ }
172
+
117
173
  emit({
118
174
  type: 'documentEdited',
119
175
  document, documentType: type, version, steps, window,
package/model.js CHANGED
@@ -75,10 +75,12 @@ async function getDocument(documentId, documentType) {
75
75
  if(!documentData) {
76
76
  return null
77
77
  }
78
+ const lastStepsBucket = await StepsBucket.rangePath([documentId], { reverse: true, limit: 1 })?.[0] ?? null
78
79
  document = {
79
80
  type: documentData.type,
80
81
  content: schema.nodeFromJSON(documentData.content),
81
82
  version: documentData.version,
83
+ lastStepsBucket,
82
84
  schema
83
85
  }
84
86
  openDocuments.set(documentId, document)
@@ -105,19 +107,22 @@ definition.event({
105
107
  openDocument.content = step.apply(openDocument.content).doc
106
108
  openDocument.version ++
107
109
  }
110
+ const bucket = {
111
+ id: App.encodeIdentifier([document, openDocument.version.toFixed().padStart(10, '0')]),
112
+ window, sessionOrUserType, sessionOrUser, timestamp: new Date(),
113
+ steps
114
+ }
115
+ console.log("DOC EDITED", bucket)
116
+ openDocument.lastStepsBucket = bucket
108
117
  await Promise.all([
109
118
  Document.update(document, {
110
119
  content: openDocument.content.toJSON(),
111
120
  version: openDocument.version,
112
121
  lastModified: timestamp
113
122
  }),
114
- StepsBucket.create({
115
- id: App.encodeIdentifier([document, openDocument.version.toFixed().padStart(10, '0')]),
116
- window, sessionOrUserType, sessionOrUser, timestamp: new Date(),
117
- steps
118
- })
123
+ StepsBucket.create(bucket)
119
124
  ])
120
125
  }
121
126
  })
122
127
 
123
- module.exports = { Document, StepsBucket, schemas }
128
+ module.exports = { Document, StepsBucket, schemas, getDocument }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/prosemirror-service",
3
- "version": "0.2.47",
3
+ "version": "0.2.48",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -28,5 +28,5 @@
28
28
  "progress-stream": "^2.0.0",
29
29
  "prosemirror-model": "^1.18.1"
30
30
  },
31
- "gitHead": "710dc205163bf09d6b9469822101e9dfc11b74cb"
31
+ "gitHead": "cb09b064a6756221f1b773fe79e5b61de87c5ca5"
32
32
  }