Skip to content

fix: update ensemble storage #1061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions apps/kitchen-sink/src/ensemble/screens/help.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,37 @@ View:
inputs:
input1: "hello"
input2: "world"

- Button:
label: toggle modal
onTap:
showDialog:
options:
minWidth: 500px
body:
MultiSelect:
label: test
data: ${ensemble.storage.get('testoptions')}
labelKey: label
valueKey: value
value: ${ensemble.storage.get('testselect') || []}
onChange:
executeCode: |
ensemble.storage.set('testselect', option)
onSearch:
executeCode: |
const tempOptions = ensemble.storage.get('testlistoptions')
const filteredResult = tempOptions.filter(option =>
option.label.toLowerCase().startsWith(search.toLowerCase())
)
ensemble.storage.set('testoptions', filteredResult)

- Button:
label: set storage variable
onTap:
executeCode: |
ensemble.storage.set('isTestAvailable', 'yes !!!')

- Text:
styles:
names: heading-1
Expand Down
13 changes: 13 additions & 0 deletions apps/kitchen-sink/src/ensemble/screens/testActions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ View:
label: close all screens
onTap:
closeAllScreens:
- Button:
label: navigate screen modal for test storage
onTap:
navigateModalScreen:
name: help
height: 200px
width: 900px
- Button:
label: check storage variable
onTap:
executeCode: |
ensemble.storage.set('storageTested', 'okay')
console.log(ensemble.storage.get('isTestAvailable'))

Socket:
echo:
Expand Down
12 changes: 4 additions & 8 deletions packages/framework/src/hooks/useCommandCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import type {
ShowDialogAction,
} from "../shared";
import { deviceAtom } from "./useDeviceObserver";
import { createStorageApi, screenStorageAtom } from "./useEnsembleStorage";
import { useEnsembleStorage } from "./useEnsembleStorage";
import { useCustomScope } from "./useCustomScope";
import { useLanguageScope } from "./useLanguageScope";

Expand All @@ -53,21 +53,17 @@ export const useCommandCallback = <
): ReturnType<typeof useAtomCallback<R, T>> => {
const customScope = useCustomScope();
const { i18n } = useLanguageScope();
const storage = useEnsembleStorage();

return useAtomCallback(
useCallback(
(get, set, ...args: T) => {
const applicationContext = get(appAtom);
const screenContext = get(screenAtom);
const storage = get(screenStorageAtom);
const device = get(deviceAtom);
Comment on lines +56 to 63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not right - the reason we use get instead of the hook here is to prevent unnecessary re-renders whenever the storage is changed.

const theme = get(themeAtom);
const user = get(userAtom);

const storageApi = createStorageApi(storage, (next) =>
set(screenStorageAtom, next),
);

const customWidgets =
applicationContext.application?.customWidgets.reduce(
(acc, widget) => ({ ...acc, [widget.name]: widget }),
Expand All @@ -83,7 +79,7 @@ export const useCommandCallback = <
...user,
setUser: (userUpdate: EnsembleUser) => set(userAtom, userUpdate),
},
storage: storageApi,
storage,
formatter: DateFormatter(),
env: applicationContext.env,
secrets: applicationContext.secrets,
Expand All @@ -110,7 +106,7 @@ export const useCommandCallback = <
ensemble: {
env: applicationContext.env,
secrets: applicationContext.secrets,
storage: storageApi,
storage,
},
},
undefined,
Expand Down
8 changes: 3 additions & 5 deletions packages/framework/src/hooks/useEnsembleStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@ export const screenStorageAtom = atom(

export const useEnsembleStorage = (): EnsembleStorage => {
const [storage, setStorage] = useAtom(screenStorageAtom);

// Use a buffer so we can perform imperative changes without forcing re-render
const storageBuffer = useMemo<{ [key: string]: unknown }>(() => ({}), []);

useMemo(() => {
merge(storageBuffer, storage);
}, [storageBuffer, storage]);

const storageApi = useMemo(
() => createStorageApi(storageBuffer, setStorage),
[setStorage, storageBuffer],
);
const storageApi = createStorageApi(storage, setStorage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?


return storageApi;
};
Expand All @@ -64,8 +62,8 @@ export const createStorageApi = (
update[key] = value;
if (storage) {
assign(storage, update);
setStorage?.(storage);
}
setStorage?.(update);
},
get: (key: string): unknown => {
return storage?.[key];
Expand Down