diff --git a/v2/internal/runtime/js/core/main.js b/v2/internal/runtime/js/core/main.js index f14e1e6d0..e98fbb32b 100644 --- a/v2/internal/runtime/js/core/main.js +++ b/v2/internal/runtime/js/core/main.js @@ -16,6 +16,7 @@ import { Callback, SystemCall } from './calls'; import { AddScript, InjectCSS } from './utils'; import { AddIPCListener } from 'ipc'; import * as Platform from 'platform'; +import * as Store from './store'; export function Init() { // Backend is where the Go struct wrappers get bound to @@ -42,7 +43,8 @@ export function Init() { Init, AddIPCListener, SystemCall, - } + }, + Store, }; // Do platform specific Init diff --git a/v2/internal/runtime/js/core/store.js b/v2/internal/runtime/js/core/store.js new file mode 100644 index 000000000..58e471781 --- /dev/null +++ b/v2/internal/runtime/js/core/store.js @@ -0,0 +1,82 @@ +/* + _ __ _ __ +| | / /___ _(_) /____ +| | /| / / __ `/ / / ___/ +| |/ |/ / /_/ / / (__ ) +|__/|__/\__,_/_/_/____/ +The lightweight framework for web-like apps +(c) Lea Anthony 2019-present +*/ +/* jshint esversion: 6 */ + +/** + * Creates a new sync store with the given name and optional default value + * + * @export + * @param {string} name + * @param {*} optionalDefault + */ +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 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 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 callbacks + callbacks.forEach( function(callback) { + callback(data); + }); + + }); + + // Set to the optional default if set + if( optionalDefault ) { + this.set(optionalDefault); + } + + return this; +} \ No newline at end of file diff --git a/v2/internal/runtime/js/runtime/main.js b/v2/internal/runtime/js/runtime/main.js index 5f89f1aec..87b10e3d4 100644 --- a/v2/internal/runtime/js/runtime/main.js +++ b/v2/internal/runtime/js/runtime/main.js @@ -14,6 +14,7 @@ const Browser = require('./browser'); const Events = require('./events'); const Init = require('./init'); const System = require('./system'); +const Store = require('./store'); module.exports = { Log: Log, @@ -21,4 +22,5 @@ module.exports = { Events: Events, Init: Init, System: System, + Store: Store, }; \ No newline at end of file diff --git a/v2/internal/runtime/js/runtime/store.js b/v2/internal/runtime/js/runtime/store.js new file mode 100644 index 000000000..3f12fe93b --- /dev/null +++ b/v2/internal/runtime/js/runtime/store.js @@ -0,0 +1,27 @@ +/* + _ __ _ __ +| | / /___ _(_) /____ +| | /| / / __ `/ / / ___/ +| |/ |/ / /_/ / / (__ ) +|__/|__/\__,_/_/_/____/ +The lightweight framework for web-like apps +(c) Lea Anthony 2019-present +*/ + +/* jshint esversion: 6 */ + + +/** + * Create a new Store with the given name and optional default value + * + * @export + * @param {string} name + * @param {*} optionalDefault + */ +function New(name, optionalDefault) { + return window.wails.Store.New(name, optionalDefault); +} + +module.exports = { + New: New, +};