Skip to content
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

feat!: Add support for custom isEqual #59

Merged
merged 9 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"react-dom": ">=16.0 <18.0",
"replicache": "^13.0.0",
"snowpack": "^3.8.8",
"typescript": "^4.4.3"
"typescript": "^5.2.2"
},
"files": [
"out/index.js",
Expand Down
216 changes: 214 additions & 2 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {expect} from '@esm-bundle/chai';
import {resolver} from '@rocicorp/resolver';
import React from 'react';
import {render} from 'react-dom';
import type {JSONValue} from 'replicache';
import type {JSONValue, ReadTransaction, ReadonlyJSONValue} from 'replicache';
import {Replicache, TEST_LICENSE_KEY, WriteTransaction} from 'replicache';
import {useSubscribe} from './index';
import {Subscribable, SubscribableWithIsEqual, useSubscribe} from './index';

function sleep(ms: number | undefined): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
Expand Down Expand Up @@ -204,3 +204,215 @@ test('changing subscribable instances', async () => {
await rep1.close();
await rep2.close();
});

test('using isEqual', async () => {
const {promise, resolve} = resolver();

const sentinel = Symbol();

class FakeReplicache implements SubscribableWithIsEqual<ReadTransaction> {
subscribe<Data>(
query: (tx: ReadTransaction) => Promise<Data>,
{
onData,
isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b),
}: {
onData: (data: Data) => void;
isEqual?: ((a: Data, b: Data) => boolean) | undefined;
},
): () => void {
const data = query({} as ReadTransaction);
let previous: Data | typeof sentinel = sentinel;
void data.then(data => {
if (previous === sentinel || isEqual(previous, data)) {
previous = data;
return onData(data);
}
});

return () => undefined;
}
}

function A({
rep,
def,
}: {
rep: FakeReplicache | null | undefined;
def: string;
}) {
const subResult = useSubscribe(
rep,
async () => {
resolve();
return 123n;
},
{
isEqual(a, b) {
return a === b;
},
default: def,
},
);
return (
<div>
{typeof subResult}, {String(subResult)}
</div>
);
}

const div = document.createElement('div');

render(<A key="a" rep={null} def="a" />, div);
expect(div.textContent).to.equal('string, a');

render(<A key="b" rep={undefined} def="b" />, div);
expect(div.textContent).to.equal('string, b');

const rep = new FakeReplicache();

render(<A key="c" rep={rep} def="c" />, div);
expect(div.textContent).to.equal('string, c');
await promise;
await sleep(1);
expect(div.textContent).to.equal('bigint, 123');
});

test.skip('using isEqual [type checking]', async () => {
const use = (...args: unknown[]) => args;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function expectType<T>(_expression: T) {
// intentionally empty
}

class WithIsEqual<Tx> implements SubscribableWithIsEqual<Tx> {
subscribe<Data>(
query: (tx: Tx) => Promise<Data>,
options: {
onData: (data: Data) => void;
isEqual?: ((a: Data, b: Data) => boolean) | undefined;
},
): () => void {
use(query, options);
return () => undefined;
}
}

{
const s = useSubscribe(
new WithIsEqual<ReadTransaction>(),
tx => {
use(tx);
return Promise.resolve(123n);
},
{isEqual: (a, b) => a === b},
);
expectType<bigint | undefined>(s);
}

{
// This is invalid because the return type is not json
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized this kind of advanced type checking could be put into replicache/reflect too.

I don't think it makes sense to have advanced type check here but not in replicache/reflect.

// @ts-expect-error index.ts(61, 3): An argument for 'options' was not provided.
const s = useSubscribe(new WithIsEqual<ReadTransaction>(), tx => {
use(tx);
return Promise.resolve(123n);
});
use(s);
}

{
// default not passed so it is undefined
const s = useSubscribe(new WithIsEqual<ReadTransaction>(), tx => {
use(tx);
return Promise.resolve(123);
});
expectType<123 | undefined>(s);
}

{
const s = useSubscribe(
new WithIsEqual<ReadTransaction>(),
tx => {
use(tx);
return Promise.resolve(123);
},
456,
);
expectType<123 | 456>(s);
}

{
const s: bigint | 'abc' = useSubscribe(
new WithIsEqual<ReadTransaction>(),
tx => {
use(tx);
return Promise.resolve(123n);
},
{isEqual: (a, b) => a === b, default: 'abc'},
);
expectType<bigint | 'abc'>(s);
}
});

test('No isEqual [type checking]', async () => {
const use = (...args: unknown[]) => args;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function expectType<T>(_expression: T) {
// intentionally empty
}

class WithNoIsEqual<Tx> implements Subscribable<Tx> {
subscribe<Data extends ReadonlyJSONValue | undefined>(
query: (tx: Tx) => Promise<Data>,
{onData}: {onData: (data: Data) => void},
): () => void {
use(query, onData);
return () => undefined;
}
}

{
const s = useSubscribe(new WithNoIsEqual<ReadTransaction>(), tx => {
use(tx);
return Promise.resolve(123);
});
expectType<123 | undefined>(s);
}

{
// When subscribe does not support isEqual, the options is treated as default :'(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty unfortunate.

Maybe we should do a major release and not support all this crazy overloads?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's just do:

useSubscribe(r, query, opts?)

Where opts is def, isEqual, and deps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the name default over def. I'm fine with deps because dependencies is so long but maybe it would be better to be more clear... especially if we consider dependencies to be very rarely used. I guess that convinced me. I will update the PR with the major breaking changes.

const s = useSubscribe(
new WithNoIsEqual<ReadTransaction>(),
tx => {
use(tx);
return Promise.resolve(123);
},
{isEqual: (a: number, b: number) => a === b},
);
expectType<123 | {isEqual: (a: number, b: number) => boolean}>(s);
}

{
// This is invalid because the return type is not json
// @ts-expect-error index.ts(61, 3): An argument for 'options' was not provided.
const s = useSubscribe(new WithNoIsEqual<ReadTransaction>(), tx => {
use(tx);
return Promise.resolve(123n);
});
use(s);
}

{
const s = useSubscribe(
new WithNoIsEqual<ReadTransaction>(),
tx => {
use(tx);
return Promise.resolve(123);
},
456,
);
expectType<123 | 456>(s);
}
});
Loading