-
Notifications
You must be signed in to change notification settings - Fork 453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vite example #598
Merged
Merged
Vite example #598
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,4 @@ jspm_packages | |
|
||
# Don't commit generated JS bundles | ||
examples/**/static/dist/bundle.js | ||
examples/**/dist |
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,24 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
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,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>ShareDB Counter (ottype json1 with Vite)</title> | ||
</head> | ||
<body> | ||
<div style="font-family: Helvetica Neue, Helvetica, Arial, sans-serif; font-size: 36px;"> | ||
You clicked <span id="num-clicks"></span> times. | ||
<button style="font-size: 36px;" class="increment">+1</button> | ||
</div> | ||
<script type="module" src="/main.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,33 @@ | ||
import ReconnectingWebSocket from 'reconnecting-websocket'; | ||
import json1 from 'ot-json1'; | ||
import sharedb from 'sharedb-client-browser/dist/sharedb-client-umd.cjs'; | ||
|
||
// Open WebSocket connection to ShareDB server | ||
var socket = new ReconnectingWebSocket('ws://' + window.location.host); | ||
sharedb.types.register(json1.type); | ||
var connection = new sharedb.Connection(socket); | ||
|
||
// Create local Doc instance mapped to 'examples' collection document with id 'counter' | ||
var doc = connection.get('examples', 'counter'); | ||
|
||
// Get initial value of document and subscribe to changes | ||
doc.subscribe(showNumbers); | ||
// When document changes (by this client or any other, or the server), | ||
// update the number on the page | ||
doc.on('op', showNumbers); | ||
|
||
function showNumbers() { | ||
document.querySelector('#num-clicks').textContent = doc.data.numClicks; | ||
}; | ||
|
||
// When clicking on the '+1' button, change the number in the local | ||
// document and sync the change to the server and other connected | ||
// clients | ||
function increment() { | ||
// Increment `doc.data.numClicks`. See | ||
// https://github.com/ottypes/json1/blob/master/spec.md for list of valid operations. | ||
doc.submitOp(['numClicks', {ena: 1}]); | ||
} | ||
|
||
var button = document.querySelector('button.increment'); | ||
button.addEventListener('click', increment); |
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,24 @@ | ||
{ | ||
"name": "counter-json1-vite", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"preview": "vite preview", | ||
"start": "node server.js" | ||
}, | ||
"dependencies": { | ||
"@teamwork/websocket-json-stream": "^2.0.0", | ||
"express": "^4.18.2", | ||
"ot-json1": "^1.0.2", | ||
"reconnecting-websocket": "^4.4.0", | ||
"sharedb": "^3.2.4", | ||
"sharedb-client-browser": "^4.2.0", | ||
"ws": "^8.12.1" | ||
}, | ||
"devDependencies": { | ||
"vite": "^4.1.4" | ||
} | ||
} |
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,41 @@ | ||
import http from 'http'; | ||
import express from 'express'; | ||
import ShareDB from 'sharedb'; | ||
import {WebSocketServer} from 'ws'; | ||
import WebSocketJSONStream from '@teamwork/websocket-json-stream'; | ||
import json1 from 'ot-json1'; | ||
|
||
ShareDB.types.register(json1.type); | ||
var backend = new ShareDB(); | ||
createDoc(startServer); | ||
|
||
// Create initial document then fire callback | ||
function createDoc(callback) { | ||
var connection = backend.connect(); | ||
var doc = connection.get('examples', 'counter'); | ||
doc.fetch(function(err) { | ||
if (err) throw err; | ||
if (doc.type === null) { | ||
doc.create({numClicks: 0}, json1.type.uri, callback); | ||
return; | ||
} | ||
callback(); | ||
}); | ||
} | ||
|
||
function startServer() { | ||
// Create a web server to serve files and listen to WebSocket connections | ||
var app = express(); | ||
app.use(express.static('dist')); | ||
var server = http.createServer(app); | ||
|
||
// Connect any incoming WebSocket connection to ShareDB | ||
var wss = new WebSocketServer({server: server}); | ||
wss.on('connection', function(ws) { | ||
var stream = new WebSocketJSONStream(ws); | ||
backend.listen(stream); | ||
}); | ||
|
||
server.listen(8080); | ||
console.log('Listening on http://localhost:8080'); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this repo in general uses ES3 syntax at the moment, I'd recommend doing a glob override to use this custom config just for the new example, instead of for the entire repo:
https://eslint.org/docs/latest/use/configure/configuration-files#configuration-based-on-glob-patterns