Skip to content

Instantly share code, notes, and snippets.

@jakwuh
Last active November 25, 2016 11:55
Show Gist options
  • Select an option

  • Save jakwuh/9e65099a3a84a23d0434a41f22d93ec3 to your computer and use it in GitHub Desktop.

Select an option

Save jakwuh/9e65099a3a84a23d0434a41f22d93ec3 to your computer and use it in GitHub Desktop.
DI@2 syntax proposal
export {YandexMetrics as Metrics} from './lib/Metrics/YandexMetrics.js';
export const RouterEvent = Symbol(); // quick replacement for router event interface
// export all deps which should be hidden by interfaces (however it is js so we do not have any interfaces)
export * from './default-interfaces.js';
export {ClientRequest as Request} from './lib/ClientRequest';
export {ClientLogger as Logger} from './lib/ClientLogger';
export * from './default-interfaces.js';
export {ServerRequest as Request} from './lib/ServerRequest';
export {ServerLogger as Logger} from './lib/ServerLogger';
import {assignDependencies} from 'di2.js';
export class Card {
}
/**
Default DIDefinition is {
provider: Card,
updateMethod: container.defaultUpdateMethod,
dependencies: Card.dependencies || {},
annotations: Card.annotations || []
}
**/
export const CardWithCustomUpdate = new DIDefinition({
provider: Card, // class will be instantiated with new Class()
updateMethod: assignDependencies,
annotations: []
});
export const FactoryCardWithCustomUpdate = new DIDefinition({
provider: Card.factory, // function will be used as factory
updateMethod: 'updateFromQuery',
annotations: [
new TransientLabel() // do not cache inside container
]
});
export const CardWithCustomUpdate1 = new DIDefinition(Card); // CardWithCustomUpdate1 is pretty same as Card, however these are 2 different DI tokens
export const CardWithCustomUpdate2 = new DIDefinition(Card, assignDependencies); // sugar
export const CardWithCustomUpdate3 = new DIDefinition(Card, 'updateDependencies'); // sugar
export const CardWithCustomUpdate4 = new DIDefinition(Card, 'updateDependencies', [/* annotations */]]); // sugar
import {SessionContainer, RouterEvent} from 'webpack/interfaces'; // this will be replaced by webpack in runtime (e.g. -> client-interfaces.js)
import {Card, CardWithCustomUpdate, FactoryCardWithCustomUpdate} from '../models/Card.js';
export class BasePage {
updateDependencies({SessionContainer, RouterEvent, title, card}) {
}
}
BasePage.dependencies = {
SessionContainer,
RouterEvent,
card: Card
};
BasePage.annotations = [
new ReuseLabel() // always reuse BasePage
];
export const HomePage = new DIDefinition({
provider: BasePage,
dependencies: {
card: CardWithCustomUpdate,
title: new DIDefinition(BaseTitle, 'home')
}
});
export const CustomHomePage = new DIDefinition({
provider: HomePage,
dependencies: {
card: FactoryCardWithCustomUpdate,
title: CustomHomeTitle
}
});
// session mechanism remains the same
// serialization
// only DIDefinitions could be serialized
// this is because we do not have an `id` on instances (objects are used as a tokens)
let routeData = di.serialize(HomeRoute)
// ...
di.restore(HomeRoute, routeData)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment