Skip to content

Commit bff1b12

Browse files
committed
[persisters] PartyKitPersister
1 parent 919fc2d commit bff1b12

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

src/persisters/persister-partykit-client.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import {
77
deconstruct,
88
} from './partykit/common';
99
import {
10+
PartyKitPersister,
1011
PartyKitPersisterConfig,
1112
createPartyKitPersister as createPartyKitPersisterDecl,
1213
} from '../types/persisters/persister-partykit-client';
13-
import {Persister, PersisterListener} from '../types/persisters';
1414
import {ifNotUndefined, isString} from '../common/other';
1515
import {EMPTY_STRING} from '../common/strings';
1616
import PartySocket from 'partysocket';
17+
import {PersisterListener} from '../types/persisters';
1718
import {createCustomPersister} from '../persisters';
1819
import {jsonString} from '../common/json';
1920

@@ -26,7 +27,7 @@ export const createPartyKitPersister = ((
2627
connection: PartySocket,
2728
configOrStoreProtocol?: PartyKitPersisterConfig | 'http' | 'https',
2829
onIgnoredError?: (error: any) => void,
29-
): Persister => {
30+
): PartyKitPersister => {
3031
const {host, room} = connection.partySocketOptions;
3132
const {
3233
storeProtocol = 'https',
@@ -100,5 +101,5 @@ export const createPartyKitPersister = ((
100101
delPersisterListener,
101102
onIgnoredError,
102103
['getConnection', connection],
103-
);
104+
) as PartyKitPersister;
104105
}) as typeof createPartyKitPersisterDecl;

src/types/docs/persisters/persister-partykit-client.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,42 @@
1919
* @since 4.3.0
2020
*/
2121
/// persister-partykit-client
22+
/**
23+
* The PartyKitPersister interface is a minor extension to the Persister
24+
* interface.
25+
*
26+
* It simply provides an extra getConnection method for accessing the
27+
* PartySocket the Store is being persisted to.
28+
* @since v4.3.14
29+
*/
30+
/// PartyKitPersister
31+
{
32+
/**
33+
* The getConnection method returns the PartySocket the Store is being
34+
* persisted to.
35+
* @returns The PartySocket.
36+
* @example
37+
* This example creates a Persister object against a newly-created Store and
38+
* then gets the PartySocket back out again.
39+
*
40+
* ```js yolo
41+
* const partySocket = new PartySocket({
42+
* host: PARTYKIT_HOST,
43+
* room: 'my_room',
44+
* });
45+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
46+
* const persister = createPartyKitPersister(store, partySocket);
47+
*
48+
* console.log(persister.getConnection() == partySocket);
49+
* // -> true
50+
*
51+
* persister.destroy();
52+
* ```
53+
* @category Getter
54+
* @since v4.3.14
55+
*/
56+
/// PartyKitPersister.getConnection
57+
}
2258
/**
2359
* The PartyKitPersisterConfig type describes the configuration of a PartyKit
2460
* Persister on the client side.
@@ -102,9 +138,9 @@
102138
* @param onIgnoredError An optional handler for the errors that the Persister
103139
* would otherwise ignore when trying to save or load data. This is suitable for
104140
* debugging persistence issues in a development environment.
105-
* @returns A reference to the new Persister object.
141+
* @returns A reference to the new PartyKitPersister object.
106142
* @example
107-
* This example creates a Persister object and persists the Store to the
143+
* This example creates a PartyKitPersister object and persists the Store to the
108144
* browser's IndexedDB storage.
109145
*
110146
* ```js yolo

src/types/persisters/persister-partykit-client.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import PartySocket from 'partysocket';
44
import {Persister} from '../persisters';
55
import {Store} from '../store';
66

7+
/// PartyKitPersister
8+
export interface PartyKitPersister extends Persister {
9+
/// PartyKitPersister.getConnection
10+
getConnection: () => PartySocket;
11+
}
12+
713
/// PartyKitPersisterConfig
814
export type PartyKitPersisterConfig = {
915
/// PartyKitPersisterConfig.storeProtocol
@@ -20,4 +26,4 @@ export function createPartyKitPersister(
2026
connection: PartySocket,
2127
configOrStoreProtocol?: PartyKitPersisterConfig | 'http' | 'https',
2228
onIgnoredError?: (error: any) => void,
23-
): Persister;
29+
): PartyKitPersister;

src/types/with-schemas/persisters/persister-partykit-client.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import {OptionalSchemas, Store} from '../store';
44
import PartySocket from 'partysocket';
55
import {Persister} from '../persisters';
66

7+
/// PartyKitPersister
8+
export interface PartyKitPersister<Schemas extends OptionalSchemas>
9+
extends Persister<Schemas> {
10+
/// PartyKitPersister.getConnection
11+
getConnection: () => PartySocket;
12+
}
13+
714
/// PartyKitPersisterConfig
815
export type PartyKitPersisterConfig = {
916
/// PartyKitPersisterConfig.storeProtocol
@@ -20,4 +27,4 @@ export function createPartyKitPersister<Schemas extends OptionalSchemas>(
2027
connection: PartySocket,
2128
configOrStoreProtocol?: PartyKitPersisterConfig | 'http' | 'https',
2229
onIgnoredError?: (error: any) => void,
23-
): Persister<Schemas>;
30+
): PartyKitPersister<Schemas>;

0 commit comments

Comments
 (0)