Skip to content

Commit

Permalink
refactor(gems): use exoClass instance built-in state
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis committed Sep 19, 2024
1 parent 69126c4 commit e156711
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 38 deletions.
25 changes: 6 additions & 19 deletions packages/gems/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export const makeKernel = baggage => {
/*
GemZone:
(store) 'data': { recipe }
(store) 'instances': WeakMap<gem instance, StorageMap>
(subzone) 'gemRegistry': SubZone<name, GemZone>
*/

Expand All @@ -64,7 +63,6 @@ export const makeKernel = baggage => {
const loadGemNamespaceFromGemZone = gemZone => {
const childCache = new Map();
const data = gemZone.mapStore('data');
const instances = gemZone.weakMapStore('instances');
const registry = gemZone.subZone('gemRegistry');
const childKeys = gemZone.setStore('childKeys');
let exoClass;
Expand All @@ -75,21 +73,6 @@ export const makeKernel = baggage => {
getRecipe() {
return data.get('recipe');
},
getStoreForInstance(instance, initFn) {
if (!instances.has(instance)) {
const value = harden(initFn());
instances.init(instance, value);
}
const store = {
get() {
return instances.get(instance);
},
set(value) {
instances.set(instance, harden(value));
},
};
return store;
},
// methods
lookupChild(name) {
if (childCache.has(name)) {
Expand Down Expand Up @@ -142,11 +125,15 @@ export const makeKernel = baggage => {
} = constructGem({
M,
gemName: name,
getStore: instance => gemNs.getStoreForInstance(instance, init),
defineChildGem,
lookupChildGemClass: childName => loadGem(gemNs, childName),
});
return gemNs.exoClass(name, interfaceGuards, initWithPassthrough, methods);
return gemNs.exoClass(
name,
interfaceGuards,
init || initWithPassthrough,
methods,
);
};

// reincarnate all registered gems
Expand Down
29 changes: 10 additions & 19 deletions packages/gems/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,19 @@ import { makeVat } from './util.js';
test('persistence - simple json counter', async t => {
const gemRecipe = {
name: 'CounterGem',
code: `${({ M, gemName, getStore }) => ({
code: `${({ M, gemName }) => ({
interface: M.interface(gemName, {
increment: M.call().returns(M.number()),
getCount: M.call().returns(M.number()),
}),
init: () => ({ count: 0 }),
init: (count = 0) => ({ count }),
methods: {
increment() {
const store = getStore(this.self);
let { count } = store.get();
count += 1;
store.set({ count });
return count;
this.state.count += 1;
return this.state.count;
},
getCount() {
const store = getStore(this.self);
const { count } = store.get();
return count;
return this.state.count;
},
},
})}`,
Expand Down Expand Up @@ -50,23 +45,19 @@ test('persistence - simple json counter', async t => {
test('kumavis store - serialization of gem refs', async t => {
const friendsListRecipe = {
name: 'FriendsList',
code: `${({ M, gemName, getStore }) => ({
code: `${({ M, gemName }) => ({
interface: M.interface(gemName, {
addFriend: M.call(M.any()).returns(M.string()),
getFriends: M.call().returns(M.any()),
}),
init: () => ({ friends: [] }),
init: () => harden({ friends: [] }),
methods: {
addFriend(friend) {
const store = getStore(this.self);
const { friends } = store.get();
store.set({ friends: [...friends, friend] });
return `added friend ${friend} (${friends.length} friends total)`;
this.state.friends = harden([...this.state.friends, friend]);
return `added friend ${friend} (${this.state.friends.length} friends total)`;
},
getFriends() {
const store = getStore(this.self);
const { friends } = store.get();
return friends;
return this.state.friends;
},
},
})}`,
Expand Down

0 comments on commit e156711

Please sign in to comment.