Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save simulator registry state for every change. Add screen brightness #289

Merged
merged 3 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/pos/drivers/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import getBaseDriver from './base.js';
/** Used to extend a driver with the base driver */
export default function(driver, ...modifiers) {
if (__DEV__ && typeof driver === 'undefined') {
throw new Error('[@mamba/pos/api] Could not find the loaded driver.');
throw new Error('[@mamba/pos] Could not find the loaded driver.');
}

Object.assign(driver, getBaseDriver());
Expand Down
2 changes: 2 additions & 0 deletions packages/pos/simulator/libs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ export function error(...args) {
export function warn(...args) {
console.warn([LOG_PREFIX, ...args].join(' '));
}

export const deepCopy = o => JSON.parse(JSON.stringify(o));
20 changes: 15 additions & 5 deletions packages/pos/simulator/plugins/driver/manager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Registry from '../registry/manager.js';
import Signal from '../../libs/signal.js';
import { LOG_PREFIX } from '../../libs/utils.js';
import { LOG_PREFIX, deepCopy } from '../../libs/utils.js';
import extendDriver from '../../../drivers/extend.js';

const DriverManager = extendDriver({});
Expand All @@ -9,6 +9,8 @@ DriverManager.attachDrivers = driverModules => {
if (__DEBUG_LVL__ >= 1 && __BROWSER__)
console.groupCollapsed(`${LOG_PREFIX} Attaching drivers`);

const savedState = Registry.getSavedState();

driverModules.forEach(driverModule => {
const driver = {};
const driverRef = driverModule.NAMESPACE;
Expand All @@ -21,10 +23,18 @@ DriverManager.attachDrivers = driverModules => {
console.log('Default settings:', driverModule.SETTINGS);
}

Registry.set(
driverModule.NAMESPACE,
JSON.parse(JSON.stringify(driverModule.SETTINGS)),
);
if (savedState[driverModule.NAMESPACE]) {
Registry.set(
draft => {
draft[driverModule.NAMESPACE] = savedState[driverModule.NAMESPACE];
},
{ save: false },
);
} else {
Registry.set(draft => {
draft[driverModule.NAMESPACE] = deepCopy(driverModule.SETTINGS);
});
}
}

/** Register the driver signals */
Expand Down
1 change: 1 addition & 0 deletions packages/pos/simulator/plugins/hardware/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Signal.register(HardwareManager, [
'startPrinting',
'endPrinting',
'toggleCard',
'changeBrightness',
]);

export default HardwareManager;
3 changes: 0 additions & 3 deletions packages/pos/simulator/plugins/load.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import Core from '../core.js';
import './registry/register.js';
import './hardware/register.js';
import './driver/register.js';
import './app/register.js';
import './view/register.js';

Core.Registry.setBoot(true);
45 changes: 32 additions & 13 deletions packages/pos/simulator/plugins/registry/includes/data.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import produce, { setAutoFreeze } from 'immer';

import { log } from '../../../libs/utils.js';
import { log, deepCopy } from '../../../libs/utils.js';

setAutoFreeze(false);

export default Registry => {
Registry.save = () => {
localStorage.setItem('_mamba_web_', JSON.stringify(Registry._data));
};

/** Data */
Registry.get = keyPath => {
if (keyPath === undefined) {
Expand All @@ -18,19 +22,30 @@ export default Registry => {
}

if (typeof value === 'object') {
return JSON.parse(JSON.stringify(value));
return deepCopy(value);
}

return value;
};

Registry.set = (keyPath, value, fireSignal = true) => {
Registry.set = (keyPath, value, opts) => {
if (keyPath === undefined) {
return;
}

if (typeof keyPath === 'function') {
opts = value;
}

opts = { dispatch: true, save: true, ...opts };

const { dispatch, save } = opts;

if (typeof keyPath === 'function') {
Registry._data = produce(Registry._data, keyPath);
if (save) {
Registry.save();
}
return;
}

Expand All @@ -44,22 +59,26 @@ export default Registry => {
if (keys.length === 1) {
Registry._data[keyPath] = value;

if (fireSignal) {
if (dispatch) {
// todo: deprecate in favor of immer
Registry.fire('shallowChange', { key: keyPath, value });
}
} else {
let object = Registry._data[keys[0]];
for (let i = 1; i < keys.length - 1; i++) {
object = object[keys[i]];
}

return;
}
object[keys[keys.length - 1]] = value;

let object = Registry._data[keys[0]];
for (let i = 1; i < keys.length - 1; i++) {
object = object[keys[i]];
if (dispatch) {
// todo: deprecate in favor of immer
Registry.fire('deepChange', { key: keyPath, path: keys, value });
}
}

object[keys[keys.length - 1]] = value;

if (fireSignal) {
Registry.fire('deepChange', { key: keyPath, path: keys, value });
if (save) {
Registry.save();
}
};
};
32 changes: 24 additions & 8 deletions packages/pos/simulator/plugins/registry/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import initData from './includes/data.js';

const RegistryManager = extendDriver(
{
_version: '2.5.2',
_version: '2.5.3',
_clock: { hours: null, min: null },
_booted: false,
_data: {},
},
initClock,
Expand All @@ -24,13 +23,30 @@ Signal.register(RegistryManager, [
'clock', // fired at every clock update
]);

RegistryManager.setBoot = isBooted => {
if (RegistryManager._booted) {
return;
RegistryManager.getVersion = () => RegistryManager._version;

let lastCachedStatedJson;
let savedState;

RegistryManager.getSavedState = () => {
if (!localStorage) {
return {};
}
RegistryManager._booted = isBooted;
};

RegistryManager.getVersion = () => RegistryManager._version;
const cachedStatedJson = localStorage.getItem('_mamba_web_');

if (!cachedStatedJson) {
return {};
}

if (lastCachedStatedJson === cachedStatedJson) {
return savedState;
}

lastCachedStatedJson = cachedStatedJson;
savedState = JSON.parse(cachedStatedJson);

return savedState;
};

export default RegistryManager;
19 changes: 11 additions & 8 deletions packages/pos/simulator/plugins/view/ControlPanel.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

<script>
import { Registry } from '../../index.js';
import { deepCopy } from '../../libs/utils.js';

let lastKeypressTime = 0;

const deepCopy = o => JSON.parse(JSON.stringify(o));

export default {
components: {
Keystroke: '@mamba/app/Keystroke.html',
Expand Down Expand Up @@ -62,12 +61,16 @@
handlePanelUpdate(namespace, { changed, current, previous }) {
if (previous) {
Object.keys(changed).forEach(key => {
const value = current[key];
/** See if the prop name has already the namespace path */
if (key.indexOf(`${namespace}.`) === -1) {
key = `${namespace}.${key}`;
}
Registry.set(key, value, false);
Registry.set(
draft => {
draft[namespace] = {
...draft[namespace],
[key]: current[key],
};
},
// todo: should save, but not possible right now without resetting every external driver
{ dispatch: false, save: false },
);
});
}
},
Expand Down
13 changes: 13 additions & 0 deletions packages/pos/simulator/plugins/view/hardware/Screen.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

<script>
import Registry from '../../registry/manager.js';
import HardwareManager from '../../hardware/manager.js';

const { hours: initHours, minutes: initMinutes } = Registry.getCurrentTime();

Expand All @@ -43,6 +44,18 @@
brightnessOpacity: ({ brightnessLevel }) => 1 - brightnessLevel / 10,
},
oncreate() {
// TODO: find another way to do this which doesn't directly access the driver state
const savedRegistryState = Registry.getSavedState();
if (savedRegistryState.ScreenBrightness) {
this.set({
brightnessLevel: savedRegistryState.ScreenBrightness.level,
});
}

HardwareManager.on('changeBrightness', brightnessLevel => {
this.set({ brightnessLevel });
});

/** Update the clock after each second */
Registry.on('clock', (newHours, newMinutes) => {
this.set({ time: `${newHours}:${newMinutes}` });
Expand Down