Skip to content

Commit e13d6df

Browse files
committed
fix: update ensemble storage
1 parent 53d8ac8 commit e13d6df

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
lines changed

apps/kitchen-sink/src/ensemble/screens/help.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,37 @@ View:
2828
inputs:
2929
input1: "hello"
3030
input2: "world"
31+
32+
- Button:
33+
label: toggle modal
34+
onTap:
35+
showDialog:
36+
options:
37+
minWidth: 500px
38+
body:
39+
MultiSelect:
40+
label: test
41+
data: ${ensemble.storage.get('testoptions')}
42+
labelKey: label
43+
valueKey: value
44+
value: ${ensemble.storage.get('testselect') || []}
45+
onChange:
46+
executeCode: |
47+
ensemble.storage.set('testselect', option)
48+
onSearch:
49+
executeCode: |
50+
const tempOptions = ensemble.storage.get('testlistoptions')
51+
const filteredResult = tempOptions.filter(option =>
52+
option.label.toLowerCase().startsWith(search.toLowerCase())
53+
)
54+
ensemble.storage.set('testoptions', filteredResult)
55+
56+
- Button:
57+
label: set storage variable
58+
onTap:
59+
executeCode: |
60+
ensemble.storage.set('isTestAvailable', 'yes !!!')
61+
3162
- Text:
3263
styles:
3364
names: heading-1

apps/kitchen-sink/src/ensemble/screens/testActions.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ View:
191191
label: close all screens
192192
onTap:
193193
closeAllScreens:
194+
- Button:
195+
label: navigate screen modal for test storage
196+
onTap:
197+
navigateModalScreen:
198+
name: help
199+
height: 200px
200+
width: 900px
201+
- Button:
202+
label: check storage variable
203+
onTap:
204+
executeCode: |
205+
ensemble.storage.set('storageTested', 'okay')
206+
console.log(ensemble.storage.get('isTestAvailable'))
194207
195208
Socket:
196209
echo:

packages/framework/src/hooks/useCommandCallback.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ import type {
2929
ShowDialogAction,
3030
} from "../shared";
3131
import { deviceAtom } from "./useDeviceObserver";
32-
import { createStorageApi, screenStorageAtom } from "./useEnsembleStorage";
32+
import {
33+
createStorageApi,
34+
screenStorageAtom,
35+
useEnsembleStorage,
36+
} from "./useEnsembleStorage";
3337
import { useCustomScope } from "./useCustomScope";
3438
import { useLanguageScope } from "./useLanguageScope";
3539

@@ -53,21 +57,17 @@ export const useCommandCallback = <
5357
): ReturnType<typeof useAtomCallback<R, T>> => {
5458
const customScope = useCustomScope();
5559
const { i18n } = useLanguageScope();
60+
const storage = useEnsembleStorage();
5661

5762
return useAtomCallback(
5863
useCallback(
5964
(get, set, ...args: T) => {
6065
const applicationContext = get(appAtom);
6166
const screenContext = get(screenAtom);
62-
const storage = get(screenStorageAtom);
6367
const device = get(deviceAtom);
6468
const theme = get(themeAtom);
6569
const user = get(userAtom);
6670

67-
const storageApi = createStorageApi(storage, (next) =>
68-
set(screenStorageAtom, next),
69-
);
70-
7171
const customWidgets =
7272
applicationContext.application?.customWidgets.reduce(
7373
(acc, widget) => ({ ...acc, [widget.name]: widget }),
@@ -83,7 +83,7 @@ export const useCommandCallback = <
8383
...user,
8484
setUser: (userUpdate: EnsembleUser) => set(userAtom, userUpdate),
8585
},
86-
storage: storageApi,
86+
storage,
8787
formatter: DateFormatter(),
8888
env: applicationContext.env,
8989
secrets: applicationContext.secrets,
@@ -110,7 +110,7 @@ export const useCommandCallback = <
110110
ensemble: {
111111
env: applicationContext.env,
112112
secrets: applicationContext.secrets,
113-
storage: storageApi,
113+
storage,
114114
},
115115
},
116116
undefined,

packages/framework/src/hooks/useEnsembleStorage.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,15 @@ export const screenStorageAtom = atom(
3939

4040
export const useEnsembleStorage = (): EnsembleStorage => {
4141
const [storage, setStorage] = useAtom(screenStorageAtom);
42+
4243
// Use a buffer so we can perform imperative changes without forcing re-render
4344
const storageBuffer = useMemo<{ [key: string]: unknown }>(() => ({}), []);
4445

4546
useMemo(() => {
4647
merge(storageBuffer, storage);
4748
}, [storageBuffer, storage]);
4849

49-
const storageApi = useMemo(
50-
() => createStorageApi(storageBuffer, setStorage),
51-
[setStorage, storageBuffer],
52-
);
50+
const storageApi = createStorageApi(storage, setStorage);
5351

5452
return storageApi;
5553
};
@@ -64,8 +62,8 @@ export const createStorageApi = (
6462
update[key] = value;
6563
if (storage) {
6664
assign(storage, update);
65+
setStorage?.(storage);
6766
}
68-
setStorage?.(update);
6967
},
7068
get: (key: string): unknown => {
7169
return storage?.[key];

0 commit comments

Comments
 (0)