mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Revert "Remove requirement for JSON of messages (#7426)"
This reverts commit 132c51018b.
This commit is contained in:
parent
67dc74530b
commit
119e91b4b2
@ -6,4 +6,7 @@ export {
|
|||||||
export type {
|
export type {
|
||||||
ChannelObserver,
|
ChannelObserver,
|
||||||
ComputedChannelFactory,
|
ComputedChannelFactory,
|
||||||
|
JsonifiableObject,
|
||||||
|
JsonifiableArray,
|
||||||
|
Jsonifiable,
|
||||||
} from "./src/computed-channel/computed-channel.injectable";
|
} from "./src/computed-channel/computed-channel.injectable";
|
||||||
|
|||||||
@ -1,9 +1,15 @@
|
|||||||
import { reaction } from "mobx";
|
import { reaction } from "mobx";
|
||||||
|
|
||||||
import { getMessageChannelListenerInjectable } from "@k8slens/messaging";
|
import { getMessageChannelListenerInjectable } from "@k8slens/messaging";
|
||||||
import { sendMessageToChannelInjectionToken } from "@k8slens/messaging";
|
import { sendMessageToChannelInjectionToken } from "@k8slens/messaging";
|
||||||
|
import type { JsonPrimitive } from "type-fest";
|
||||||
import { computedChannelObserverInjectionToken } from "./computed-channel.injectable";
|
import { computedChannelObserverInjectionToken } from "./computed-channel.injectable";
|
||||||
import { getMessageChannel } from "@k8slens/messaging";
|
import { getMessageChannel } from "@k8slens/messaging";
|
||||||
|
|
||||||
|
export type JsonifiableObject = { [Key in string]?: Jsonifiable } | { toJSON: () => Jsonifiable };
|
||||||
|
export type JsonifiableArray = readonly Jsonifiable[];
|
||||||
|
export type Jsonifiable = JsonPrimitive | JsonifiableObject | JsonifiableArray;
|
||||||
|
|
||||||
export type ComputedChannelAdminMessage = {
|
export type ComputedChannelAdminMessage = {
|
||||||
channelId: string;
|
channelId: string;
|
||||||
status: "became-observed" | "became-unobserved";
|
status: "became-observed" | "became-unobserved";
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { getInjectable, getInjectionToken } from "@ogre-tools/injectable";
|
import { getInjectable, getInjectionToken } from "@ogre-tools/injectable";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
_getGlobalState,
|
||||||
computed,
|
computed,
|
||||||
IComputedValue,
|
IComputedValue,
|
||||||
observable,
|
observable,
|
||||||
@ -12,8 +13,13 @@ import {
|
|||||||
import type { MessageChannel } from "@k8slens/messaging";
|
import type { MessageChannel } from "@k8slens/messaging";
|
||||||
import { getMessageChannelListenerInjectable } from "@k8slens/messaging";
|
import { getMessageChannelListenerInjectable } from "@k8slens/messaging";
|
||||||
import { sendMessageToChannelInjectionToken } from "@k8slens/messaging";
|
import { sendMessageToChannelInjectionToken } from "@k8slens/messaging";
|
||||||
|
import type { JsonPrimitive } from "type-fest";
|
||||||
import { computedChannelAdministrationChannel } from "./computed-channel-administration-channel.injectable";
|
import { computedChannelAdministrationChannel } from "./computed-channel-administration-channel.injectable";
|
||||||
|
|
||||||
|
export type JsonifiableObject = { [Key in string]?: Jsonifiable } | { toJSON: () => Jsonifiable };
|
||||||
|
export type JsonifiableArray = readonly Jsonifiable[];
|
||||||
|
export type Jsonifiable = JsonPrimitive | JsonifiableObject | JsonifiableArray;
|
||||||
|
|
||||||
export type ComputedChannelFactory = <T>(
|
export type ComputedChannelFactory = <T>(
|
||||||
channel: MessageChannel<T>,
|
channel: MessageChannel<T>,
|
||||||
pendingValue: T,
|
pendingValue: T,
|
||||||
@ -23,12 +29,14 @@ export const computedChannelInjectionToken = getInjectionToken<ComputedChannelFa
|
|||||||
id: "computed-channel-injection-token",
|
id: "computed-channel-injection-token",
|
||||||
});
|
});
|
||||||
|
|
||||||
export type ChannelObserver<T> = {
|
export type ChannelObserver<T extends Jsonifiable> = {
|
||||||
channel: MessageChannel<T>;
|
channel: MessageChannel<T>;
|
||||||
observer: IComputedValue<T>;
|
observer: IComputedValue<T>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const computedChannelObserverInjectionToken = getInjectionToken<ChannelObserver<unknown>>({
|
export const computedChannelObserverInjectionToken = getInjectionToken<
|
||||||
|
ChannelObserver<Jsonifiable>
|
||||||
|
>({
|
||||||
id: "computed-channel-observer",
|
id: "computed-channel-observer",
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -41,7 +49,19 @@ const computedChannelInjectable = getInjectable({
|
|||||||
return ((channel, pendingValue) => {
|
return ((channel, pendingValue) => {
|
||||||
const observableValue = observable.box(pendingValue);
|
const observableValue = observable.box(pendingValue);
|
||||||
|
|
||||||
const computedValue = computed(() => observableValue.get());
|
const computedValue = computed(() => {
|
||||||
|
const { trackingDerivation } = _getGlobalState();
|
||||||
|
|
||||||
|
const contextIsReactive = !!trackingDerivation;
|
||||||
|
|
||||||
|
if (!contextIsReactive) {
|
||||||
|
throw new Error(
|
||||||
|
`Tried to access value of computed channel "${channel.id}" outside of reactive context. This is not possible, as the value is acquired asynchronously sometime *after* being observed. Not respecting that, the value could be stale.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return observableValue.get();
|
||||||
|
});
|
||||||
|
|
||||||
const valueReceiverInjectable = getMessageChannelListenerInjectable({
|
const valueReceiverInjectable = getMessageChannelListenerInjectable({
|
||||||
id: `computed-channel-value-receiver-for-${channel.id}`,
|
id: `computed-channel-value-receiver-for-${channel.id}`,
|
||||||
|
|||||||
@ -292,6 +292,14 @@ const TestComponent = observer(({ someComputed }: { someComputed: IComputedValue
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("when accessing the computed value outside of reactive context, throws", () => {
|
||||||
|
expect(() => {
|
||||||
|
computedTestChannel.get();
|
||||||
|
}).toThrow(
|
||||||
|
'Tried to access value of computed channel "some-channel-id" outside of reactive context. This is not possible, as the value is acquired asynchronously sometime *after* being observed. Not respecting that, the value could be stale.',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it("no value gets listened in di-1 anymore", () => {
|
it("no value gets listened in di-1 anymore", () => {
|
||||||
expect(latestValueMessage).toBeUndefined();
|
expect(latestValueMessage).toBeUndefined();
|
||||||
});
|
});
|
||||||
@ -373,6 +381,14 @@ const TestComponent = observer(({ someComputed }: { someComputed: IComputedValue
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("when accessing the computed value outside of reactive context, throws", () => {
|
||||||
|
expect(() => {
|
||||||
|
computedTestChannel.get();
|
||||||
|
}).toThrow(
|
||||||
|
'Tried to access value of computed channel "some-channel-id" outside of reactive context. This is not possible, as the value is acquired asynchronously sometime *after* being observed. Not respecting that, the value could be stale.',
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("given observation of unrelated computed channel is stopped, observation of other computed channel still works", async () => {
|
it("given observation of unrelated computed channel is stopped, observation of other computed channel still works", async () => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user