Closed grabbou closed 10 years ago
@grabbou personally I like the simplicity of the proposed syntax. You may even consider something like this(?):
Storage.provider.XXX.pre('upload', function (next) { next(err); });
@webteckie, thanks for your input! Unfortunately, we can't write here dot access as under Storage.providers we have constructors of available providers to use. Writing this kind of access may occur multiple errors for users as they might try to write something like this Storage.provider.XXX.upload(...)
. In some cases, they might forget about _init()
method meaning for example that they will try to upload a file to MongoDB database without even opening connection to it.
Leaving open for now as one extra thing is missing.
To clarify:
config
cache
cache
and _init
method of it has been called at least once (happens inside get
method)Problem:
_If generic version called, we only check whether _cache
size is bigger than 0. If yes -> we simply iterate through it and hook methods of every instance inside it._ If not -> we simply try to get default instance using _getInstance
that will automatically raise an error if something is wrong. That method will update our _cache
automatically.
Bug: User configured 5 providers and already initialised one of them (it means that our cache is not empty). Current version will add hook to that one only skipping the rest 4 providers configured but not yet created/initialised.
Solution: We have to compare _cache against _config to make sure that every provider configured has been already created (doesn't matter whether it's been initialised or not).
It'd be better to declare hooks like so:
instead of getting storage client in async manner like we do now:
What you think? Similar code for
post
hook.In first case - we simply check whether provider we want to hook is already defined. If not -> we create it by calling its constructor (please note that we do not call
_init
method so that operation is synchronous). If it's not available - error is being thrown fromStorage.get('provider')
that's unable to obtain an instance.In second case - we declare hooks for all of the providers available in our cache. If nothing is available -> we try to declare default provider (so we perform
Storage.get()
in the background without_init
method). If default provider is not available - same as above.Async
_init
method will be invoked on firstStorage.get()
called by the user.Method and arguments are subject to discuss and probably -> change.