-
Notifications
You must be signed in to change notification settings - Fork 452
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
modernize code #202
Comments
Seems like a heavy lift without much gain to me. Also it would break working code and require a major version release (and the project is not even at 1.0 yet). As a user of ShareDB not using TypeScript, what do I stand to gain from the proposed changes? |
This would make sense to do in a hard fork, then release 1.0 there. I fear this project will never ever reach 1.0. /cc @nateps |
The only point that would require a change for the users of the library is |
Indeed, the benefit for users of ShareDB seems to be the switch from callbacks to Promises. The other benefits are for maintainers and contributors. |
Example, it you want to create a new PubSub you just need to extend the abstract PubSub : export interface IPubSubOptions {
prefix?: string;
}
export interface IStream {
id: string;
}
export declare abstract class PubSub {
private static shallowCopy(object);
protected prefix?: string;
protected nextStreamId: number;
protected streamsCount: number;
protected streams: {
[channel: string]: IStream;
};
protected subscribed: {
[channel: string]: boolean;
};
protected constructor(options?: IPubSubOptions);
close(callback?: (err:Error|null) => void): void;
publish(channels: string[], data: object, callback: (err: Error | null) => void): void;
subscribe(channel: string, callback: (err: Error | null, stream?: IStream) => void): void;
protected abstract _subscribe(channel: string, callback: (err: Error | null) => void): void;
protected abstract _unsubscribe(channel: string, callback: (err: Error | null) => void): void;
protected abstract _publish(channels: string[], data: Object, callback: (err: Error | null) => void): void;
protected _emit(channel: string, data: object): void;
private _createStream(channel);
private _removeStream(channel, stream);
} This definition is not manually generated, it's just the existing code with types (and a modernization even if I didn't changed the callback to Promise because I wanted exactly the same implementation) |
we could probably make more stuff private but because there is no contract in the current library implementation I think only @nateps knows. |
As much as I would love to see the death of all callbacks in this codebase, I agree that it might be a lot of work. As a stop-gap, would it be possible to at least return At the moment, my own codebase is littered with code like: function submitOp(document, op) {
return Promise((resolve, reject) => {
document.submitOp(op, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
} whereas obviously I'd love to be able to just write: async function() {
// Do stuff
await document.submitOp(op);
// Do other stuff
} We could definitely add this support without dropping callback support. It's a bit icky, but it's definitely a common pattern when trying to support both styles. If people are keen for this, I could try to go around and replace method calls by wrapping callbacks: Document.prototype.fetch = function(..., callback) {
// Do stuff...
return new Promise((resolve, reject) => {
var wrappedCallback = function (error) {
callback(error);
if (error) {
reject(error);
} else {
resolve();
}
};
doThingThatPassesOnCallback(..., wrappedCallback);
});
}; That way you could support both |
So I got kind of curious about this, and started a quick spike into a proof-of-concept switch over to TypeScript. I ported all the files in https://github.com/share/sharedb/compare/master...alecgibson:typescript?expand=1 A couple of my thoughts from this exercise:
What do people think? I'm personally a huge fan of TypeScript. I think it's a spectacular tool for refactoring with confidence (on top of tests of course!), and some of the modules in here really could do with some refactoring (the Finishing the job would still be a chunky bit of work (probably another week or so of full-time work on top of what I've already done). I'm not sure I really have time for it, but could try to keep chipping away at it, if people think it's worthwhile? Alternatively, we could set up a build pipeline on what I've done so far and keep incrementally updating the codebase, given that it retains backwards-compatibility? |
I agree obviously, low effort compared to the benefits. The goal is still that this original project get back on track anyway and not to multiplicate the forks. I would be able to contribute actively on the initial effort and on maintaining this but let's see if @gkubisa who has the most interesting fork would agree on that. I have 2 small pending PR on teamwork and they don't get any comments or merge so that may also be a dead end. |
Yes. It's a shame about all the forking that happens (we may have to work on a fork for document history if my other PR doesn't get merged). But if we do want to add TypeScript, I can definitely try to help chip away at this. |
I'd love to see the move to TypeScript. Also yes, it would be great to use Promises in the public API, even if the internals are left mostly untouched. Also I've been toying with the idea of exposing ShareDB subscriptions as Observables, which seems a more natural representation than the current callback system, or Promises. But I digress, that would be a separate issue. |
TypeScript and Promises in the public API sound great to me. I'll certainly accept the PRs in @teamwork/sharedb. I will also go through all the other PRs this week (I didn't do it yet because I was on holidays). Hopefully we'll have a PR review on Wednesday and get the main project going again, see #163 (comment). If not, I'll continue supporting the forks at @Teamwork. |
During the meeting last week @nateps was not keen to the idea of Promises in ShareDB. :-( Re the @teamwork/sharedb* forks, if there's no chance of introducing Promises to the official repos, than I'd rather stay away from them in my forks too, to avoid maintenance overhead. |
Ah I missed that. That's a shame. Guess I'll close that then (but for the record, I really, really, really hate callbacks). |
FYI, I made a preliminary type definition file for ShareDB in DefinitelyTyped. The PR is here: DefinitelyTyped/DefinitelyTyped#28089 I also created a TypeScript wrapper for ShareDB here: https://github.com/soney/sdb-ts There's still lots of work (and I'm not sure that the style will match everyone's preferred TS style) but I have found it helpful in a few of my own projects. |
Would also like to see this happen. A lot of the code would be much easier to grok as TS/modern js of some sort. As for the promise/callback thing, it's pretty trivial to support both with ~30 lines of vanillas js, so might as well, right? I know I promise-wrapped ShareDB for my usages, imagine many others have too. Willing to contribute to see it happen but a little confused about the various forks and how they relate. |
We discussed Typescript briefly at the weekly PR discussion meeting, and we all do think it's a good idea. (I somehow missed this thread when I started doing some ShareDB work.) Looks like there are two major ideas here. My current thoughts are that it'd be better to separate them out, to keep discussion focused on each and so it's easier to make progress.
This is the original repo, published as Right now, the largest fork I'm aware of is the Teamwork fork maintained by @gkubisa , which has user presence broadcasting. I believe the Teamwork fork pulls in upstream changes occasionally, so non-breaking changes here should also show up there. From both ends, we do want to get presence into the original repo here. No ETA yet, though, as it's a big feature, and finding enough time has been tricky. |
I'm happy to contribute some time to this (although I don't know how much I can promise). I hopefully have a better understanding of the library as a whole now, so should be able to make a better stab at it than last time. You're right - we can definitely do these separately. In theory, we can do TypeScript class by class (because it's a superset of JS), although in practice I tend to find that the first pass winds up requiring defining interfaces for everything anyway. I'm keen for Promises. Pretty sure hybrid Promises/callbacks are fine in TypeScript. I think you can overload methods in TypeScript. I guess the thing to do is get a priority call on one or the other of these. I personally would find both super beneficial and a huge help. In theory both can be done in non-breaking ways, so I don't think there's any priority defined by that. |
I can not dedicate time for that but if you have questions feel free to contact me. |
I'm happy to see ShareDB moving to TypeScript and Promises. Unfortunately, I won't have time to help with that. Regarding the Teamwork fork, I've been merging the latest upstream changes occasionally as they integrated cleanly, however, I'm not likely to have time to resolve bigger conflicts. To be honest, I'm rather reluctant to support the Teamwork fork in the long term. All my changes (presence and undo/redo in particular) are in the PRs, so feel free to tweak them as you see fit and merge (or just close the PRs :-) ), ideally before starting any work on TypeScript and Promises. |
@dcharbonnier @gkubisa any update on this in 2020? Should this issue be reopened? |
it seems like the @types/sharedb is still targeting the an outdated version of sharedb, is the types package built automatically from source? or do we have to manually update it? |
@issacclee it's manual. I've raised DefinitelyTyped/DefinitelyTyped#61202 |
Thanks for your work. It seems like the current type definition still has some inconsistency compare to the documentation. For example the Backend Api doc states that it contains a method called "submit" . However there's no such definition in the index.d.ts file. |
Yes, this is because the types were written manually. Please feel free to raise a Pull Request! |
It would be nice to modernize this code, this include :
The text was updated successfully, but these errors were encountered: