本文整理匯總了TypeScript中react-plugin.createPlugin函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript createPlugin函數的具體用法?TypeScript createPlugin怎麽用?TypeScript createPlugin使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了createPlugin函數的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: getNewPluginName
export function getMethodsOf<Spec extends PluginSpec>(
pluginName: Spec['name']
) {
const name = getNewPluginName();
createPlugin({ name }).register();
return getPluginContext(name).getMethodsOf<Spec>(pluginName);
}
示例2: mockPlug
export function mockPlug(
slotName: string,
component: PlugComponentType<any, any>
) {
const name = getNewPluginName();
const testPlugin = createPlugin({ name });
testPlugin.plug(slotName, component);
testPlugin.register();
}
示例3: onServerMessage
import { createPlugin, PluginContext } from 'react-plugin';
import { Message } from 'react-cosmos-shared2/util';
import { BuildMessage } from 'react-cosmos-shared2/build';
import { MessageHandlerSpec } from './../MessageHandler/public';
import { NotificationsSpec } from './../Notifications/public';
import { BuildNotificationsSpec } from './public';
type Context = PluginContext<BuildNotificationsSpec>;
const { on, register } = createPlugin<BuildNotificationsSpec>({
name: 'buildNotifications'
});
on<MessageHandlerSpec>('messageHandler', {
serverMessage: onServerMessage
});
export { register };
function onServerMessage(context: Context, msg: Message) {
const { getMethodsOf } = context;
const notifications = getMethodsOf<NotificationsSpec>('notifications');
const buildMsg = msg as BuildMessage;
switch (buildMsg.type) {
case 'buildStart':
return notifications.pushStickyNotification({
id: 'build',
type: 'loading',
title: 'Rebuilding...',
info: 'Your code is updating.'
示例4: getConnectedRendererIds
import { setFixtureState } from './setFixtureState';
import { receiveResponse } from './receiveResponse';
import { onRouterFixtureChange } from './onRouterFixtureChange';
import { RendererCoreSpec } from './public';
import { Context } from './shared';
const { on, register } = createPlugin<RendererCoreSpec>({
name: 'rendererCore',
initialState: {
connectedRendererIds: [],
primaryRendererId: null,
fixtures: {},
fixtureState: {}
},
methods: {
getConnectedRendererIds,
getPrimaryRendererId,
getFixtures,
getFixtureState,
isRendererConnected,
isValidFixtureSelected,
setFixtureState,
selectPrimaryRenderer,
receiveResponse
}
});
on<RouterSpec>('router', { fixtureChange: onRouterFixtureChange });
export { register };
function getConnectedRendererIds({ getState }: Context) {
示例5: onRendererResponse
import { createPlugin, PluginContext } from 'react-plugin';
import { Message } from 'react-cosmos-shared2/util';
import { WebpackRendererResponse } from 'react-cosmos-shared2/webpack';
import { RendererCoreSpec } from './../RendererCore/public';
import { NotificationsSpec } from './../Notifications/public';
import { WebpackHmrNotificationSpec } from './public';
type Context = PluginContext<WebpackHmrNotificationSpec>;
const { on, register } = createPlugin<WebpackHmrNotificationSpec>({
name: 'webpackHmrNotification'
});
on<RendererCoreSpec>('rendererCore', {
response: onRendererResponse
});
export { register };
function onRendererResponse(context: Context, msg: Message) {
const { getMethodsOf } = context;
const notifications = getMethodsOf<NotificationsSpec>('notifications');
const rendererResponse = msg as WebpackRendererResponse;
switch (rendererResponse.type) {
case 'rendererHmrFail':
notifications.pushTimedNotification({
// This event could potentially be triggered by multiple renderers at
// once, but it only makes sense that hot reloading should fail the same
// in all of them. To prevent duplicating this error in such cases the
// notification ID is _not_ unique per renderer.
示例6: subscribeToLocationChanges
import {
UrlParams,
getUrlParams,
pushUrlParams,
subscribeToLocationChanges
} from '../../shared/url';
import { RouterSpec } from './public';
type Context = PluginContext<RouterSpec>;
const { onLoad, register } = createPlugin<RouterSpec>({
name: 'router',
initialState: {
urlParams: {}
},
methods: {
getSelectedFixtureId,
isFullScreen,
selectFixture,
unselectFixture
}
});
onLoad(context => {
const { setState } = context;
setState({ urlParams: getUrlParams() });
return subscribeToLocationChanges((urlParams: UrlParams) => {
const { fixtureId } = context.getState().urlParams;
const fixtureChanged = !isEqual(urlParams.fixtureId, fixtureId);
setState({ urlParams }, () => {
示例7: loadCache
import * as localForage from 'localforage';
import { PluginContext, createPlugin } from 'react-plugin';
import { StorageSpec } from './public';
type Context = PluginContext<StorageSpec>;
const { register } = createPlugin<StorageSpec>({
name: 'storage',
initialState: {
cache: null
},
methods: {
loadCache,
getItem,
setItem
}
});
export { register };
async function loadCache(context: Context, projectId: string) {
const items = (await localForage.getItem(getProjectKey(projectId))) || {};
context.setState({ cache: { projectId, items } });
}
function getItem(context: Context, key: string) {
const { cache } = context.getState();
if (!cache) {
throw new Error(`Can't retrieve item "${key}" before loading storage`);
}
示例8:
export function mockMethodsOf<Spec extends PluginSpec>(
pluginName: Spec['name'],
methods: Partial<MethodHandlers<Spec>>
) {
createPlugin<any>({ name: pluginName, methods }).register();
}