本文整理汇总了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();
}