Major updates

Automatic type conversion
Support struct data
Custom error handler
This commit is contained in:
Lea Anthony
2020-08-31 20:00:39 +10:00
parent 0ab6a93e0c
commit 43a5f410d9
6 changed files with 139 additions and 20 deletions

View File

@@ -20,36 +20,53 @@ export function New(name, optionalDefault) {
var data;
// Check we are initialised
if( !window.wails) {
throw Error('Wails is not initialised');
}
// Store for the callbacks
let callbacks = [];
// Subscribe to updates by providing a callback
this.subscribe = (callback) => {
callbacks.push(callback);
};
// sets the store data to the provided `newdata` value
this.set = (newdata) => {
data = newdata;
// Emit the data
window.wails.Events.Emit('wails:sync:store:updated:'+name, data);
// Emit a notification to back end
window.wails.Events.Emit('wails:sync:store:updatedbyfrontend:'+name, JSON.stringify(data));
// Notify callbacks
callbacks.forEach( function(callback) {
callback(data);
});
};
// update mutates the value in the store by calling the
// provided method with the current value. The value returned
// by the updater function will be set as the new store value
this.update = (updater) => {
var newValue = updater(data);
this.set(newValue);
};
// Setup event listener
window.wails.Events.On('wails:sync:store:updated:'+name, function(result) {
// Setup event callback
window.wails.Events.On('wails:sync:store:updatedbybackend:'+name, function(result) {
// Parse data
result = JSON.parse(result);
// Todo: Potential preprocessing?
// Save data
data = result;
// Notify listeners
// Notify callbacks
callbacks.forEach( function(callback) {
callback(data);
});