Skip to content

Commit b0be806

Browse files
chore: upgrade dependencies and migrate example to vite (#68)
* replace create-react-app example with vite * upgrade root repo eslint * wip: fix vulns * wip: fix vulns, adjust eslint to ignore website gen * patch most others left * add resolutions for ws and cookie * fix ci * give example dev server static port * remove explicit package resolutions by upgrading the intermediate `socket.io` and `engine.io` deps. --------- Co-authored-by: Jonathan Reyes <[email protected]>
1 parent 68c6f54 commit b0be806

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+6323
-12059
lines changed

.eslintignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

.eslintrc

Lines changed: 0 additions & 61 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: 'Install and Build'
2020
run: |
2121
yarn
22-
cd examples/create-react-app-typescript-example
22+
cd examples/vite-typescript-example
2323
yarn
2424
cd -
2525
yarn types

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CHANGELOG.md

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
"semi": false,
55
"tabWidth": 2,
66
"trailingComma": "all"
7-
}
7+
}

README.md

Lines changed: 72 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ yarn add @dittolive/ditto @dittolive/react-ditto
2828
```tsx
2929
<DittoProvider setup={createDittoInstance}>
3030
{({ loading, error }) => {
31-
if (loading) return <span>Loading Ditto...</span>;
31+
if (loading) return <span>Loading Ditto...</span>
3232
if (error)
3333
return (
3434
<span>There was an error loading Ditto. Error: {error.toString()}</span>
35-
);
36-
return <App />;
35+
)
36+
return <App />
3737
}}
3838
</DittoProvider>
3939
```
@@ -59,14 +59,14 @@ const createDittoInstance = () => {
5959
return ditto
6060
}
6161

62-
<DittoProvider setup={createDittoInstance}>
62+
;<DittoProvider setup={createDittoInstance}>
6363
{({ loading, error }) => {
64-
if (loading) return <span>Loading Ditto...</span>;
64+
if (loading) return <span>Loading Ditto...</span>
6565
if (error)
6666
return (
6767
<span>There was an error loading Ditto. Error: {error.toString()}</span>
68-
);
69-
return <App />;
68+
)
69+
return <App />
7070
}}
7171
</DittoProvider>
7272
```
@@ -91,14 +91,14 @@ const createDittoInstance = async (path) => {
9191
}
9292
}
9393

94-
<DittoLazyProvider setup={createDittoInstance}>
94+
;<DittoLazyProvider setup={createDittoInstance}>
9595
{({ loading, error }) => {
96-
if (loading) return <span>Loading Ditto...</span>;
96+
if (loading) return <span>Loading Ditto...</span>
9797
if (error)
9898
return (
9999
<span>There was an error loading Ditto. Error: {error.toString()}</span>
100-
);
101-
return <App />;
100+
)
101+
return <App />
102102
}}
103103
</DittoLazyProvider>
104104
```
@@ -132,16 +132,20 @@ const createDittoInstance = (forPath: string) => {
132132
### Online Identities
133133

134134
```ts
135-
const { create, getAuthenticationRequired, getTokenExpiresInSeconds } = useOnlineIdentity()
135+
const { create, getAuthenticationRequired, getTokenExpiresInSeconds } =
136+
useOnlineIdentity()
136137

137138
const createDittoInstance = (forPath: string) => {
138139
// Example of how to create an online instance with authentication enabled
139140
const dittoOnline = new Ditto(
140-
create({
141-
// If you're using the Ditto cloud, this ID should be the app ID shown on your app settings page on the portal.
142-
appID: uuidv4(),
143-
enableDittoCloudSync: true,
144-
}, forPath),
141+
create(
142+
{
143+
// If you're using the Ditto cloud, this ID should be the app ID shown on your app settings page on the portal.
144+
appID: uuidv4(),
145+
enableDittoCloudSync: true,
146+
},
147+
forPath,
148+
),
145149
forPath,
146150
)
147151
dittoOnline.startSync()
@@ -158,8 +162,8 @@ const createDittoInstance = (forPath: string) => {
158162
create({
159163
// If you're using the Ditto cloud, this ID should be the app ID shown on your app settings page on the portal.
160164
appID: uuidv4(),
161-
// If you're using the Ditto cloud, this token should be the Online Playground Authentication Token shown on your app settings page on the portal.
162-
token: 'my-token'
165+
// If you're using the Ditto cloud, this token should be the Online Playground Authentication Token shown on your app settings page on the portal.
166+
token: 'my-token',
163167
}),
164168
forPath,
165169
)
@@ -170,9 +174,9 @@ const createDittoInstance = (forPath: string) => {
170174

171175
You can find more information on working with online apps [here](#working-with-online-apps)
172176

173-
## Quick Start with `create-react-app`
177+
## Quick Start with `vite`
174178

175-
This is a quick guide on using Ditto with `create-react-app` builds.
179+
This is a quick guide on using Ditto with `vite` builds.
176180

177181
1. Install this library with npm or yarn
178182

@@ -189,12 +193,12 @@ yarn add @dittolive/ditto @dittolive/react-ditto
189193
2. In `./src/index.js`, or `./src/index.tsx` if you're using typescript, setup Ditto with the `DittoProvider` like so:
190194

191195
```tsx
192-
// ... other imports from create-react-app above
193-
import { Ditto } from "@dittolive/ditto";
196+
// ... other imports from vite above
197+
import { Ditto } from '@dittolive/ditto'
194198
import {
195199
DittoProvider,
196200
useOnlinePlaygroundIdentity,
197-
} from "@dittolive/react-ditto";
201+
} from '@dittolive/react-ditto'
198202

199203
/**
200204
* This configuration is optional for web browser-based react applications. This
@@ -206,12 +210,12 @@ import {
206210
* details.
207211
**/
208212
const initOptions = {
209-
webAssemblyModule: "/ditto.wasm",
210-
};
213+
webAssemblyModule: '/ditto.wasm',
214+
}
211215

212216
/** Example of a React root component setting up a single ditto instance that uses a development connection */
213217
const RootComponent = () => {
214-
const { create } = useOnlinePlaygroundIdentity();
218+
const { create } = useOnlinePlaygroundIdentity()
215219

216220
return (
217221
<DittoProvider
@@ -221,43 +225,43 @@ const RootComponent = () => {
221225
// Create an app on https://portal.ditto.live/ and follow the
222226
// instructions to get your token. Replace the placeholders below
223227
// with your app id and token.
224-
appID: "your-app-id",
225-
token: "your-online-playground-token",
228+
appID: 'your-app-id',
229+
token: 'your-online-playground-token',
226230
}),
227-
"testing"
228-
);
229-
await ditto.disableSyncWithV3();
230-
ditto.startSync();
231-
return ditto;
231+
'testing',
232+
)
233+
await ditto.disableSyncWithV3()
234+
ditto.startSync()
235+
return ditto
232236
}}
233237
/* initOptions={initOptions} */
234238
>
235239
{({ loading, error }) => {
236-
if (loading) return <p>Loading</p>;
237-
if (error) return <p>{error.message}</p>;
238-
return <App />;
240+
if (loading) return <p>Loading</p>
241+
if (error) return <p>{error.message}</p>
242+
return <App />
239243
}}
240244
</DittoProvider>
241-
);
242-
};
245+
)
246+
}
243247

244-
const root = ReactDOM.createRoot(document.getElementById("root"));
248+
const root = ReactDOM.createRoot(document.getElementById('root'))
245249
root.render(
246250
<React.StrictMode>
247251
<RootComponent />
248-
</React.StrictMode>
249-
);
252+
</React.StrictMode>,
253+
)
250254
```
251255

252256
3. In your `App` component, you can now use hooks like `usePendingCursorOperation` or `usePendingIDSpecificOperation` to get your documents like so:
253257

254258
```tsx
255-
import { usePendingCursorOperation, useMutations } from '@dittolive/react-ditto';
259+
import { usePendingCursorOperation, useMutations } from '@dittolive/react-ditto'
256260

257261
export default function App() {
258262
const { documents } = usePendingCursorOperation({
259263
collection: 'tasks',
260-
});
264+
})
261265

262266
const { removeByID, upsert } = useMutations({ collection: 'tasks' })
263267

@@ -282,26 +286,26 @@ export default function App() {
282286
Alternatively, you can also choose to go with the lazy variants of these hooks (`useLazyPendingCursorOperation` and `useLazyPendingIDSpecificOperation`), in order to launch queries on the data store as a response to a user event:
283287

284288
```tsx
285-
import { useLazyPendingCursorOperation } from '@dittolive/react-ditto';
289+
import { useLazyPendingCursorOperation } from '@dittolive/react-ditto'
286290

287291
export default function App() {
288-
const { documents, exec } = useLazyPendingCursorOperation();
292+
const { documents, exec } = useLazyPendingCursorOperation()
289293

290294
if (!documents?.length) {
291295
return (
292-
<button onClick={() => exec({ collection: "tasks" })}>
296+
<button onClick={() => exec({ collection: 'tasks' })}>
293297
Click to load!
294298
</button>
295-
);
299+
)
296300
}
297-
301+
298302
return (
299303
<ul>
300304
{documents.map((doc) => (
301305
<li key={doc._id}>{JSON.stringify(doc.value)}</li>
302306
))}
303307
</ul>
304-
);
308+
)
305309
}
306310
```
307311

@@ -314,22 +318,28 @@ Using the [Portal](http://portal.ditto.live) you can create apps that sync to th
314318
import { useEffect } from 'react'
315319

316320
const RootComponent = () => {
317-
const { create, getAuthenticationRequired, getTokenExpiresInSeconds } = useOnlineIdentity()
321+
const { create, getAuthenticationRequired, getTokenExpiresInSeconds } =
322+
useOnlineIdentity()
318323

319324
return (
320325
<>
321326
<DittoProvider
322327
setup={() => {
323-
const ditto = new Ditto(create({ appID: 'your-app-id', path: '/my-online-path' }, '/my-online-path'))
328+
const ditto = new Ditto(
329+
create(
330+
{ appID: 'your-app-id', path: '/my-online-path' },
331+
'/my-online-path',
332+
),
333+
)
324334
ditto.startSync()
325335
return ditto
326336
}}
327337
/*initOptions={initOptions} */
328338
>
329339
{({ loading, error, ditto }) => {
330-
if (loading) return <p>Loading</p>;
331-
if (error) return <p>{error.message}</p>;
332-
return <App />;
340+
if (loading) return <p>Loading</p>
341+
if (error) return <p>{error.message}</p>
342+
return <App />
333343
}}
334344
</DittoProvider>
335345
</>
@@ -340,24 +350,22 @@ const App = () => {
340350
const ditto = useDitto()
341351

342352
useEffect(() => {
343-
if(ditto) {
344-
ditto
345-
.auth
353+
if (ditto) {
354+
ditto.auth
346355
.loginWithToken('token', 'provider')
347356
.then(() => console.log('Login successful'))
348357
}
349358
}, [ditto])
350-
351-
return (<div>Hello world!</div>)
352-
}
353359

360+
return <div>Hello world!</div>
361+
}
354362
```
355363

356364
For Online apps, the `useOnlineIdentity` hook returns the following set of properties that can be used to manage authentication for your app:
357365

358-
* `create`: Creates an `onlineWithAuthentication` object preconfigured such that the hook can manage the authentication flow using the exposed `authenticate` function.
359-
* `getAuthenticationRequired`: Is a function that takes in the app path for which to check the authentication required state, and will return true if your Ditto instance is requiring the current user to authenticate with the app. You can configure authentication webhooks on the [Portal](http://portal.ditto.live), from your app settings area, in order to provide your own set of validation services for your app.
360-
* `getTokenExpiresInSeconds`: Is a function that takes in the app path for which to check the token expiry second, and returns the number of seconds in which your current token expires if this has been reported by the Ditto SDK.
366+
- `create`: Creates an `onlineWithAuthentication` object preconfigured such that the hook can manage the authentication flow using the exposed `authenticate` function.
367+
- `getAuthenticationRequired`: Is a function that takes in the app path for which to check the authentication required state, and will return true if your Ditto instance is requiring the current user to authenticate with the app. You can configure authentication webhooks on the [Portal](http://portal.ditto.live), from your app settings area, in order to provide your own set of validation services for your app.
368+
- `getTokenExpiresInSeconds`: Is a function that takes in the app path for which to check the token expiry second, and returns the number of seconds in which your current token expires if this has been reported by the Ditto SDK.
361369

362370
## Building this library and running tests
363371

0 commit comments

Comments
 (0)