Skip to content

Commit cf7e151

Browse files
committed
Add rayforce WASM files for deployment
1 parent 05a12e8 commit cf7e151

6 files changed

Lines changed: 2756 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ coverage/
3838
.env.local
3939
.env.*.local
4040

41-
# WASM files (generated by prepare:wasm)
42-
public/rayforce/*.wasm
43-
public/rayforce/*.js
41+
# WASM build artifacts are tracked in public/rayforce/
4442

4543
# Vitest
4644
.vitest/

public/rayforce/index.js

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/**
2+
* RayforceDB JavaScript SDK - Main Entry Point
3+
*
4+
* This is the main entry point for the RayforceDB SDK.
5+
* It initializes the WASM module and provides the full SDK API.
6+
*
7+
* Usage (ESM):
8+
* import { init } from 'rayforce';
9+
* const rf = await init();
10+
* const result = rf.eval('(+ 1 2)');
11+
*
12+
* Usage (CDN script tag):
13+
* <script src="https://cdn.../rayforce.umd.js"></script>
14+
* <script>
15+
* Rayforce.init().then(rf => {
16+
* console.log(rf.eval('(+ 1 2)').toJS());
17+
* });
18+
* </script>
19+
*
20+
* @module rayforce
21+
* @version 0.1.0
22+
*/
23+
24+
import { createRayforceSDK, Types, Expr } from './rayforce.sdk.js';
25+
26+
// SDK version
27+
export const version = '0.1.0';
28+
29+
// Re-export types and utilities
30+
export { Types, Expr };
31+
export * from './rayforce.sdk.js';
32+
33+
// Global SDK instance (for singleton pattern)
34+
let _sdkInstance = null;
35+
let _initPromise = null;
36+
37+
/**
38+
* Initialize the RayforceDB SDK.
39+
*
40+
* @param {Object} [options] - Configuration options
41+
* @param {string} [options.wasmPath] - Custom path to rayforce.js WASM loader
42+
* @param {boolean} [options.singleton=true] - Use singleton pattern (reuse instance)
43+
* @param {Function} [options.onReady] - Callback when WASM is fully initialized
44+
* @returns {Promise<RayforceSDK>} The initialized SDK instance
45+
*
46+
* @example
47+
* // Basic usage
48+
* const rf = await init();
49+
*
50+
* // Custom WASM path
51+
* const rf = await init({ wasmPath: './dist/rayforce.js' });
52+
*
53+
* // Force new instance
54+
* const rf = await init({ singleton: false });
55+
*/
56+
export async function init(options = {}) {
57+
const {
58+
wasmPath = './rayforce.js',
59+
singleton = true,
60+
onReady = null,
61+
} = options;
62+
63+
// Return existing instance if singleton
64+
if (singleton && _sdkInstance !== null) {
65+
return _sdkInstance;
66+
}
67+
68+
// Wait for existing init if in progress
69+
if (singleton && _initPromise !== null) {
70+
return _initPromise;
71+
}
72+
73+
const initFn = async () => {
74+
try {
75+
// Dynamic import of WASM module
76+
const wasmModule = await import(wasmPath);
77+
const createRayforce = wasmModule.default;
78+
79+
// Initialize WASM
80+
const wasm = await createRayforce({
81+
// Optional: configure WASM module
82+
rayforce_ready: (msg) => {
83+
if (onReady) onReady(msg);
84+
}
85+
});
86+
87+
// Create SDK wrapper
88+
const sdk = createRayforceSDK(wasm);
89+
90+
if (singleton) {
91+
_sdkInstance = sdk;
92+
}
93+
94+
return sdk;
95+
} catch (error) {
96+
if (singleton) {
97+
_initPromise = null;
98+
}
99+
throw new Error(`Failed to initialize RayforceDB: ${error.message}`);
100+
}
101+
};
102+
103+
if (singleton) {
104+
_initPromise = initFn();
105+
return _initPromise;
106+
}
107+
108+
return initFn();
109+
}
110+
111+
/**
112+
* Get the singleton SDK instance (must call init() first)
113+
* @returns {RayforceSDK|null}
114+
*/
115+
export function getInstance() {
116+
return _sdkInstance;
117+
}
118+
119+
/**
120+
* Check if SDK is initialized
121+
* @returns {boolean}
122+
*/
123+
export function isInitialized() {
124+
return _sdkInstance !== null;
125+
}
126+
127+
/**
128+
* Reset the singleton instance (for testing)
129+
*/
130+
export function reset() {
131+
_sdkInstance = null;
132+
_initPromise = null;
133+
}
134+
135+
// ============================================================================
136+
// Convenience Functions (work on singleton instance)
137+
// ============================================================================
138+
139+
/**
140+
* Evaluate a Rayfall expression using singleton instance
141+
* @param {string} code
142+
* @returns {RayObject}
143+
*/
144+
export function evaluate(code) {
145+
if (!_sdkInstance) {
146+
throw new Error('RayforceDB not initialized. Call init() first.');
147+
}
148+
return _sdkInstance.eval(code);
149+
}
150+
151+
/**
152+
* Create common types using singleton instance
153+
*/
154+
export const create = {
155+
get i64() {
156+
if (!_sdkInstance) throw new Error('RayforceDB not initialized');
157+
return (v) => _sdkInstance.i64(v);
158+
},
159+
get f64() {
160+
if (!_sdkInstance) throw new Error('RayforceDB not initialized');
161+
return (v) => _sdkInstance.f64(v);
162+
},
163+
get string() {
164+
if (!_sdkInstance) throw new Error('RayforceDB not initialized');
165+
return (v) => _sdkInstance.string(v);
166+
},
167+
get symbol() {
168+
if (!_sdkInstance) throw new Error('RayforceDB not initialized');
169+
return (v) => _sdkInstance.symbol(v);
170+
},
171+
get vector() {
172+
if (!_sdkInstance) throw new Error('RayforceDB not initialized');
173+
return (type, data) => _sdkInstance.vector(type, data);
174+
},
175+
get list() {
176+
if (!_sdkInstance) throw new Error('RayforceDB not initialized');
177+
return (items) => _sdkInstance.list(items);
178+
},
179+
get dict() {
180+
if (!_sdkInstance) throw new Error('RayforceDB not initialized');
181+
return (obj) => _sdkInstance.dict(obj);
182+
},
183+
get table() {
184+
if (!_sdkInstance) throw new Error('RayforceDB not initialized');
185+
return (cols) => _sdkInstance.table(cols);
186+
},
187+
get date() {
188+
if (!_sdkInstance) throw new Error('RayforceDB not initialized');
189+
return (v) => _sdkInstance.date(v);
190+
},
191+
get time() {
192+
if (!_sdkInstance) throw new Error('RayforceDB not initialized');
193+
return (v) => _sdkInstance.time(v);
194+
},
195+
get timestamp() {
196+
if (!_sdkInstance) throw new Error('RayforceDB not initialized');
197+
return (v) => _sdkInstance.timestamp(v);
198+
},
199+
};
200+
201+
// ============================================================================
202+
// Default Export
203+
// ============================================================================
204+
205+
export default {
206+
init,
207+
getInstance,
208+
isInitialized,
209+
reset,
210+
evaluate,
211+
create,
212+
Types,
213+
Expr,
214+
version,
215+
};

public/rayforce/rayforce.js

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)