Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

fix: fix datastore factory #65

Merged
merged 4 commits into from
Jan 22, 2021
Merged

fix: fix datastore factory #65

merged 4 commits into from
Jan 22, 2021

Conversation

hugomrdias
Copy link
Member

The factory reference type can't extend the Datastore interface directly.

We want the class instance to implement the interface and not the class (Object) reference.

The factory reference type can't extend the Datastore interface directly.

We want the class instance to implement the interface and not the class (Object) reference.
@achingbrain
Copy link
Member

Why would the factory extend the datastore? It should create datastores, not be a datastore.

@hugomrdias
Copy link
Member Author

exactly thats the problem this PR fixes

@achingbrain
Copy link
Member

I can see that in the removal of extends Datastore but then why would we set the prototype of the DatastoreFactory interface to be the Datastore interface?

@hugomrdias
Copy link
Member Author

hugomrdias commented Jan 22, 2021

I can see that in the removal of extends Datastore but then why would we set the prototype of the DatastoreFactory interface to be the Datastore interface?

that way TS can infere if a the output of new DatastoreFactory implements Datastore correctly, and the object DatastoreFactory can still be just a abstract object with an constructor.

Without it i can assign a partially implemented DatastoreFactory to a var that will be used to create Datastore and only see error later in the code path or even just when a not implemented method is called.

@achingbrain
Copy link
Member

But in the factory pattern a DatastoreFactory would not be a Datastore, it just creates them:

const factory = new DatastoreFactoryImpl()
const datastore = factory.create()

A datastore impl might implement both Datastore and DatastoreFactory, but one interface should not extend the other:

class MyDatastore implements Datastore, DatastoreFactory {
  // ...Datastore methods

  create () {
    return new MyDatastore()
  }
}

@hugomrdias
Copy link
Member Author

hugomrdias commented Jan 22, 2021

humm this isn't actually the factory pattern, its just an extra type for the class object because we can't write class types in .ts files without an implementation. (ts 4.2 will allow for abstract classes in .ts)
So we need a type for the class object with a constructor and a type for the instance interface.

@achingbrain
Copy link
Member

If it's not the factory pattern, why is it called DatastoreFactory?

@hugomrdias
Copy link
Member Author

If it's not the factory pattern, why is it called DatastoreFactory?

Suggestions ?

@achingbrain
Copy link
Member

Will this not work? ipfs/js-ipfs-repo#275 (comment)

Then you don't need DatastoreFactory at all.

@hugomrdias
Copy link
Member Author

Will this not work? ipfs/js-ipfs-repo#275 (comment)

Then you don't need DatastoreFactory at all.

sure we can do that, and i also confirmed that prototype is not needed.

i would still like to have a type here, i changed the name to DatastoreConstructor, sounds good ?

@achingbrain
Copy link
Member

It's a better name, less confusing, but do we really need it or can we delete it entirely?

@hugomrdias
Copy link
Member Author

hugomrdias commented Jan 22, 2021

your suggestion works and is enough, i just think having a type properly defined here can't hurt.
for example in the future if we want to defined params for the constructor we would do it here and dependents would just update instead of having to change it manually

@achingbrain
Copy link
Member

for example in the future if we want to..

The YAGNI principle says we should add that when we need it..

@hugomrdias
Copy link
Member Author

gone

@achingbrain achingbrain merged commit 586f883 into master Jan 22, 2021
@achingbrain achingbrain deleted the fix/datastore-factory branch January 22, 2021 17:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants