I think this is all deprecated & should be removed

This commit is contained in:
Norbert de Langen 2019-12-23 17:08:53 +01:00
parent e6bd6f4d76
commit dcbbdbb778
No known key found for this signature in database
GPG Key ID: 976651DA156C2825
4 changed files with 2 additions and 154 deletions

View File

@ -1,16 +1,13 @@
/* eslint no-underscore-dangle: 0 */
import deprecate from 'util-deprecate';
import isPlainObject from 'is-plain-object';
import { logger } from '@storybook/client-logger';
import addons, { StoryContext, StoryFn, Parameters } from '@storybook/addons';
import Events from '@storybook/core-events';
import { StoryContext, StoryFn, Parameters } from '@storybook/addons';
import { toId } from '@storybook/csf';
import mergeWith from 'lodash/mergeWith';
import isEqual from 'lodash/isEqual';
import get from 'lodash/get';
import { ClientApiParams, DecoratorFunction, ClientApiAddons, StoryApi } from './types';
import subscriptionsStore from './subscriptions_store';
import { applyHooks } from './hooks';
import StoryStore from './story_store';
@ -60,28 +57,6 @@ export const defaultDecorateStory = (storyFn: StoryFn, decorators: DecoratorFunc
storyFn
);
const metaSubscriptionHandler = deprecate(
subscriptionsStore.register,
'Events.REGISTER_SUBSCRIPTION is deprecated and will be removed in 6.0. Please use useEffect from @storybook/client-api instead.'
);
const metaSubscription = () => {
addons.getChannel().on(Events.REGISTER_SUBSCRIPTION, metaSubscriptionHandler);
return () =>
addons.getChannel().removeListener(Events.REGISTER_SUBSCRIPTION, metaSubscriptionHandler);
};
const withSubscriptionTracking = (storyFn: StoryFn) => {
if (!addons.hasChannel()) {
return storyFn();
}
subscriptionsStore.markAllAsUnused();
subscriptionsStore.register(metaSubscription);
const result = storyFn();
subscriptionsStore.clearUnused();
return result;
};
let _globalDecorators: DecoratorFunction[] = [];
let _globalParameters: Parameters = {};
@ -274,7 +249,6 @@ export default class ClientApi {
...(allParam.decorators || []),
...localDecorators,
..._globalDecorators,
withSubscriptionTracking,
],
}
);

View File

@ -1,92 +0,0 @@
import { createSubscriptionsStore } from './subscriptions_store';
const mockSubscription = () => {
let listening = false;
const listener = jest.fn();
return {
listener,
subscribe() {
listening = true;
return () => {
listening = false;
};
},
trigger(value?) {
if (listening) {
listener(value);
}
},
};
};
describe('preview.subscriptions_store', () => {
describe('register', () => {
it('should register a subscription', () => {
const { listener, subscribe, trigger } = mockSubscription();
const store = createSubscriptionsStore();
trigger('foo');
store.register(subscribe);
trigger('bar');
expect(listener).toHaveBeenCalledTimes(1);
expect(listener).toHaveBeenCalledWith('bar');
});
it("shouldn't subscribe when subscription is already registered", () => {
const subscribe = jest.fn();
const store = createSubscriptionsStore();
store.register(subscribe);
store.register(subscribe);
expect(subscribe).toHaveBeenCalledTimes(1);
});
});
describe('clearUnused', () => {
it('should stop unused subscriptions', () => {
const { listener, subscribe, trigger } = mockSubscription();
const store = createSubscriptionsStore();
store.register(subscribe);
store.markAllAsUnused();
store.clearUnused();
trigger();
expect(listener).not.toHaveBeenCalled();
});
it("shouldn't stop used subscriptions", () => {
const { listener, subscribe, trigger } = mockSubscription();
const store = createSubscriptionsStore();
store.register(subscribe);
store.markAllAsUnused();
store.register(subscribe);
store.clearUnused();
trigger();
expect(listener).toHaveBeenCalled();
});
it('should subscribe again after unsubscribing', () => {
const { listener, subscribe, trigger } = mockSubscription();
const store = createSubscriptionsStore();
store.register(subscribe);
store.markAllAsUnused();
store.clearUnused();
trigger('foo');
store.register(subscribe);
trigger('bar');
expect(listener).toHaveBeenCalledTimes(1);
expect(listener).toHaveBeenCalledWith('bar');
});
});
});

View File

@ -1,34 +0,0 @@
export const createSubscriptionsStore = () => {
const subscriptions = new Map();
return {
register(subscribe: () => void): void {
let subscription = subscriptions.get(subscribe);
if (!subscription) {
subscription = {
unsubscribe: subscribe(),
};
subscriptions.set(subscribe, subscription);
}
subscription.used = true;
},
markAllAsUnused(): void {
subscriptions.forEach(subscription => {
// eslint-disable-next-line no-param-reassign
subscription.used = false;
});
},
clearUnused(): void {
subscriptions.forEach((subscription, key) => {
if (subscription.used) return;
subscription.unsubscribe();
subscriptions.delete(key);
});
},
};
};
export default createSubscriptionsStore();

View File

@ -6,7 +6,7 @@ import AnsiToHtml from 'ansi-to-html';
import addons from '@storybook/addons';
import createChannel from '@storybook/channel-postmessage';
import { ClientApi, StoryStore, ConfigApi } from '@storybook/client-api';
import { ClientApi, StoryStore, ConfigApi, useEffect } from '@storybook/client-api';
import { toId, storyNameFromExport, isExportStory } from '@storybook/csf';
import { logger } from '@storybook/client-logger';
import Events from '@storybook/core-events';