Skip to content

Commit

Permalink
wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
soohoonc committed Aug 23, 2024
1 parent f8d44fc commit da6d355
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 37 deletions.
Binary file modified bun.lockb
Binary file not shown.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"dev": "wasm-pack build ./src/wasm --target web && next dev",
"build": "next build",
"build:wasm": "wasm-pack build ./src/wasm --target web",
"start": "next start",
Expand Down Expand Up @@ -34,7 +34,9 @@
"react-dom": "18.2.0",
"remark": "^15.0.1",
"remark-html": "^16.0.1",
"semver": ""
"semver": "",
"wasm": "^1.0.0",
"wasm-pack": "^0.13.0"
},
"devDependencies": {
"@types/node": "20.4.2",
Expand Down
59 changes: 24 additions & 35 deletions src/providers/shell.tsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,66 @@


'use client';

import * as React from 'react';
import init, { Shell as WASMShell } from '@/wasm/pkg';

const initialShellState: ShellState = {
history: [] as Message[],
user: 'guest',
hostname: 'soohoonchoi.com',
shell: {} as Shell
}
shell: {} as Shell,
};

const ShellStateReducer = (state: ShellState, action: ShellStateAction): ShellState => {
switch (action.type) {
case 'addHistory':
return {
...state,
history: [...state.history, action.payload]
history: [...state.history, action.payload],
};
case 'clearHistory':
return {
...state,
history: []
history: [],
};
case 'setUser':
return {
...state,
user: action.payload
user: action.payload,
};
case 'setHostname':
return {
...state,
hostname: action.payload
hostname: action.payload,
};
case 'setShell':
return {
...state,
shell: action.payload
shell: action.payload,
};
default:
return state;
}
}
};

interface ShellContext {
history: {
get: () => Message[];
add: (message: Message) => void;
clear: () => void;
},
};
user: {
get: () => string;
set: (user: string) => void;
},
};
hostname: {
get: () => string;
set: (hostname: string) => void;
},
};
execute: (command: string) => string;
}

const ShellContext = React.createContext<ShellContext | undefined>(undefined);


export function useShell() {
const context = React.useContext(ShellContext);
if (!context) {
Expand All @@ -77,38 +75,29 @@ export function ShellProvider({ children }: { children: React.ReactNode }) {
history: {
get: () => state.history,
add: (message: Message) => dispatch({ type: 'addHistory', payload: message }),
clear: () => dispatch({ type: 'clearHistory' })
clear: () => dispatch({ type: 'clearHistory' }),
},
user: {
get: () => state.user,
set: (user: string) => dispatch({ type: 'setUser', payload: user })
set: (user: string) => dispatch({ type: 'setUser', payload: user }),
},
hostname: {
get: () => state.hostname,
set: (hostname: string) => dispatch({ type: 'setHostname', payload: hostname })
set: (hostname: string) => dispatch({ type: 'setHostname', payload: hostname }),
},
execute: (command: string) => {
return state.shell.run(command);
}
}
},
};
React.useEffect(() => {
async function init() {
const shell = {
run: (command: string) => {
return command;
}
} as Shell;
// const shell = await getShell();
async function initialize() {
await init();
// https://github.com/rustwasm/wasm-bindgen/issues/166
const shell = new WASMShell('guest', 'soohoonchoi.com');
dispatch({ type: 'setShell', payload: shell });
}
init();
initialize();
}, []);

return (
<ShellContext.Provider
value={shellContext}
>
{children}
</ShellContext.Provider>
);
}
return <ShellContext.Provider value={shellContext}>{children}</ShellContext.Provider>;
}

0 comments on commit da6d355

Please sign in to comment.