-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Awareness emits different events for update and for change
- Loading branch information
Showing
8 changed files
with
152 additions
and
38 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
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
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,35 @@ | ||
|
||
import * as Y from 'yjs' | ||
import * as t from 'lib0/testing.js' | ||
import * as awareness from './awareness.js' | ||
|
||
/** | ||
* @param {t.TestCase} tc | ||
*/ | ||
export const testAwareness = tc => { | ||
const doc1 = new Y.Doc() | ||
doc1.clientID = 0 | ||
const doc2 = new Y.Doc() | ||
doc2.clientID = 1 | ||
const aw1 = new awareness.Awareness(doc1) | ||
const aw2 = new awareness.Awareness(doc2) | ||
aw1.on('update', /** @param {any} p */ ({ added, updated, removed }) => { | ||
const enc = awareness.encodeAwarenessUpdate(aw1, added.concat(updated).concat(removed)) | ||
awareness.applyAwarenessUpdate(aw2, enc, 'custom') | ||
}) | ||
let lastChange = /** @type {any} */ (null) | ||
aw2.on('change', /** @param {any} change */ change => { | ||
lastChange = change | ||
}) | ||
aw1.setLocalState({ x: 4 }) | ||
t.compare(aw2.getStates().get(0), { x: 4 }) | ||
t.assert(/** @type {any} */ (aw2.meta.get(0)).clock === 1) | ||
t.compare(lastChange.added, [0]) | ||
lastChange = null | ||
aw1.setLocalState({ x: 4 }) | ||
t.assert(lastChange === null) | ||
t.assert(/** @type {any} */ (aw2.meta.get(0)).clock === 2) | ||
aw1.setLocalState(null) | ||
t.assert(lastChange.removed.length === 1) | ||
t.compare(aw1.getStates().get(0), undefined) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,18 +1,33 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import resolve from '@rollup/plugin-node-resolve' | ||
import commonjs from '@rollup/plugin-commonjs' | ||
|
||
const files = fs.readdirSync('./').filter(file => /(?<!\.(test|config))\.js$/.test(file)) | ||
const files = ['awareness.js', 'auth.js', 'sync.js', 'test.js'] | ||
|
||
export default files.map(file => ({ | ||
input: file, | ||
export default [{ | ||
input: './test.js', | ||
output: { | ||
file: path.join('./dist', file.slice(0, -3) + '.cjs'), | ||
file: './dist/test.js', | ||
format: 'iife', | ||
sourcemap: true | ||
}, | ||
plugins: [ | ||
resolve({ mainFields: ['module', 'browser', 'main'] }), | ||
commonjs() | ||
] | ||
}, { | ||
input: files, | ||
output: { | ||
dir: './dist', | ||
format: 'cjs', | ||
sourcemap: true, | ||
entryFileNames: '[name].cjs', | ||
chunkFileNames: '[name]-[hash].cjs', | ||
paths: /** @param {any} path */ path => { | ||
if (/^lib0\//.test(path)) { | ||
return `lib0/dist/${path.slice(5, -3) + '.cjs'}` | ||
} | ||
return path | ||
} | ||
} | ||
})) | ||
}, | ||
external: /** @param {any} id */ id => /^lib0\/|yjs/.test(id) | ||
}] |
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,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Testing y-protocols</title> | ||
</head> | ||
<body> | ||
<script src="./dist/test.js"></script> | ||
</body> | ||
</html> |
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,19 @@ | ||
import { runTests } from 'lib0/testing.js' | ||
import * as log from 'lib0/logging.js' | ||
import * as awareness from './awareness.test.js' | ||
|
||
import { isBrowser, isNode } from 'lib0/environment.js' | ||
|
||
/* istanbul ignore if */ | ||
if (isBrowser) { | ||
log.createVConsole(document.body) | ||
} | ||
|
||
runTests({ | ||
awareness | ||
}).then(success => { | ||
/* istanbul ignore next */ | ||
if (isNode) { | ||
process.exit(success ? 0 : 1) | ||
} | ||
}) |